/* ++MenuNav Script+++++++++ */
var coord;
var mcnt = -1;
var timeout;
var OK = false;
var menunav = true;

var pattern = /Opera|Gecko/;

function show_layer(x){
        if (OK) {
           clearTimeout(timeout);
           document.getElementById(x).style.visibility = "visible";
        }
}

function hide_layer(x){
        if (OK){
            document.getElementById(x).style.visibility = "hidden";
        }
}

function do_menu(x){
        hide_all();
        show_layer(x);
        // Section for determining to load dropdown_hider.js
        if (!pattern.test(navigator.userAgent)){
                 hideColliders(x);
        }
}

function hide_all(){
    if (!pattern.test(navigator.userAgent)) {
        showHiddenDD();
        var hidArray = new Array();
    } // end if

    for (n=0; n< mcnt+1; n++) {
      //  eval("hide_layer('menu"+n+"')");
			hide_layer("menu"+n)
    }
} //last for hide all

function timeOut(sec){
	seconds = parseInt(sec) * 1000;
	timeout = setTimeout("hide_all()", seconds);
}

/* +++++++++++++++++++++++++++++++++++++++ */
function addLink(name, url){
        this.linkarray = this.linkarray.concat(new Array(new Array(name, url)));
}


function writeMenuNav() {
  var str = "";
  str += '\n<div  id="'+this.name+'" style="position: absolute; z-index: 2; top:'+this.topPosition+'px; left:'+this.leftPosition+'px; visibility: hidden" onmouseover="clearTimeout(timeout)" onmouseout="timeOut(0.75)">\n';
	str += '<table cellspacing="0" cellpadding="0" border="0" style="border: 1px solid ' + this.border +'">\n';
	//If it is a help tip
	if (this.info){
		str += '<tr>\n';
		str += '<td class="tinactive" style="padding:10px" bgcolor="'+this.bgcolor+'">'
		str +=  this.info;
		str += '</td>\n';
		str += '</tr>\n';
	} else {
	   // links write
			for (i=0; i<this.linkarray.length; i++) {
				str += '<tr>\n';
				str += '<td class="tinactive" onmouseover="this.className=\'tactive\'" onmouseout="this.className=\'tinactive\'"';
				str += 'onclick="location.href=\''+this.linkarray[i][1]+'\'" ';
				str += 'bgcolor="'+this.bgcolor+'">';
				str += '<a style="text-decoration: none" href="'+this.linkarray[i][1]+'">'+this.linkarray[i][0]+'</a>&nbsp;</td>\n';
				str += '</tr>\n';
	  	} // end for
 }

  str += '</table>\n';
  str +='</div>\n\n';

   // uncomment the next two lines to get debugging information
   //alert(this.linkarray.join("\n"));
  //alert(str);

   document.write(str);
}

/*Functions that dictate look of menus*/

function menu_nav(name, info){
  mcnt++;
  mlinkID = "mlink" + mcnt;
  coord = getAnchorPosition(mlinkID);
  this.border = "#111111";
  this.bgcolor = "#e7ecef";
   // this.overcolor = "#D1D9EF";
  this.name = name;
   // this.width = width;
    this.leftPosition = coord.x - 10;
    this.topPosition = coord.y + document.getElementById(mlinkID).offsetHeight+2;
    this.linkarray = new Array();
    this.addLink = addLink;
   this.info = info;
    //this.addSeparator = addSeparator;
    this.write = writeMenuNav;
    return this

}


// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page. It uses getPageOffsetLeft and Top contained in common.js.

function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	// Logic to find position
	if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=getPageOffsetLeft(o);
		y=getPageOffsetTop(o);
	} else {
		coordinates.x=0; coordinates.y=0; return coordinates;
	}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}


/* DropDown Hider */
/* Script finds out if menus collide with layers, and hides them */
var debug = "FALSE";
var cArray = new Array();
var hidArray = new Array();
var xmin;
var ymin;
var xmax;
var ymax;

