/* VARIABLES */
var version_IE;
var menu_current;
var vie_current;
var annee_current;
var predications_current_number;
var predications_current_page;

/* AFFICHAGE DE LA PAGE */
$(document).ready(function()
{
	// Initialiser les variables
	version_IE = "!IE";
	menu_current = 0;
	vie_current = 1;
	annee_current = "";
	predications_current_number = 0;
	predications_current_page = "";
	
	// Obtenir la version d'IE
	internet_explorer();
	
	// Charger les pages
	$("div#content").load("pages/html/content.html",function()
	{
		// Gestion des backgrounds
		$(window).resize(function(){background();});
		background();
		
		// Charger les pages
		$("div.content_item:nth-child(1) td").load("pages/html/actualites.html");
		$("div.content_item:nth-child(2) td").load("pages/html/vie.html",function()
		{
			// Créer les jScrollPane
			if (version_IE != "IE7" && version_IE != "IE6")
			{
				$("div.vie_container_item:nth-child(1) div.vie_container_content").jScrollPane({showArrows: true, autoReinitialise: true});
				$("div.vie_container_item:nth-child(2) div.vie_container_content").jScrollPane({showArrows: true, autoReinitialise: true});
			}
		});
		$("div.content_item:nth-child(3) td").load("pages/html/predications.html",function()
		{
			// Ouvrir l'année courante
			predications_annee_click("2011_2012");
		});
		$("div.content_item:nth-child(4) td").load("pages/html/productions.html");
		$("div.content_item:nth-child(5) td").load("pages/html/medias.html",function()
		{
			// Créer les jScrollPane
			if (version_IE != "IE7" && version_IE != "IE6")
			{
				$("div.medias_list").jScrollPane({showArrows: true, autoReinitialise: true});
			}
		});
		$("div.content_item:nth-child(6) td").load("pages/html/paroisse.html");
		$("div.content_item:nth-child(7) td").load("pages/html/contacts.html",function()
		{
			// Créer les jScrollPane
			if (version_IE != "IE7" && version_IE != "IE6")
			{
				$("div.contacts_text").jScrollPane({showArrows: true, autoReinitialise: true});
			}
			url();
		});
	});
	
	// Analyser l'url
	//url();
});

/* INTERNET EXPLORER */
function internet_explorer ()
{
	var jscriptVersion;

	// Ligne exécutée par IE uniquement
	/*@cc_on var jscriptVersion = @_jscript_version; @*/

	if (jscriptVersion)
	{
		// Détection de la version d'IE via la version du moteur JScript
		version_IE = "IE";
		if (jscriptVersion==9)   version_IE = "IE9";
		if (jscriptVersion==5.8) version_IE = "IE8";
		if (jscriptVersion==5.7) version_IE = "IE7";
		if (jscriptVersion==5.6) version_IE = "IE6";

		// Cas particulier: IE6 sur WinXP SP3 utilise JScript 1.7
		if (jscriptVersion==5.7 && navigator.userAgent.indexOf("MSIE 6.0") != -1) version_IE = "IE6";
	}
}

/* URL */
function get_url_vars ()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}
function url ()
{
	var parameters = get_url_vars();
	
	if (parameters["m"] != null && parseInt(parameters["m"]) >= 1 && parseInt(parameters["m"]) <= 7)
	{
		menu_item_click(parseInt(parameters["m"]));
		
		if (parameters["t"] != null)
		{
			if (parameters["t"] == "l" && parameters["p"] != null && parameters["h"] != null && parameters["w"] != null)
			{
				if (parseInt(parameters["h"]) >= 150 && parseInt(parameters["h"]) <= 500 && parseInt(parameters["w"]) >= 300 && parseInt(parameters["w"]) <= 1000)
				{
					lightbox_open(parseInt(parameters["h"]),parseInt(parameters["w"]),parameters["p"]);
				}
			}
			if (parameters["t"] == "v" && parameters["p"] != null && parseInt(parameters["m"]) == 2 && parseInt(parameters["p"]) >= 1 && parseInt(parameters["p"]) <= 3)
			{
				vie_menu_click(parseInt(parameters["p"]));
			}
			if (parameters["t"] == "p" && parseInt(parameters["m"]) == 3 && parameters["a"] != null && parameters["a"] != "")
			{
				predications_annee_click(parameters["a"]);
				
				if (parameters["p"] != null && parameters["n"] != null)
				{
					predications_item_click(parseInt(parameters["n"]),parameters["p"]);
				}
			}
		}
	}
	else
	{
		menu_item_click(1);
	}
}

/* BACKGROUND */
function background ()
{
	var background_height = 1600 * $("div#content").width() / 10 / 2000;
	var background_top = (background_height - $("div#content").height()) / 2;
	$("img.content_background").css("top","-"+background_top+"px");
}

