//Declare global variables
//var menuTimeout = 0;
var nav_size, menuState;

//Init global variables
function initNav(size)
{
	nav_size = size;
}

//Show menu
function showMenu(nav)
{

	if(document.formCandidature != null)
	{	
		for (i = 1; i <= 5; i++)
		{
			document.getElementById('select_' + i).style.display = "none";
		}	
	}
	
	if( document.formEmploi != null )
	{
		document.getElementById('select_1').style.display = "none";
		document.getElementById('select_2').style.display = "none";
	}
	
	//window.clearTimeout(menuTimeout);
	//menuTimeout = self.setTimeout('displayMenu(' + nav + ')', 0);
	menuState = document.getElementById('parentNav' + nav).className;
	displayMenu(nav);
}

//Display menu
function displayMenu(nav)
{
	//Menu
	if (menuState == "menu")
	{
		document.getElementById('titreNav' + nav).className = "menuSelectionne";
	} else if (menuState == "menuCourant")
	{
		document.getElementById('titreNav' + nav).className = "menuCourantSelectionne";
	}

	//Sub Menu
	subnav = 'subnav' + nav;
	
	if (document.getElementById) { //IE5
		document.getElementById(subnav).style.display = "";		
	}
	else if (document.layers){ //Netscape
		document.subnav.display = (document.subnav.display == "none")?"":"none";
	}
	else{//IE4
		document.all.subnav.style.display = (document.all.subnav.style.display == "none")?"":"none";
	}
}

//Hide menu
function hideMenu()
{
	for (sm = 1; sm <= nav_size; sm++)
	{
		document.getElementById('subnav' + sm).style.display = "none";
	}
	
	if(document.formCandidature != null)
	{
		var nbSelectForm = 5;		
		for (i = 1; i <= nbSelectForm; i++)
		{
			if(document.getElementById('select_' + i) != null)
				document.getElementById('select_' + i).style.display = "block";
		}	
	}
	
	if( document.formEmploi != null )
	{
		document.getElementById('select_1').style.display = "block";
		document.getElementById('select_2').style.display = "block";
	}
}


//Highlight current element
function setElementOn(elem)
{
	if (menuState == "menu") 
	{
		document.getElementById('elem' + elem).className = "elemSelectionne";
	} else if (menuState == "menuCourant") 
	{
		document.getElementById('elem' + elem).className = "elemCourantSelectionne";
	}
	
}

//Unhighlight previously selected element
function setElementOff(elem)
{
	document.getElementById('elem' + elem).className = "elem";
}

function init2Colonnes(nbPara)
{
	var col1 = document.getElementById("col1");
	var col2 = document.getElementById("col2");
	var col2Parent = document.getElementById("col2Parent");
	col2.innerHTML = col1.innerHTML;
	var maxHeight = Math.round(col1.offsetHeight/2)+1; //378
	var colHeight = 0;
	var lastP = 1;
	for (p = 1; p <= nbPara; p++)
	{
		var divPara = document.getElementById("paragraph"+p);
		if (maxHeight>(colHeight+divPara.offsetHeight + 14) && lastP==p)
		{
			colHeight = colHeight + divPara.offsetHeight + 14;
			lastP = p+1;
		}
	}
	if (lastP<=nbPara)
	{
		var divP = document.getElementById("paragraph"+lastP);
		colHeight = colHeight + divP.offsetHeight + 14;
	}
		
	col1.style.height = colHeight+"px";
	col2Parent.style.height = colHeight+"px";
	col2.style.marginTop = (-1 * col1.offsetHeight)+"px";
}

function displayTemoignage(nbPara)
{
	var divTemoignage = document.getElementById("temoignage");
	var divOmbre = document.getElementById("ombre");
	divOmbre.style.display = "block";
	divTemoignage.style.display = "block";
	var divTitre = document.getElementById("titreTemoignage");
	var col1 = document.getElementById("col1T");
	var col2 = document.getElementById("col2T");
	if (col2.innerHTML!=col1.innerHTML)
	{
		var col2Parent = document.getElementById("col2ParentT");
		col2.innerHTML = col1.innerHTML;
		var maxHeight = divTemoignage.offsetHeight-divTitre.offsetHeight-22;
		var colHeight = 0;
		var colHeight2 = 0;
		var col = 1;
		for (p = 1; p <= nbPara; p++)
		{
			var divPara = document.getElementById("paraTemoignage"+p);
			if (maxHeight>=(colHeight+divPara.offsetHeight+10) && col==1)
			{
				colHeight = colHeight + divPara.offsetHeight + 10;
			}
			else
			{
				colHeight2 = colHeight2 + divPara.offsetHeight + 10;
				col = 2;
			}
		}
		col1.style.height = colHeight+"px";
		col2Parent.style.height = colHeight2+"px";
		col2.style.marginTop = (-1 * col1.offsetHeight)+"px";
	}
}

