/*--------------------------------------------------------------*/
/* Variables globales                                           */
/*--------------------------------------------------------------*/

/*--------------------------------------------------------------*/
function main()
	{
	if($.client.browser=="Explorer" && $.client.version == 6) {$('.logo').pngFix(); $('.slideshow').pngFix();}

	/*------------- Diaporama -------------*/
	$('.slideshow_img').cycle({ 
			prev:   '.left_slide', 
			next:   '.right_slide',
			timeout: 6000,
			fx: 'fade',		// ok: fade, turnDown, turnLeft, growX - marche pas : scrollLeft
			after: cycle_onAfter 
		});
	
	/*------------- Event click Pause Diaporama -------------*/
	$('.pause_slide').click( 
		function() 
			{
			$('.pause_slide').hide();
			$('.slideshow_img').cycle('pause');
			$('.play_slide').show();
			}
		);

	/*------------- Event click Play Diaporama -------------*/
	$('.play_slide').click( 
		function() 
			{
			$('.play_slide').hide();
			$('.slideshow_img').cycle('resume');
			$('.pause_slide').show();
			}
		);

	/*------------- Event click open Diaporama -------------*/
	$('.top_show_diaporama_button').click( 
		function() 
			{
			diaporama_open=1;

			$('.slideshow').slideDown('slow', 
				function()
					{
					$('.top_show_diaporama').slideUp('slow');
					$('.buttons-slideshow, .masque_pano').show();
					}
				);
			}
		);

	/*------------- Event click menu -------------*/
	$('.menu ul li').unbind('click').click( 
		function() 
			{
			group = $(this).attr('group');
			//console.log(' ');
			//console.log("click group="+group);

			$('.menu ul li').removeClass('current-menu');		
			$(this).addClass('current-menu');					// sélectionne premier menu

			$('.elm_smenu').hide();
			$('#saison_'+saison_active+' .smenu_'+group).show();	// affiche sous-menu du GROUP sélectionné
			

			/*------------- Event click smenu -------------*/
			$('#saison_'+saison_active+' .smenu_'+group).unbind('click').click( 
				function() 
					{
					cacher_diaporama();

					cat = $(this).attr('cat');
					scat = $(this).attr('scat');
					//class_cat = $(this).attr('class_cat');
					//console.log("click smenu scat="+scat+" cat="+cat);		//+" class_cat="+class_cat);

					$('#saison_'+saison_active+' .elm_smenu').removeClass('current-smenu');
					$(this).addClass('current-smenu');

					$('#para_content'+saison_active).fadeOut('slow',
						function()
							{
							html='';
							elm_cat_photo_fait =  new Array();
							elm_cat_lnk_fait =  new Array();
							
							selector = '#saison_'+saison_active+' .all_smenu .para_content[scat='+scat+']';
							//console.log("selector="+selector);

							$(selector).each(function(e)	// Pour chaque élément ajouter le bloc
								{
								////console.log("find element class="+ $(this).attr('class') );

								elm_cat = $(this).attr('cat');
								
								//console.log("elm_cat="+elm_cat);

								html+= "<div class='paragraphe'>";
									html+= "<table>";
										html+= "<tr>";
											html+= "<td class='para_left'>";

											// Le contenu
											html+= $(this).html();

											// Les liens
											string_liens = '';
											$('#saison_'+saison_active+' .all_smenu .L_'+elm_cat).each(
												function()
													{
													html_lien = $(this).html();
													if(html_lien!=null) string_liens+= html_lien;
													}
												);
											if(elm_cat_lnk_fait[elm_cat]==undefined)
												{
												html+= string_liens;
												elm_cat_lnk_fait[elm_cat]=1;
												}
											//console.log(elm_cat_lnk_fait);
											html+= "</td>";
										
											// les photos
											if(elm_cat_photo_fait[elm_cat]==undefined)
												{
												html+= "<td class='para_right'>";
												$('#saison_'+saison_active+' .all_smenu .P_'+elm_cat).each(
													function()
														{
														html_photo = $(this).html();
														if(html_photo!=null) html+= html_photo
														}
													);
												html+= "</td>";
												elm_cat_photo_fait[elm_cat] = 1;
												}
											//html+= "<div class='clear'></div>";
										html+= "</tr>";
									html+= "</table>";
								html+= "</div>";

								});
							$('#para_content'+saison_active).empty().append(html);
							$('#para_content'+saison_active).fadeIn('slow');
							
							$('#para_content'+saison_active+' .para_right a').lightBox();
							if($.client.browser=="Explorer" && $.client.version == 6) $('#para_content'+saison_active).pngFix();
							}
						);
					}
				);
			$('#saison_'+saison_active+' .smenu_'+group+':first').click();
			}
		);
		
	//--------------------------------- lancer la saison
	loadSaison();
	apply_onglet_saison(saison_active);
	}

