// JavaScript Document
var timeoutBg;
var indexBgDiv = 0;
var indexBg = 1;
var totBg = 15;
var selectedRef = "home";
var footer = false;
var tendina = false;
var tendinaOpenMove = false;
var galleryIndex = 0;
var totGallery = 47;

$(document).ready(function() {
	
	if (jQuery.browser.msie) {	
		if (parseInt(jQuery.browser.version) < 8) {
			$(".facebookWidgetContenitor").css("marginLeft","-490px");
		}
		if (parseInt(jQuery.browser.version) < 7) {
			$(".facebookOpen").wrap("<a href='http://www.facebook.com/pastagarofalo' target='_blank'></a>");
			$(".mipiacecontenitor").html("");
		}
	}
	
	moveGallery(1);
	
	//$(".inAnteprima").css("opacity","0");
	//$(".inAnteprima").css("marginTop","-55px");
	$(".imagePreloader").css("opacity","0");
	$(".tendina").hide();
	
	//timeoutBg = setInterval(changeBg,9000);
	$(".bg:eq(0)").css("background","url(img/bg/bg_"+indexBg+".jpg) repeat top center");
	$(".bg:eq(1)").css("background","url(img/bg/bg_"+(indexBg+1)+".jpg) repeat top center");
	checkResize();
	
	$(".hover").mouseover(function() {
		$(this).attr("src",$(this).attr("src").replace("_OFF","_ON"));
	}).mouseout(function() {
		if (!$(this).hasClass("selected") && !$(this).hasClass("tendinaBtnSelected")) {
			$(this).attr("src",$(this).attr("src").replace("_ON","_OFF"));
		}
	});
	
	$(".sectionOpener").click(function() {
		if (selectedRef == $(this).attr("ref")) return;
		changeSection($(this).attr("ref"));
		deselectAllBtns();
		$(this).addClass("selected");
	});
	
	$(".openAlCinemaBtn").click(openAlCinema);
	$(".alCinema .chiudi").click(closeAlCinema);
	
	$(".openInTvBtn").click(openInTv);
	$(".inTv .chiudi").click(closeInTv);
	
	$(".openCreditsBtn").click(openCredits);
	$(".credits .chiudi").click(closeCredits);
	
	$(".galleryAvanti").click(function() { moveGallery(1); });
	$(".galleryIndietro").click(function() { moveGallery(0); });
	
	
	$(".facebookOpen").click(function() {
		if (jQuery.browser.msie) {	
			if (parseInt(jQuery.browser.version) < 7) {
				
			} else {
				if (parseInt($("#facebookWidget").css("marginTop")) < 0) {
					$("#facebookWidget").animate({marginTop:0},500,"linear");
				} else {
					$("#facebookWidget").animate({marginTop:"-465px"},500,"linear");
				}	
			}
		} else {
			if (parseInt($("#facebookWidget").css("marginTop")) < 0) {
				$("#facebookWidget").animate({marginTop:0},500,"linear");
			} else {
				$("#facebookWidget").animate({marginTop:"-465px"},500,"linear");
			}	
		}
		
		
	});
	
	if (jQuery.browser.msie) {	
		if (parseInt(jQuery.browser.version) < 7) {
			$.ifixpng('img/pixel.gif');
			$('img[src$=.png]').ifixpng();
			$(".siteContenitor").ifixpng();
			DD_belatedPNG.fix(".footerBG")
		}
		
		$('#backstage, #versioneintegrale, #trailer, #premiere').each(function(){
			$(this).data('htmlcontent', $(this).html());
		});
	}
});
$(window).resize(function() {
	//alert("resized");	
	checkResize();
});
function checkResize() {
	var documentHeight = parseInt($(document).height());
	var windowHeight = parseInt($(window).height());
	//alert("documentHeight: "+documentHeight+" windowHeight:"+windowHeight);
	if ((documentHeight > windowHeight) && (windowHeight >= 784)) {
		$(".bg").css("height",windowHeight+"px");
	} else {
		$(".bg").css("height",documentHeight+"px");
	}
	if (parseInt($(".bg").css("height")) < documentHeight) {
		$(".bg").css("height",documentHeight+"px");																					  
	}


	var marginTop = Math.floor((parseInt($(window).height()) - 784) / 2);
	if (marginTop < 0) marginTop = 0;
	$(".siteContenitor").css("top",marginTop+"px");
}

function changeBg() {
	if (jQuery.browser.msie) {
		$(".bg:eq("+indexBgDiv+")").css("display","none");
		indexBgDiv = (indexBgDiv+1 > 1) ? 0 : 1;
		indexBg = (indexBg+1 > totBg) ? 1 : indexBg+1;
		$(".bg:eq("+indexBgDiv+")").css("display","block").css("background","url(img/bg/bg_"+indexBg+".jpg) repeat top center");
	} else {
		$(".bg:eq("+indexBgDiv+")").css("opacity","1").css("display","block").animate({opacity:0},1000,"linear");
		indexBgDiv = (indexBgDiv+1 > 1) ? 0 : 1;
		indexBg = (indexBg+1 > totBg) ? 1 : indexBg+1;
		$(".bg:eq("+indexBgDiv+")").css("opacity","0").css("display","block").css("background","url(img/bg/bg_"+indexBg+".jpg) repeat top center").animate({opacity:1},1000,"linear");
	}
}

