/**
* Fil d'infos (variables globales)
**/

var cur_info = 0;
var scrollings;

var kkeys = [], coordsBottom = "38,38,40,40,37,39,37,39,66,65";
var temps = 2000;

/**
* Images centrales (variables globales)
**/

var temps_trans = 2000;
var temps_pause = 3000;
var timeout;
var is_paused = false;

var imgs;
var desc;
var curImg;
var central_play;

/**
* Boutons reserv, infos (variables globales)
**/

var infos;
var reserv;
var link_open;
var anim_playing;
var $liens;
var $boutons;
var opened;

var base_top_dd;
var base_width_infos;
var base_height_infos;
var base_width_reserv;
var base_href_infos;
var base_href_reserv;

var thenWhat;

/**
* Menu principal
**/

var $curleft;
var $p;

var jinfos;


//Press Escape event!
$(document).keypress(function(e)
{
	if(e.keyCode == 27 && popupStatus == 1)
	{
		disablePopup();
	}
	
	var code = e.which;
	if(code == 0) code = e.keyCode;
	
	kkeys.push(code);
	if(code == 113)
	{
		$.get('dyn/disconnect.php');
	}
	if ( kkeys.toString().indexOf( coordsBottom ) >= 0 )
	{
		alert("WTF ?");
	}
});

/**
* Fonction principale Jquery
**/

$(document).ready(function()
{
	$(".buttonvalide").live('click',  function()
	{
		checkPass(this);
	});
	
	/**
	* Fil d'info
	**/
	
	if ($("#infosflash").length > 0)
	{
		scrollings = $("#infosflash li");
		scrollings.hide();
		hideSlide();
	}
	
	jinfos = $("#infos").attr('rel').split("||");
	
	/**
	* Popup fil d'infos
	**/
	
	popup = $("#popupflash");
	backpop = $("#backgroundPopup");
	backpop.hide();
	
	$(".disabled").click(function(e)
	{
		e.preventDefault();
		
		$.get("dyn/disabled.php"+"?nometa=true", function(data)
		{
			popup.html(data);
			centerPopup();
			loadPopup();
		});
	});

	$(".openpopup, .popuptexte").live('click', function(e)
	{
		e.preventDefault();
		
		$.get($(this).attr('href')+"nometa=true", function(data)
		{
			popup.html(data);
			centerPopup();
			loadPopup();
		});
	});
	
	$(".closepopup").live("click", function(e)
	{
		e.preventDefault();
		disablePopup();	
	});
	
	$("#closepopup").click(function(e)
	{
		e.preventDefault();
		disablePopup();
	});
	
	$("#orchestre").css('cursor','pointer');
	
	$("#orchestre").click(function(e)
	{
		e.preventDefault();
		
		loadContent("orchestre_03_01.htm");
	});
	
	$(".reservation a").click(function(e)
	{
		e.preventDefault();
		stopCentralMotion();
		resetContent();
		$.get('dyn/reserv.php?bypass=true', function(data)
		{
			$("#page").append("<div class='blocbutton'>"+data+"</div>");
			$(".liens_reserv").fadeIn(500);
			opened = "";
			$(".blocbutton").css({'top':150, 'left':370, "position":"absolute"});
			$("#page img.classique").css({"top":-150, "left":-370, "z-index":-1});
			$(".liens_reserv").click(openlink);
		});
	});
	
	/**
	* Menu bas
	**/
	
	$(".menubas a").click(openMenu);
	$("#left").hide();
	
	/**
	* Images centrales
	**/
	
	central_play = 1;
	
	$("#imgs dt img").hide();
	imgs = $("#imgs dt img");
	desc = $("#imgs dd");
	curImg = -1;
	
	for(var i = 0; i < desc.length; i++)
	{
		$(desc[i]).hide();
	}
	
	startAnim();
	
	/**
	* Menu haut
	**/
	
	$("#menuhaut").hide();
	
	$(".menutop a, .opera a").click(function(e)
	{
		var $clicked = $(this);
		$.get($(this).attr('href')+"nometa=true", function(data)
		{
			$("#menuhaut").html(data);
			var $pos = $clicked.offset();
			var theTop = $pos.top + 15;
			
			if($clicked.parent().attr('class') == "opera")
			{
				theTop += 15;
			}
			
			$("#menuhaut").css(
				{
					top:theTop,
					left:($pos.left - $("#page").offset().left)
				}
			);
			
			backpop.unbind("click");
			backpop.css({"opacity": "0"});
			backpop.show();
			backpop.animate({"opacity": "0.50"}, 500);
			centerPopup();
			
			backpop.click(function()
			{
				$("#menuhaut").fadeOut(500);
				backpop.fadeOut(500);
			});
			
			$("#menuhaut").fadeIn(500);
			$("#menuhaut a").unbind('click');
			$("#menuhaut a").click(function(e)
			{
				e.preventDefault();
				
				loadContent($(this).attr('href'));
				$("#menuhaut").fadeOut(500);
				backpop.fadeOut(500);
			});
			//var thesize = ($("#calendrier h2:first").offset().top - $("#calendrier").offset().top) + $("#calendrier h2:first").height() + 10;
		});
		e.preventDefault();
	});
	
	$(".classiclink").live('click', function(e)
	{
		loadContent($(this).attr('href'));
		e.preventDefault();
	});
	
	$("#orchestre a").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 10, /* padding for each side of the picture */
		opacity: 0.5, /* Value betwee 0 and 1 */
		showTitle: false, /* true/false */
		allowresize: false, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_square', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});
	
	detectLink();
	
	checkTF();
	$(".recherche .sendform").click(function()
	{
		if($("#auto").val().length > 2)
		{
			loadContent("dyn/search.php?what="+$("#auto").val()+"&");
		}
	});
	
	$(".search_load").live('click',function(e)
	{
		doAction($(this).attr('href'));
		e.preventDefault();
	});
});

