/*
 *	
 *	Developed on a mbp late 2008
 *	10/10/2010
 *	
 *	Tested on Safari, Firefox, Chrome, IE 7+
 *	
 */

// Global Vars //
var g_w;
var g_h;
var last;
var js = new Array();
var call;
var controllo_altezza = 0;
/////////////////

$(function() {
	
	////////////////////////////////////////////
	/////__ _GOOGLE ANALYTICS WORK OUT_ __//////
	////////////////////////////////////////////
	$("#heyAnaliticsLoveMe").hide(); $("a").live("click", function() { 
		$("#heyAnaliticsLoveMe").find("iframe").attr("src", "/analytics/?p=" + $(this).attr("href"));
	});
	////////////////////////////////////////////
	/////__ _GOOGLE ANALYTICS WORK OUT_ __//////
	////////////////////////////////////////////
	
	$("#debug").hide();
	
	// Set that every XXX milliseconds the history will be checked.
	setInterval('readHistory()', 100);
	setInterval('altezzaContent()', 50);
	
	$("#content").fadeIn();

	$("#menu").css({
		paddingTop: ($(window).height() / 2) - $("#menu").height()/1.8,
		height: $(window).height()/2 + $("#menu").height()/1.8
	});
	
	$("#footer").css({
		position: 'absolute',
		padding: 10,
		top: $(window).height() - 200,
		left: 15
	})
	
	$(window).bind("resize", function() {
		
		g_w = $(window).width();
		g_h = $(window).height();
		
		// Posiziono in maniera giusta.
		$("#logo").css({
			paddingTop: (g_h / 2) - 81,
			height: g_h /2 + 81
		});
		
		$("#background").css({
			position: 'fixed',
			top: 0,
			left: 0,
			zIndex: -1
		});
		
		$("#background div").css({
			width: g_w,
			marginTop: -g_h/2
		});
		
	}).trigger("resize");
	
	$("a").live("click", function() {
		if($(this).attr("ajax") != undefined) {
			location.hash = $(this).attr("ajax");
			return false;
		}
	});
	
	// SHOWROOM \\
	$("#showroom").css({
		width: 980,
		height: 300,
		position: 'absolute',
		top: -400,
		padding: '15px',
		left: (g_w/2) - 505,
		backgroundImage: 'url(/static/images/bg75.png)'
	});
	
	// OPERE \\
	$("#fullscreen").css({
		position: 'fixed',
		width: '830px',
		height: g_h,
		top: 0,
		left: 194,
		backgroundColor: '#383a42',
		zIndex: 999
	}).hide();
	
	$("#chiudiFullscreen").css({
		cursor: 'pointer',
		color: 'white',
		padding: '5px',
		margin: '5px'
	}).live("click", function() {
		$("#fullscreen").fadeOut();
	});
	
	$("#showOpera").css({
		color: 'white',
		height: g_h - 40
	});
	
	$("#prevOpera").live("click", function() {
		$(".elemento"+(parseInt(attuale)-1)).trigger("click");
	});
	
	$("#nextOpera").live("click", function() {
		$(".elemento"+(parseInt(attuale)+1)).trigger("click");
	});
	///////////
	
	$("#SendSposi").live("keypress", function(event) {
		if (event.keyCode == '13') {
			$.get("/services/nozze/index.php", { nome: $("#SendSposi").val() }, function(a) {
				if(a.length > 0)
					$("#r").html(a);
				else
					$("#p").text("Non trovo nessuna lista con queste caratteristiche!");
			});
		}
	});
	///////////
	
	$("form").live("submit", function() {
		
		$.post($(this).attr("action"), $(this).serialize(), function(a) {
			if(a != "1")
				alert(a);
			else
				$(".res").html("Il messaggio è stato inviato correttamente.");
		})
		
		return false;
	});
	
});

