var altezzaFinestra;
var CanMove = 1;
//172.16.196.2
//var url = "http://172.16.196.2:8888/";
var url = "http://www.sarazanier.com/";

$(document).ready(function() {

	$(window).resize(function(){
		$("div#background").fadeOut();
		sizeOfWindow();
	});

	$("li#fb").click(function() {
		document.location.href = "http://www.facebook.com/sara.zanier";
	});
	$("li#ms").click(function() {
		document.location.href = "http://www.myspace.com/sarazanierfanspace";
	});
	$("li#blog").click(function() {
		document.location.href = "http://sarazanier.iobloggo.com/";
	});
	//////////////////////////////////////////////////
	$("div#header h1").html('<img src="media/iconeWeb/logo.png" />');
	$("#home").html('<img src="media/iconeWeb/home.png" />');
	$("#biografia").html('<img src="media/iconeWeb/biografia.png" />');
	$("#galleria").html('<img src="media/iconeWeb/galleria.png" />');
	$("#news").html('<img src="media/iconeWeb/news.png" />');
	$("#contatti").html('<img src="media/iconeWeb/contatti.png" />');
	//////////////////////////////////////////////////
	$("li#galleria").click(function () {
		if($("#pagina").html() == "gallery") {
			
			$("#pagina").html("none");
			TiraSuGalleria();
			
		} else {
			$("#pagina").html("gallery");			
			if($("div#body").css("display") == "block")
				$("div#body").fadeOut();
			TiraGiuGalleria();
		}
	});
	
	$("li#biografia").click(function () {

		if($("#pagina").html() == "biografia") {
			
			$("#pagina").html("none");			
			$("div#body").fadeOut();
			
		} else {
				
			$("div#pagina").html("biografia");
		
			if(parseInt($("div#gallery").css("top")) > 0)
				TiraSuGalleria();
				$("div#body").fadeOut(
					function () {		
						$("div#content").load(url + "documenti/biografia2.html", function() {				
							if($("div#body").height() > altezzaFinestra - 180)
						$("div#controller").show();

						$("div#body").css("overflow", "hidden");
						$("div#body").css("height", altezzaFinestra - 180);
						$("div#body").fadeIn();
					}
				);
			});
			
		}		
				
	});
	
	$("li#contatti").click(function () {

		if($("#pagina").html() == "contatti") {
			
			$("#pagina").html("none");			
			$("div#body").fadeOut();
			
		} else {
				
			$("div#pagina").html("contatti");
		
			if(parseInt($("div#gallery").css("top")) > 0)
				TiraSuGalleria();
				$("div#body").fadeOut(
					function () {		
						$("div#content").load(url + "documenti/contatti.html", function() {				
							if($("div#body").height() > altezzaFinestra - 180)
						$("div#controller").show();

						$("div#body").css("overflow", "hidden");
						$("div#body").css("height", altezzaFinestra - 180);
						$("div#body").fadeIn();
					}
				);
			});
			
		}		
				
	});	
	
	$("li#news").click(function () {
		
		if($("#pagina").html() == "notizie") {
			$("#pagina").html("none");			
			$("div#body").fadeOut();
		} else {
			$("div#pagina").html("notizie");
			if(parseInt($("div#gallery").css("top")) > 0)
				TiraSuGalleria();
				$("div#body").fadeOut(
					function () {
						$("div#content").load(url + "documenti/news.php", function() {				
							if($("div#body").height() > altezzaFinestra - 180)
								$("div#controller").show();
							$("div#body").css("overflow", "hidden");
							$("div#body").css("height", altezzaFinestra - 180);
							$("div#body").fadeIn();						
					}
				);
			});

		}		

	});	

	$("li#home").click(function () {

		$("#pagina").html("none");			
		$("div#body").fadeOut();
		if(parseInt($("div#gallery").css("top")) > 0)
				TiraSuGalleria();
				
	});

	$("span#su").click(function () {

		max = altezzaFinestra-180 - $("div#content").height();

		if(parseInt($("div#content").css("margin-top")) > max) {
			$("div#content").animate({
				marginTop: "-=150"
			});
		}

	});

	$("span#giu").click(function() {
		if(parseInt($("div#content").css("margin-top")) < 0) {
			$("div#content").animate({
				marginTop: "+=150"
			});
		}
	});

	$("div#header, div#footer").live("mouseover", function () {
		CanMove = 0;
	});
	
	$("div#header, div#footer").live("mouseout", function () {
		CanMove = 1;
	});	

	sizeOfWindow();
	sviluppaGallery("#gallery ul");

	move = 1;
	mMouse();	
});