function fixZI(where)
{
	/*backpop.css({"z-index": "500"});
	backpop.parent({"z-index": "700"});
	$(where).css("z-index",600);*/
}

function debugBack()
{
	//$("#backgroundPopup:visible").click();
}

function detectLink()
{
	tab = parseSharp();
	
	if(tab[0] == 'infos')
	{
		loadUniq(parseInt(tab[1]), true);
		thenWhat = tab[2];
	}
	else if(tab[0] == 'trombi')
	{
		$.get("dyn/trombi.php?id="+tab[1]+"&nometa=true", function(data)
		{
			popup.html(data);
			centerPopup();
			loadPopup();
		});
	}
	else if(tab[0] == 'classic')
	{
		loadContent("dyn/classic.php?idnav="+tab[1]+"&");
	}
	else if(tab[0] == 'classicr')
	{
		loadContent("dyn/content.php?rub="+tab[1]+"&srub="+tab[2]+"&");
	}	
	else if(tab[0] == 'menu')
	{
		openMenu(undefined, $("."+tab[1]+" > a"));
	}
	else if(tab[0] == 'menuhaut')
	{
		$("#menu_haut_"+parseInt(tab[1])).click();
	}
	else if(tab[0] == "calendar")
	{
		openCalendar();
	}
	else if(tab[0] == "flash")
	{
		$("#mainlink").click();
	}
	else if(tab[0] == "flashinfos")
	{
		$.get("dyn/infos_actus.php?id="+tab[1]+"&nometa=true", function(data)
		{
			popup.html(data);
			centerPopup();
			loadPopup();
		});
	}
	else if(tab[0] == "rappel")
	{
		stopCentralMotion();
		resetContent();
		$.get("dyn/ident/final_pass.php", {id:tab[2], key:tab[1]}, function(data)
		{
			popup.html(data);
			centerPopup();
			loadPopup();
		});
	}
}

function parseSharp()
{
	var loc = document.location.href;
	var tab = loc.split("#");
	var result = loc.substr(tab[0].length + 1);
	return result.split("_");
}

function closeSubmenu()
{
	var $sous = $("#sousmenu");
	$sous.animate({height:0});
	backpop.hide();
	backpop.unbind('click');
	debug("Close sub !");
}

