function init() {
	if ($$('body.project').first() && $$('#sidebar img').first() && !$('feature-project')) setup_map_popup()
	if ($('feature-project') && navigator.appVersion.split("MSIE") && parseFloat(navigator.appVersion.split("MSIE")[1])) replace_feature()
}

function gallery_init(pictures, path) {
	picture_list = pictures.split(",")
	picture = $('gallery_picture')
	$('gallery_picture').src = path + "/" + picture_list[0]
	$('gallery_picture').setAttribute('rel', 0)
	new PeriodicalExecuter(function() { change_picture(picture_list, path) }, 5);
}

function change_picture(picture_list, path) {
	last = ($('gallery_picture').getAttribute('rel') == null) ? 0 : parseInt($('gallery_picture').getAttribute('rel'))
	next = (last + 1) % picture_list.length
	new Effect.Opacity('gallery_picture', {duration:1.5, from:1, to:0});
	setTimeout(function(){$('gallery_picture').src = path + "/" + picture_list[next]}, 1600)
	Event.observe($('gallery_picture'), 'load', fadeIn); 
	new Effect.Opacity('gallery_picture', {duration:1.5, from:0.0, to:1, delay: 1.7});
	$('gallery_picture').setAttribute('rel', next)
}

function fadeIn(){
	new Effect.Opacity('gallery_picture', {duration:1.5, from:0.0, to:1});
}

function setup_map_popup(){	
	$$('#sidebar p')[0].innerHTML += '<span id="map-label">&nbsp;</span>'
	$('main').innerHTML += '<div id="map"><img src=""/><div id="close"><a href="javascript:close_map()"><img src="/stylesheets/images/close.gif"></a></div></div>'
	$$('#map img')[0].src = "/images/projects/maps/" + $('mini-map').src.split('/').last().substring("mini-".length)
	$('map').hide()
	var tmp = $('mini-map').ancestors().first().innerHTML
	$('mini-map').ancestors().first().innerHTML = '<a href="javascript:show_map()">' + tmp + '</a>'
	//Event.observe($('mini-map'), 'click', show_map)
	//Event.observe($('map-label'), 'click', show_map)
	$('mini-map').setStyle({cursor: 'pointer'})
}

function show_map(){
	$('top').innerHTML += '<div id="overlay"></div>'
	
	$('overlay').setStyle({'height': (document.viewport.getDimensions()["height"]+document.viewport.getScrollOffsets()["top"]) + 'px'})
	$('overlay').setStyle({'width': document.viewport.getDimensions()["width"] + 'px'})
	$('overlay').setStyle({'left': '-' + $('top').positionedOffset()["left"] +'px'})
	$('overlay').setStyle({'zIndex': $('map').getStyle('zIndex')-1 })

	$('map').setStyle({'top' : Math.ceil(document.viewport.getScrollOffsets()["top"] + ($('map').getDimensions()["height"]/2) ) + "px"})
	$('map').setStyle({'left' : Math.ceil( ($('top').getDimensions()["width"]/2) - ($('map').getDimensions()["width"]/2) ) + "px"})
	
	$('map').show()
}

function close_map(){
	$('map').hide()
	$('top').removeChild($('overlay'))
	
	//arrange_zindexes()
	/*Event.observe($('map-label'), 'click', show_map)
	Event.observe($('mini-map'), 'click', show_map)*/
}

function replace_feature(){
	src = $('feature-project').src
	$('feature-project').ancestors().first().innerHTML = '<img src="/images/blank.gif" id="feature-project" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\'"/>'
}

function arrange_zindexes(){
	var blocks = $$('div')
	var zi = 1000;
	
	if(blocks){
	    blocks.each(function(block, i) {
	        block.style.zIndex = zi--;
	    });
	}
}

document.observe('dom:loaded', init)