var sOpenMenuID = "";
var layerobject;
var allobject;
var dom;
var activeMenu;
var NavCount;
var currentSection = null;
var TopNavOnSrc = new Array();
var TopNavOffSrc = new Array();
var flashloaded = false;
var timerId;

(document.layers) ? layerobject = true : layerobject = false;
(document.all) ? allobject = true : allobject = false;
(document.getElementById) ? dom = true : dom = false;
   
function changeImages() {
  if (document.images) {
    for (var i=0; i<changeImages.arguments.length; i+=2) {
      document[changeImages.arguments[i]].src = eval(changeImages.arguments[i+1] + ".src");
    }
  }
}

function enableGlobalMenus() {
  var status = true;
  //ensure all flash components have loaded
  for( var ctr = 0; ctr < NavCount; ctr++ ) {
    var navObj = getElement( "globalswf_" + ctr, null );
      if( navObj ) {
        if(navObj.readystate != 4) {
          status = false;
        }
      } //dont set else status = false if there is no navObj, this 
        //protects against the case when there is a missing nav image
      
  }    
  
  if(status == false) {
    setTimeout("enableGlobalMenus()", 500);
  } else {
    flashloaded = true;
  }
}

//start the timer - if user stays over link for 500ms, show flash
function doMenu(menuID) {
  timerId = window.setTimeout("doMenuAfterTimeOut('" + menuID + "')", 500);
}

//clear the delay - this ensures flash menu wont be shown if user moves mouse out in time
function clearDelay() {
	window.clearTimeout(timerId);
}

function doMenuAfterTimeOut(menuID) { 
  if (flashloaded && getElement("globalswf_" + menuID, null) != null) {//this will only be != null in IE    
    //flash object is present - tip open flash menu
    var thisMenu = "globalnav_" + menuID;
    if (( sOpenMenuID != "") && (sOpenMenuID != thisMenu))  {
      getElement(sOpenMenuID, null).style.display = "none";
      sOpenMenuID = "";
    }
    var menuObj = getElement( thisMenu, null ); 
    if( menuObj ) {
      menuObj.style.display = "block";
      sOpenMenuID = thisMenu;
    }
    activeMenu = sOpenMenuID;    
  }   
}


function getElement( layerName, parentLayer ) {
  var dString;
  if( layerobject ) {  
    parentLayer = (parentLayer) ? parentLayer : self;
    layerCollection = parentLayer.document.layers;
    if( layerCollection[layerName]) {
      return (layerCollection[layerName]);
    }
    for( i = 0; i < layerCollection.length; )   {
      return (getElement( layerName, layerCollection[i++] ));
    }
  }
  if( allobject )   {
    return document.all.item(layerName);
  }
  if( dom ) {
    return (document.getElementById(layerName));
  }
}

function MMove(e) {
  var theLayer;
  if( activeMenu != null ) {
    theLayer = getElement( activeMenu, null );
    if( theLayer) {   
      if( (window.event.x < theLayer.offsetLeft ) 
        || (window.event.x > (theLayer.offsetLeft + theLayer.offsetWidth))  
        || ( window.event.y < (theLayer.offsetTop - 28)) 
        || ( window.event.y > (theLayer.offsetTop + theLayer.offsetHeight)) ) {
        HideAllMenus();
      } 
    }
  }
}    

function HideAllMenus() {
  var ctr;
  for( ctr = 0; ctr < NavCount; ctr++ ) {
    lName = "globalnav_" + ctr;
    layerHide(lName);
  }
  activeMenu = null;
}

function layerHide( layerName ) {
  var theLayer = getElement( layerName, null );
  if( theLayer) {
    if( layerobject) {
      theLayer.visibility = 'hide';
    } else {
      theLayer.style.display = "none";
    }
  }
}

function sizeMenu(section, size) {
  var thisMenu = "globalnav_" + section;
  var menuObj;
  var openMenuObj;
  if (( sOpenMenuID != "") && (sOpenMenuID != thisMenu))  {
    openMenuObj = getElement( sOpenMenuID, null );
    if( openMenuObj ) {
      openMenuObj.style.display = "none";
    }
    sOpenMenuID = "";
  } else {
    menuObj = getElement(thisMenu, null);
    if( menuObj ) {
      var clipString = "rect(0px 213px " + size + "px 0px)";
      menuObj.style.clip = clipString;
      sOpenMenuID = thisMenu;
    }
  }
}

