/* jQuery */
var $ = jQuery;		
var noSlideShow = false;
var isHome = false;

/* Cufon */
var s = "";
Cufon.replace(s, {
	hover: true
});
		
$(function(){
	// Hide arrows.
	$("#arrowLeft, #arrowRight").hide();

	// Vertical-align fix.
	alignVertically();
	
	$("#home_first_picture").fadeIn();
	
	/* Start Box Left */
	if (noSlideShow == false)
	{
		var onLeftBox = false;
		var onLeftArrow = false;
		$("#boxLeft").hover(
			function(){
				onLeftBox = true;
				$("#arrowLeft").fadeIn();
			},
			function(){
				onLeftBox = false;
				setTimeout(function(){
					if ((onLeftBox == false) && (onLeftArrow == false))
					{
						$("#arrowLeft").fadeOut();
					}
				}, 500);
			}
		);
		$("#arrowLeft").hover(
			function(){
				onLeftArrow = true;
				$("#arrowLeft").fadeIn();
			},
			function(){
				onLeftArrow = false;
				setTimeout(function(){
					if ((onLeftBox == false) && (onLeftArrow == false))
					{
						$("#arrowLeft").fadeOut();
					}
				}, 500);			
			}
		);	
	}
	/* End Box Left */
	
	/* Start Box Right */
	if (noSlideShow == false)
	{
		var onRightBox = false;
		var onRightArrow = false;
		$("#boxRight").hover(
			function(){
				onRightBox = true;
				$("#arrowRight").fadeIn();
			},
			function(){
				onRightBox = false;
				setTimeout(function(){
					if ((onRightBox == false) && (onRightArrow == false))
					{
						$("#arrowRight").fadeOut();
					}
				}, 500);
			}
		);
		$("#arrowRight").hover(
			function(){
				onRightArrow = true;
				$("#arrowRight").fadeIn();
			},
			function(){
				onRightArrow = false;
				setTimeout(function(){
					if ((onRightBox == false) && (onRightArrow == false))
					{
						$("#arrowRight").fadeOut();
					}
				}, 500);			
			}
		);	
	}	
	/* End Box Right */
	

	
	var timeout = 0;
	if (isHome)
	{
		timeout = 500;
	}
	setTimeout(function(){
		// Start the slideshow.
		$("#slideshow").cycle({
			fx: "fade",
			next: "#arrowRight img",
			prev: "#arrowLeft img"
		});			
	}, timeout);
	
			
	// Set autoplay to "true".
	SaveCookie("autoplay", "true");
	
	// Configure Click Event Handler for Arrows
	$("#arrowLeft img, #arrowRight img").click(function(){	
		$("#slideshow").cycle("pause");
		$("#autoplay").html("Autoplay Off");
		return false;
	})
	
	// Menu
	$("#open_close_menu").click(function(){
		if ($("#header").is(":visible"))
		{
			$("#header").slideUp();
			$("#logo, #top_menu").fadeOut("slow", function(){
				$("#header").hide("slow");
			});
			$("#open_close_menu").html("Menu On");
		}
		else
		{
			$("#header").slideDown();
			$("#logo, #top_menu").fadeIn("slow");
			$("#open_close_menu").html("Menu Off");
		}
	});
	
	// Autoplay
	$("#autoplay").click(function(){
		// Read the cookie.
		var autoplay = ReadCookie("autoplay");
		
		// Default setting is "true".
		if (autoplay == undefined) autoplay = "true";
		
		// If autoplay is ON (true), then stop autoplay.
		if (autoplay == "true")
		{
			$("#slideshow").cycle('pause');
		}
		else
		{
			$("#slideshow").cycle('resume');
		}
		
		// Invert the value in the cookie.
		if (autoplay == "true") 
		{
			autoplay = "false"
		}
		else
		{
			autoplay = "true";
		}
		SaveCookie("autoplay", autoplay);
		
		// Configure "Autoplay" button.
		if (autoplay == "true") 
		{
			$("#autoplay").html("Autoplay On");
		}
		else
		{
			$("#autoplay").html("Autoplay Off");
		}
	});
	

	
	// Menu handlers
	$("#my_style").click(function(){
		$("#submenu_featured").hide();
		$("#submenu_my_style").show();
	});
	$("#featured").click(function(){
		$("#submenu_featured").show();
		$("#submenu_my_style").hide();
	});	
	
	// Date Picker
	$(".datepicker").datepicker({
        dateFormat: "dd/mm/yy"
    });
	
	// Contact form validation.
	$("#btnSend").click(function(){
		if ($("#firstName").val() == "")
		{
			alert('Per favore inserisci il tuo nome.');
			return false;
		}
		if ($("#lastName").val() == "")
		{
			alert('Per favore inserisci il tuo cognome.');
			return false;
		}		
		if ($("#telephone").val() == "")
		{
			alert('Per favore inserisci il tuo numero di telefono.');
			return false;
		}		
		if ($("#emailAddress").val() == "")
		{
			alert('Per favore inserisci il tuo indirizzo e-mail.');
			return false;
		}		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var address = $("#emailAddress").val()
		if(reg.test(address) == false) {
		  alert('Indirizzo e-mail non valido.');
		  return false;
		}	
		if ($("#message").val() == "")
		{
			alert('Per favore inserisci il messaggio.');
			return false;
		}				
		return true;
	});

}
);

