var global = window.document;
global.shadows = new Array;
var lastmenu,lasttitle,menuactive,dropshadowcount;
var overmenu=false;
var shadowdepth =4;
//var lastmenu.shadows = new Array;
activesubmenus=0;
function GetX (obj)
{
	var x = 0;
	do
	{
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	while (obj);
	return x;
}
function GetY (obj)
{
	var y = 0;
	do
	{
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	while (obj);
	return y;
}

//
// hide a control such as IFRAME
//
function HideControl (tagName, menu)
{
	var x = GetX (menu);
	var y = GetY (menu);
	var w = menu.offsetWidth;
	var h = menu.offsetHeight;
	var i;
	for (i = 0; i < document.all.tags(tagName).length; ++i)
	{
		var obj = document.all.tags(tagName)[i];
		if (!obj || !obj.offsetParent)
			continue;
		// check if the object and the menu overlap
		var ox = GetX (obj);
		var oy = GetY (obj);
		var ow = obj.offsetWidth;
		var oh = obj.offsetHeight;
		if ((ox > (x + w) || (ox + ow) < x))
			continue;
		if ((oy > (y + h) || (oy + oh) < y))
			continue;
		if(obj.style.visibility == "hidden")
			continue;
		menu.ctrlOverlap[menu.ctrlOverlap.length] = obj;
		obj.style.visibility = "hidden";
	}
}

//
// show the control hidden by the menu
//
function ShowControls ()
{
	if (lastmenu.ctrlOverlap)
	{
		var i;
		
		for (i = 0; i < lastmenu.ctrlOverlap.length; ++i)
		{
			lastmenu.ctrlOverlap[i].style.visibility = "";
		}
	}
	lastmenu.ctrlOverlap = null;
}



function HideSelectBoxes(menu) 
{
	if (!menu ) { return; }
	if (document.all)	// it is IE
	{
		/* part of Felix Zaslavskiy's fix on hiding controls
		   not really sure if this part is necessary, but shouldn't
		   hurt. */
		if (!menu.ctrlOverlap)
			menu.ctrlOverlap = new Array ();
/*@cc_on @*/
/*@if (@_jscript_version >= 5.5)
@else @*/
		
/*@end @*/
		HideControl ("SELECT", menu);
		HideControl ("OBJECT", menu);
		HideControl ("IFRAME", menu);
	}
} 


function MyAdjacentElement (e,where, element) 
{
	switch (where.toLowerCase()) 
	{
		case "beforebegin":
			e.parentNode.insertBefore(element, e);
			break;
		case "afterbegin":
			e.insertBefore(element, e.firstChild);
			break;
		case "beforeend":
			e.appendChild(element);
			break;
		case "afterend":
			e.parentNode.insertBefore(element, e.nextSibling);
			break;
	}
}

 // Apply dropshadow once the menu position has been calculated.

      /*      subMenu.insertBefore(ds, subMenu.firstChild);
             ds.style.position = 'absolute';
             ds.style.left = '5px';
             ds.style.top = '5px';
             ds.style.width = subMenu.offsetWidth + 'px';
             ds.style.height = subMenu.offsetHeight + 'px';
             ds.style.visibility = 'inherit';
             ds.style.backgroundColor = '#000';
             if (window.opera) {
                 ds.style.backgroundColor = '#666'; // opacity doesn't work in Opera
             } else {
               ds.style.filter = 'alpha(opacity=' + this._config.dropShadow + ')';
             }
             ds.style.opacity = this._config.dropShadow / 100;*/
    
	function MenuDropShadow(element,color,offset,depth)
	{

		if(document.getElementById&&document.all)//if IE5+ exists make the shadow
		{
  
         //element.shadows = new Array();
        for (i=depth; i>0; i--)
        {
            var rect = document.createElement('span');
						var menubar=document.getElementById("nav");
            var rs = rect.style;
            rs.position = 'absolute';
            rs.width = element.offsetWidth-offset+i + 'px';
            rs.height = element.offsetHeight-offset+i + 'px';
            rs.zIndex = element.style.zIndex - i;
            rs.backgroundColor = color;
            var opacity = 1 - i / (i + 1);
            rs.filter = 'alpha(opacity=' + (100 * opacity) + ')';
						rs.visibility = 'inherit';
						element.parentNode.insertBefore(element);
						//menubar.insertBefore(rect);
            //global.insertAdjacentElement('afterEnd', rect);
            rs.display='block';
            rs.top='110%';
            rs.left='2px';
            global.shadows[global.shadows.length] = rect;
        }
		}
	}   
	/*function MenuDropShadow(element,color,offset,depth)
	{
	 	var isIE=false;
		if(document.getElementById&&document.all)//if IE5+ exists make the shadow
		{
  
         //element.shadows = new Array();
        for (i=depth; i>0; i--)
        {
            var rect = document.createElement('span');
            var rs = rect.style;
            rs.position = 'absolute';
            rs.width = element.offsetWidth-offset+i + 'px';
            rs.height = element.offsetHeight-offset+i + 'px';
            rs.zIndex = element.style.zIndex - i;
            rs.backgroundColor = color;
            var opacity = 1 - i / (i + 1);
            rs.filter = 'alpha(opacity=' + (100 * opacity) + ')';
            global.insertAdjacentElement('afterEnd', rect);
            rs.display='block';
            rs.top='110%';
            rs.left='2px';
            global.shadows[global.shadows.length] = rect;
        }
		}
	}*/
	
	function ShowMenuOver(menutitle)
	{
		if(menuactive)
			ShowMenu(menutitle);
	}
	
	function HandleClick()
	{
		if(!overmenu)
			MenuOff();
	}
	
	function MenuOff()
	{
		HideMenu();
		menuactive=false;
	}
	
	function HideMenu()
	{
		HideSubMenu(lastmenu);
		if(lasttitle)
			lasttitle.className='menutitle';
		if(lastmenu)
			lastmenu.style.display = 'none';
	if(lastmenu)
		{
		for (i=0; i<global.shadows.length; i++)
         	global.shadows[i].removeNode(true);
		}
		if(lastmenu)			
		  ShowControls();
	}
	
	function HideSubMenu(thismenu)
	{
	  if(thismenu)
		{
  		listneighbours=thismenu.getElementsByTagName("UL");//Get all lists within this link's list
  	  for(var j=listneighbours.length;j>0;j--)//IE will not hide elements within non-visible elements so step backwards
  		{
  		   listneighbours[j-1].style.display='none';//hide the  list
  		}
      linkneighbours=thismenu.getElementsByTagName("A"); //Get all links in the same list
       for(var j=0;j<linkneighbours.length;j++)
       {
      	if(linkneighbours[j].className=='submenudown' )
      	 	linkneighbours[j].className='submenutitle';//If the link has a submenu switch the class back to submenu class
       }
		 }
	}

	function ShowMenu(menutitle)
	{
		var menu = menutitle.nextSibling;  
    while(menu.nodeType!=1)//loop through white space
    {
      	menu=menu.nextSibling;
    }
		HideMenu();
		menutitle.className='menutitledown';
		lastmenu=menu;
		lasttitle=menutitle;
		menuactive=true;
		overmenu=true;
		menu.style.display = 'none';
		menu.style.display = 'block';
		HideSelectBoxes(lastmenu);
    if(document.getElementById&&document.all)
    {
  		menu.style.display = 'none';//need to do this twice in IE  because of border bug
  		menu.style.display = 'block';
    }
		return false;
	}
	
	function ShowSubMenu(menutitle)
	{
		var menu = menutitle.nextSibling;
		while(menu.nodeType!=1)//loop through white space
		{
			menu=menu.nextSibling;
		}
		menutitle.className='submenudown';
		lastsubtitle=menutitle;
		listneighbours=menu.getElementsByTagName("UL");
		for(var j=0;j<listneighbours.length;j++)
			 listneighbours[j].style.display='none';
		menu.style.display = 'none';
		menu.style.display = 'block';
		HideSelectBoxes(menu);
		return false;
	}
	
function ClipBoard(copyurl,copytext) 
{
	holdtext.innerText = "<a href=\"http://"+copyurl+"\" target=\"_blank\">"+copytext+"</a>";
	Copied = holdtext.createTextRange();
	Copied.execCommand("Copy");
}

function confirmDelete()
{
	var agree=confirm("Are you sure you want to delete this item?");
	if (agree)
		return true ;
	else
		return false ;
}


function SetupMenu(thismenu,level)
{
  var menuItems=thismenu.childNodes;
 for(var i=0;i<menuItems.length;i++)
  {
		switch(menuItems[i].tagName)
		{
		 case 'UL':
		   switch(level)
		 	 {
				case 0:	   
				break;
				case 1: 
				 	menuItems[i].className='menu';
					menuItems[i].onmouseout=function()
					{
					  overmenu=false;
					}
				break;
				default:
					var lastlink = menuItems[i].previousSibling;
					while(lastlink.nodeType!=1)//loop through white space
					{
						lastlink=lastlink.previousSibling;
					}
					lastlink.onmouseover=function()
					{
					  overmenu=true;
						ShowSubMenu(this);
					}
					menuItems[i].onmouseover=function()
					{
					  overmenu=true;
					}
					menuItems[i].onmouseout=function()
					{
					  overmenu=false;
					}
					lastlink.className='submenutitle';
					menuItems[i].className='submenu';
				break;
				}
				SetupMenu(menuItems[i],level+1)
		 break;
		 case 'LI':
				if(menuItems[i].className!='menudivider')
				{
					SetupMenu(menuItems[i],level)
				}
		 break;
		 case 'A':
				switch(level)
				{
				 case 1://level 1 is the menu bar
					menuItems[i].className='menutitle';
					menuItems[i].onmouseout=function()
					{
						overmenu=false;
					}
					menuItems[i].onmouseover=function()
					{
						ShowMenuOver(this);
					}
					menuItems[i].onclick=function()
          {
            ShowMenu(this);
          }
				 break;
				 default:
					menuItems[i].className='menu';
					menuItems[i].onmouseover=
					function()
					{
						listneighbours=this.parentNode.parentNode.getElementsByTagName("UL");//Get all lists within this link's list
						 for(var j=listneighbours.length;j>0;j--)//IE will not hide elements within non-visible elements so step backwards
						 {
							 	listneighbours[j-1].style.display='none';//hide the  list
						}
						linkneighbours=this.parentNode.parentNode.getElementsByTagName("A"); //Get all links in the same list
						 for(var j=0;j<linkneighbours.length;j++)
						 {
							if(linkneighbours[j].className=='submenudown' )
							 	linkneighbours[j].className='submenutitle';//If the link has a submenu switch the class back to submenu class
						 }
					}
				 break;
				}
		 break;
		}
		 //SetupMenu(menuItems[i],level)
	}			 
}

function MenuInit()
{
// check if DOM is available, return if not
  if(!document.createTextNode){return;}
	var menuBar=document.getElementById("nav");
	SetupMenu(menuBar,0);
	document.getElementById("nav").style.display='block';

}

//window.onload=MenuInit;
document.onclick=HandleClick;