var menu_reduced = false;
var menu_btn_current;
var openingPage = true;
$(document).ready(function(){

	// Add Email address
	$('#myemail').html($.rotate13('vasb@sreebtvbvryyv.vg')).attr("href","mailto:"+$.rotate13('vasb@sreebtvbvryyv.vg'));
	
	$("#btn_disclaimer, #btn_privacy").fancybox({
        "autoDimensions" : false,
        "transitionIn"	: "elastic",
		"transitionOut"	: "elastic", 
		"width": 640,
		"height": "auto"
	});

	openPage("btn_home");
	
	if(menu_btn_current == null)
	{
		menu_btn_current = $("#btn_home");
		$("#btn_home").addClass("menu_selected");
	}
	
	$("#menu a").click(function(){
		if(openingPage == false)
		{
			if($(this).attr("id") != $(menu_btn_current).attr("id")){
				$(this).addClass("menu_selected");
				$(menu_btn_current).removeClass("menu_selected");
				openPage($(this).attr("id"));
				menu_btn_current = $(this);
			}
		}
	});
    
    $("#mp3player a").click(function(){
        toggleMusic();
    });
});

soundManager.waitForWindowLoad = false;
soundManager.url = 'javascripts/soundmanager/swf/';
soundManager.flashVersion = 9; // optional: shiny features (default = 8)
soundManager.useFlashBlock = false; // optionally, enable when you're ready to dive in
//soundManager.useHighPerformance = true;
soundManager.onload = function() {
    $.log("soundManager is ok")
    // SM2 has loaded - now you can create and play sounds!
    soundManager.createSound({
        id: "music",
        url: "mp3/spirit_of_success.mp3",
        volume: 60,
        onfinish: function() {
            playMusic("play");
        }
    });
    // Controlla il cookie
    if(musicCookie()){
        playMusic();
    } else {
        stopMusic();
    }
}
soundManager.music_status = false;
soundManager.spot_status = false;

/* Controlla lo stato del cookie musicale */
function musicCookie(mode){
    if(!mode) {
  		mode = "read";
   	}
    switch(mode){
        case "read":
            var cookie = $.readCookie('fMusic');
            if(!cookie){
                $.log("Setting cookie...");
                soundManager.music_status = true;
                musicCookie("write");
            } else {
                soundManager.music_status = eval(cookie);
            }
            $.info("Lo stato della musica è: " + soundManager.music_status);
            return eval(soundManager.music_status);
        break;
        case "write":
            $.setCookie('fMusic', soundManager.music_status, { duration: 180 });
        break;
    }
}
/* Play Music */
function playMusic(mode){
    if(!mode) {
  		mode = "play";
   	}
    if(mode != "resume"){
        soundManager.play("music");
    } else {
        soundManager.resume("music");
    }
    soundManager.music_status = true;
    $("#mp3player img").attr("src","images/mp3_pause.png");
    $("#mp3player span").text("MUSICA OFF");
}
/* Stop Music */
function stopMusic(mode){
    if(!mode) {
  		mode = "stop";
   	}
    if(mode != "pause"){
        soundManager.stop("music");
    } else {
        soundManager.pause("music");
    }
    soundManager.music_status = false;
    $("#mp3player img").attr("src","images/mp3_play.png");
    $("#mp3player span").text("MUSICA ON");
}
/* Toggle Music */
function toggleMusic(){
    if (soundManager.music_status){
        stopMusic();
    } else {
        playMusic();
    }
    musicCookie("write");
}

function infoBox(mode)
{
    var largh = substr($('#colonna_dx').css("width"),0,-2);
    $('#info_content').html("").hide();
    if (menu_reduced) {
        $('#colonna_sx,#colonna_dx').animate({height:"430px"}, 300, 'easeInQuint');
        menu_reduced = false;
    }
    switch(mode) {
        case "show":
        if(largh == 0){
            setTimeout(function(){$('#colonna_dx').animate({width:"200px"}, 300, 'easeInQuint');}, 200);
        }
        break;
        case "hide":
        if(largh > 0){
            setTimeout(function(){$('#colonna_dx').animate({width:"0px"}, 300, 'easeOutQuint');}, 200);
        }
        break;
    }
}

	function openPage(id)
	{
		openingPage = true;
		$('#ajax_loader').fadeIn(500);
		$('#loader').fadeOut(1000, function(){
			switch(id) {
				case "btn_home":
					infoBox("show");
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('templates/home.html?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
				case "btn_storia":
					infoBox("show");
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('templates/storia.html?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
				case "btn_atelier":
					infoBox("show");
					menu_reduced = true;
					$('#colonna_sx,#colonna_dx').animate({height:"370px"}, 300, 'easeInQuint');
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('templates/atelier_orafo.html?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
				case "btn_sposi":
					infoBox("show");
					menu_reduced = true;
					$('#colonna_sx,#colonna_dx').animate({height:"370px"}, 300, 'easeInQuint');
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('templates/sposi.html?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
				case "btn_filosofia":
					infoBox("show");
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('templates/filosofia.html?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
				case "btn_comunicazione":
					infoBox("show");
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('templates/comunicazione.html?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
                case "btn_eventi":
					infoBox("hide");
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('templates/eventi.html?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
				case "btn_marchi":
					infoBox("hide");
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('templates/marchi.html?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
				case "btn_glossario":
					infoBox("hide");
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('glossario.php?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
				case "btn_service":
					infoBox("hide");
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('templates/service.html?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
				case "btn_reca_conta":
					infoBox("hide");
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('reca_conta.php?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
				case "btn_orari":
					infoBox("hide");
					$('#'+id).addClass("menu_selected");
					setTimeout(function(){
						$('#loader').load('templates/orari.html?'+Math.random*1000, function(){ reShow() });
					}, 300);
				break;
			}
		});
	}

function reShow() {
	$('#loader').show();
	$('#ajax_loader').fadeOut(500);
}

function imageFade(div,time) {
	if(time == "") { time = 500; }
	$(div+' img').hide();
	$(div+' img').load(function(){
		$(this).fadeIn(time);
	});
}

function resetPageCounter()
{
	setTimeout(function(){
		openingPage = false;
	}, 300);
}
