//Fonction qui permet de fermer tous les items du menu et de garder ouvert
//le module sélectionné par l'utilisateur.
function contractall()
{
	try
	{
		if (isRemote == true) c = detectModule(category);
	}
	catch (err)
	{
		var regexS = "[\\?&]c=([^&#]*)";
	  	var regex = new RegExp( regexS );
	  	var tmpURL = window.location.href;
	  	var results = regex.exec( tmpURL );
		if(results == null)
			var c = -1;		
		else
			var c = results[1];
		c = detectModule(c);
	}
	//alert ("c is " + c);
		
		
	for(i=0; i<titles.length; i++) 
	{
		arrows[i].src = document.form.path.value + 'collapsed.gif';
	}
		
	if(c=='4')
		document.form.idsubject.value = 2;
	for(i=0; i<subcategories.length; i++)
	{
		if(subcategories[i].id.substr(9,1) != document.form.idsubject.value)
		{
			// Si ce n'est pas la bonne catégorie, la fermer
			subcategories[i].style.display="none";
			subcategories[i].style.height = 0;	
		}
		else if(i != c)
			{
				// Si ce n'est pas le bonne année, la fermer
				subcategories[i].style.display="";
				subcategories[i].style.visibility='visible';		
				subcategories[i].style.height = eval(heights[i]-heightsLink[i]);
			}
			else
			{
				// Si c'est la bonne catégorie et la bonne année
				arrowsCat[i].src = document.form.path.value + 'expanded.gif';
				arrows[Math.floor(i/2)].src = document.form.path.value + 'expanded.gif';
				titles[Math.floor(i/2)].onclick = contract_title;
				subcategories[i].onclick =contract_title;
				subcategories[i].style.visibility='visible';
			}
	}
}



function contract_title(e)
{
	
	if (!e)
        var e = window.event;
	
    var ce = (e.target) ? e.target : e.srcElement;
	
	ce = detectParent(ce);
	if((ce.id.substr(0,4) != "link") && (ce.id.substr(0,4) != "desc") && (ce.className != "about"))
	{
		var elem;
		var menutitle = "";
		for(var i in titles) {
			if(titles[i] == ce || arrows[i] == ce){
				menutitle = i;
				elem = 2 * i;
			}
		}//Pour fermer le 2ième niveau
		if(menutitle == '')
		{
			for(var i in subcategories) {
				if(subcategories[i] == ce || arrowsCat[i] == ce){
					menutitle = i;
					elem = i;				
				}
			}
			var nb =  findCategorie(menutitle);
			hideTitle(elem,true);
		}
		else
		{   
			// Pour fermer le premier niveau
			var nb =  findCategorie(menutitle);
			if(nb != 0)
			{
				for(var a=eval((elem+nb)-1);a>=elem;a--)
				{
					hideTitle(a,false);
				}
			}
			else // S'il n'y a pas de 2ieme niveau au menu 
			{
				elem = 5;
				hidemenu(elem);
			}
			
			if(titles[Math.floor(elem/2)].id != ""){
				titles[Math.floor(elem/2)].onclick = openmenu;
				arrows[Math.floor(elem/2)].onclick = openmenu;
			}
			else
				titles[Math.floor(elem/2)].onclick = openModuleParent;
		}
		
	}
}

// Utilisé pour mac
function detectParent(ce)
{
	var access = false;
	if(ce.id == 'access')
		access = true;
	if((ce.tagName == "TD") || (isUndefined(ce.tagName)) || (access == true))
	{
		if((/MSIE/.test(n)) && (access != true))
			ce = ce.parentElement;
		else
		{
			ce = ce.parentNode;
			if (((navigator.appVersion.toLowerCase().indexOf("mac") > 0) && (ce.tagName == "TD")) || ((access == true) && (ce.tagName != "DIV")))
				ce= ce.parentNode;
		}
	}

	return ce;
}

function detectModule(c)
{	
	try
	{
		var theModID = modID;
		// should read "&modID=22,22"
	}
	catch (err)
	{
		var regexS = "[\\?&]modID=([^&#]*)";
		var regex = new RegExp( regexS );
		var tmpURL = window.location.href;
		var results = regex.exec( tmpURL );
		var theModID = results [1];
		var elem = -1;
	}

  if(theModID != undefined)
  {
	  var lien = "desc" + theModID;
	 // alert ("lien is " + lien);
	  var isnull = false;
	  if(c == -1)
		  isnull = true;
	  
	  for(var i in descriptions)
	  {
		
		if(typeof descriptions[i] != "function") 
		{
			
			var parent = getParent(i);
			if(isnull == true)
				c = parent;
			if((descriptions[i].id == lien) && (parent == c))
		 	{
				 isnull = false;
				 descriptions[i].style.display="";
				 descriptions[i].style.height = heightsDesc[i]+"px";
				 elem = i;
		 	}
		}
		
	  }
  }
  for(j=0; j<submenus.length; j++)
  {
	if(submenus[j].id != c)
	{
		submenus[j].style.display="none";
		submenus[j].style.height = 0;	
	}
	else
	{
		submenus[j].style.height = parseInt(submenus[j].style.height) + parseInt(descriptions[elem].style.height);
		subcategories[j].style.height = parseInt(subcategories[j].style.height) + parseInt(descriptions[elem].style.height);		 			
		
	} 		
  } 
  return c;
 
}

