var numofitems = 8;

var menuitem1 = new menu(numofitems,1,"hidden");
var menuitem2 = new menu(numofitems,2,"hidden");
var menuitem3 = new menu(numofitems,3,"hidden");
var menuitem4 = new menu(numofitems,4,"hidden");
var menuitem5 = new menu(numofitems,5,"hidden");
var menuitem6 = new menu(numofitems,6,"hidden");
var menuitem7 = new menu(numofitems,7,"hidden");
var menuitem8 = new menu(numofitems,8,"hidden");

var catchmousemove = false;

//menu constructor
function menu(allitems,thisitem,startstate){
	var callname= "gl"+thisitem;
	var divname="subglobal"+thisitem;

	if (document.getElementById(callname) == null)
	{
		this.caller = null;
		this.thediv = null;
	} else {
		if (document.getElementById(divname) == null){
			this.thediv = null;
		} else {
			this.numberofmenuitems = allitems;
			this.caller = document.getElementById(callname);
			this.thediv = document.getElementById(divname);
			this.thediv.style.visibility = startstate;
		}
	}
	//alert(this.thediv.style.visibility);
}


function ehandler(event,theobj){
	var marquee = document.getElementById('marquee');
	for (var i=1; i<= theobj.numberofmenuitems; i++){
		if (typeof(eval('menuitem'+i)) == "object"){
			if (typeof(eval('menuitem'+i+'.thediv'))== "object")
			{
				var shutdiv =eval( "menuitem"+i+".thediv");
				if (shutdiv!=null){
					shutdiv.style.visibility='hidden';
					if (marquee!=null){
						marquee.style.visibility='visible';
					}
					endbodymousemove();
				}
			}
		}
	}
	if (theobj.thediv!=null){
		theobj.thediv.style.visibility="visible";
		if (marquee!=null){
			marquee.style.visibility='hidden';
		}
		catchmousemove = true;
		//playmusic('sound/mousemove.wav',theobj.thediv);
	}
	//Alert(theobj.thediv.style.visibility);
}

function closesubnav(event)
{
	var header = document.getElementById('pageheader');
	var navigator = document.getElementById('navigator');
	var marquee = document.getElementById('marquee');

	var onMenu = false;
	var subMenuHeight = 0;

	for (var i=1; (i<= numofitems); i++){
		if (typeof(eval('menuitem'+i))== "object"){
			if (typeof(eval('menuitem'+i+'.thediv'))== "object")
			{
				var gl = document.getElementById('gl' + i);
				if ((gl!=null) && (!onMenu))
				{
					onMenu = testEvent(gl,event);
					if (onMenu) catchmousemove = true;
				}
			}
		}
	}

	if ( !catchmousemove ) return;

	for (var i=1; (i<= numofitems); i++){
		if (typeof(eval('menuitem'+i+'.thediv'))== "object")
		{
			var subglobal = document.getElementById('subglobal' + i);
			if ((subglobal!=null) && (!onMenu) && (subMenuHeight<=0))
			{
				onMenu = testEvent(subglobal,event);
				subMenuHeight=subglobal.clientHeight;
			}
		}
	}
	var headerHeight = 0;
	if (header!=null){
		header.style.top = "0px";
		headerHeight = header.clientHeight;
	}
	var navigatorHeight = 0;
	if (navigator!=null){
		navigator.style.top = headerHeight + "px";
		navigatorHeight = navigator.clientHeight;
	}
	if (!((event.clientY + window.document.documentElement.scrollTop >= (headerHeight + navigatorHeight - 2 * subMenuHeight))&&(event.clientY + window.document.documentElement.scrollTop <= (headerHeight + navigatorHeight))) && (!onMenu)){
		//if (!onMenu){
		for (var i=1; i<= numofitems; i++){
			if (typeof(eval('menuitem'+i))== "object"){
				if (typeof(eval('menuitem'+i+'.thediv'))== "object")
				{
					var shutdiv =eval('menuitem'+i+'.thediv');
					if (shutdiv!=null){
						shutdiv.style.visibility='hidden';
						if (marquee!=null){
							marquee.style.visibility='visible';
						}
						endbodymousemove();
					}
				}
			}
		}
	}
	//alert(shutdiv.style.visibility);
	//adjustFooterPosition();
}

function testEvent(oObject,event) {
	//alert(oObject);
	//this function is to test if the event happens on pointed object
	//if(oObject.contains(event.srcElement)) {
	//alert(oObject) ;
	//alert(event.srcElement) ;
	//alert((oObject == event.srcElement));
	var srce = event.srcElement;
	var tare = event.target;
	if((oObject == srce)||(oObject == tare)) {
		return true;
	}
	return false;
}