function hideColliders (menu) {

  var h = 0;
  var cArray = new Array();

  // First, we find out if there are any forms under this menu
  if (debug == "TRUE") { alert ("There are " + document.forms.length  + " forms in this document") };
  for (i = 0; i < document.forms.length; i++)  {

    var xmin = document.getElementById(menu).offsetLeft ;
    var ymin = document.getElementById(menu).offsetTop  ;

    var xmax = xmin + document.getElementById(menu).offsetWidth  ;
    var ymax = ymin + document.getElementById(menu).offsetHeight ;

    if (debug == "TRUE") {
      alert ("the " + menu+ " box is " + xmin + " pixels from the left, and is " +
      document.getElementById(menu).offsetWidth + " wide for a coordinate area of " +
      xmax);
    } // end if
    if (debug == "TRUE") {
      alert ("the "+ menu + " box is " + ymin + " pixels from the top  and is " +
      document.getElementById(menu).offsetHeight+ " high for a coordinate area of " + ymax)
    } // end if

    formsCollide(menu);

    // Now we find the drop downs in the forms that collide
    if (cArray.length >0) {
      for (i = 0; i < cArray.length; i++)  {
        findDropDowns(menu);
      } // end for
    } // end if

  } // end for

  function formsCollide (menu) {
    for (l = 0; l < document.forms.length; l++)  {
      //Now, we determine if the Form is within the menu
      if (getPageOffsetTop(document.forms[l]) < ymax) {
        //The top is somewhere colliding, but we dont know left or right.
        if (debug == "TRUE") alert ("yes the top of the formid " + l + " is within the top boundaries of the layer")
        if (getPageOffsetLeft (document.forms[l]) > xmax) {
          if (debug == "TRUE") alert ("The left of the formid " + l + " is clear right of the layer")
        } else if (getPageOffsetLeft(document.forms[l]) + document.forms[l].offsetWidth < xmin ) {
          if (debug == "TRUE") alert ("The right of the formid " + l + " is clear left of the layer")
        } else {
          if (debug == "TRUE") alert ("yes there is aformid " + l +
            " is colliding with " + menu +
            " the menu, I am putting it into the array of colliding forms as index " + l);
          cArray[cArray.length] = l;
          if (debug == "TRUE") { alert ("Here are the ids of the forms we need to look at" + cArray.join()) };
        } // end if
      } // end if
      //if it gets here, it is not in top to bottom
      if (debug == "TRUE") alert ("YOU ARE CLEAR!");
    } // end for
  } // end function formsCollide

  /* Find drop downs finds drop down menus and hides em if they collide */
  function findDropDowns (menu) {
    if (debug == "TRUE") {
      alert ("OK We have cArray here it is " + cArray.join());
    } // end if
    for (var i in cArray) {
      cFormIndex = cArray[i];
      if (debug == "TRUE") { alert ("Here we go looping through form LOOK " + cFormIndex + "To see if there are any drop downs that hit " + menu);  }
      for (j = 0; j < document.forms[cFormIndex].elements.length; j++) {
        if (isFinite(cFormIndex)) {
          if ( document.forms[cFormIndex].elements[j].type == "select-one" || document.forms[cFormIndex].elements[j].type == "select-multiple") {
            if (debug == "TRUE") { alert ("I have found a Drop Down list box"); }
            isHitting(menu);
          } // end if
        } // end if
      } // end for
    } // end for
  } // end function findDropDowns

  /* Is Hitting finds if the drop down boxes that we foundhit the menu in question */
  function isHitting (menu) {
    if (debug == "TRUE") alert ("the offsetTop of the lb named " +
      document.forms[i].elements[j].name + " is " +
      document.forms[i].elements[j].offsetTop);
    if (debug == "TRUE") alert ("the offsetLeft of the lb is " +
      document.forms[i].elements[j].name + " is "+
      document.forms[i].elements[j].offsetLeft);
    // Now, we determine if the box is within the menu
    if (getPageOffsetTop(document.forms[cFormIndex].elements[j]) < ymax) {
      // The top is somewhere colliding, but we dont know left or right.
      if (debug == "TRUE") alert ("yes the top of the list box is within the top boundaries of the layer")
      if (getPageOffsetLeft(document.forms[cFormIndex].elements[j]) > xmax) {
        if (debug == "TRUE") alert ("The left of the list box is clear right of the layer")
      } else if (getPageOffsetLeft(document.forms[cFormIndex].elements[j]) +
      document.forms[cFormIndex].elements[j].offsetWidth < xmin ) {
        if (debug == "TRUE") alert ("The right of the list box is clear left of the layer")
      } else {
        document.forms[cFormIndex].elements[j].style.visibility = "hidden";
        if (debug == "TRUE")  alert (document.forms[cFormIndex].elements[j].sourceIndex)
        //alert ("putting" + document.forms[cFormIndex].elements[j].sourceIndex);
        num = h++;
        //alert (num);
        hidArray[hidArray.length] = document.forms[cFormIndex].elements[j].sourceIndex;
      } // end IF
    } // end if
    // if it gets here, it is not in top to bottom
    return h;
  } //end function isHitting

} // end function hideColliders

function showHiddenDD () {
  for (i = 0; i < hidArray.length; i++)  {
    j = hidArray[i];
    if (j >0){
      document.all[j].style.visibility = "visible";
    } // end if
  } // end for
  hidArray = new Array();
  // alert ("This is the blown out array" + hidArray.join());
} // end function showHiddenDD