function getQueryString(queryToUse)
{
	// First, define the defaults
	var obArguments = new Object();

	// Now examine the Query String
	var query = (queryToUse == undefined)? location.search.substring(1) : queryToUse;

	if (query.length > 0)
	{		
		// Split query at the ampersand
		var pairs = query.split("&");
		
		var counter = 0;
		// Begin loop through the querystring
		for (var i = 0; i < pairs.length;  i++)
		{
			// Look for "name=value"
			var pos = pairs[i].indexOf('=');
			
			// if not found, skip to next
			if (pos == -1) continue;
	
			// Extract the name
			var argname = pairs[i].substring(0,pos);
			
			// Extract the value
			var value = pairs[i].substring(pos+1); 
			
			// Store as a property
			obArguments[argname] = unescape(value);
			
			//alert ("found " + argname + ", which is " + unescape (value));
		}
	}
	return obArguments; // Return the Object
}

/* Fonction qui permet d'initier le menu */
function init()
{
	if (document.all)
   		menu = document.all["menu"];
	else 
	{
  		if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) 
		{
			menu = document["menu"]; 
		}
		else
		{
			menu = document.getElementById("menu");
		}
	}	

	titles = getElementsByClassName("", "tr", menu);
	submenus = getElementsByClassName("submenu", "div", menu);
	arrows = getElementsByClassName("arrow", "img", menu);
	arrowsCat = getElementsByClassName("arrowCat", "img", menu);
	subcategories = getElementsByClassName("subCategorie", "div", menu);
	descriptions = getElementsByClassName("fadeDesc", "div", menu);	
	
	for(i=0; i<titles.length; i++) 
	{
		if(titles[i].id != "")
			titles[i].onclick = openmenu;
		else
			titles[i].onclick = openModuleParent;
	}
	for(var j=0;j<descriptions.length;j++)
	{
		heightsDesc[j] = descriptions[j].offsetHeight;
		descriptions[j].style.display="none";
		descriptions[j].style.height = 0;
	}
	for(i=0; i<submenus.length; i++)
	{
		heightsLink[i] = submenus[i].offsetHeight;
		submenus[i].style.height = submenus[i].offsetHeight+"px";
	}
	for(i=0; i<subcategories.length; i++)
	{
		heights[i] = subcategories[i].offsetHeight;
		subcategories[i].style.height = subcategories[i].offsetHeight+"px";
		subcategories[i].onclick = openmenu;
	}
	
	contractall();
		
	var obQueryString = getQueryString ();
	if (obQueryString.mvc != undefined) // if modViewChoice (mvc) was passed in the url
	{		
		showIncludedFile(obQueryString.mvc);
	}
	else
	{
		/*
		var theSWF;
		var modViewChoice;
		if(/MSIE/.test(n))
		{
			theSWF = getFlashMovieObject ("sharedObject");
			modViewChoice = theSWF.returnModViewChoice (window.modID);
		}

		if(/MSIE/.test(n))
		{
			showIncludedFile(modViewChoice);
		}
		*/
	}
}