function setTopNavImages( onUrl, offUrl, index ) {
  TopNavOnSrc[index] = onUrl;
  TopNavOffSrc[index] = offUrl;
}

function setNavCount(count, current) {
  NavCount = count;
  currentSection = current;
}
		
function preLoadNavImages() {
  if (getElement("globalswf_0", null) != null) {//this should only occur in IE
    for( ctr = 0; ctr < NavCount; ctr++ ) {
      setTopNavPosition( "navImg" + ctr, "globalnav_" + ctr);
    }
    enableGlobalMenus();
  }
  //return true;
}

function setTopNavPosition( baseId, navId ) {
  var buttonObj;
  var navObj;
  navObj = getElement( navId, null );
  buttonObj = getElement( baseId, null );	
  if( navObj && buttonObj) {
    var y = findPosY( buttonObj );
    var x = findPosX( buttonObj );
    setLayerY( navObj, eval(y + 26));
    if ( eval(x + 213) > 762) {
    	x = 763 - 213;
    }
    if( eval(x - 4) < 0 ) { 
      setLayerX( navObj, x);
    } else {
      setLayerX( navObj, eval(x - 4));
    }
  }
}

function setLayerX( theLayer, x )  {
  if( theLayer) {    
    if( layerobject ) {
      theLayer.left = x;
    } else {
      theLayer.style.left = x + "px";
    }
  }
}

function setLayerY( theLayer, y )  {
  if( theLayer) {    
    if( layerobject ) {
      theLayer.top = y;
    } else {
      theLayer.style.top = y + "px";
    }
  }
}

function setLayerZ( theLayer, zIndex )  {
  var theLayer = getElement( layerName, null );
  if( theLayer) {    
    if( layerobject ) {
      theLayer.zIndex = zIndex;
     } else {
       theLayer.style.zIndex = zIndex;
     }
  }
}

function findPosX(obj) {
  var curleft = 0;
  if (obj.offsetParent)	{
    while (obj.offsetParent) {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  } else if (obj.x) {
    curleft += obj.x;
  }
	return curleft;
}

function findPosY(obj) {
  var curtop = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  } else if (obj.y) {
    curtop += obj.y;
  }
	return curtop;
}

function printPage(){
  if(window.print){
    window.print();
  }
  else {
    alert("Printing Instructions:\n\n - If you are using a Macintosh, please select \"Print\" from the \"File\" menu.\n - If you are using Windows, right click the page and select \"Print\" from the pop-up menu.");
  }
}

//variables and functions needed for HBX flash linking
var flashMovieWithLinksArray = new Array();
var itemWidth = 139;
var itemHeight = 51;

function flashMovieWithLinks(movieName, swfWidth, swfHeight) {
  this.movieName = movieName;
  this.width = swfWidth;
  this.height = swfHeight;
  this.linkArray = new Array();
}

function flashLink(linkId, linkUrl, linkType, popupSettings) {
  this.linkId = linkId;
  this.linkUrl = linkUrl;
  this.linkType = linkType;
  this.popupSettings = popupSettings;
}

function findPosXByName(nm) {
    return findPosX(document.getElementById(nm));
}

function findPosYByName(nm) {
    return findPosY(document.getElementById(nm));
}

