// Define variable to keep track of created objects
var objno=1;

// Controls z stacking of layers
var depth=100;

// Dropdown menu constructor
DropDown=function(n,p,d,po){
	
	// Create a reference to the visibility timer
	this.waiting=null;
	
	// Define position relative to parent menu
	this.pos=d;	
	
	// Create reference to DIV element
	if(n.style) this.obj=n; else this.obj=gE(n);
	
	// Identify this DIV element as a menu
	this.obj.menu=1;
	
	// Create reference to ths object from the DIV element
	this.obj.dropdown=this;
	
	// Attach mouse behaviours to the DIV elements
	aE(this.obj,"mouseover",DropDownShow,true);
	aE(this.obj,"mouseout",DropDownHide,true);
	// aE(this.obj,"click",SubmenuClick,false);
	
	// Create reference to the parent DIV element
	if(p.style) this.parentobj=p; else this.parentobj=gE(p);
	
	// Identify the parent DIV element as a menu
	this.parentobj.menu=1;
	
	// Create reference to ths object from the parent DIV element
	this.parentobj.dropdown=this;
	
	// Attach mouse behaviours to the parent DIV element
	aE(this.parentobj,"mouseover",DropDownShow,true);
	aE(this.parentobj,"mouseout",DropDownHide,true);	
	
	// Associate the parent menu with this object
	this.parentmenu=po;
	
	// Create a reference to self
	this.self='Object'+objno++;
	eval(this.self+'=this');
	
	// Return the created object
	return this;
	
}
// DropDown menu show
DropDown.prototype.show=function(ShowNow){

	// Clear the visibility timer
	clearTimeout(this.waiting);
	
	// Wait before showing
	if(!ShowNow){
		
		// Recursively show the parent menu
		if(this.parentmenu) this.parentmenu.show();
		
		// Set interval 
		this.waiting=setTimeout(this.self+".show(true)",25);
		return;
		
	}	
	
	// Hilight the parent menu option
	SubmenuHilite(this.parentobj)
	
	// Set the position
	// The check fixes flickering bug in Mozilla based browers
	if(gV(this.obj)!="visible")this.setposition();
	
	// Show the DIV element
	this.setdisplay(true);	
	
	// Hide all select boxes
	ToggleSelectBoxes(false);
	
}
// DropDown menu hide
DropDown.prototype.hide=function(HideNow){
	
	// Clear visiblity timer
	clearTimeout(this.waiting);
	
	// Wait before hiding
	if(!HideNow){
		
		// Show selectboxes
		ToggleSelectBoxes(true);
		
		// Recursively hide the parent menu
		if(this.parentmenu) this.parentmenu.hide();
		
		// Set interval 
		this.waiting=setTimeout(this.self+".hide(true)",50);
		return;
		
	}
	
	// Hide DIV element
	this.setdisplay(0);
	
	// Remove hilight from parent menu option
	SubmenuLolite(this.parentobj);	

}
// DropDown set visibility
DropDown.prototype.setdisplay=function(s){
	
	if(s){
		
		// Move element to top of stack
		sZ(this.obj,++depth);
		
		// Show element
		sE(this.obj);
		
	}else{
		
		// Hide element
		hE(this.obj);
		
	}

}
//DropDown set position
DropDown.prototype.setposition=function(s){
	
	// Find offset hieght relative to parent DIV element
	this.top=0;
	this.left=0;
	var tmpo=this.parentobj;
	do{ 
		this.top+=tmpo.offsetTop; 
		this.left+=tmpo.offsetLeft; 
		tmpo=tmpo.offsetParent; 
	}while(tmpo!=this.obj.offsetParent);
	
	// Set position
	switch(this.pos){
		
		case "b":
			// Below parent menu option
			sY(this.obj,this.top+gH(this.parentobj)+1);
			sX(this.obj,this.left-1);
			break;
			
		case "r":
			// Right of parent menu option
			sY(this.obj,this.top-1);
			sX(this.obj,this.left+gW(this.parentobj)+1);
			
			// X-BROWSER TWEAK
			// Mac IE 5 anomaly
			if(mac && ua.indexOf("msie 5")>-1){
				sY(this.obj,this.top-4);
				sX(this.obj,this.left+gW(this.parentobj)-6);
			}
			// Mac Safari anomaly
			if(mac && ua.indexOf("safari")>-1){
				sY(this.obj,this.top-2);
				sX(this.obj,this.left+gW(this.parentobj));
			}
			
			break;
			
	}
	
	// Bounds
	// Calculate offsets relative to document root node
	var t=0,l=0,b=0,r=0;
	var tmpo=this.obj;
	while(tmpo.nodeName!="BODY"){
		
		t+=gY(tmpo);
		l+=gX(tmpo);
		tmpo=tmpo.offsetParent;
		
	}
	b=t+gH(this.obj);
	r=l+gW(this.obj);	
	
	// Menu offscreen on right
	if(r>docW()){
		
		switch(this.pos){
			
			case "r":
			
				// Right of parent menu option
				// Move to left of parent menu option (and slightly down)
				// Make sure repositioned menu is obvious
				sX(this.obj,this.left-gW(this.parentobj));
				sY(this.obj,this.top+2);
				
				// X-BROWSER TWEAK
				// Mac IE 5 anomaly
				if(mac && ua.indexOf("msie 5")>-1){
					sX(this.obj,this.left-gW(this.parentobj)-5);
				}
				
				break;		
			
		}	
		
	}
	
}

