// JavaScript Document
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();

function setLinkClass(menuObj,clssObj,parentNode){
	//alert("setLinkClass: "+parentNode == true);
	//create a new browser object
	var browser = new Browser();
	//get the current URL string
	var Curl = window.document.URL;
	//get the current hostname
	var Chost = "http://"+window.location.hostname;
	
	//get the menu container
	if(browser.isIE || browser.isOP){
		var mobj = eval("document.all."+menuObj);
	}
	if(browser.isNS || browser.isFF){
		var mobj = document.getElementById(menuObj);
	}
	
	//get an array of the sub-menu links
	var aLinks = mobj.getElementsByTagName("A");
	//loop through the sub menu links
	for(i=0;i<aLinks.length;i++){
		//get the link href
		var tempA = aLinks[i];
		//alert("setLinkClass: for loop:\ncurrent link: "+tempA.href+"\ndocument url: "+Curl+"\ndomain: "+Chost);
		//check the current link to see if it matches the current page url
		if(checkLink(tempA.href,Curl,Chost)){
			//set the class property of the parent node  to selected
			if(browser.isIE){
				if(parentNode){
					tempA.parentNode.setAttribute('className',clssObj);
				}else{
					tempA.setAttribute('className',clssObj);
				}
			}else{
				if(parentNode){
					tempA.parentNode.setAttribute('class',clssObj);
				}else{
					tempA.setAttribute('class',clssObj);
				}
			}
			//alert(aLinks[i].parentNode.class);
		}
	}
}

function setLinkImage(menuObj,imageStr,sitehost){
	//create a new browser object
	var browser = new Browser();
	//get the current URL string
	var Curl = window.document.URL;
	//alert("setLinkImage: window url: "+Curl);
	
	//if the sitehost is undefined set the host base on the window hostname. 
	//this is so I can set the host like www.site.com/kidscreaturefactory
	if(sitehost == undefined){
		//get the current hostname
		var Chost = "http://"+window.location.hostname;
	}else{
		var Chost = sitehost;
	}
	
	//get the menu container
	if(browser.isIE || browser.isOP){
		var mobj = eval("document.all."+menuObj);
	}
	if(browser.isNS || browser.isFF){
		var mobj = document.getElementById(menuObj);
	}
	
	//get an array of the DIV elements in the menu
	var divArray = mobj.getElementsByTagName("DIV");
	//now check to see if the array has a length
	if(divArray.length == 0){
		//assign the container object to the array.
		divArray = [mobj];
	}
	//now loop through the div elements and get the A elements
	for(d=0; d<divArray.length;d++){
		//get the array of A tags in the DIV, there should only be one.
		var divAtagArray = divArray[d].getElementsByTagName("A");
		//alert("setLinkImage: number of a tags in array: "+divAtagArray.length);
		for(tag=0;tag<divAtagArray.length;tag++){
			//get the href string
			var divAStr = divAtagArray[tag].href;
			//alert("setLinkImage: a tag loop:\n a href:"+divAStr+"\nCurl: "+Curl+"\nChost: "+Chost);
			if(checkLink(divAStr,Curl,Chost)){
				//this is the same as the document. set the new image source
				//get the image tag object
				var tdiv = divAtagArray[tag];
				var timg = tdiv.getElementsByTagName("IMG");
				//get the src string
				var imgsrc = timg[0].src;
				//now get the sub-string before the image file extension and the file extension string.
				var iSrcFile = imgsrc.substr(0,imgsrc.length-4);
				var iSrcExt = imgsrc.substr(imgsrc.length-4,imgsrc.length-1);
				imgsrc = iSrcFile+imageStr+iSrcExt;
				//alert("orginal source:"+timg[0].src+"\nnew source:"+imgsrc);
				//set the new image source
				timg[0].src = imgsrc;
			}
		}
	}
}

/**
*This function compares the URL strings of the window and the link.
*
*@param		curDiv:String
*@param		durl:String
*@param 	host:String
*@return	Boolean
*/
function checkLink(curLink,durl,host){
	//setup the isRoot to be false
	var isRoot = false;
	//assign the string value of link to urlstr
	var urlstr = curLink;
	
	//get the urlstr  and durl without the hostname or any trailing file names or parameters
	var lpath = urlstr.substring(host.length,urlstr.lastIndexOf("/"))+"/";
	var dpath = durl.substring(host.length,durl.lastIndexOf("/"))+"/";
	//alert("checkLink: window url path: "+dpath+"\ncurrent href path: "+lpath+"\n current link is subNode:"+isActiveRoot(dpath,lpath,host));
	
	//check to see if a file name is specified in durl by checking to see if the last character of durl is '/'
	//alert("checkLink: the last index of / is "+durl.lastIndexOf("/"));
	if(durl.lastIndexOf("/") == durl.length-1){
		//alert("checkLink: no file specified");
		//set isRoot to ture
		isRoot = true;
		//check to see if there is an instance of 'index' or 'default' in urlstr because these are the 
		//most common directory default files.
		if(urlstr.indexOf("index") >= 0 || urlstr.indexOf("default") >= 0){
			//alert("checkLink: url string file name is index or default is true");
			//strip everyting off after the last / because this is the directory index file
			var tstr = urlstr.substr(0,urlstr.lastIndexOf("/"));
			urlstr = tstr+"/";
		}
	}
	
	//I also want to check to see if the durl path without the file name is found in the urlstr. 
	//If this is the case then the image button is a parent link to the current link and should be 
	//shown as active.
	
	//check the current link href against the window url
	if(urlstr == durl ){
		//return true 
		return true;
	}else{
		//check to see if the link is the active section root
		return isActiveRoot(durl,urlstr,host);
	}
}

function isActiveRoot(documentPath,linkPath,host){
	//setup the isActive boolean
	var isActive = false;
	//check to see if the link is to a section root. meaning it ends with '/' or a index/default filename
	var lastIndex = linkPath.lastIndexOf("/");
	var fileName = linkPath.substring(lastIndex+1,linkPath.indexOf("."));
	
	if(lastIndex == linkPath.length-1 || fileName == "index" || fileName == "default"){
		//this link is for a section root. now find the section name for both paths
		var lpath = linkPath.substring(host.length+1,linkPath.lastIndexOf("/"));
		var dpath = documentPath.substring(host.length+1,documentPath.lastIndexOf("/"));
		//split the paths into arrays of section names
		var larray = lpath.split("/");
		var darray = dpath.split("/");
		//alert("isActiveRoot: \nlarray:"+larray+"\ndarray:"+darray);
		//all the section names in the larray should match the corresponding section names in the darray
		//check to see if the larray is length 1 and value is null. this would mean it is the home directory
		if(larray.length == 1 && larray[0] == null){
			return false;
		}else{
			for(var i=0;i<larray.length;i++){
				if(larray[i] == darray[i]){
					isActive = true;
				}else{
					isActive = false;
				}
			}
		}
		//alert("isActiveRoot:\nlast index of / is "+lastIndex+"\nfile name is "+fileName+"\nlpath:"+lpath+"\ndpath:"+dpath);
		//return the value of isActive
		return isActive;
	}else{
		return false;
	}
}