/*--------------------------------------------------------------*/
/* cacher_diaporama : slide pour voir mieux le content          */
/*--------------------------------------------------------------*/
function cacher_diaporama()
	{
	//console.log("diaporama_open="+diaporama_open+" first_open="+first_open);
	if(diaporama_open==1 && first_open==1) 
		{
		diaporama_open=0;

		$('.buttons-slideshow, .masque_pano').hide()
		$('.slideshow').slideUp('slow', 
				function()
					{
					$('.top_show_diaporama').slideDown('slow');
					}
				);
		}
	}

/*--------------------------------------------------------------*/
/* Titre page                                                   */
/*--------------------------------------------------------------*/
function maj_title(saison)
	{
	//console.log("bornes_saison="+bornes_saison);
	//var str = $("#tab_saison_"+saison+"_a").html();
	//str = str.stripTags();
	title = $("#titre_"+saison).html();
	if(bornes_saison==2)
		{
		if(saison==1) str = title_saison_1; 
			else str = title_saison_2;
		ext = ' ('+str+')';
		title+=ext;
		}
	document.title = title;
	}

/*--------------------------------------------------------------*/
/* Affichage legend-slideshow                                   */
/*--------------------------------------------------------------*/
function cycle_onAfter()
	{
	id_saison_diapo = $(this).parent().attr('id_saison');
	//console.log(id_saison_diapo);
	$('#saison_'+id_saison_diapo+' .legend-slideshow').html(this.title); 
	}

/*--------------------------------------------------------------*/
/* apply_onglet_saison                                          */
/*--------------------------------------------------------------*/
function apply_onglet_saison(id_saison)
	{
	/*------------- Gestion saison -------------*/
	//console.log("applique saison id_saison="+id_saison);
	saison_active = id_saison;

	$('.saison').hide();

	//------------------------------------------------------ Montre diaporama
	diaporama_open=1;
	first_open=0;
	$('.top_show_diaporama').hide();
	$('.buttons-slideshow, .masque_pano, .slideshow').show();

	maj_title(saison_active);
	
	//------------------------------------------------------ BI-SAISONS
	if(nb_saisons==2)
		{
		$('.button-saison').removeClass('bouton-selected');
		$('#button-saison'+id_saison).addClass('bouton-selected');
		}
	//console.log("show="+'#saison_'+saison_active);
	$('#saison_'+saison_active).show();

	//------------------------------------------------------ click first menu
	$('#saison_'+saison_active+' .menu ul li:first').click();	
	first_open=1;		
	//console.log(' apply_onglet_saison first_open='+first_open);				

	return false;
	}
	
/*--------------------------------------------------------------*/
/* "Ajouter aux favoris"                                        */
/*--------------------------------------------------------------*/
function addToFavorites(href, title)
	{
	if(navigator.appName=='Microsoft Internet Explorer')
		window.external.AddFavorite(href,title);
	else
		window.sidebar.addPanel(title,href,"");
	}



