$(document).ready(function()
{
	// make all tall column equal in height
	$(".column:not(.short)").equalHeights();
	
	// dinamically resize map viewport
	if ( !($('#content_container').hasClass("content_only")) )
	{
		adjustContentPosition();
		$(window).resize (function() {adjustContentPosition();});
	}

	// toggle content
	$("#toggle_content_button").live("click", function() {
		if (!$("#content").hasClass("loader")) {
			toggleContent();
		} else {
			// msg to wait or reload
		}
	})
	.css("cursor", "pointer");
//	$(".toggle_bottom_button").click();

	if (location.hash) {
		// remove id diary_menu from ul for trigger the right behavior
		if ($("ul#diary_menu").length > 0) {
			$("ul#diary_menu").attr("id", "");
			$("#content_container").removeClass("content_container_diary").css("height", "");
			$("#center_content").attr("style", "");
			$("#content").attr("style", "");
			$("#toggle_content_button").removeClass("toggle_content_button_diary");
	
			var nickname = location.hash;
			nickname = nickname.replace("#", "");
			if ($(".triggermarker[rel=" + nickname + "]").length > 0) {
				hideContent();
				$("#content").children().css("visibility", "hidden");
			}
	
		}
	}
	
});


jQuery.fn.equalHeights = function() {
	var maxHeight=0;
	this.each(function(){
		if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
	});
	this.each(function(){
		$(this).height(maxHeight + "px");
		if (this.offsetHeight>maxHeight) {
			$(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
		}
	});
};



/*
 *  dynamic containers resizers
 */
function adjustContentPosition () {
		var targetMargin;
		var targetMapHeight =  $(window).height() - $('#top_container').height() - $('#bottom_container').height();
		$('#map_container').height(targetMapHeight);

		if ( ($('#content_container').hasClass("content_off")) ) {
			targetMargin = $('#top_container').height() + targetMapHeight - minimumContentHeight;
		}
		else {
			targetMargin = $('#top_container').height() + targetMapHeight/2;
			if ($('.column').height() < targetMapHeight/2) {
				
				// resize height only if not diary
				if ($("ul#diary_menu").length == 0)
					$('#content').height(targetMapHeight/2);
			}
		}

		// IE7 cannot manage correctly top margin	
		if($.browser.msie && parseInt($.browser.version, 10) == 7) {
			$('#content_container').css("padding-top", targetMargin);
		} else
			$('#content_container').css("margin-top", targetMargin);
}


function toggleContent () {
	$("#toggle_content_button").toggleClass ("toggle_content_button_open");
	
	var contentElem = $('#content_container');
	var targetMargin;
	var contentCss = {};
	
	if ( ($('#content_container').hasClass("content_off")) ) {
		targetMargin = $('#top_container').height() + ($(window).height() - $('#top_container').height() - $('#bottom_container').height())/2;
		// cambio subito il css del content così quando sale il contenuto non vedo spazio vuoto
		// overflow deve rimanere "auto" verrà resettato dopo l'animazione per problemi su firefox
		$("#content").css({"height": "", "overflow": "auto"});

		if (window['isHome'] != undefined) {
				$('#content_container').css({ height: homeContentHeight + "px"});
		} else if ($("ul#diary_menu").length > 0) {
				$('#content_container').css({ height: diaryContentHeight + "px"});
		} else {
				$('#content_container').css({ height: ""});
		}

	} else {
		targetMargin = $('#top_container').height() + ($(window).height() - $('#top_container').height() - $('#bottom_container').height()) - minimumContentHeight;
		contentCss = {"height": minimumContentHeight, "overflow": "hidden"};
		$('#content_container').css({ height: minimumContentHeight + "px"});
	}


	// animate this
	$(window).scrollTop(0);

	// animate this
	// but IE7 cannot manage correctly top margin -> no anim	
	if($.browser.msie && parseInt($.browser.version, 10) == 7) {
		$('#content_container').css("padding-top", targetMargin);
		contentElem.toggleClass ("content_off");
		if (contentCss.height != undefined) {
			$("#content").css(contentCss);
		} else {
			// reset overflow dopo l'animazione per problemi con firefox
			$("#content").css("overflow","");
		}
		
		$(".column:not(.short)").equalHeights();
		adjustContentPosition();
	} else {
		contentElem.animate({
			marginTop: targetMargin
		}, toggleSpeed, function (){
			contentElem.toggleClass ("content_off");
			if (contentCss.height != undefined) {
				$("#content").css(contentCss);
			} else {
				// reset overflow dopo l'animazione per problemi con firefox
				$("#content").css("overflow","");
			}
			
			$(".column:not(.short)").equalHeights();
			adjustContentPosition();
		});
	}	
}

function showContent() {
	if ($('#content_container').hasClass("content_off")) {
		toggleContent();
	}
}

function hideContent() {
	if (!$('#content_container').hasClass("content_off")) {
		toggleContent();
	}
}


