﻿// JavaScript Document
function addButtonEvents(){
	$(".submenu").hover(
	  function () {
	    $(this).children("ul").css("display","block");
		var lol = $(this).position();
		$(this).css('background-color','#FFFFFF');
	  }, 
	  function () {
		  deselectAll();	
		$(this).children("ul").css("display","none");
		$(this).css('background-color','#DAD9D7');
	  }
	);
	
	$(".menu").hover(
		function(){
			$(this).css('background-color','#EC873C');
		},
		function (){
			deselectAll();			
		}
	);
	$(".thirdsub").hover(
	  function () {
		$(".thirdmenu").css("display","block");
		$(this).css("background-image","url(../images/bckroll.png)");
		$(this).css("color","#000000");
		var lol = $(this).position();
	  }, 
	  function () {
		$(this).css("background-image","none");
		$(this).css("color","#FFFFFF");
		$(".thirdmenu").css("display","none");
	  }
	);
}

function doSelect(what){
	selected=what;
	deselectAll();
}

function doSubSelect(what){
	lastHover = what;
	deselectAll();
}

function deselectAll(){
 $(".menu").each(function(id){
		if(selected != $(this).attr("menu")){
			$(this).css('background-color','#2B2117');		
	    }else {
			$(this).css('background-color','#EC873C');		
		}
 });
 $(".thirdsub").each(function(id){
		if(lastHover != $(this).attr("menu")){
			$(this).css("background-image","none");
			$(this).css('background-color','#2B2117');
			$(this).css("color","#FFFFFF");	
	    }else {
			$(this).css("background-image","url(../images/bckroll.png)");
			$(this).css("color","#000000");		
		}
 });
}