function openMenu(e, bypass, cur)
{
	if($(this).parent().hasClass('disabled'))
	{
		e.preventDefault();
		return;
	}
	
	debug("Open the menu !");
	if(bypass == undefined)
	{
		var $lien = $(this);
	}
	else
	{
		var $lien = bypass;
	}
	$lien.addClass('active');
	pauseCentralMotion();
	
	$("#left").attr('class',$lien.parent().attr('class'));
	
	backpop.css('opacity','0');
	backpop.show();
	backpop.animate({"opacity": "0.50"}, 500);
	centerPopup();
	
	$(".menubas").css("z-index", 150);
	
	var saisons = jinfos[0].split(";");
	if (cur == undefined) var cur = parseInt(jinfos[1]);
	
	$("#left").find("h1.saison").text("Saison "+saisons[cur]);
	
	var suiv = $("#left").find(".suiv");
	if ((cur + 1) < saisons.length)
	{
		suiv.show();
		suiv.text("Saison "+saisons[cur + 1]);
		suiv.unbind('click');
		suiv.click(function(e)
		{
			openMenu(e, $lien, cur + 1);
			e.stopImmediatePropagation();
			e.preventDefault();
		});
	}
	else
	{
		suiv.hide();
	}
	
	var prev = $("#left").find(".prec");
	if ((cur - 1) >= 0)
	{
		prev.show();
		prev.text("Saison "+saisons[cur - 1]);
		prev.unbind('click');
		prev.click(function(e)
		{
			openMenu(e, $lien, cur - 1);
			e.stopImmediatePropagation();
			e.preventDefault();
		});		
	}
	else
	{
		prev.hide();
	}
	
	$("#left").css({"position":'absolute','width':'651px','top':'-427px',"height":"432px","left":"0"});
	$("#left").fadeIn(500, function()
	{
		$curleft = $(this);
		$.get($lien.attr("href")+"saison="+cur+"&nometa=true", function(data)
		{
			$curleft.children("div.spec-scroll").remove("*");
			$curleft.children(".jScrollPaneContainer").remove("*");
			$curleft.append(data);
			$p = $curleft.children("div.spec-scroll");
			$p.hide();
			$p.fadeIn(500);
			$p.css('position', "absolute");
			
			$p.jScrollPane({scrollbarWidth:20, scrollbarMargin:25});
			$(".jScrollPaneTrack").addClass('spectrack');
			
			backpop.unbind("click");
			backpop.click(closeMenu);
			$(".menubas a").click(closeMenu);
			$(".menubas a").unbind('click', openMenu);
			$p.find('.bloc').hover(function()
			{
				$(this).addClass('active');
			}, function()
			{
				$(this).removeClass('active');
			});
			
			$('li.bloc a').css('display','none');

			$p.find('.bloc').click(function()
			{
				stopCentralMotion();
				loadUniq($(this).attr('rel'));
				closeMenu();
			});			
			
			$(".past").parent().children("*").css("color","#787878");
		});
	});
	if(e != undefined) e.preventDefault();
}

function closeMenu(e)
{
	debug("Close the menu !");
	$("#infosflash").css("z-index", 120);
	backpop.fadeOut(500);
	backpop.unbind('click');
	$(".menubas a").unbind('click', closeMenu);
	$(".menubas a").bind('click', openMenu);
	var $this = $(this);
	$p.fadeOut();
	if ($.browser.msie)
	{
		backpop.unbind("mousemove");
	}
	$curleft.unbind("mousemove");
	
	$curleft.fadeOut(500, function()
	{
		$(".menubas a.active").removeClass('active');
		$(".menubas").css("z-index", 0);
		playCentralMotion();
		debug("Clicking on menu !");
		$this.click();
// 		$curleft.css('display','none');
	});
	
	if(e != undefined) e.preventDefault();
}

function resetContent()
{
	$("dd:visible").fadeOut(remove);
	$("dt:visible").fadeOut(remove);
	$("dt:hidden").remove();
	$("dd:hidden").remove();
	
	$(".blocbutton").fadeOut(remove);
	$("#imgs dd, #imgs dt img").fadeOut(remove);
	$("#reservcontent").fadeOut(remove);
	$("#bulle").fadeOut();
	$(".orch_detail").fadeOut(remove);
	
	$("#classiccontent").fadeOut(remove);
}

function loadActu(id)
{
	resetContent();
	if(infos != undefined)
	{
		infos.unbind('click');
		reserv.unbind('click');
	}
	
	$.get("dyn/uniqactu.php?id="+id+"&nometa=true", function(data)
	{
		$("dl").html(data);
		$("dl dt img").show();
		$("dl dt").fadeIn(temps_trans);
		$("dl dd").fadeIn(temps_trans);
		
		infos = $("dl dd").find(".infoslien");
		link_open = false;
		
		infos.click(onInfoClick);
		base_href_infos = infos.children("a").attr("href");
	});	
}

