
var canReturnToHome = false;

$(document).ready(function() {
	homeScripts();
	mainScripts();
	setFlashMenu();
});

function setFlashMenu()
{
	swfobject.embedSWF("swf/nav.swf", "menu", "100%", "113", "8.0.0", null, null, {
		allowScriptAccess: "always",
		wmode: "transparent",
		menu: "false"
	});
	
	$("div#contentWrapper").css("top", "-32px");
	$(".home div#contentWrapper").css("top", "-2px");	
}
function getFlashMenu() {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window["menu"]
    }
    else {
        return document["menu"]
    }
}
function executeArtistsLinkFromFlash()
{
	return gotoPage("artists.php", "flash", artistsScripts);
}
function executeEventsLinkFromFlash()
{
	return gotoPage("events.php", "flash", eventsScripts);
}
function executeContactsLinkFromFlash()
{
	return gotoPage("contacts.php", "flash");
}

function setPlayer()
{
	swfobject.embedSWF("swf/player/player.swf", "player", "250", "105", "8.0.0", null, null, {
		wmode: "transparent",
		menu: "false",
		FlashVars: "configxml=swf/player/config.xml"
	});
}

function mainScripts()
{
	/* add the loader */
	$("body").append("<div id='loader'><div /></div>");
	
	
	/* header link */
	$("h1#header a").click(function() {
		
		if( canReturnToHome )
		{
			/* flash callback */
			if(getFlashMenu()) getFlashMenu().unselectFlashMenuItems();
			
			/* remove selected menus */
			$("div#menu ul li a").removeClass("selected");

			// equivalents to $("body").addClass("home");
			$("div#contentWrapper").css("top", "18px");
			$("#menu").css("top", "0px");
			$("div#menu").css("top", "18px");


			loadPage("index.php", "div#contentWrapper", "div#content", homeScripts);
			
			$("div#master").css("background-image", "url(img/layout/home_bg.jpg)");
			$("div#master").css("background-position", "center 50px");

			$("h1#header").css("height", "0px");
			
			$("div#contentWrapper").css("height", "550px");
		}
		
		return false;
		
	});

	
	/* menu links */
	$("div#menu ul li#artists a").click(function() {
		return gotoPage("artists.php", $(this), artistsScripts);
	});
	$("div#menu ul li#events a").click(function() {
		return gotoPage("events.php", $(this), eventsScripts);
	});
	$("div#menu ul li#contacts a").click(function() {
		return gotoPage("contacts.php", $(this));
	});
	
	
	/* footer links */
	$("div#footer ul li a#about").click(function() {
		return gotoPage("about.php");
	});
	$("div#footer ul li a#mailing").click(function() {
		return gotoPage("mailing.php");
	});
}

function homeScripts()
{
	/* nextEvent start */
	$("div#homeContainer div.event")
		.css({"display":"none"})
		.fadeIn(500).animate({marginLeft: "30px"}, 200, "easeOutBack");
	
	/* nextEvent click */
	$("div#homeContainer div.event h4 a").click(function() {
		/* flash callback */
		if(getFlashMenu()) getFlashMenu().selectFlashMenuEventsItem();
		return gotoPage("events.php", $("div#menu ul li#events a"), eventsScripts);
	});
	
	/* nextEvent hover */
	$("div#homeContainer div.event h4 a").hover(function(){
			$("div#homeContainer div.event h3").stop(true).animate({marginTop: "-30px"}, 300, "easeInBack");
			$("div#homeContainer div.event h3").animate({marginTop: "0px"}, 300, "easeOutBounce");
		}, function(){}
	);
}

function artistsScripts()
{
	/* player */
	setPlayer();
	
	/* artists link */
	$("div#artistsContainer ul li a").click(function() {
		return gotoPage($(this).attr("href"));
	});
	
	$("div#artistsContainer ul li").hover(
		function(){ $(this).find("img").stop(true).fadeTo("200", 0.5); },
		function(){ $(this).find("img").stop(true).fadeTo("200", 1); }
	);
}