function openmenu(e) {
   
	
	var maxcategories = 0;
    if (!e)
        var e = window.event;
	
    var ce = (e.target) ? e.target : e.srcElement;
	
	ce = detectParent(ce);
	
	if(ce.className != "about") 
	{
		var elem;
		var menutitle = "";
		for(var i in titles) {
			if(titles[i] == ce || arrows[i] == ce){
				menutitle = i;
				elem = i * 2;
				
			}
		}
		if(menutitle == '')//Ouvre le deuxième menu, affiche les modules
		{
			for(var i in subcategories) {
				if(subcategories[i] == ce || arrowsCat[i] == ce){
					menutitle = i;
					elem = i;
					
				}
			}
			showsubmenu(elem, true);
		}
		else // Ouvre le premier menu, affiche les catégories
		{
			maxcategories = findCategorie(menutitle);
			
			if(maxcategories != 0 )
			{
				for(var i = elem; i<eval(elem + maxcategories);i++)
				{
					 subcategories[i].style.visibility='visible';
					 showmenu(i);
				}
			}
			else //Ouvre le menu n'ayant pas de 2ieme niveau, menu pour l'école saine
			{
				elem = 5;
				showsubmenu(elem, false);
			}
		}
		// Ferme le menu qui est ouvert
		for(var a in submenus)
		{
			if((typeof submenus[a] != "function") && (parseInt(submenus[a].style.height) != 0))
			{
				if(!subcategories[a]) 
					hidemenu(a);			
				else if(a != elem)
					{
						//window.alert(subcategories[elem].style.height );
						switch (a)
						{
						
							case '4':	if(isClosing == false){
											isClosing = true;
											hideTitle(a,false);
										}
										//window.alert('dans case false');
										break;
							default:	hideTitle(a,true);
						}
					}
			}
		}
	}
	
}

function findCategorie(menutitle)
{
	var nb = 0;
	for(var i = 0; i<subcategories.length;i++)
	{
		if(!subcategories[i])
			nb = 0;
		else if(subcategories[i].id.substr(9,1) == menutitle)
			nb += 1;
	}
	return nb;
}

function hidemenu(elem)
{
	var nr = submenus[elem].getElementsByTagName("a").length*pixel;
	submenus[elem].style.height = (parseInt(submenus[elem].style.height)-nr)+"px";
  	var to = setTimeout("hidemenu("+elem+")", 60);
	if(parseInt(submenus[elem].style.height) <= nr) {
    	clearTimeout(to);
		submenus[elem].style.display = "none";
    	submenus[elem].style.height = "0px";
		arrows[3].src = document.form.path.value + 'collapsed.gif';
		titles[3].onclick = openmenu;
	}
	
}

function hideTitle(elem,exist)
{
	
	var nr = 3 * pixel;
	if(exist == true)
		var len = eval(heights[elem]-heightsLink[elem]);
	else
		var len = nr;
	subcategories[elem].style.height = (parseInt(subcategories[elem].style.height)-nr)+"px";
	var to = setTimeout("hideTitle("+elem+","+exist+")", 60);
	if(parseInt(subcategories[elem].style.height) <= len) 
	{
		clearTimeout(to);
	  	if(exist == true){
			subcategories[elem].style.height = eval(heights[elem]-heightsLink[elem])+"px";
			arrowsCat[elem].src = document.form.path.value + 'collapsed.gif';
			subcategories[elem].onclick = openmenu;
		}
		else{
			
			subcategories[elem].style.display = "none";
			subcategories[elem].style.height = "0px";
			arrows[Math.floor(elem/2)].src = document.form.path.value + 'collapsed.gif';
			arrowsCat[elem].src = document.form.path.value + 'collapsed.gif';
			subcategories[elem].onclick = openmenu;
			if(titles[Math.floor(elem/2)].id != ""){
				titles[Math.floor(elem/2)].onclick = openmenu;
				arrows[Math.floor(elem/2)].onclick = openmenu;
			}
			else
				titles[Math.floor(elem/2)].onclick = openModuleParent;
		}
		submenus[elem].style.display = "none";
		submenus[elem].style.height = "0px";
		closeAll(elem);
    } 
}
function showmenu(elem)
{
	var nr = 3 * pixel;
	subcategories[elem].style.display = "";
    subcategories[elem].style.height = (parseInt(subcategories[elem].style.height)+nr)+"px";
	var to = setTimeout("showmenu("+elem +")", 60);
	if(parseInt(subcategories[elem].style.height) >= eval(heights[elem]-heightsLink[elem]))
	{
		clearTimeout(to);
        subcategories[elem].style.height = eval(heights[elem]-heightsLink[elem])+"px";
        arrows[subcategories[elem].id.substr(9,1)].src = document.form.path.value + 'expanded.gif';
		//window.alert('dans showmenu');
		//window.alert(subcategories[elem].id.substr(9,1));
		titles[subcategories[elem].id.substr(9,1)].onclick = contract_title;
		
    }
	
}