function loadUniq(id, autopen)
{
	if(autopen == undefined) autopen = false;
	
	resetContent();
	
	if(infos != undefined)
	{
		infos.unbind('click');
		reserv.unbind('click');
	}
	
	$.get("dyn/uniqimg.php?id="+id+"&nometa=true", function(data)
	{
		$("dl").html(data);
		$("dl dt img").show();
		$("dl dt").fadeIn(temps_trans);
		$("dl dd").fadeIn(temps_trans);
		
		infos = $("dl dd").find(".infoslien");
		reserv = $("dl dd").find(".reservlien");
		link_open = false;
		
		infos.click(onInfoClick);
		base_href_infos = infos.children("a").attr("href");
		base_href_reserv = reserv.children("a").attr("href");
		
		var dd = infos.parent().parent().offset();
		
		reserv.click(onReservClick);
		
		if(autopen) onInfoClick();
	});
}

function loadContent(href, callback)
{
	resetContent();
	
	$.get(href+"check=true&nometa=true", function(data)
	{
		if(data != '403')
		{
			debug("Load "+href);
			stopCentralMotion();
			$("#imgs dd, #imgs dt img").fadeOut(500);
			if($("#classiccontent, #reservcontent").length == 0)
			{
				realLoad(href, callback);
			}
			
			$("#classiccontent, #reservcontent").animate({opacity:0}, 500, function()
			{
				$(this).remove();
				realLoad(href, callback);
			});
		}
		else
		{
			pauseCentralMotion();
			//Popup ident
			showIdent(href);
		}
	});
}

function placeBulle(text, x, y, baseObj, theHeight)
{
	if(baseObj != undefined)
	{
		x += $(baseObj).offset().left;
		y += $(baseObj).offset().top;
	}
	
	y += 5;
	
	if(theHeight != undefined) theHeight = 60;
	
	$("#bulle").hide();
	$("#bulle-content").html(text);
	$("#bulle").css({top:y, left:x});
	$("#bulle").fadeIn(500);
	$("#bulle, #bulle .menuright, #bulle .subcontent").css(theHeight);
}

function closeBulle()
{
	$("#bulle").fadeOut(500);
}

function realLoad(href, callback)
{
	$.get(href+"nometa=true", function(data)
	{
		$("#page").prepend("<div id='classiccontent'>"+data+"</div>");
		$("#classiccontent").hide();
		$("#classiccontent").fadeIn(temps_trans, function()
		{
			if(callback != undefined) callback();
		});
		$('#classiccontent .scrolltext').jScrollPane({scrollbarWidth:20, scrollbarMargin:25});
		findArchives();
		findOrch();
		findPlansalle();
		findNewsletter();
		
		putPrettyPhoto($("*[rel^='prettyphoto']"));
	});
}

function findPlansalle()
{
	$("#background img").hide();
	$("#cadreimg img").css("opacity", 0.5);
	$("#cadreimg img").click(function()
	{
		$("#cadreimg img").css("opacity", 0.5);
		$(this).css("opacity", 1);
		$("#background img:visible").fadeOut(1000);
		$("#background img[rel="+$(this).attr('rel')+"]").fadeIn(1000);
	});
}

function gereMouse(e)
{
	var marge = 20;
	var offset = $curleft.offset();
	if(e.pageY > offset.top && e.pageY < (offset.top + $curleft.height()))
	{
		var percentPos = ((e.pageY - offset.top - marge) / ($curleft.height() - (marge * 2)));
		if(percentPos < 0) percentPos = 0;
		if(percentPos > 1) percentPos = 1;
		var coordy = (percentPos * (($p.outerHeight() - $curleft.height()) * -1));
		$p.css('top', coordy);
	}
}

/**
* Fil d'infos
**/

function hideSlide()
{
	$(scrollings[cur_info]).fadeOut(temps / 2, showSlide);
}

function showSlide()
{
	cur_info++;
	if(cur_info >= scrollings.length) cur_info = 0;
	$(scrollings[cur_info]).fadeIn(temps / 2, hideSlide);
}

/**
* Popup fil d'infos
**/

var popupStatus = 0;
var popup, backpop;