function readHistory() {
	
	if(location.hash != last) {
		
		if(location.hash.length == 0) {
			location.src = "/";
			/*$("#content").css({
				width: 0
			});*/
		}
		
		last = location.hash;
		
		if(last == "#home") {
		/*	$("#content").animate({
				width: 0
			});*/

			$("#submenu").animate({
				width: 0
			});
		}
		
		// Ok, now we'll have to call *_framework_/structure.php?page=last* for check what sort of page is it. \\
		$.post("/_framework_/structure.php?page=" + last.replace("#", ""), function(a) {
			
			if(a.error == 0) {
				
				controllo_altezza = 0;
				
				if(a.tipologia == "contenutistica") {
					
					$("#top").die("click").remove();
					$("#bottom").die("click").remove();
					$("#content div").animate({
						marginTop: "0px"
					});
					
					typeContenutistica(a.pagina, a.callback, a.callout);
					
				}
				
				if(a.tipologia == "prodotti") {
					
					$("#top").die("click").remove();
					$("#bottom").die("click").remove();
					$("#content div").animate({
						marginTop: "0px"
					});
					
					typeProdotti(a.pagina, a.callback, a.callout);
					
				}
				
				if(a.tipologia == "notizie") {
					
					$("#top").die("click").remove();
					$("#bottom").die("click").remove();
					$("#content div").animate({
						marginTop: "0px"
					});
					
					typeProdotti(a.pagina, a.callback, a.callout);
				}
				
				if(a.menu.length > 0) {
					
					$("#submenu").css({
						height: 210
					});
					
					$("#submenu").load(a.menu, function() {
						$("#submenu").css({
							height: g_h/2 + $("#submenu").height()/2,
							paddingTop: g_h/2 - $("#submenu").height()/2
						}).show();
						$("#submenu").animate({
							width: 170
						});
					});
					
				} else {
					$("#submenu").animate({
						width: 0
					});
				}
				
				if(a.js.length > 0) {
					
					if($.inArray(a.js, js))
					{
						js.push(a.js);
						$.getScript(a.js);
					}
					
				}
			}
		}, "json");
		
	}
	
}

function typeContenutistica(what, x, y) {
	
//	alert(what);
	
	$("#content").animate({
		width: 0
	}, function() {
		
		$("#content div").load(what, function() {
			
			$("#content").animate({
				width: 500
			});
			
			altezzaContent();
			
		});
		
	});
	
}

function typeProdotti(what, x, y) {
	
	$("#debug").append("Called " + new Date().getTime() + "<br />");
	$("#debug").append("The animation ended " + new Date().getTime() + "<br />");
	$("#content div").load(what, function() {
			
		$("#debug").append("Content Loaded " + new Date().getTime() + "<br />");
			
		$("#content").animate({
			width: 500
		});
		
		eval(x);
		
		altezzaContent();
		
	});
	
}

/*function typeNotizie(what, x, y) {
	
	$("#debug").append("Called " + new Date().getTime() + "<br />");
	$("#debug").append("The animation ended " + new Date().getTime() + "<br />");
	$("#content div").load(what, function() {
			
		$("#debug").append("Content Loaded " + new Date().getTime() + "<br />");
			
		$("#content").animate({
			width: 500
		});
		
		eval(x);
		
		altezzaContent();
		
	});
	
}*/

function altezzaContent() {
	
	if(controllo_altezza == 0) {
		
		if(g_h < $("#content").height()) {
			
			controllo_altezza = 0;
			
			$("#content").css({
				paddingTop: 0
			});
			
		} else {
			$("#content").css({
				paddingTop: (g_h/2) - $("#content").height()/2,
				paddingBottom: (g_h/2) - $("#content").height()/2
			});
		}
		
	}
	
}

function showroom() {

	$("#gavillucci").fadeTo(500, 0.3);
	
	$("#close").css({
		position: 'absolute',
		left: (g_w/2) + 410 + 75,
		top: (g_h/2) - 165,
		zIndex: 999,
		cursor: 'pointer'
	}).fadeIn().live("click", function() {
		if ($.browser.msie) {
			window.location.reload();
		}
		else {
			$("#showroom").animate({
				top: -900
			});
			$("#close").fadeOut();
			$("#gavillucci").fadeTo(500, 1);
		}
	});
	
	$("#showroom").animate({
		top: (g_h/2) - 150,
		zIndex: 999
	});
	
	return false;
	
}