function adjustNavigatorPosition()
{
	var navigator = document.getElementById('navigator');
	var subglobal1 = document.getElementById('subglobal1');
	var subglobal2 = document.getElementById('subglobal2');
	var subglobal3 = document.getElementById('subglobal3');
	var subglobal4 = document.getElementById('subglobal4');
	var subglobal5 = document.getElementById('subglobal5');
	var subglobal6 = document.getElementById('subglobal6');
	var subglobal7 = document.getElementById('subglobal7');
	var subglobal8 = document.getElementById('subglobal8');
	var pagecell = document.getElementById('pagecell');
	var footer = document.getElementById('pagefooter');
	var header = document.getElementById('pageheader');

	var headerHeight = 0;
	var navigatorHeight = 0;
	var subMenuHeight = 0;
	var pagecellHeight = 0;
	if (header!=null){
		headerHeight = header.clientHeight;
	}
	if (navigator!=null){
		navigatorHeight = navigator.clientHeight;
	}
	if (subglobal1!=null && subglobal1.innerHTML=="") {subglobal1.style.height="0px";}
	if (subglobal2!=null && subglobal2.innerHTML=="") {subglobal2.style.height="0px";}
	if (subglobal3!=null && subglobal3.innerHTML=="") {subglobal3.style.height="0px";}
	if (subglobal4!=null && subglobal4.innerHTML=="") {subglobal4.style.height="0px";}
	if (subglobal5!=null && subglobal5.innerHTML=="") {subglobal5.style.height="0px";}
	if (subglobal6!=null && subglobal6.innerHTML=="") {subglobal6.style.height="0px";}
	if (subglobal7!=null && subglobal7.innerHTML=="") {subglobal7.style.height="0px";}
	if (subglobal8!=null && subglobal8.innerHTML=="") {subglobal8.style.height="0px";}
  if((window.navigator.userAgent.indexOf("Firefox"))>0){

  	if (subglobal1!=null){subglobal1.style.top = ( navigatorHeight - subglobal1.clientHeight - 2 + 6) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal1.clientHeight);}
  	if (subglobal2!=null){subglobal2.style.top = ( navigatorHeight - subglobal1.clientHeight - 2 + 6) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal2.clientHeight);}
  	if (subglobal3!=null){subglobal3.style.top = ( navigatorHeight - subglobal1.clientHeight - 2 + 6) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal3.clientHeight);}
  	if (subglobal4!=null){subglobal4.style.top = ( navigatorHeight - subglobal1.clientHeight - 2 + 6) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal4.clientHeight);}
  	if (subglobal5!=null){subglobal5.style.top = ( navigatorHeight - subglobal1.clientHeight - 2 + 6) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal5.clientHeight);}
  	if (subglobal6!=null){subglobal6.style.top = ( navigatorHeight - subglobal1.clientHeight - 2 + 6) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal6.clientHeight);}
  	if (subglobal7!=null){subglobal7.style.top = ( navigatorHeight - subglobal1.clientHeight - 2 + 6) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal7.clientHeight);}
  	if (subglobal8!=null){subglobal8.style.top = ( navigatorHeight - subglobal1.clientHeight - 2 + 6) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal8.clientHeight);}

  	if (subglobal1!=null){subglobal1.style.left = navigator.offsetLeft +"px";}
  	if (subglobal2!=null){subglobal2.style.left = navigator.offsetLeft +"px";}
  	if (subglobal3!=null){subglobal3.style.left = navigator.offsetLeft +"px";}
  	if (subglobal4!=null){subglobal4.style.left = navigator.offsetLeft +"px";}
  	if (subglobal5!=null){subglobal5.style.left = navigator.offsetLeft +"px";}
  	if (subglobal6!=null){subglobal6.style.left = navigator.offsetLeft +"px";}
  	if (subglobal7!=null){subglobal7.style.left = navigator.offsetLeft +"px";}
  	if (subglobal8!=null){subglobal8.style.left = navigator.offsetLeft +"px";}
  }else{
    if (subglobal1!=null){subglobal1.style.top = ( navigatorHeight - subglobal1.clientHeight - 2) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal1.clientHeight);}
  	if (subglobal2!=null){subglobal2.style.top = ( navigatorHeight - subglobal1.clientHeight - 2) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal2.clientHeight);}
  	if (subglobal3!=null){subglobal3.style.top = ( navigatorHeight - subglobal1.clientHeight - 2) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal3.clientHeight);}
  	if (subglobal4!=null){subglobal4.style.top = ( navigatorHeight - subglobal1.clientHeight - 2) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal4.clientHeight);}
  	if (subglobal5!=null){subglobal5.style.top = ( navigatorHeight - subglobal1.clientHeight - 2) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal5.clientHeight);}
  	if (subglobal6!=null){subglobal6.style.top = ( navigatorHeight - subglobal1.clientHeight - 2) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal6.clientHeight);}
  	if (subglobal7!=null){subglobal7.style.top = ( navigatorHeight - subglobal1.clientHeight - 2) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal7.clientHeight);}
  	if (subglobal8!=null){subglobal8.style.top = ( navigatorHeight - subglobal1.clientHeight - 2) + "px";subMenuHeight=Math.max(subMenuHeight,subglobal8.clientHeight);}

  }

}

adjustNavigatorPosition();

function startbodymousemove()
{
	var body = document.getElementsByTagName('body')[0];
	body.onmousemove = bodymousemove;
}

function endbodymousemove()
{
	var body = document.getElementsByTagName('body')[0];
	body.onmousemove = empty;
	catchmousemove = false;
}

function empty(){
}

function bodymousemove(e)
{
	if (typeof(event)=="object"){
		e = event;
	}
	closesubnav(e);
}

var lastobject = null;
function playmusic(file, obj)
{
	/*try{
		if((window.navigator.userAgent.indexOf("Firefox"))>0){
	  	var backgroundmusicff = document.getElementById('backgroundmusicff');
	  	if (lastobject != obj){
	  		if (backgroundmusicff != null) {
	  			if (file == '') {
	  				file = 'sound/mousemove.wav';
	  			}
	  			backgroundmusicff.filename = file;
	  			backgroundmusicff.data = file;
	        //alert(file);
	  		}else{
	  		}
	  		lastobject = obj;
	  	}
			backgroundmusicff.controls.stop();
			backgroundmusicff.controls.play();
		}else{
			if (lastobject != obj){
	  		if (backgroundmusic != null) {
	  			if (file == '') {
	  				file = 'sound/mousemove.wav';
	  			}
	  			backgroundmusic.filename = file;
	        //alert(file);
	  		}else{
	  		}
	  		lastobject = obj;
	  	}
			backgroundmusic.stop();
			backgroundmusic.play();
		}
  }catch(exception){
  		//alert(exception)
  }*/
}