function handleChange(event){
	var urlToLoad = '';
	var fistaddress = event.path.split("/");
	doSelect(fistaddress[1]);
	switch(fistaddress[1]){
		case "":
			hideAll();
			$("#container").css("display","none");
			stopRotation();
			SWFAddress.setTitle("Arredamenti Verona - Castellani e Cunico");
			_gaq.push(['_trackPageview']);
			break;
		case "sitemap":
			/*hideAll();
			$("#container").css("display","block");
			urlToLoad = 'sitemap.aspx';
			SWFAddress.setTitle("Sitemap - Castellani e Cunico");
			stopRotation();
			_gaq.push(['_trackPageview','sitemap']);*/
			hideAll();
			$("#container").css("display","block");
			urlToLoad = 'sitemap.aspx';
			stopRotation();
			_gaq.push(['_trackPageview','grazie']);
			break;
		case "errore":
			hideAll();
			$("#container").css("display","block");
			urlToLoad = 'errore.aspx?'+fistaddress[2];
			stopRotation();
			_gaq.push(['_trackPageview','errore']);
			break;
			
		case "grazie":
			hideAll();
			$("#container").css("display","block");
			urlToLoad = 'grazie.aspx';
			stopRotation();
			_gaq.push(['_trackPageview','grazie']);
			break;

        case "chi-siamo":
			hideAll();
			$.fn.background.changeGallery(['images/galleria/test1.png', 'images/galleria/test2.jpg', 'images/galleria/test3.jpg', 'images/galleria/test1.png']);
			urlToLoad = 'chi-siamo.aspx';
			stopRotation();
			SWFAddress.setTitle("Chi Siamo - Castellani e Cunico");
			_gaq.push(['_trackPageview','chi-siamo']);
			break;
		case "proposte":
		
		    $("#showall-container").css("display", "block");
		    $("#showall-content").css("display", "block");
			$("#sub-menu").css("display","block");
			$('#loaded').css("display","none");
			$(".thirdmenu").css("display","block");
			$(".thirdsub:first").css("background-image","url(../images/bckroll.png)");
			$(".thirdsub:first").css("color","#000000");
			$(".thirdsub").css("visibility","visible");
			var pos = $(".submenu:first").position();
			$(".thirdmenu:first").css("left",pos.left);
			$("#sxArrow").css("display", "block");
			$("#rxArrow").css("display", "block");
			$("#container").css("display","none");
			$.fn.background.howToPlay(false);
			//TODO:OPEN SUB MENU
			break;
		case "dove-siamo":
			hideAll();
			urlToLoad = 'dove-siamo.aspx';
			stopRotation();
			SWFAddress.setTitle("Dove Siamo - Castellani e Cunico");
			_gaq.push(['_trackPageview','dove-siamo']);
			break;
		case "contatti":
			hideAll();
			$.fn.background.changeGallery(['images/galleria/test3.jpg', 'images/galleria/test2.jpg', 'images/galleria/test1.png', 'images/galleria/test.png']);
			urlToLoad = 'contatti.aspx?'+fistaddress[2];
			stopRotation();
			SWFAddress.setTitle("Contattaci - Castellani e Cunico");
			_gaq.push(['_trackPageview','contatti']);
			break;
	}
	section = fistaddress[1];
	if(urlToLoad != ''){
		$.ajax({
				url: urlToLoad,
				success: function(data) {
					$('#loaded').html(data);
					$('#loaded').css("display","block");
					$('#container').css("bottom","-500px");
					openPage(section);
					if($("#map-canvas") != null){
						if(typeof(map) !== 'undefined'){
							var marker = new google.maps.Marker({position: new google.maps.LatLng(45.355553,11.339006),map:map,title:"Castellani e Cunico"});
							//var contentString = '<div id="content" style="color:#000">'+
							//'<h4 style="color:#000">Castellani e Cunico S.R.L.</h4>'+
							//'Via Marcabella, 37040 Zimella Verona, Veneto, Italia'+
							//'</div>';
							
							var addrTo = "Via Marcabella, 37040 Zimella Verona, Veneto, Italia";
							var contentString = "<div style='color:#000'><b>Castellani e Cunico S.R.L.</b><br />" +
								"Via Marcabella, 37040 Zimella Verona, Veneto, Italia<br />" +
								"<form id='tfrm' action='http://maps.google.it/maps' target='_blank'>" +
								"	<input value='1' name='lsm' type='hidden'/>" +
								"	<input value='" + addrTo + "' name='daddr' type='hidden'/>" +
								"	<div style='text-align:center'>Indirizzo di partenza</div><br />" +
								"	<div style='text-align:center'>" +
								"		<input style='border:1px #000 solid;'class='inptsmall' id='iwsaddr' name='saddr' type='text' value=''/>" +
								"		<div class='sbmt'><input type='submit' value='Go'/></div>" +
								"	</div>" +
								"</form></div>";
							var infowindow = new google.maps.InfoWindow({
								content: contentString
							});
							google.maps.event.addListener(marker, 'click', function() {
							  infowindow.open(map,marker);
							});
						}
					}
				}
		});
	}
	if(fistaddress[1] == "proposte"){
	    doSubSelect(fistaddress[2] + "/" + fistaddress[3]);
	    if (fistaddress[4] == undefined || fistaddress[5] == undefined) {
	        fistaddress[4] = "4";
	        fistaddress[5] = "121";
	    }
	    $.ajax({
	        url: "gallery.aspx?idCat=" + fistaddress[5] + "&idFam=" + fistaddress[4] + "&idFoto="+fistaddress[5],
	        success: function(data) {
	            $("#showall-container").html(data);
	            eval($("#script").html());
				SWFAddress.setTitle($("#title").html());
	            $("#script").empty();
				$("#title").empty()
	            var maxH = document.getElementById("showall-container").offsetHeight - 15;
	            if ($("#showall-container").attr("state") != "shown") {
	                $("#showall-container").animate({ top: 0 }, 500, 'linear');
	                $("#showall-content").animate({ top: maxH - 15 }, 500, 'linear');
	                $("#showall-container").attr("state", "shown");
	            }
	            $(window).resize(setMaxShowAllH);
	        }
	    });		
	}
}