// Dynamically attached show behaviour
function DropDownShow(e){ 

	// Get reference to object registering the event
	var Div;
	if(!e) var e=window.event;
	if(e.target) Div=e.target;
	else if(e.srcElement) Div=e.srcElement;
	if(Div.nodeType==3) Div=Div.parentNode;
	
	// Find the menu element
	while(!Div.menu){ Div=Div.parentNode; } 
	
	// Show the dropdown object
	Div.dropdown.show();	
	
}

// Dynamically attached hide behaviour
function DropDownHide(e){ 

	// Get reference to object registering the event
	var Div;
	if(!e) var e=window.event;
	if(e.target) Div=e.target;
	else if(e.srcElement) Div=e.srcElement;
	if(Div.nodeType==3) Div=Div.parentNode;

	// Find the menu element
	while(!Div.menu){ Div=Div.parentNode; } 
	
	// Hide the dropdown object
	Div.dropdown.hide();

}

// Highlight a cell
function SubmenuHilite(o){
	
	// Ignore if not a DIV element
	if(o.nodeName!="DIV") return;
	
	// Check for images
	var tmpo=o;
	while(tmpo.childNodes.length>0){
		
		if(tmpo.childNodes[0].nodeName=="IMG"){
			
			tmpo.childNodes[0].src=tmpo.childNodes[0].src.toString().replace("._off.","._on.");
			
		}
		
		tmpo=tmpo.childNodes[0];
		
	}
	
	// Set the original background color
	if(o.OriginalBackgroundColor==null) o.OriginalBackgroundColor=gK(o);
	
	// Set new background colour
	sK(o,"#928310");
	
}

// Remove highlight from a cell
function SubmenuLolite(o){
	
	// Ignore if not a DIV element
	if(o.nodeName!="DIV") return;
	
	// Check for images
	var tmpo=o;
	while(tmpo.childNodes.length>0){
		
		if(tmpo.childNodes[0].nodeName=="IMG"){
			
			tmpo.childNodes[0].src=tmpo.childNodes[0].src.toString().replace("._on.","._off.");
			
		}
		
		tmpo=tmpo.childNodes[0];
		
	}
	
	// X-BROWSER TWEAK
	// Mac IE 5 anomaly
	if(o.OriginalBackgroundColor==null) o.OriginalBackgroundColor="#7d6f0c";
	
	// Set new background colour
	sK(o,o.OriginalBackgroundColor);
	
}