function showsubmenu(elem,exist)
{

	var nr = submenus[elem].getElementsByTagName("a").length*pixel;		
	submenus[elem].style.display = "";
    submenus[elem].style.height = (parseInt(submenus[elem].style.height)+nr)+"px";
	
	if(exist == true)
	{
		subcategories[elem].style.display = "";
		subcategories[elem].style.height = parseInt(submenus[elem].style.height) + eval(heights[elem]-heightsLink[elem]);
	}
    var to = setTimeout("showsubmenu("+elem+","+exist+")", 60);
	
    if(parseInt(submenus[elem].style.height) >= (parseInt(heightsLink[elem])-nr)){
        clearTimeout(to);
        submenus[elem].style.height = heightsLink[elem]+"px";
		if(exist == true)
			subcategories[elem].style.height = heights[elem]+"px";
	    if(!arrowsCat[elem])
		{
			// pour le menu divers 
			arrows[3].src = document.form.path.value + 'expanded.gif';
			titles[3].onclick = contract_title;
		}
		else
		{
			// pour les menus des années (ex : k-3, 4-6)
			arrowsCat[elem].src = document.form.path.value + 'expanded.gif';
			subcategories[elem].onclick = contract_title;
		}
    }
}

// Fontion qui permet d'obtenir un tableau d'éléments ayant tous le meme nom de classe.

function getElementsByClassName(strClassName, strTagName, oElm){
	if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) 
	{
		var arrElements = (strTagName == "*" && document.layers)? document.layers : oElm.document.layers[strTagName]; 
	}
	else
		var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName); 
	var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
     for(var i=0; i<arrElements.length;i++){
        oElement = arrElements[i]; 
        if(oRegExp.test(oElement.className))
		{		
			arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

//fonction qui permet de fermer toutes les descriptions

function closeAll(categorie)
{
	for(var j=0;j<descriptions.length;j++)
	{
		if(categorie == getParent(j)){
			descriptions[j].style.display="none";
			descriptions[j].style.height = 0;			
		}
	}
	
}

//Fonction qui n'est pas utilisé, mais si elle l'était elle permettrait de fermer une
//description en particulier avec un effet comme pour les autres items du menu.
function closeDescription(elem,m,c,parent)
{
		var nr = 3 * pixel;
		descriptions[elem].style.height = (parseInt(descriptions[elem].style.height)-nr)+"px";
		submenus[parent].style.height = (parseInt(submenus[parent].style.height)-nr)+"px";
		subcategories[parent].style.height = (parseInt(subcategories[parent].style.height)-nr)+"px";
		var to = setTimeout("closeDescription("+elem+","+m+","+c+","+parent+")", 30);
		if(parseInt(descriptions[elem].style.height) <= nr) {
			clearTimeout(to);
			descriptions[elem].style.display = "none";
			descriptions[elem].style.height = "0px";
			submenus[parent].style.height = heightsLink[parent] + "px";
			subcategories[parent].style.height = heights[parent] + "px";
			for(var j in descriptions)
			{					
				if(typeof descriptions[j] != "function") {
					var temp = getParent(j);
					if((descriptions[j].id.substr(4,descriptions[j].id.length)== m) && (temp == c))
						openDescription(j,temp);
				}
			}
		}
		
}


// Fonction qui retourne le parent d'un module, donc la sous-categorie.
function getParent(j)
{
	if((/MSIE/.test(n)) && (navigator.appVersion.toLowerCase().indexOf("mac") < 0)){
		var parent = descriptions[j].parentElement.id;
	}
	else
	{
		var parent = descriptions[j].parentNode.id;
	}
	
	return parent;
	
}

// Fonction qui vérifie si l'object est défini.
function isUndefined(property) 
{
  return (typeof property == 'undefined');
}



//Fonction qui permet d'initialiser le css pour le mac
function initScroll()
{
	if (navigator.appVersion.toLowerCase().indexOf("mac") > 0)
	{
		if (document.all)
			var incscroll = document.all['scroll1'];
		else
			var incscroll = document.getElementById("scroll1");
		incscroll.style.overflow = "scroll";
	}
}


//Fonction qui permet d'initialiser la langue
function setLanguage(L)
{
	Lang = L;
}

function nothing()
{
 
}

//Function qui permet d'ouvrir le module des parents
function openModuleParent()
{
	if(Lang == '1')
		var modParent = '46';
	else
		var modParent =  '45';
	window.location = "core.cfm?p=modView.cfm&L="+ Lang +"&modID="+ modParent +"&c=4&navID=modView";
}
// Main Application

// Declaration des variables

var menu, titles, submenus, arrows, arrowsCat, pixel, subcategories, heights, heightsDesc, descriptions;
var heightsLink = new Array();
var heights = new Array();
var heightsDesc = new Array();
var undefined;

var n = navigator.userAgent;
var browserVersion = parseInt(navigator.appVersion);
var pixel = 2;
var Lang;
var isClosing = false;

// utilisée par IE5.1 sur Mac car la fonctionnalité Array.push n'existe pas sur cette version.

if( typeof Array.prototype.push == 'undefined' )
{
  Array.prototype.push = function( element ) { this[this.length] = element; }
}


window.onload = init;