function continueLoadPage(callback){

}


function hideAll(){
	$("#showall-container").css("display", "none");
	$("#showall-content").css("display", "none");
	$("#sub-menu").css("display","none");
	$(".thirdmenu").css("display", "none");
	$("#sxArrow").css("display", "none");
	$("#rxArrow").css("display", "none");
	$("#container").css("display","block");
}

function setMaxShowAllH() {
    var maxH = document.getElementById("showall-container").offsetHeight - 15;
    $("#showall-container").css("top", 0);
    $("#showall-content").css("top", maxH + "px");
}

function stopRotation(){
	//$.fn.background.pause();
}

function initializeSite(){
    //$.fn.background({ images: ['images/galleria/test.png', 'images/galleria/test1.png', 'images/galleria/test.png', 'images/galleria/test1.png'], delay: 15000 });
	$.fn.background({ images: immagini, delay: 15000 });
	$.fn.background.play();
	$.fn.background.howToPlay(true);
}

function preInitializeSite(){
	if(initialized != true){
		window.setTimeout(initializeSite,100);
		initialized = true;
	}
}

function selectCurrImg(backwards){
		$("#showall-container img").each(function (){
			$(this).removeClass("active");
		});
		
		if (navigator.appName != "Microsoft Internet Explorer"  && backwards != 1) {
			$("img[big=" + ($.fn.background.settings.counter) + "]").addClass("active");
		} else {
			$("img[big=" + ($.fn.background.settings.counter - 1) + "]").addClass("active");
		}
}

function loadImage(vUrl){
	openUpperSlide();
	$.fn.background.loadImage(vUrl);
}

function trace(what) {
    $("#console").append(what + "<br />");
    $("#console").animate({ scrollBottom: $("#console").height() }, 2);
}


$.fn.image = function(params){
	var img = new Image();
	
	$(img).load( function() {
        $(img).hide();
        $(img).fadeIn("medium");
    }).attr({ "src": params.src }).load(function() {
        $(img).appendTo("#curr-image");
        $.fn.background.removeLoading();
    });
	
	return $(img);
}



function openUpperSlide() {
    var contH = document.getElementById("showall-container").offsetHeight;
    if ($("#showall-container").attr("state") == "shown") {
        $("#showall-container").animate({ top: -(contH - 15) }, 500, 'linear');
        $("#showall-content").animate({ top: 0 }, 500, 'linear');
        $("#showall-container").attr("state", "collapse");
		$("#showall-content img").attr("src","/images/showallbutton.png");
    } else {
        $("#showall-container").animate({ top: '0px' }, 500, 'linear', function() { setMaxShowAllH() });
        $("#showall-content").animate({ top: contH-15 }, 500, 'linear');
        $("#showall-container").attr("state", "shown");
		$("#showall-content img").attr("src","/images/showallbutton_up.png");
    }

}

function cpyArray(src) {
    var dest = new Array();
    for (var i = 0; i <= src.lenght - 2; i++) {
        dest[i] = src[i];
    }
    return dest;
}

function hoverImage(img){
	img.addClass("active");	
}

function outImage(img){
	img.removeClass("active");
}

function openPage(section){
		if(section != "dove-siamo" && section != "sitemap"){
			$("#container").animate({bottom:"0px"},1000);
			$("#container").css("height","270px");
			$("#loaded").css("height","250px");
		} else if(section == "sitemap"){
			$("#container").css("height","600px");
			$("#loaded").css("height","570px");
			$("#container").animate({bottom:"0px"},1000);
		} else {
			$("#container").css("height","520px");
			$("#loaded").css("height","500px");
			$("#container").animate({bottom:"0px"},1000);
		}
}


var stile = "top=10, left=10, width=700, height=750, status=no, menubar=no, toolbar=no scrollbar=no";
 function Popup(apri) {
	window.open(apri, "", stile);
 }
