/////////////////////////////////////
//Custom Crafted for RD HILL	   //
//By Hans Brough	    	   //
//April 7th, 2003	      	   //
/////////////////////////////////////

///////////////
//Global Vars//
///////////////

navObj='empty';//initialized as string, later set to object
btnObj='empty';//initialized as string, later set to object
var gImages= document.images;//set to var to avoid repeated multiple obj lookups by browser
var rolloverSfx='-on';//suffix given to rollover main navigation images (mouseover state)
var rolloverPrx='nav';
var buttonSfx='-on';//suffix given to context menu rollover button images (mouseover state)
var buttonPrx='btn';//prefix given to context menu rollover button images 
var defaultAreaInfocus='home';//name of navigation area to set as highlighted by default.
var defaultBtnInfocus= null;//name of button to set as highlighted by default.
var imgDirectoryPath = 'images/';//relative location of images folder in htdocs on webserver
var imgDelimiter = "_";//delimiting character used in image names ie 'nav_home.gif'
///////////////
//Functions  //
///////////////

///////////////
//NAVIGATION///
///////////////
//Description: look for navigation menu images. Then save images name to an array
//this assumes that page contains images with supporting nomenclature
function getNavList(){
	list= new Array();
	for(i=0;i<gImages.length;i++){
		var brokenSrc= gImages[i].src.split('_');
		if(brokenSrc[0].indexOf(rolloverPrx)==brokenSrc[0].length-rolloverPrx.length){
			list[list.length]=gImages[i].name;//add this image to our list
		}
	}
	return list;
}

///////////
//Description: look for context menu rollover button images and save it's name to an array
//this assumes that page contains images with supporting nomenclature
function getBtnList(){
	list= new Array();
	for(i=0;i<gImages.length;i++){
		var brokenSrc= gImages[i].src.split('_');
		if(brokenSrc[0].indexOf(buttonPrx)==brokenSrc[0].length-buttonPrx.length){
			list[list.length]=gImages[i].name;//add this image to our list
		}
	}
	return list;
}

///////////////////
//Description: matches area fragment passed with a name in navList
//Note: must be a unique fragment. For example don't use 'co' for both 'Company View' and 
//'Contact' - instead would be 'com' and 'con' respectively. hb
function findArea(list,area,defaultVal){
	for(i=0;i<list.length;i++){
		fragLoc= list[i].indexOf(area);//is area fragment in the current list item?
		if(fragLoc== 0){
			return list[i];
		}
	}
	return defaultVal;//if can't find area string return the default
}

/////////
//Description:
function initNavObj(area){
	navObj=new Object();
	navObj.items = getNavList();//encapsulate list of image names
	navObj.inFocus=(!area)?defaultAreaInfocus:findArea(navObj.items,area,defaultAreaInfocus);//if no argument passed use default navigation inFocus string
	navObj.selectedIndex= isItemInList(navObj.inFocus,navObj.items);//last parent item menu clicked on by the user.
	for(i=0;i<navObj.items.length;i++){
		navObj[navObj.items[i]]= new Image();
		navObj[navObj.items[i]].src= "images/nav_"+navObj.items[i]+rolloverSfx+".gif";
	}
	gImages[navObj.inFocus].src= navObj[navObj.inFocus].src;//highlite menu item 'area' (argument)
	
	return this;
}

///////////////////
//Description:
function initBtnObj(area){
	btnObj=new Object();
	btnObj.items = getBtnList();//encapsulate list of image names
	btnObj.inFocus=(!area)?defaultBtnInfocus:findArea(btnObj.items,area,defaultBtnInfocus);//if no argument passed use default button inFocus string
	btnObj.selectedIndex= isItemInList(btnObj.inFocus,btnObj.items);//last button item clicked on by the user.
	for(i=0;i<btnObj.items.length;i++){
		btnObj[btnObj.items[i]]= new Image();
		btnObj[btnObj.items[i]].src= imgDirectoryPath+buttonPrx+imgDelimiter+"circle"+buttonSfx+".gif";
	}
	if(btnObj.inFocus!= defaultBtnInfocus){//this allows for those pages which do not want a button highlighted at all.
		gImages[btnObj.inFocus].src= btnObj[btnObj.inFocus].src;//highlite menu item 'area' (argument) 
	}else{
		return this;//do not highlight just return object.
	}
	return this;
}
///////////////////
//Description:toggle between 'on' and 'off' versions of images
function toggleImg(imgObj){
	
	tempPrx=retrieveImgPrx(imgObj);
	//alert("toggleImg:tempPrx= "+tempPrx);
	switch(tempPrx){
		case 'btn':
			tempObj=btnObj;
			prefix= buttonPrx;
			suffix= buttonSfx;
			break;
		case 'nav':
			tempObj=navObj;
			prefix=rolloverPrx;
			suffix=rolloverSfx;			
			break
	}
	if(tempObj !='empty'){//this set initially as a flag to let us know the object has not had a chance to load yet.
		var tempImg= retrieveImgName(imgObj);
		temp= tempImg.indexOf(suffix);
		if(temp!= -1){
			tempImg=tempImg.slice(0,temp);
		}
		//if it's on turn it off and vice/versa
		if(imgObj.name != tempObj.inFocus){//swap only if link is not currently active.
			imgObj.src=(temp==-1)?tempObj[imgObj.name].src:imgDirectoryPath+prefix+imgDelimiter+tempImg+".gif";
		}
	}
}

//////////////////
function turnOn(imgObj){
	//alert("src of item in focus: "+gImages[navObj.inFocus].src);
	gImages[navObj.inFocus].src="images/nav_"+navObj.inFocus+".gif";//turn off menu item currently in focus
	//alert("new src of item in focus: "+gImages[navObj.inFocus].src);
	imgObj.src= navObj[imgObj.name].src;//turn on menu item clicked
	navObj.inFocus=imgObj.name;
}
//////////////////
function isItemInList(theItem,theList){
	var nTemp=-1;
	for(iL=0;iL<theList.length;iL++){
		if(theList[iL] == theItem){
			nTemp=iL;
		}
	}
	return nTemp;
}

//////////////////
function retrieveImgName(imgObj){
	var temp=imgObj.src.toString();
	var temp=temp.split("_");
	temp= temp[temp.length-1].split(".");
	var tempName= temp[0];
	return tempName;
}
//////////////////
function retrieveImgPrx(imgObj){
	var temp=imgObj.src.toString();
	//alert("temp= "+temp);
	var temp=temp.split(imgDelimiter);
	//alert("temp[0]= "+temp[0]);
	temp= temp[0].split("/");
	//alert("temp= "+temp);
	//alert("temp[temp.length-1]= "+temp[temp.length-1]);
	var prefix= temp[temp.length-1];
	return prefix;
}
/////////////////
//Description: Load argument url into parent frameset with optional 2nd arguement
/////////////////
function loadFrames(url){
	if(arguments.length >1){
		frameObjRef = eval("parent."+arguments[1]);
		frameObjRef.location.href = url;
		return false;
	}
	parent.location.href = url;
	return false;
}