function mMouse() {
	$().mousemove(function(e){
		x = e.pageX;
		y = e.pageY;
		
		// Posizione?
		if(move == 2) {
			media = altezzaFinestra/2;
		
			if(y < media)
				$("#consolle").html("sopra");
			else
				$("#consolle").html("sotto");
		
			$("#consolle").html(media-y);
		
			// Massimo e minimo! però!
			altezzaImg = $("#loader").height();
		
			rapporto = altezzaImg/altezzaFinestra;

			$("div#background").css("background-position", "center "+ -(y*rapporto)/2 + "px");

		}
	});	
}

function sviluppaGallery(dove) {
	
	$(dove + " li").each(function (e) {
		
		immagine = url+"/php/resize.php?img=" + $($(this).find("img")).attr("imm") + "&h=" + (altezzaFinestra - 43 - 80);
		$(this).html('<img src="' + immagine + '" />');

	});
	
}

function spostaGalleria(v) {

 	var larghezzaA = 0;

	$("div#gallery ul li img").each(function(e) {
		if($(this).width() > 0)
		{
			larghezzaA = larghezzaA + parseInt($(this).width());
		}
	});

	larghezzaA-=larghezzaFinestra;

	ratio = larghezzaA/larghezzaFinestra;

}

function TiraGiuGalleria() {
	move = 0;
	
	creaInterfacciaSpostamento();

	$("div#gallery").animate({
		top: 70
	});

	spostaGalleria();

	var larghezzaA = 0;

	$("div#gallery ul li img").each(function(e) {
		if($(this).width() > 0)
		{
			larghezzaA = larghezzaA + parseInt($(this).width());
		}
	});

	larghezzaA-=larghezzaFinestra;

	ratio = larghezzaA/larghezzaFinestra;

	/*if(-(x * ratio) > -larghezzaA-larghezzaFinestra)
		$("div#gallery ul").css("margin-left", -(x * ratio) + "px");*/
	
}

function TiraSuGalleria() {
	move = 1;
	discrea();
	
	$("div#gallery").animate({
		top: - $("div#gallery").height()
	});
}

function sizeOfWindow() {

	larghezzaFinestra = $(window).width();
	altezzaFinestra = $(window).height();	

	var immagine = url+"/php/resize.php?img="+Math.ceil(Math.random()*3)+"&w=" + larghezzaFinestra;
	//var immagine = "http://172.16.196.2:8888/php/resize.php?img=5&w=" + larghezzaFinestra;	
	$("img#loader").attr("src", immagine);
	$("img#loader").bind("load", function() {
		$("div#background").css("background-image", "url("+immagine+")").fadeIn();
	});

	$("div#gallery").css("height", altezzaFinestra - 70 - 43);
	$("div#gallery").css("top", - altezzaFinestra - 70 - 43)

	builtInterface();	
}

function builtInterface() {

	$("body").css("overflow", "hidden");
	$("div#footer").css("top",  altezzaFinestra - 43);
	$("div#footer").css("width", larghezzaFinestra);

	$("li#scriviAsara").html('<a href="mailto:info@sarazanier.com"><img border="0" src="media/iconeWeb/scriviAsara.png" /></a>');
	$("li#fb").html('<img src="media/iconeWeb/fb.png" />');
	$("li#ms").html('<img src="media/iconeWeb/myspace.png" />');
	$("li#blog").html('<img src="media/iconeWeb/blog.png" />');
	
}

function discrea() {
	$("#spostamentoDestra").remove();
	$("#spostamentoSinistra").remove();	
}

function creaInterfacciaSpostamento() {

	$("#contenitore").append('<div id="spostamentoSinistra" style="background-image: url(\'/media/sx.png\'); background-position: center center"> </div>');
	$("#contenitore").append('<div id="spostamentoDestra" style="background-image: url(\'/media/dx.png\'); background-position: center center"> </div>');

	$("#spostamentoSinistra").css({
		position: 'absolute',
		top: '75px',
		width: '90px',
		height: altezzaFinestra - 123
	});
	
	$("#spostamentoDestra").css({
		position: 'absolute',
		left: larghezzaFinestra - 90,
		top: '75px',
		width: '90px',
		height: altezzaFinestra - 123
	});

	var larghezzaA = 0;

	$("div#gallery ul li img").each(function(e) {
		if($(this).width() > 0)
		{
			larghezzaA = larghezzaA + parseInt($(this).width());
		}
	});
	larghezzaA -= larghezzaFinestra - 100;

	$("#spostamentoDestra").bind("click", function() {
		if(parseInt($("div#gallery ul").css("margin-left")) > -1 * larghezzaA + larghezzaFinestra) {
			$("div#gallery ul").animate({
				marginLeft: "-=" + (larghezzaFinestra/2)
			});
		}
	});
	
	$("#spostamentoSinistra").bind("click", function() {
		if(parseInt($("div#gallery ul").css("margin-left")) < 0) {		
			$("div#gallery ul").animate({
				marginLeft: "-=" + (-1 * larghezzaFinestra/2)
			});
		}
	});

}