function changeSection(section) {
	if (jQuery.browser.msie) {
		$("#"+selectedRef).css("display","none");
		$("#"+section).css("display","block");
	} else {
		$("#"+selectedRef).animate({opacity:0},500,"linear",function() {
			$(this).css("display","none");													 
		});
		$("#"+section).css("opacity","0").css("display","block").animate({opacity:1},500,"linear");
	}
	selectedRef = section;
	openInAnteprima();
	
	
	
	if (jQuery.browser.msie){
		$('#backstage, #versioneintegrale, #trailer, #premiere').html('');
		
		if(section=='backstage' || section=='versioneintegrale' || section=='trailer' || section=='premiere')
			$('#'+section).html($('#'+section).data('htmlcontent'));
	}
	
	switch(section) {
		case "cast":
			if (!$("#paneCast").parent().hasClass("jScrollPaneContainer")) $('#paneCast').jScrollPane();
			//openInAnteprima();
		break;
		case "ilfilm":
		case "immagini":
		case "trailer":
		case "backstage":
			
		break;
		case 'versioneintegrale':
			var pageTracker = _gat._getTracker("UA-126990-63");
			pageTracker._trackPageview("/versioneintegrale");
			$.get("countVersioneIntegrale.php");
			
		break;
		default:
			//closeInAnteprima();
			//footer = false;
		break;	
	}
	changeBg();
}

function openInAnteprima() {
	//if ((tendina == false) && (tendinaOpenMove == false)) {
		if (jQuery.browser.msie) {
			$(".inAnteprima").css("marginTop","0px");
		} else {
			$(".inAnteprima").animate({opacity:1},500,"linear");
		}
	//}
	footer = true;
}

function closeInAnteprima() {
	if (jQuery.browser.msie) {
		$(".inAnteprima").css("marginTop","-55px");
	} else {
		$(".inAnteprima").animate({opacity:0},500,"linear");
	}
	
}

function deselectAllBtns() {
	$(".selected").each(function(i) {
		$(this).attr("src",$(this).attr("src").replace("_ON","_OFF"));							 
	}).removeClass("selected");
}

function openAlCinema() {
	tendinaOpenMove = true;
	closeInTv();
	closeCredits();
	tendina = true;
	$(".openAlCinemaBtn").addClass("tendinaBtnSelected");
	//$(".alCinema").slideDown();
	/*$(".alCinema").css("height","1px");
	$(".alCinema").animate({height:"84px",display:"block"},500,"linear");*/
	$(".alCinema").show();
	tendinaOpenMove = false;
	setTimeout("scrollToBottom()",500);
}
function closeAlCinema() {
	tendina = false;
	$(".alCinema").hide();
	$(".openAlCinemaBtn").removeClass("tendinaBtnSelected");
	$(".openAlCinemaBtn").attr("src",$(".openAlCinemaBtn").attr("src").replace("_ON","_OFF"));
}

function openInTv() {
	tendinaOpenMove = true;
	closeAlCinema();
	closeCredits();
	tendina = true;
	$(".openInTvBtn").addClass("tendinaBtnSelected");
	$(".inTv").show();
	tendinaOpenMove = false;
	setTimeout("scrollToBottom()",500);
}
function closeInTv() {
	tendina = false;
	$(".inTv").hide();
	$(".openInTvBtn").removeClass("tendinaBtnSelected");
	$(".openInTvBtn").attr("src",$(".openInTvBtn").attr("src").replace("_ON","_OFF"));
}

function openCredits() {
	tendinaOpenMove = true;
	closeAlCinema();
	closeInTv();
	tendina = true;
	$(".openCreditsBtn").addClass("tendinaBtnSelected");
	$(".credits").show();
	tendinaOpenMove = false;
	setTimeout("scrollToBottom()",500);
}
function closeCredits() {
	tendina = false;
	$(".credits").hide();
	$(".openCreditsBtn").removeClass("tendinaBtnSelected");
	$(".openCreditsBtn").attr("src",$(".openCreditsBtn").attr("src").replace("_ON","_OFF"));
}

function moveGallery(action) {
	$("#imageContenitor img").attr("src","");
	if (action == 1) { galleryIndex = ((galleryIndex+1) > totGallery) ? 1 : galleryIndex+1; }
	if (action == 0) { galleryIndex = ((galleryIndex-1) <= 0) ? totGallery : galleryIndex-1; }
	$("#imageContenitor img").attr("src","img/gallery/"+galleryIndex+".jpg");
	
	var url = "http://www.facebook.com/sharer.php?u="+BASE_URL+"%3Fimg="+galleryIndex;
	$("#immagini .share").attr("href",url);
}

function mipiace(url) {
	return '<iframe src="http://www.facebook.com/plugins/like.php?href='+url+'&amp;layout=button_count&amp;show_faces=false" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:350px; height:30px;" allowTransparency="true"></iframe>';
}

function gotoHomeFromFlash() {
	if (selectedRef == "home") return;
	changeSection("home");
	deselectAllBtns();	
}

function scrollToBottom() {
	checkResize();
	$.scrollTo(parseInt($(document).height()),500, {easing:'easeOutCubic'});	
}