function eventsScripts()
{
	// PAST EVENTS
	$("div#eventsContainer div.passed table.eventList").css("display", "none");
	
	$("div#eventsContainer div.passed h3").append("<img class='toggle_more' src='img/layout/icons/toggle_more_PONGED.png' />");
	
	$("div#eventsContainer div.passed h3 img").click(function() {
		
		if($(this).attr("class") == "toggle_more") {
			$(this)
				.attr("class", "toggle_minus")
				.attr("src", "img/layout/icons/toggle_minus_PONGED.png");
		} else {
			$(this)
				.attr("class", "toggle_more")
				.attr("src", "img/layout/icons/toggle_more_PONGED.png");
		}
		
		$(this).parent().next()
			.toggle()
			.css("margin-left", "-150%")
			.animate({marginLeft: "0%"}, 200, "easeOutQuint");
	})
}






function gotoPage(asPageName, adLink, afScript)
{
	if(adLink != "flash" && typeof adLink != "object") {
		/* flash callback */
		if(getFlashMenu()) getFlashMenu().unselectFlashMenuItems();
	}
	
	//if( !$(adLink).hasClass("selected") ) {
		loadPage(asPageName, "div#contentWrapper", "div#content", afScript);
		
		/*if( adLink!=null && adLink!=undefined )
		{*
			/* remove selected menus */
			$("div#menu ul li a").removeClass("selected");

			/* select this link */
			$(adLink).addClass("selected");
		//}
	//}
	return false;	
}

function loadPage(asPage, asWrapper, asFilter, afCallback)
{
	canReturnToHome = false;
	
	var toto = $(asWrapper).innerHeight();
	
	$(asWrapper).html("");
	
	$(asWrapper).css("height", toto);
	
	/* show loader */
	$("div#loader").css("visibility", "visible");
	
	/* we are at home */
	if( $("h1#header").css("height") == '0px' )
	{
		// equivalents to $("body").removeClass("home");
		
		/* hide background */
		$("div#master").css("background-image", "none");
		$("div#contentWrapper").css("top", "-12px");
		$("#menu").css("top", "-30px");
		$("div#menu").css("top", "-12px");
		$("h1#header").css("height", "30px");
		
		$(asWrapper).css("height", "200px");
		/* hide footer */
		$("div#footer").css("display", "none");
		
		$("h1#header").animate({height: "186px"}, 600, "easeOutQuint", function(){
			loadPage2(asPage, asWrapper, asFilter, afCallback);
		});
	}
	/* we are out of home */
	else
	{
		if(asPage != "index.php") {
			$("h1#header")
				.animate({height: "+=0px"}, 400, "easeOutQuint", function(){
					loadPage2(asPage, asWrapper, asFilter, afCallback);		
			});
		} else loadPage2(asPage, asWrapper, asFilter, afCallback);
	}
}

function loadPage2(asPage, asWrapper, asFilter, afCallback)
{
	$(asWrapper)
		//.css("display", "none")
		.load(asPage + " " + asFilter, function(){
			
			/* header goes down */
			if(asPage != "index.php") {
				
				$("h1#header").animate({height: "186px"}, 800, "easeOutQuint", function(){
					
					$("h1#header a").css("height", "186px");
					
					/* now you can return to home */
					canReturnToHome = true;
				});
				
			} else {
				$("h1#header a").css("height", "0px");
			}
			
			/* hide loader */
			$("div#loader").css("visibility", "hidden");
			
			/* execute callback */
			if(typeof afCallback == "function") afCallback();
			
			if(asPage != "index.php") {
				/* show new background */
				$("div#master").css("background-image", "url(img/layout/content_bg.jpg)");
				$("div#master").css("background-position", "center -180px");
			}
			
			$(asWrapper).css("height", "auto");
			
			/* fade content */
			$(asWrapper).find("div#content").css("margin-left", "-200%")
				.animate({marginLeft: "0%"}, 400, "easeOutQuint");
			
			/* show footer */
			$("div#footer").css("display", "block");
		});	
}