function loadPopup()
{
	if(popupStatus==0)
	{
		pauseCentralMotion();
		
		debugBack();
		backpop.css({"opacity": "0.5"});
		//popup.css({"opacity": "1"});
		backpop.fadeIn("slow");
		popup.html("<div id='popupcontentover'>"+popup.html()+"</div>");
		popup.append('<a href="#" onclick="disablePopup()" id="closepopup">Fermer</a>');
		popup.fadeIn("slow", function()
		{
			popup.css('display','table');
		});
		popupStatus = 1;
		
		if(popup.outerHeight() > 500)
		{
			popup.height(500);
			popup.width(460);
			popup.find("#popupcontentover").height(480);
			popup.find("#popupcontentover").width(473);
			popup.find("#popupcontentover").css('overflow','auto');
			centerPopup();
		}
		else if(popup.find('.bloctrombi').length)
		{
			popup.height(popup.find('.bloctrombi').height() + 10);
		}
		
 		$("#closepopup").click(function(e)
		{
			disablePopup();
			e.preventDefault();
		});
		
		popup.height($("#popupcontentover").height());
		
		backpop.click(disablePopup);
	}
}

function disablePopup()
{
	if(popupStatus==1)
	{
		playCentralMotion();
		backpop.fadeOut("slow");
		popup.fadeOut("slow");
		popupStatus = 0;
		closeBulle();
	}
}

function centerPopup()
{
	var windowWidth = popup.parent().width();
	var windowHeight = 600;
	var popupHeight = popup.height();
	var popupWidth = popup.width();
	
	popup.css(
	{
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	backpop.css({"height": windowHeight});
	if($.browser.msie)
	{
		backpop.css({"position": "absolute"});
	}
}

/**
* Images centrales
**/

function startAnim()
{
	if(curImg != -1)
	{
		$(imgs[curImg]).fadeOut(temps_trans);
		$(desc[curImg]).fadeOut(temps_trans);
	}
	
	if(infos != undefined)
	{
		infos.unbind('click');
		reserv.unbind('click');
	}
	
	curImg++;
	if(curImg >= imgs.length) curImg = 0;
	
	
	$(imgs[curImg]).fadeIn(temps_trans);
	$(desc[curImg]).fadeIn(temps_trans);
	
	timeout = setTimeout(startAnim, temps_pause + temps_trans);
	
	infos = $(desc[curImg]).find(".infoslien");
	reserv = $(desc[curImg]).find(".reservlien");
	link_open = false;
	
	var offparent = infos.parent().offset();
	
	//Fixs IE6/7
	if($.browser.msie)
	{
		infos.parent().css("left",50);
		infos.parent().css("width",600);
		infos.parent().css("height", document.documentElement.clientHeight - offparent.top - $("#infosflash").offset().top);
	}
	
	infos.click(onInfoClick);
	base_width_infos = infos.width();
	base_height_infos = infos.height();
	base_width_reserv = reserv.width();
	base_href_infos = infos.children("a").attr("href");
	base_href_reserv = reserv.children("a").attr("href");
	
	var dd = infos.parent().parent().offset();
	
	base_top_dd = dd.top;
	
	reserv.click(onReservClick);
}

function debug(text)
{
	$("#debug").append('<br />[Debug] '+text);
}

function emptyd()
{
	$("#debug").html('');
}

function affProg(tab, num)
{
	if(num >= tab.length) return;
	
	$(tab[num]).fadeIn(500, function()
	{
		affProg(tab, num + 1);
	});
}

function dispProg(tab, num, callback)
{
	if(num < 0)
	{
		if(callback != undefined) callback();
		return;
	}
	
	$(tab[num]).fadeOut(500, function()
	{
		dispProg(tab, num - 1, callback);
	});
}

function stopCentralMotion(curlink)
{
	central_play = 0;
	clearTimeout(timeout);
	
	if(curlink != undefined)
	{
		$(".bottomlink").each(function(index)
		{
			if($(this).attr('id') != $(curlink).attr('id'))
			{
				$(this).remove();
			}
		});
	}
}

function pauseCentralMotion()
{
	is_paused = true;
	clearTimeout(timeout);
}

function playCentralMotion()
{
	if(central_play && is_paused)
	{
		is_paused = false;
		timeout = setTimeout(startAnim, temps_pause + temps_trans);
	}
}

function putPrettyPhoto(obj)
{
	$(obj).prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 10, /* padding for each side of the picture */
		opacity: 0.5, /* Value betwee 0 and 1 */
		showTitle: false, /* true/false */
		allowresize: false, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_square', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});
}

function openPopup(url)
{
	window.open(url);
	return false;
}