// <![CDATA[
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//  HISTORY
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
var userAgentStr = navigator.userAgent.toLowerCase();
var isMSIE = userAgentStr.indexOf('msie');
if(isMSIE !== -1)	{isMSIE = true;}

function loadAndShow(element,template,sector)	{
	jQuery(function($){ // on page ready
  		$(element).onImagesLoaded(function(_this){
			document.getElementById('LOADING').style.display = 'none'; 
			fadeChanging('in',element);
			if(template == 'regular.php')	{}
			if(template == 'accueil.php')	{$('#NEWSBOX, #TOPBOX, #logo_little').show(400); setTimeout("ShowRadmenu()",200);}
  		});
	}); // end on ready
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ANIMATION MENU COLONNE GAUCHE

var mouse = false;
var process = new Array();

function MouseStatus(n)	{	mouse = n;	}
function getMouseStatus()	{ 	return mouse;	}
function getProcessStatus(id)	{ 	return process[id];	}

function ActivateMenu(idblock,idlink,idsub,color1,color0)	{
	process[idlink] = false;
	$("#"+idlink).animate({backgroundColor: color1},200);
	if(document.getElementById(idsub))	{
		if(!(getProcessStatus(idlink)))	{
			process[idlink] = true;
		//	if(mouse == true)	{
				$("#"+idsub).show(200,function()	{
					process[idlink] = false;
				});		
		//	}		
		}
	}
	$("#"+idblock).mouseleave(function(){
		$("#"+idlink).stop(true, false).animate({backgroundColor: color0},180);
		if(document.getElementById(idsub))	{
			process[idlink] = true;
			$("#"+idsub).hide(200,function()	{
				process[idlink] = false;
			});
		}
	});
}

function MoveItem(id)	{
	if(!(sector == id))	{
		$("#LINK_"+id).animate({"padding-left":"40px"},460);
	}
}

function ResetItem(id)	{
	if(!(sector == id))	{
		$("#LINK_"+id).stop(true, false).animate({"padding-left":"0px"},260);
	 }
}

function setMenu(x)	{
	$("#LINK_"+x).animate({"padding-left":"40px"},200);		
	$("#ARROW_"+x).fadeIn(400);
	setTimeout('eval("sector = \''+x+'\'")',600);
}

function hideMenu(x)	{
	$("#LINK_"+x).animate({"padding-left":"0px"},200);	
	$("#ARROW_"+x).fadeOut(300);	
}

function changeMenu(x)	{
	if(!(x == sector))	{setMenu(x); hideMenu(sector); sector = x;}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CHANGEMENT DE CONTENUS/PAGES
function Transition(href)	{
	var base; var hash;
	if(href.indexOf('#'))	{	var tmp = href.split('#'); base = tmp[0]; hash = tmp[1];	}	else	{	base = href;	}
	var OldHref = document.getElementById('HASH').innerHTML;
	if(!(OldHref == 'accueil') && !(OldHref == ''))	{var OldLayoutFigure = 'B';}	else	{var OldLayoutFigure = 'A';}
	var Page = contents[base];
	if(!(Page.map == 'ACCUEIL') && !(Page.map == ''))	{var NewLayoutFigure = 'B';}	else	{var NewLayoutFigure = 'A';}
	var NewHrefParts = base.split('/');
	var NewDepth = NewHrefParts.length; 
	var OldHrefParts = OldHref.split('/');
	var OldDepth = OldHrefParts.length; 
	var SameSector = 0;
	var SamePage = 0;
	if(NewHrefParts[0] == OldHrefParts[0] && !(NewHrefParts[0]==-1))	{
		SameSector = 1;
		if(NewHrefParts[1] == OldHrefParts[1] && !(NewHrefParts[1]==-1))	{
			if(NewHrefParts[2] == OldHrefParts[2] && !(NewHrefParts[2]==-1))	{
				SamePage = 1;
			}	
		}	
	}	
	
		if(!(OldLayoutFigure == NewLayoutFigure))	{ 	// CASE LAYOUT A -> LAYOUT B
		// alert(OldLayoutFigure+'/'+NewLayoutFigure+'/'+Page.map);
			if(Page.map == 'SECTORS' || Page.map == 'MENUTOP')	{	loadContent('scripts/php/generate_LINKPAD.php?map='+Page.map+'&sector='+Page.sector,"#BUFFER"); 
																	if(!(Page.sector==href))	{	changeSRCS(Page.map,href);	}	}
			fadeChanging('out',"#MAINCELLCONTENT");
			window.document.title = Page.title;
			if(Page.map == 'SECTORS' || Page.map == 'MENUTOP')	{
				var Buffer = templates['regular']; 
				Buffer = Buffer.replace('<!-- PAGECONTENTS SEGMENT -->',Page.html.replace(/(')/g, '\\'+"$1"));
				Buffer = Buffer.replace('<!-- FILDARIANE SEGMENT -->',Page.fil.replace(/(')/g, '\\'+"$1"));
				Buffer = Buffer.replace('<!-- LEFTCOL SEGMENT -->',leftcol.replace(/(')/g, '\\'+"$1"));
				Buffer = Buffer.replace('<!-- PRINT SEGMENT -->',Page.Print.replace(/(')/g, '\\'+"$1"));
			//	setTimeout('var Linkpad = document.getElementById("BUFFER").innerHTML',600);
				setTimeout('document.getElementById("LINKPAD").innerHTML = document.getElementById("BUFFER").innerHTML; document.getElementById("BUFFER").innerHTML = ""',620);
			}
			if(Page.map == 'MENUBOTTOM')	{
				var Buffer = templates['simple'];
				Buffer = Buffer.replace('<!-- PAGECONTENTS SEGMENT -->',Page.html.replace(/(')/g, '\\'+"$1"));
				Buffer = Buffer.replace('<!-- FILDARIANE SEGMENT -->',Page.fil.replace(/(')/g, '\\'+"$1"));
				Buffer = Buffer.replace('<!-- LEFTCOL SEGMENT -->',leftcol.replace(/(')/g, '\\'+"$1"));
				Buffer = Buffer.replace('<!-- PRINT SEGMENT -->',Page.Print.replace(/(')/g, '\\'+"$1"));
			}
			if(!(Page.map == 'ACCUEIL'))	{
				setTimeout('$("#MAINCELLCONTENT").html(\''+Buffer+'\')',400);
				setTimeout('changeMenu("'+Page.sector+'")',680);
				setTimeout('document.getElementById("bgLabel").src = \'scripts/php/generate_LABEL.php?label='+Page.label.toUpperCase()+'\'',650);
			}
			if(Page.map == 'ACCUEIL')	{
				var Buffer = templates['accueil'];
				Buffer = Buffer.replace(/(')/g, '\\'+"$1");
				Buffer = Buffer.replace('<!-- NEWSBOX SEGMENT -->',newsblock.replace(/(')/g, '\\'+"$1"));
				Buffer = Buffer.replace('<!-- RADIALBOX SEGMENT -->',radialcontainer.replace(/(')/g, '\\'+"$1"));
				setTimeout('$("#MAINCELLCONTENT").html(\''+Buffer+'\')',400);
				setTimeout("$('#NEWSBOX, #TOPBOX, #logo_little').show(400)",450);
				setTimeout('ShowRadmenu()',600);
			}
			setTimeout('fadeChanging(\'in\',\'#MAINCELLCONTENT\')',650);
		}	else	{ 									// CASE SAME LAYOUT FIGURE
			if(SameSector == 0)	{
				if(!(Page.map == 'MENUBOTTOM'))	{loadContent('scripts/php/generate_LINKPAD.php?map='+Page.map+'&sector='+Page.sector,"#BUFFER");}
				fadeChanging('out','#MAINDIV');
				fadeChanging('out','#bgLabel');	
				changeMenu(Page.sector);
				if(document.getElementById('LINKPAD').style.display == 'none' && !(Page.map == 'MENUBOTTOM'))	{setTimeout('document.getElementById(\'LINKPAD\').style.display = \'block\'', 400);  setTimeout('document.getElementById(\'LABEL\').style.borderBottom = \'1px dotted #AAA\'', 400); setTimeout('document.getElementById(\'LABEL\').style.paddingTop = \'5px\'', 400);}
				else if(Page.map == 'MENUBOTTOM')	{setTimeout('document.getElementById(\'LINKPAD\').style.display = \'none\'', 400); setTimeout('document.getElementById(\'LABEL\').style.borderBottom = \'0px\'', 400); setTimeout('document.getElementById(\'LABEL\').style.paddingTop = \'28px\'', 400);}
				window.document.title = Page.title;
				setTimeout('document.getElementById("PAGECONTENTS").innerHTML = \''+Page.html.replace(/(')/g, '\\'+"$1")+'\'',410);
				setTimeout('document.getElementById("LABEL").innerHTML = \''+Page.fil.replace(/(')/g, '\\'+"$1")+'\'',420);
				setTimeout('document.getElementById("PRINT").innerHTML = \''+Page.Print.replace(/(')/g, '\\'+"$1")+'\'',430);
				setTimeout('document.getElementById("bgLabel").src = \'scripts/php/generate_LABEL.php?label='+Page.label.toUpperCase()+'\'',440);
				if(!(Page.map == 'MENUBOTTOM'))	{
				//	setTimeout('var Linkpad = document.getElementById("BUFFER").innerHTML',440);
					setTimeout('document.getElementById("LINKPAD").innerHTML = document.getElementById("BUFFER").innerHTML; document.getElementById("BUFFER").innerHTML = ""',450);
				}
				setTimeout('fadeChanging(\'in\',\'#bgLabel\')',470);
				setTimeout('fadeChanging(\'in\',\'#MAINDIV\')',460);
			}
			if(SameSector == 1)	{
				changeSRCS(Page.map,href);
				fadeChanging('out','');
				$("#LINKPAD img[class=changin]").fadeTo(400,0);
				setTimeout('document.getElementById("PAGECONTENTS").innerHTML = \''+Page.html.replace(/(')/g, '\\'+"$1")+'\'',420);
				setTimeout('document.getElementById("LABEL").innerHTML = \''+Page.fil.replace(/(')/g, '\\'+"$1")+'\'',430);
				setTimeout('document.getElementById("PRINT").innerHTML = \''+Page.Print.replace(/(')/g, '\\'+"$1")+'\'',440);
				window.document.title = Page.title;
				setTimeout('fadeChanging(\'in\',\'\')',500);
			}
		}
		
	document.getElementById('HASH').innerHTML = href;
	window.location.hash = '/'+base;
	setTimeout("ScrollToElement('"+hash+"')",700);
	return true;
}

function ChangePage(map,x,sector,titre)	{
		document.getElementById('HASH').innerHTML = sector;
		window.location.hash = '/'+sector;
		fadeChanging('out','');
		$("#LINKPAD img[class=changin]").fadeTo(400,0);
		setTimeout('changeSRCS("'+map+'","'+sector+'")',400);
		var href = document.getElementById('menuLinkHidden'+x).value;
		setTimeout('document.getElementById("PAGECONTENTS").innerHTML = \''+contents[sector].html.replace(/(')/g, '\\'+"$1")+'\'',420);
		setTimeout('document.getElementById("LABEL").innerHTML = \''+contents[sector].fil.replace(/(')/g, '\\'+"$1")+'\'',430);
		setTimeout('document.getElementById("PRINT").innerHTML = \''+contents[sector].Print.replace(/(')/g, '\\'+"$1")+'\'',440);
		window.document.title = 'Institution de Lavigny : '+titre;
		setTimeout('fadeChanging(\'in\',\'\')',500);
		return true;
}

function ScrollToElement(id){
	var height = $("#"+id).offset().top;
	$('html,body').animate({	scrollTop: $("#"+id).offset().top	},600);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// - - - - - - - - AJAX IMAGE RELOADING
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function changeSRCS(map,sector)	{
	var links = $("#LINKPAD img[class=changin]");
	var ids = new Array();
	var newSRC = new Array();
	for(var a = 0; a<links.length; a++)	{ids[a] = links[a].id;}
	$.ajax({
	  	url: 'scripts/php/generate_SRC.php?ids='+ids.join(',')+'&map='+map+'&href='+sector,
	  	success: function(data){
			eval(data);
		}
	});
	setTimeout('$("#LINKPAD img[class=changin]").fadeTo(400,1);',500);
//	alert('changeSRCS OK');
}

// IMAGE PRELOAD...
// IE DOES'NT ALLOW .indexOf() PROPERTY ON ARRAYS... NEED THIS PROTOTYPE FIX

if(!Array.indexOf){Array.prototype.indexOf = function(obj){for(var i=0; i<this.length; i++){if(this[i]==obj){return i;}}return -1;}}
var PreloadedSRCS = new Array();
function ChargePreloader(src)	{
	if(!(PreloadedSRCS.indexOf(src)>0))	{
		document.getElementById("PRELOADER").innerHTML += '<img src="'+src+'" style="position: absolute;" />';	
		PreloadedSRCS.push(src);
	}	else	{
	}
}

// sous-dossiers... A GARDER
function ChangeText(x,y,z)	{
	var href = z.split('.');
	var href2 = href[0].replace(/_/g, '\/');
	fadeChanging('out','#PAGECONTENTS');
	if(z.indexOf('.') == -1)	{z += '.txt';}
	setTimeout('loadContent("scripts/php/generate_FOURTHCONTENT.php?map='+x+'&sector='+y+'&URL=../../contents/texts/'+z+'","#PAGECONTENTS")',300);
	if(href2)	{document.getElementById("PrintMe1").href = 'print='+href2; document.getElementById("PrintMe2").href = 'print='+href2;}
	setTimeout('fadeChanging(\'in\',\'#PAGECONTENTS\')',400);
	return true;
}

function ChangeText2(x,y,z)	{
		fadeChanging('out','#PAGECONTENTS');
		setTimeout('loadContent("contents/texts/'+z+'","#PAGECONTENTS")',300);
		setTimeout('fadeChanging(\'in\',\'#PAGECONTENTS\')',400);
		return true;
}

function loadContent(x,target)	{
	$.ajax({
	  url: x,
	  success: function(data){
	 	$(target).html(data);
	  },
	  complete: function()	{
		//applyHandlersOnTarget(target);  
	  }
	});
}

function NewsShowHide(id)	{
	if($('#CONTAINER-'+id).attr('class')=='reduced')	{
		$('.expanded').animate({
			height: '80'							 
		},400);
		$('.expanded').attr('class','reduced');
		$('#CONTAINER-'+id).attr('class','expanded');
		$('#CONTAINER-'+id).animate({
			height: document.getElementById('MARK-'+id).offsetTop							 
		},400);
	}	else	{
		$('#CONTAINER-'+id).attr('class','reduced');
		$('#CONTAINER-'+id).animate({
			height: '80'								 
		},400);
	}
}
function ShowHide(id)	{
	if($('#'+id).attr('class')=='hidden')	{
		$('.visible').attr('class','hidden');
		$('#'+id).attr('class','visible');
		$('#'+id).show(400);
	}	else	{
		$('#'+id).attr('class','hidden');
		$('#'+id).hide(400);
	}
}
var closeTimeout;
function CheckAndSend(x)	{
	var value = document.getElementById('e-mail').value;
	var catvalue = new Array();
	if(document.getElementById('cat01').checked)	{catvalue.push(document.getElementById('cat01').value);}
	if(document.getElementById('cat02').checked)	{catvalue.push(document.getElementById('cat02').value);}
	if(document.getElementById('cat03').checked)	{catvalue.push(document.getElementById('cat03').value);}
	if(document.getElementById('cat04').checked)	{catvalue.push(document.getElementById('cat04').value);}
	var catstring = catvalue.join(' - ');
	if(value.indexOf('@')>0 && value.indexOf('.')>0)	{
		$('#ABOCONTAINERCONTENT').fadeTo(300,0);
		setTimeout("loadContent('scripts/php/add_EMAIL.php?x="+value+"&prenom="+document.getElementById('prenom').value+"&nom="+document.getElementById('nom').value+"&cat="+catstring+"','#ABOCONTAINERCONTENT')",300);	
		setTimeout("$('#ABOCONTAINERCONTENT').fadeTo(300,100)",400);
		closeTimeout = setTimeout("closeAboContainer('"+x+"')",8000);
	}	else	{
		alert('Merci de fournir une adresse e-mail valable !');	
	}
}
function closeAboContainer(x)	{
	$('#ABOCONTAINER').hide(600);
	setTimeout("document.getElementById('LABEL').innerHTML = contents[\'"+x+"\'].fil",600);
	closeTimeout = clearTimeout(closeTimeout);
}

// - - - - VISUAL ANIMATION

function fadeChanging(x,area)	{
if(x=='in')	{var opacity = 1;}	else	{var opacity = 0;}
if(!(navigator.userAgent.toLowerCase().indexOf('msie') == -1))	{opacity = 100*opacity;}
	if(area == '')	{
		$("#PAGECONTENTS").fadeTo(400,opacity);
	}	else	{
		$(area).fadeTo(400,opacity);
	}
}

function showMe(id)	{
	$("#"+id).show(400);
}
function contactTransition()	{
	fadeChanging('out','#CONTACTFORM');
	setTimeout("document.getElementById('contact_form').submit()",400);
}
// ]]>
