var adjust = 0;
if (navigator.appVersion.indexOf("Chrome")>-1) adjust = 20;

var req;
//var menu_width=180;
var offset=8;
var toffset=0+adjust;
var menu_top=0;
var msize;
var mypath="";

function loadXMLDoc(xmlfile,myfunction)
{
	if(document.location.pathname.indexOf("utilities")>-1) xmlfile = "../"+xmlfile;
    // branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = eval(myfunction);
        req.open("GET", xmlfile, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = eval(myfunction);
            req.open("GET", xmlfile, true)
            req.send();
        }
    }
}

function createMenus()
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        if (req.status == 200) {
            var docXML  = req.responseXML.documentElement;
			var menus = docXML.getElementsByTagName("menu");
            var menu_html="";
        	msize = menus.length;
		for (var a = 0; a < msize; a++)
		{
		  // var w = menus[a].getAttribute("width"); //alert(w);     // width:'+w+'px;
		   var menuid = menus[a].getAttribute("menuid"); //alert(menuid);  width:'+menu_width+'px;
		   menu_html += '<div class="dropdown" id="'+menuid+'" style="visibility:hidden;position:absolute;top:'+menu_top+'px;left:0px;" onMouseOver="showMenu(\''+menuid+'\',\'marker'+a+'\')" onMouseOut="hideMenu(\''+menuid+'\')">';
		   var items = menus[a].getElementsByTagName("item");
		   for (var i = 0; i < items.length; i++)
		   {
			   var label = items[i].getAttribute("label");
			   var link = items[i].getAttribute("link");
               if (items[i].getAttribute("target")) var target = ' target="'+ items[i].getAttribute("target")+'"';
			   else var target = "";
			   if (link.indexOf('http')>-1||link.indexOf('pdf')>-1) target=' target="_blank"';
			   menu_html +=  '<a href="' + mypath + link + '" class="menuitem"' + target + '>' + label + '</a>';
		   }
		   menu_html +="</div>";
		   // put the assembled menu_html contents into the menu div
            }
		document.getElementById("menus").innerHTML =  menu_html;
	     //	setup(menus.length);
        }
	  else   {
	  	alert("There was a problem retrieving the XML data:\n" + req.statusText);
	  }
    }
}

// credit to Peter-Paul Koch & Alex Tingle.
function findPosX(obj)
{
 var curleft = 0;
 if(obj.offsetParent)  {
     while(1)
     {
       curleft += obj.offsetLeft;
       if(!obj.offsetParent)
         break;
       obj = obj.offsetParent;
     }
 }
 else if(obj.x) curleft += obj.x;
 return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent) {
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}

function showMenu(objid,marker) //    ,onid
{
    //	if (document.getElementById)
    //	{
      	var obj = document.getElementById(marker);
		m = msize-1;
		var ed = document.getElementById('marker'+m);
		var edge = ed.offsetWidth;
		var menuobj = document.getElementById(objid);
 		var wid = obj.offsetWidth;
        var mwid = menuobj.offsetWidth;
		var ex = findPosX(ed)+edge;
		var lef = findPosX(obj)+offset;
	     //	alert(wid);
        if (lef+mwid>ex)lef = ex - mwid;
		lef+=1;
		var ht = obj.offsetHeight;
		var tp =  findPosY(obj)+ht+toffset;
 		menuobj.style.left=lef+'px';
		if (parseInt(menuobj.style.top)==0) menuobj.style.top=tp+'px';
		menuobj.style.visibility="visible";
        if (menuobj.offsetWidth<wid)	menuobj.style.width = wid+"px";
    //  }
    //	else if(document.all) document.all(menuid).style.visibility = "visible";
}

function hideMenu(menuid)
{
 //   var obj = document.getElementById(marker);
 //	obj.style.backgroundColor = 'transparent';
	//if (document.getElementById&&!document.all)
	document.getElementById(menuid).style.visibility="hidden";
   //	else if(document.all) document.all(menuid).style.visibility = "hidden";
}

//Custom JavaScript Functions by Shawn Olson
//Copyright 2006-2008
//http://www.shawnolson.net
//*******************************************

function changecss(theClass,element,value) {
	//Last Updated on June 23, 2009
	//documentation for this script at
	//http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
	var cssRules;

	var added = false;
	for (var S = 0; S < document.styleSheets.length; S++){

	if (document.styleSheets[S]['rules']) {
		cssRules = 'rules';
	} else if (document.styleSheets[S]['cssRules']) {
		cssRules = 'cssRules';
	} else {
		//no rules found... browser unknown
	}

	for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
		if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
			if(document.styleSheets[S][cssRules][R].style[element]){
				document.styleSheets[S][cssRules][R].style[element] = value;
				added=true;
				break;
			}
		}
	}
	if(!added){
		if(document.styleSheets[S].insertRule){
			document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
		} else if (document.styleSheets[S].addRule) {
			document.styleSheets[S].addRule(theClass,element+': '+value+';');
		}
	}
	}
}

