// JavaScript Document
// the timeout for the menu
timeout = 1;

// creat timeout variables for list item
// it's for avoid some warning with IE
// **************************************** NEED TO UPDATE MAX Variables IF YOU CHANGE THE MENU ********************************************************
maxSubMenu = 150;
maxSubSubMenu = 20;
for( var i = 0; i < maxSubMenu; i++ )
{
    eval("var timeoutli" + i + " = false;");
	for( var j = 0; j < maxSubSubMenu; j++)
		eval("var timeoutli" + i + "_li" + j + "= false;");
}

// this fonction apply the CSS style and the event
function initMenu()
{
	if( browser.isIE && !browser.isMac ) {
		var ieULs = document.getElementById('navList').getElementsByTagName('ul');
		for (j=0; j<ieULs.length; j++) {
			// make sure you include the iframe css class to hide the iframe
			ieULs[j].innerHTML = ('<iframe src="about:blank" scrolling="no" frameborder="0"></iframe>' + ieULs[j].innerHTML);
			var ieMat = ieULs[j].firstChild;
			ieMat.style.width=ieULs[j].offsetWidth+"px";
			ieMat.style.height=ieULs[j].offsetHeight+"px";	
			ieULs[j].style.zIndex="99";
		}
		// ie pc has problems with tabbing and flashing the layer, so increase timeout
		timeout = 200;
	}
	
	// pre-load background image

    // a test to avoid some browser like IE4, Opera 6, and IE Mac
    if ( (browser.isDOM1 || browser.isSafari)
    && !( browser.isMac && browser.isIE ) 
    && !( browser.isOpera && browser.versionMajor < 7 )
    && !( browser.isIE && browser.versionMajor < 5 ) )
    {
        // get some element
        var menu = document.getElementById('navList'); // the root element
        var lis = menu.getElementsByTagName('li'); // all the li
        
        // i am searching for ul element in li element
        for ( var i=0; i<lis.length; i++ )
        {
            // is there a ul element ?
            if ( lis.item(i).getElementsByTagName('ul').length > 0 )
            {
				// filter out ones that already have been set
				if(lis.item(i).id == "") {
					// improve IE key navigation
					if ( browser.isIE )
					{
						addAnEvent(lis.item(i),'keyup',show);
						var ancs = lis.item(i).getElementsByTagName('a'); // all the a in this li
						for( var j=0; j<ancs.length; j++ )
						{
							// link events to list item
							addAnEvent(ancs.item(j),'blur',timeoutHide_A);
						}
					}
					// link events to list item
					addAnEvent(lis.item(i),'mouseover',show);
					addAnEvent(lis.item(i),'mouseout',timeoutHide);
					addAnEvent(lis.item(i),'blur',timeoutHide);
					addAnEvent(lis.item(i),'focus',show);
					
					// add an id to list item
					lis.item(i).setAttribute( 'id', "li"+i );
					//alert(lis.item(i).innerHTML);
				} // end of li.id check to make sure it hasn't been set yet
            } // end of lis.item(i).ul check
        } // end of for loop (i)
    } // end of browser check
}

function addAnEvent( target, eventName, functionName )
{
    // apply the method to IE
    if ( browser.isIE )
    {
        //attachEvent dont work properly with this
        eval('target.on'+eventName+'=functionName');
    }
    // apply the method to DOM compliant browsers
    else
    {
        target.addEventListener( eventName , functionName , true ); // true is important for Opera7
    }
}
    
// hide the first ul element of the current element
function timeoutHide()
{
    // start the timeout
    eval( "timeout" + this.id + " = window.setTimeout('hideUlUnder( \"" + this.id + "\" )', " + timeout + " );");
}
// hide the first ul element of the parent's current element
function timeoutHide_A()
{
    // start the timeout
    eval( "timeout" + this.parentNode.parentNode.parentNode.id + " = window.setTimeout('hideUlUnder( \"" + this.parentNode.parentNode.parentNode.id + "\" )', " + timeout + " );");
}

// hide the ul elements under the element identified by id
function hideUlUnder( id )
{   
    document.getElementById(id).getElementsByTagName('ul')[0].style['left'] = '-1000px';
	document.getElementById(id).className=document.getElementById(id).className.replace(/ over/g, '');
	document.getElementById(id).className=document.getElementById(id).className.replace(/ changeBGOver/g, '');
}

// show the first ul element found under this element
function show()
{
    // show the sub menu
    this.getElementsByTagName('ul')[0].style['left'] = 'auto';
    // clear the timeout
    eval ( "clearTimeout( timeout"+ this.id +");" );
    hideAllOthersUls( this );
	this.className+=" over"; 
 	this.className += " changeBGOver";
}

// hide all ul on the same level of  this list item
function hideAllOthersUls( currentLi )
{
    var ul = currentLi.parentNode;
	var debugTxt = "";
    for ( var i=0; i<ul.childNodes.length; i++ )
    {
		debugTxt += "\nchildNode ID:" + ul.childNodes[i].id;
        if ( ul.childNodes[i].id && ul.childNodes[i].id != currentLi.id )
        {
            hideUlUnderLi( ul.childNodes[i] );
        }
    }
}

// hide all the ul wich are in the li element
function hideUlUnderLi( li )
{
    var uls = li.getElementsByTagName('ul');
    for ( var i=0; i<uls.length; i++ )
    {
        uls.item(i).style['left'] = '-1000px';
    }
	li.className=li.className.replace(/ over/g, '');
	li.className=li.className.replace(/ changeBGOver/g, '');
} 

window.onload=initMenu;

courseChangeWin = "";
 function openWindow(type, width, height)
 {
	courseChangeWin = open(type, "", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=50,left=50,width=" + width + ",height=" + height);
	if (courseChangeWin == null)
		alert("You will need to disable your popup blocker for this site in order to be able to view this link!");
	return false;
 }