function hideTemoignage()
{
	var divTemoignage = document.getElementById("temoignage");
	var divOmbre = document.getElementById("ombre");
	divOmbre.style.display = "none";
	divTemoignage.style.display = "none";
}

function getObj(name) {
  if (document.getElementById) {
  	this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
  } else if (document.all) {
			this.obj = document.all[name];
			this.style = document.all[name].style;
	  } else if (document.layers) {
		   	this.obj = document.layers[name];
		   	this.style = document.layers[name];
		  }
}
function getObjFromContainer(mycontainer, myname) {
  if (mycontainer.document.getElementById) {
  	this.obj = mycontainer.document.getElementById(myname);
		this.style = mycontainer.document.getElementById(myname).style;
  } else if (document.all) {
			this.obj = mycontainer.document.all[myname];
			this.style = mycontainer.document.all[myname].style;
	  } else if (document.layers) {
		   	this.obj = mycontainer.document.layers[myname];
		   	this.style = mycontainer.document.layers[myname];
		  }
}
//change the position of the sub menu when resolution changes
function changePosnSubMenu(){

	width = document.documentElement.clientWidth;	// inner width of the content page
	height = document.documentElement.clientHeight; // inner height 
	
	if(window.screen.width >= 1024)
	{
		// difference in px on each left hand and right hand side
		var diffpx = (parseInt(window.screen.width) - 1024)/2; 
		//	length of left value		
		// add 3 pixel - to align left position
		var len = document.getElementById('subnav1').style.left;		
		var leftposn = len.substring('0',len.length-2);				// dnt take the px into consideration			
		document.getElementById('subnav1').style.left = parseInt(leftposn) + parseInt(diffpx) + 3 +"px";
		var len2 = document.getElementById('subnav2').style.left;							
		var leftposn2 = len2.substring('0',len2.length-2);							
		document.getElementById('subnav2').style.left = parseInt(leftposn2) + parseInt(diffpx) + 3 + "px";
		
		var len3 = document.getElementById('subnav3').style.left;							
		var leftposn3 = len3.substring('0',len3.length-2);							
		document.getElementById('subnav3').style.left = parseInt(leftposn3) + parseInt(diffpx) + 3 + "px";
		
		var len4 = document.getElementById('subnav4').style.left;							
		var leftposn4 = len4.substring('0',len4.length-2);							
		document.getElementById('subnav4').style.left = parseInt(leftposn4) + parseInt(diffpx) + 3 + "px";
		
		var len5 = document.getElementById('subnav5').style.left;							
		var leftposn5 = len5.substring('0',len5.length-2);							
		document.getElementById('subnav5').style.left = parseInt(leftposn5) + parseInt(diffpx) + 3 + "px";
	
		// align top/bottom/menu vertically and horizontally
		resize();
	}
		
	// when resizing window -- align vertically and horizontally
	registerEventHandlers();	
	
}

	// left position of content (arrow + text + logo in home page)
	function adjustLeftPosition(diffWidth){
	
		//adjust left position logo incka
		if(document.getElementById('idpersonnage')!=null)
			document.getElementById('idpersonnage').style.left = 390 + parseInt(diffWidth) + "px";
		
						
		// adjust left position of zone 1			
		if(document.getElementById('idflecheHGD')!=null)
			document.getElementById('idflecheHGD').style.left = 203 + parseInt(diffWidth) + "px";		
		
		// zone 2
		if(document.getElementById('idparagrapheHG')!=null)
			document.getElementById('idparagrapheHG').style.left = 230 + parseInt(diffWidth) + "px";				
		
		// zone 3
		if(document.getElementById('idfleche_MGD')!=null)
			document.getElementById('idfleche_MGD').style.left = 362 + parseInt(diffWidth) + "px";				
		
		// zone 4
		if(document.getElementById('idtitre_MG')!=null)
			document.getElementById('idtitre_MG').style.left = 390 + parseInt(diffWidth) + "px";					
		
		// zone 5 
		if(document.getElementById('idfleche_BGB')!=null)
			document.getElementById('idfleche_BGB').style.left = 207 + parseInt(diffWidth) + "px";			
		
		// zone 6
		if(document.getElementById('idtitre_BG')!=null)
			document.getElementById('idtitre_BG').style.left = 230 + parseInt(diffWidth) + "px";					
		
		// zone 7
		if(document.getElementById('idfleche_HDD')!=null)
			document.getElementById('idfleche_HDD').style.left = 762 + parseInt(diffWidth) + "px";			
		
		// zone 8
		if(document.getElementById('idparagraphe_HD')!=null)
			document.getElementById('idparagraphe_HD').style.left = 789 + parseInt(diffWidth) + "px";				
		
		// zone 9
		if(document.getElementById('idfleche_MDH')!=null)
			document.getElementById('idfleche_MDH').style.left = 682 + parseInt(diffWidth) + "px";			
		
		//zone 10
		if(document.getElementById('idtitre_MD')!=null)
			document.getElementById('idtitre_MD').style.left = 704 + parseInt(diffWidth) + "px";				
		
		// zone 11
		if(document.getElementById('idparagraphe_BD')!=null)
			document.getElementById('idparagraphe_BD').style.left = 789 + parseInt(diffWidth) + "px";					
		
		// zone 12
		if(document.getElementById('idfleche_BDB')!=null)
			document.getElementById('idfleche_BDB').style.left = 720 + parseInt(diffWidth) + "px";			
		
		// zone 13
		if(document.getElementById('idfleche_BDD')!=null)
			document.getElementById('idfleche_BDD').style.left = 770 + parseInt(diffWidth) + "px";			
		
		// zone 14
		if(document.getElementById('idfleche_BDG')!=null)
			document.getElementById('idfleche_BDG').style.left = 823 + parseInt(diffWidth) + "px";	
	}
	
	
	// top position of content (arrow + text + logo in home page)
	function adjustTopPosition(heightpx){
		//position logo incka
		if(document.getElementById('idpersonnage')!=null)
			document.getElementById('idpersonnage').style.top = 534 + parseInt(heightpx)/2 - 425 + 4 + "px";
		
						
		// adjust top position of zone 1			
		if(document.getElementById('idflecheHGD')!=null)
			document.getElementById('idflecheHGD').style.top = 109 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 2
		if(document.getElementById('idparagrapheHG')!=null)
			document.getElementById('idparagrapheHG').style.top = 109 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 3
		if(document.getElementById('idfleche_MGD')!=null)
			document.getElementById('idfleche_MGD').style.top = 215 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 4
		if(document.getElementById('idtitre_MG')!=null)
			document.getElementById('idtitre_MG').style.top = 212 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 5 
		if(document.getElementById('idfleche_BGB')!=null)
			document.getElementById('idfleche_BGB').style.top = 289 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 6
		if(document.getElementById('idtitre_BG')!=null)
			document.getElementById('idtitre_BG').style.top = 300 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 7
		if(document.getElementById('idfleche_HDD')!=null)
			document.getElementById('idfleche_HDD').style.top = 109 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 8
		if(document.getElementById('idparagraphe_HD')!=null)
			document.getElementById('idparagraphe_HD').style.top = 110 + parseInt(heightpx)/2 + 4 +"px";			
		
		// zone 9
		if(document.getElementById('idfleche_MDH')!=null)
			document.getElementById('idfleche_MDH').style.top = 281 + parseInt(heightpx)/2 + 4 + "px";			
		
		//zone 10
		if(document.getElementById('idtitre_MD')!=null)
			document.getElementById('idtitre_MD').style.top = 265 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 11
		if(document.getElementById('idparagraphe_BD')!=null)
			document.getElementById('idparagraphe_BD').style.top = 326 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 12
		if(document.getElementById('idfleche_BDB')!=null)
			document.getElementById('idfleche_BDB').style.top = 486 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 13
		if(document.getElementById('idfleche_BDD')!=null)
			document.getElementById('idfleche_BDD').style.top = 513 + parseInt(heightpx)/2 + 4 + "px";			
		
		// zone 14
		if(document.getElementById('idfleche_BDG')!=null)
			document.getElementById('idfleche_BDG').style.top = 497 + parseInt(heightpx)/2 + 4 + "px";		
	}

	
	var isResized = false; // window resizing indicator

    // mouse leaves body in order to grab window's resizing handles
	function setExit() {
        document.body.onmouseover = setEntrance;
    }

     // mouse returns to body after resizing window (or just exiting without resize)
     function setEntrance() {
         document.body.onmouseover = doNothing;
         
         if (isResized) {
           	resize();         
            isResized = false;
         }
     }

     // handle resize events without notifying anyone
     function setIsResized() {
        isResized = true;          
     }

     function doNothing() {
        // really - do nothing...don't waste the ticks
     }

	 
     function resize() {
      
      	width = document.documentElement.clientWidth;	// inner width of the content page
      	height = document.documentElement.clientHeight; // inner height 
      	
      	// if size of window width if less than 1000, resize the window
        if(document.documentElement.clientWidth < 995){				
			window.resizeTo(window.screen.width,window.screen.height)			
		}
		// if size of window height is less than 604, resize the window
		if(document.documentElement.clientHeight<604){			
			window.resizeTo(window.screen.width,window.screen.height)			
		}
  
       	var widthSubMenu = document.getElementById('subnav5').style.width;
		var tmp = widthSubMenu.substring('0',widthSubMenu.length-2);
		
	  	// 997 = width of application ( 800 + 197 )
	  	// 187 = width of the recherche section
	  	if(width <= 997)
		{			
			document.getElementById('subnav1').style.left = "187px";					
			document.getElementById('subnav2').style.left = 187 + parseInt(tmp) + "px";					
			document.getElementById('subnav3').style.left = 187 + 2*parseInt(tmp) + "px";									
			document.getElementById('subnav4').style.left = 187 + 3*parseInt(tmp) + "px";								
			document.getElementById('subnav5').style.left = 187 + 4*parseInt(tmp) + "px";
			
			adjustLeftPosition(0);		// adjust left position of images home page only	
		}
		
		if(width > 997)
		{
			var diffWidth = (parseInt(width) - 997)/2;  // space in on the left hand side
			document.getElementById('subnav1').style.left = 187 + parseInt(diffWidth) + "px";		
			document.getElementById('subnav2').style.left = 187 + parseInt(diffWidth) + parseInt(tmp) + "px";					
			document.getElementById('subnav3').style.left = 187 + parseInt(diffWidth) + 2*parseInt(tmp) + "px";									
			document.getElementById('subnav4').style.left = 187 + parseInt(diffWidth) + 3*parseInt(tmp) + "px";								
			document.getElementById('subnav5').style.left = 187 + parseInt(diffWidth) + 4*parseInt(tmp) + "px";
			
			adjustLeftPosition(diffWidth);	// adjust left position of images home page only	
							
		}
		
		// 594 height of application (502 content + 32 bandeau + 60 menu)
		if(height >594){
			heightpx = parseInt(height) - 594;	
			adjustTopPage(heightpx);				
			
		}
		
		if(height <= 594){						
			heightpx = 2 ;				
			adjustTopPage(heightpx);
		}
			
   }

  function registerEventHandlers() {      	 
     if(navigator.appName.indexOf("Microsoft")>-1){ // BROWSER IE
    	window.onresize = setIsResized;      
     	document.body.onmouseout  = setExit;         
     	document.body.onmouseover = setEntrance;
     }else{         	
     	window.onresize = resize;
     }
     
  }
      
      
      
  // function to adjust the top positin of the content page
  function adjustTopPage(heightpx){
  	// top height = difference in height divide by 2
	document.getElementById('tdtop').height = parseInt(heightpx)/2;
	
	// 534 is the height of the application content + bandeau image
	// 180 = 240 (height of sub menu) - 60 (height of menu)
	// 6 = add 6 pixel to align it with the main menu
	// top position of menu						
	document.getElementById('subnav1').style.top = 534 + parseInt(heightpx)/2 - 180 + 6 + "px";
	document.getElementById('subnav2').style.top = 534 + parseInt(heightpx)/2 - 180 + 6 + "px";
	document.getElementById('subnav3').style.top = 534 + parseInt(heightpx)/2 - 180 + 6 + "px";
	document.getElementById('subnav4').style.top = 534 + parseInt(heightpx)/2 - 180 + 6 + "px";
	document.getElementById('subnav5').style.top = 534 + parseInt(heightpx)/2 - 180 + 6 + "px";
	
	adjustTopPosition(heightpx);	//adjust top position of images(home page only)	
  }
      
      
  //-- Scroll Text
  var delayb4scroll=1000 
  var scrollspeed=1
  var pauseit=1

  var copyspeed=scrollspeed
  var pausespeed=(pauseit==0)? copyspeed: 0
  var actualheight=''

  function initializeScrollText(){
  	scrolltext=document.getElementById("divscrollText")
  	if(scrolltext==null)
  		return
	scrolltext.style.top=0
	scrolltextheight=document.getElementById("scrollingtextcontainer").offsetHeight
	actualheight=scrolltext.offsetHeight
	if (window.opera || navigator.userAgent.indexOf("Netscape/7")!=-1){ //if Opera or Netscape 7x, add scrollbars to scroll and exit
		scrolltext.style.height=scrolltextheight+"px"
		scrolltext.style.overflow="scroll"
		return
	}	
	setTimeout('lefttime=setInterval("scrollText()",30)', delayb4scroll);
	document.getElementById("scrollingtextcontainer").onmouseover=new Function('copyspeed=pausespeed;'); 
	document.getElementById("scrollingtextcontainer").onmouseout=new Function('copyspeed=scrollspeed;'); 
 }

 function scrollText(){
 	if (parseInt(scrolltext.style.top)>(actualheight*(-1)+28))
		scrolltext.style.top=parseInt(scrolltext.style.top)-copyspeed+"px"
	else
		scrolltext.style.top=parseInt(scrolltextheight)+28+"px"
 }

 if (window.addEventListener)
	window.addEventListener("load", initializeScrollText, false)
 else if (window.attachEvent)
	window.attachEvent("onload", initializeScrollText)
 else if (document.getElementById)
	window.onload=initializeScrollText