//Function called from all Flash links
function navigateFromFlash(name, index, queryparams) {
   var fmwl; //the flash movie clicked
   for (var i = 0; i < flashMovieWithLinksArray.length; i++) {
     if (flashMovieWithLinksArray[i].movieName == name) {
       fmwl = flashMovieWithLinksArray[i];
       break;
     }
   }
   var fmLink = fmwl.linkArray[index]; //the actual link clicked
   
   if (this.hbx != null) { //only report to HBX if it is turned on in template
     var swfXpos = findPosXByName(name); //the x coordinate of the movie
     var swfYpos = findPosYByName(name); //the y coordinate of the movie
     var swfHeight = fmwl.height; 
     var swfWidth = fmwl.width; 
     var rows = swfHeight / itemHeight;
     var columns = swfWidth / itemWidth; 
     var xpos = swfXpos + ((index % columns) * itemWidth);
     var ypos = swfYpos + ((index % rows) * itemHeight);
     _hbLink(fmLink.linkId, '', xpos + "," + ypos + "," + itemWidth + "," + itemHeight); //send the report
   }
   
    var formattedQueryParams;
    if (queryparams != null && queryparams.length > 0) { //strip off param separator (if present)
      if (queryparams.charAt(0) == '?' || queryparams.charAt(0) == '&') {
        formattedQueryParams = queryparams.substring(1);
      } else {
        formattedQueryParams = queryparams;
      }
    }

    var foundUrl = fmLink.linkUrl;
    var fullUrl;
    if (formattedQueryParams != null) {
      if (foundUrl.indexOf("?") > -1) {
        fullUrl = foundUrl + '&' + formattedQueryParams;
      } else {
        fullUrl = foundUrl + '?' + formattedQueryParams;
      }
    } else {
      fullUrl = foundUrl;
    }
   
   //execute link according to link type
   if (fmLink.linkType == 2) { //open in new browser window
     window.open(fullUrl);
   } else if (fmLink.linkType == 1) { //open in popup
     window.open(fullUrl, 'newwindo', fmLink.popupSettings);
   } else { //open in current window
     window.location = fullUrl;
   } 
}
//end functions / variables for flash linking

function cwpLoadFlashObj(htmlId, flashObjId, schema, codeBase, width, 
			 height, style, movie, otherParams, qualityHtml, 
			 wmodeHtml, bgColor, pluginPg, quality ,wmode) {
	document.write('<object name="' + htmlId + '" id="' + htmlId + '" classid="');
	document.write(flashObjId + '" codebase="' + schema + codeBase + '" width="');
	document.write(width + '" height="' + height);
	if (style != null) {
		document.write('" style="' + style);
	}
	document.writeln('">');
	document.write('<param name="movie" value="' + movie + '?flashID=' + htmlId);
	if (otherParams != null) {
		document.write('&' + otherParams);
	}
	document.writeln('"/>');
	if (qualityHtml != null) {
		document.writeln(qualityHtml);
	}
	
	if (wmodeHtml != null) {
		document.writeln(wmodeHtml);
	}
	document.writeln('<param name="bgcolor" value="' + bgColor + '"/>');
	
	document.write('<embed name="' + htmlId + '" id="' + htmlId + '" src="' + movie + '?flashID=' + htmlId);
	if (otherParams != null) {
		document.write('&' + otherParams);
	}
	document.write('" quality="' + quality + '" width="' + width + '" height="' + height + '" ');
	document.write('type="application/x-shockwave-flash" pluginspage="' + schema + pluginPg + '"');
	if (wmode != null) {
		document.write(' wmode="' + wmode + '"');
	}
	document.writeln('></embed>');
	document.writeln('</object>');
}

function cwpLoadFlashMenu(htmlId, flashObjId, schema, 
			  codeBase, width, height, sytle, movie, qualityHtml, 
			  bgcolor, base, evalId, flashVars, pluginPg) {
	var divTag = document.getElementById(htmlId);
	var objectTag = '<object classid="' + flashObjId + '" codebase="' + schema + codeBase + '" id="' + evalId + '" width="' + width + '" height="' + height + '"';
	if (sytle != null) {
		objectTag += ' style="' + sytle + '"';
	}
	objectTag += '>';
	objectTag += '<param name="movie" value="' + movie + '"/>';
	if (qualityHtml != null) {
		objectTag += qualityHtml;
	}
	objectTag += '<param name="bgcolor" value="' + bgcolor + '"/>';
	objectTag += '<param name="BASE" value="' + base + '"/>';
	objectTag += '<param name="FlashVars" value="' + flashVars + '"/>';
	objectTag += '<embed src="' + movie + '" FlashVars="' + flashVars;
	objectTag += '" quality="high" bgcolor="' + bgcolor + '" width="' + width + '" height="' + height;
	objectTag += '" type="application/x-shockwave-flash" ';
	objectTag += 'pluginspage="' + schema + pluginPg + '"></embed></object>';
	divTag.innerHTML = objectTag;
}