function StartMusicAndConfigureMusicButton()
{
	// Stop the slideshow if necessary (this allows us to resume it later).
	var music = ReadCookie("music");
	if (music == undefined)
	{
		music = "true";
	}
	
	// Start Music
	if (music == "true")
	{
		soundManager.start('audio');
	}
	
	// Configure "Music" button.
	if (music == "false")
	{
		$("#music").html("Music Off");
	}
}

$(window).resize(function()
{
	alignVertically();
});

function alignVertically()
{
	// Vertical-align fix.
	var pageHeight = $("html").height();
	var footerHeight = $("#footer").height();
	var headerHeight = $("#header_wrapper").height();
	$("#wrapper").height(pageHeight - footerHeight - headerHeight - 40);
}

function ShowSubmenu(s)
{
	$("#submenu_" + s).show();
	HighlightTopmenu(s);
}

function HighlightTopmenu(s)
{
	$("#top_menu a#" + s).css("color", "#eee");
}

function HighlightSubmenu(s)
{
	$("#submenu_option_" + s + " a").css("color", "#eee");
}

/* Cookies */
var Cookies = {
    init: function () {
        var allCookies = document.cookie.split('; ');
        for (var i=0;i<allCookies.length;i++) {
            var cookiePair = allCookies[i].split('=');
            this[cookiePair[0]] = cookiePair[1];
        }
    },
    create: function (name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
        this[name] = value;
    },
    erase: function (name) {
        this.create(name,'',-1);
        this[name] = undefined;
    }
};
Cookies.init();

function SaveCookie(name, value)
{
	Cookies.create(name, value , 1);
}

function ReadCookie(name)
{
	return Cookies[name];
}

function NoSlideshow()
{
	noSlideShow = true;
	$("#arrowRight, #arrowLeft").hide();
	$("#inner_inner_wrapper").width(960);
	$("#inner_wrapper").width(960);
	$("#wrapper").width(960);
	$("#boxLeft, #boxRight").hide();
}

function PlayMusic()
{
	// Show Menu Menu
	$("#music").show();
	
	// Music
	$("#music").click(function(){
		// Read the cookie.
		var music= ReadCookie("music");
		
		// Default setting is "true".
		if (music == undefined) music = "true";
		
		// If music is ON (true), then stop.
		if (music == "true")
		{
			soundManager.stop('audio');
		}
		else
		{
			soundManager.start('audio');
		}
		
		// Invert the value in the cookie.
		if (music == "true") 
		{
			music = "false"
		}
		else
		{
			music = "true";
		}
		SaveCookie("music", music);
		
		// Configure "Music" button.
		if (music == "true") 
		{
			$("#music").html("Music On");
		}
		else
		{
			$("#music").html("Music Off");
		}
	});	
}