/* CONTENT */
function content ()
{
	if ($("div.content_item table").css("opacity") == "1")
	{
		$("div#banner_content").attr("title","Afficher le contenu");
		$("div.content_item table").animate({opacity:"0"},500);
	}
	else
	{
		$("div#banner_content").attr("title","Cacher le contenu");
		$("div.content_item table").animate({opacity:"1"},500);
	}
}

/* MENU */
function menu_item_on (number)
{	
	$("div#menu_under_1").stop();
	$("div#menu_under_1").animate({
		left:	$("div.menu_item:nth-child("+(number+2)+")").position().left+"px",
		width:	$("div.menu_item:nth-child("+(number+2)+")").outerWidth()+"px"
	},500);
}
function menu_item_off ()
{
	$("div#menu_under_1").stop();
	$("div#menu_under_1").animate({
		left:	$("div.menu_item:nth-child("+(menu_current+2)+")").position().left+"px",
		width:	$("div.menu_item:nth-child("+(menu_current+2)+")").outerWidth()+"px"
	},500);
}
function menu_item_click (number)
{	
	if (menu_current != number)
	{
		menu_current = number;
		
		location.hash = "m="+menu_current;
		if (menu_current == 2) location.hash = "m="+menu_current+"&t=v&p="+vie_current;
		if (menu_current == 3) location.hash = "m="+menu_current+"&t=p&a="+annee_current+"&p="+predications_current_page+"&n="+predications_current_number;
		
		$("div#menu_under_1").stop();
		$("div#menu_under_1").animate({
			left:	$("div.menu_item:nth-child("+(menu_current+2)+")").position().left+"px",
			width:	$("div.menu_item:nth-child("+(menu_current+2)+")").outerWidth()+"px"
		},500);
		
		$("div#menu_under_2").stop();
		$("div#menu_under_2").animate({
			left:	$("div.menu_item:nth-child("+(menu_current+2)+")").position().left+"px",
			width:	$("div.menu_item:nth-child("+(menu_current+2)+")").outerWidth()+"px"
		},500);
		
		$("div#content").stop();
		$("div#content").animate({left:(menu_current-1)*(-100)+"%"},500);
	}
}

/* LIGHTBOX */
function lightbox_close_on ()
{
	$("div#lightbox_close_background").stop();
	$("div#lightbox_close_background").animate({opacity:"1"},500);
}
function lightbox_close_off ()
{
	$("div#lightbox_close_background").stop();
	$("div#lightbox_close_background").animate({opacity:"0.75"},500);
}
function lightbox_open (height, width, page)
{
	$("div#lightbox").stop();
	$("div#lightbox_container_content").stop();
	
	$("div#lightbox").css("display","block");
	$("div#lightbox_container").css("height",height+"px");
	$("div#lightbox_container").css("width",width+"px");
	
	$("div#lightbox").animate({opacity:"1"},500,function()
	{
		$("div#lightbox").css("cursor","progress");
		$("div#lightbox_container_content").load("pages/html/"+page+".html",function(response, status, xhr)
		{			
			// Changer l'url de la page
			location.hash = "m="+menu_current+"&p="+page+"&t=l&h="+height+"&w="+width;
			
			// Si le chargement ne s'est pas déroulé correctement...
			if (status == "error")
			{
				// ... charger la page d'erreur
				$("div#lightbox_container_content").load("pages/html/erreur_lightbox.html");
				// ... changer l'url de la page
				location.hash = "m="+menu_current;
			}
			
			$("div#lightbox").css("cursor","auto");
			
			if (version_IE != "IE7" && version_IE != "IE6")
			{
				$("div#lightbox_container_text").jScrollPane({showArrows: true, autoReinitialise: true});
			}
			
			$("div#lightbox_container_content").animate({opacity:"1"},500);
		});
	});
}
function lightbox_close ()
{
	$("div#lightbox").stop();
	$("div#lightbox_container_content").stop();
	
	$("div#lightbox_container_content").animate({opacity:"0"},500,function()
	{
		$("div#lightbox").animate({opacity:"0"},500,function()
		{
			// Changer l'url de la page
			location.hash = "m="+menu_current;
			
			$("div#lightbox").css("display","none");
			$("div#lightbox_container_content").html("");
		});
	});
}

/* PLAYER */
function load_player (name, track)
{
	jwplayer(name).setup(
	{
		modes: [{type: "flash", src: "pages/jwplayer/player.swf"},{type: "html5"},{type: "download"}],
		file: track+".mp3",
		skin: "pages/jwplayer/five.zip",
		'controlbar.position': "bottom",
        height: "24",
        width: "400"
    });
}