// Dynamically attached highlight behaviour
function Hilite(e){
	
	// Get reference to object registering the event
	var El;
	if(!e) var e=window.event;
	if(e.target) El=e.target;
	else if(e.srcElement) El=e.srcElement;
	if(El.nodeType==3) El=El.parentNode;
	
	// Find the DIV element
	while(El.nodeName!="DIV"){ El=El.parentNode; } 
	
	// Set highlight
	SubmenuHilite(El);
	
}

// Dynamically attached lowlight behaviour
function Lolite(e){
	
	// Get reference to object registering the event
	var El;
	if(!e) var e=window.event;
	if(e.target) El=e.target;
	else if(e.srcElement) El=e.srcElement;
	if(El.nodeType==3) El=El.parentNode;
	
	// Find the DIV element
	while(El.nodeName!="DIV"){ El=El.parentNode; } 
	
	// Ignore if this is a menu 
	// This line avoids the brief blink when 
	// rolling onto this items submenu
	if(El.menu==1) return;
	
	// Set highlight
	SubmenuLolite(El);
	
}

// Dynamically attached click behaviour
function SubmenuClick(e){
	
	// Get reference to object registering the event
	var Div;
	if(!e) var e=window.event;
	if(e.target) Div=e.target;
	else if(e.srcElement) Div=e.srcElement;
	if(Div.nodeType==3) Div=Div.parentNode;
	
	// Find the menu element
	while(Div.nodeName!="DIV"){ Div=Div.parentNode; } 
	
	// Get the href
	Div=Div.firstChild;
	while(Div.nodeName!="A"){ Div=Div.firstChild; }
	var Url=Div.href;
	
	// Redirect
	location.href=Url;
	
}

// Hide or show all select boxes on page
var SelectBoxPause;
function ToggleSelectBoxes(MakeVisible,Paused){
	
	// Only necessary in Internet Explorer
	if(!ie) return;
	
	// Pause before showing
	clearTimeout(SelectBoxPause);
	if(MakeVisible && !Paused){
		
		SelectBoxPause=setTimeout("ToggleSelectBoxes("+MakeVisible+",true)",100);
		return;
		
	}	

	// Find and hide/show ALL select boxes
	var arr=document.getElementsByTagName("select");
	for(var i=0;i<arr.length;i++){
		arr[i].style.visibility=MakeVisible?"visible":"hidden";
	}	
	
}

// Make a dropdown menu
function MakeDropDown(n,p,d,po){

	return new DropDown(n,p,d,po);
		
}

// Make flyouts
// Recursively find submenus
function MakeFlyoutMenus(o,p){
	for(var i=0;i<o.childNodes.length;i++){
		
		// Cycle through menu options
		if(o.childNodes[i].className=="MenuOption"){
			
			// Attach rollover + click code
			aE(o.childNodes[i],"mouseover",Hilite,true);
			aE(o.childNodes[i],"mouseout",Lolite,true);
			aE(o.childNodes[i],"click",SubmenuClick,false);
			
			// Look for a submenu
			if(o.childNodes[i].nextSibling){
				
				var tmpo=o.childNodes[i].nextSibling;
				while(tmpo){				
					
					if(tmpo.className=="Menu"){
						
						//Submenu found
						
						// Attach arrow
						var NewHtml='<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td>'+o.childNodes[i].innerHTML+'</td><td align="right"><img src="/images/arrow.gif" width="10" height="10" alt=""></td></table>';
						o.childNodes[i].innerHTML=NewHtml;
						
						// Create submenu
						var a=MakeDropDown(tmpo,o.childNodes[i],"r",p);
						MakeFlyoutMenus(tmpo,a);
						tmpo=false;
						
					}else if(tmpo.className=="MenuOption"){
						
						// Submenu not found
						tmpo=false;
						
					}
					
					// Get next sibling
					if(tmpo.nextSibling) tmpo=tmpo.nextSibling; else tmpo=false;
					
				}
				
			}			
			
		}
	}
}