/*----------------------------------------------------------------------------------*/
/* GESTION DES SAISONS																*/
/*----------------------------------------------------------------------------------*/
function loadSaison()
	{
	saison_defaut=1;					// id xml par defaut
	saison_pointe = saison_defaut;		// saison à montrer

	var _GET = {};
	_GET.saison = getVar('saison');
	_GET.dtres = getVar('dtres');

	if(!isNaN(_GET.saison) && (_GET.saison>0)){
		if(_GET.saison <= bornes_saison){
			saison_active = Number(_GET.saison);
			return null;
		}
	}

	//console.log("bornes_saison="+bornes_saison);
	if(bornes_saison==2)
		{
		//---------------------------------------- Si une date passé en param
		if(_GET.dtres!=0)
			{
			//console.log("_GET.dtres="+_GET.dtres);
			var now = getDate(_GET.dtres, 1);		// Doit être au format 20070828 pour 28.08.2007
			}
		else
			{
			var now = new Date();		// Sinon prendre aujourd'hui
			//console.log("now: "+now+ " "+Date.UTC( now.getFullYear(),now.getMonth(),now.getDate(),0,0,0 ));
			}

		//---------------------------------------- CALCUL DES BORNES SAISON ID=1
		//console.log("bornes du xml s1: "+bornes_saison_1);

		saison1_start = getDate(bornes_saison_1[0]);
		saison1_start.setMonth(Number(saison1_start.getMonth()-1)); // !! Attention 20070930 donne en javascript 20071030 (1 mois de plus)
		saison1_stop = getDate(bornes_saison_1[1]); 
		saison1_stop.setMonth(Number(saison1_stop.getMonth()-1));

		//console.log("saison1_stop.getMonth="+saison1_stop.getMonth());
		//console.log("saison1_stop="+saison1_stop);

		if((Number(now.getTime())>=Number(saison1_start.getTime())) && (Number(now.getTime())<=Number(saison1_stop.getTime()))) {
			saison_pointe=1;
		}

		//---------------------------------------- CALCUL DES BORNES SAISON ID=2
		//console.log("bornes du xml s2: "+bornes_saison_2);

		saison2_start = getDate(bornes_saison_2[0]); saison2_start.setMonth(Number(saison2_start.getMonth()-1));
		saison2_stop = getDate(bornes_saison_2[1]);  saison2_stop.setMonth(Number(saison2_stop.getMonth()-1));
		//console.log("now="+now+" saison2_start="+saison2_start+" saison2_stop="+saison2_stop);

		if((Number(now.getTime())>=Number(saison2_start.getTime())) && (Number(now.getTime())<=Number(saison2_stop.getTime()))) {
			saison_pointe=2;
		}

		}
	//---------------------------------------- ATTRIBUER LE RESULTAT
	//console.log("saison_pointe="+saison_pointe);
	saison_active = saison_pointe;
	}

/*--------------------------------------------------------------*/
/* Bibiothèque de fonction : Lecture vars url					*/
/*--------------------------------------------------------------*/
function getVar(nomVariable)
	{
	var infos = location.href.substring(location.href.indexOf("?")+1, location.href.length)+"&"
	if (infos.indexOf("#")!=-1)
	infos = infos.substring(0,infos.indexOf("#"))+"&"
		var variable=0
		{
		nomVariable = nomVariable + "="
		var taille = nomVariable.length
		if (infos.indexOf(nomVariable)!=-1)
		variable = infos.substring(infos.indexOf(nomVariable)+taille,infos.length).substring(0,infos.substring(infos.indexOf(nomVariable)+taille,infos.length).indexOf("&"))
		}
	return variable
	}

/*--------------------------------------------------------------*/
/* Bibiothèque de fonction : gestion de la date					*/
/*--------------------------------------------------------------*/
function getDate(strDate, type)
	{
	year = strDate.substring(0,4);
	if(type==1) 
		month = strDate.substring(4,6)-1; 
	else
		month = strDate.substring(4,6);
	day = strDate.substring(6,8);
//	alert("year="+year+" month="+month+" day="+day);

	d = new Date();
	d.setDate(day);
	d.setMonth(month);
	d.setFullYear(year);

	return d;
	}

/*--------------------------------------------------------------*/
/* Bibiothèque de fonction : comparer une date					*/
/*--------------------------------------------------------------*/
//Return :
//   0 si date_1=date_2
//   1 si date_1>date_2
//  -1 si date_1<date_2
function compare(date_1, date_2)
	{
	diff = date_1.getTime()-date_2.getTime();
//	alert("date_1 ["+date_1+"] moins date_2 ["+date_2+"] => diff="+diff);

	return (diff==0?diff:diff/Math.abs(diff));
	}