//Javascripts for the eureka cakePHP site

/* -- DW methods -- */

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* -- Menu Methods -- */
function Browser() {

	  var ua, s, i;

	  this.isIE    = false;  // Internet Explorer
	  this.isOP    = false;  // Opera
	  this.isNS    = false;  // Netscape
	  this.isFF    = false;  //FireFox
	  this.version = null;

	  ua = navigator.userAgent;

	  s = "Opera";
	  if ((i = ua.indexOf(s)) >= 0) {
	    this.isOP = true;
	    this.version = parseFloat(ua.substr(i + s.length));
	    return;
	  }

	  s = "Netscape6/";
	  if ((i = ua.indexOf(s)) >= 0) {
	    this.isNS = true;
	    this.version = parseFloat(ua.substr(i + s.length));
	    return;
	  }

	  // Treat any other "Gecko" browser as Netscape 6.1.

	  s = "Gecko";
	  if ((i = ua.indexOf(s)) >= 0) {
	    this.isNS = true;
	    this.version = 6.1;
	    return;
	  }

	  s = "MSIE";
	  if ((i = ua.indexOf(s))) {
	    this.isIE = true;
	    this.version = parseFloat(ua.substr(i + s.length));
	    return;
	  }
	  
	  s = "FireFox"
	  if((i = ua.indexOf(s)) >= 0){
		  this.isFF = true;
		  this.version = parseFloat(ua.substr(i + s.length));
		  return;
	  }
	}

	var browser = new Browser();

	var curmenu = "";

	function expandMenu(menu){
		//alert("current menu: "+curmenu+"\nselected menu: "+menu);
		//first close the curmen if it is assigned
		
		//if curmenu isn't set check to see if it is set in the search string
		if(!curmenu){
		curmenu = getCurrentMenu();
		}
		//now turn off the current menu if it is set and isn't the same as the selected menu
		if(curmenu && curmenu != menu){
			closeMenu = document.getElementById(curmenu);
			//alert("current menu class: "+closeMenu.className);
			if(hasClassName(closeMenu,'active_menu')){
				removeClassName(closeMenu,'active_menu');
			}
		}
		//now expand the selected menu
		thisMenu = document.getElementById(menu);
		thisMenu.className += " active_menu";
		curmenu = menu;
		//now return false
		return false;
	}

	function popWin (theURL,winName,features) {
	  window.open(theURL,winName,features);
	  return false;
	}

	/**
	* this function checks the class names in the node element against the specified name and returns true or false
	* @param	el
	* @param 	name
	* @return 	boolean
	*/
	function hasClassName(el, name) {

	  var i, list;

	  // Return true if the given element currently has the given class
	  // name.

	  list = el.className.split(" ");
	  for (i = 0; i < list.length; i++)
	    if (list[i] == name)
	      return true;

	  return false;
	}

	/**
	* this function will remove one class from a element that has one or more class names assigned
	* @param	el
	* @param	name
	*/
	function removeClassName(el, name) {

	  var i, curList, newList;

	  if (el.className == null)
	    return;

	  // Remove the given class name from the element's className property.

	  newList = new Array();
	  curList = el.className.split(" ");
	  for (i = 0; i < curList.length; i++)
	    if (curList[i] != name)
	      newList.push(curList[i]);
	  el.className = newList.join(" ");
	}

	function getCurrentMenu(){
		curSearch = window.location.search;
		
		if(curSearch){
			curSearch = curSearch.slice(1);
			//alert("getCurrentMenu: "+curSearch);
			//find out if a menu value is set in the search string. split the string and look for 'menu'
			sItems = curSearch.split("&");
			selmenu = null;
			for(i=0;i<sItems.length;i++){
				//split the item with '='
				tarr = sItems[i].split("=");
				if(tarr[0] == 'menu'){
					selmenu = tarr[0]+tarr[1];
				}
			}
			return selmenu;
		}else{
			//no current menu is active. return null
			return null;
		}
	}
