

//This script is property of the Ross Group Inc.  www.rossgroupinc.com copyright 2004
//This code may not be borrowed without the written permission of the Ross Group Inc.
//This script supports dynamically loading menu items by level to reduce bandwidth consumption.

var iconpath="/i/";

//The span tags are a visual filter which gives a 3d drop shadow appwarance. 
var folderviewheader="<html><body  topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0 style='cursor:hand;' onmouseleave=window.parent.starthidetimer()>";
    folderviewheader+="<LINK rel='stylesheet' type='text/CSS' href='stylesheets/menus.css'>\n";
    folderviewheader+="<SPAN id=imgObj style='FILTER: progid:DXImageTransform.Microsoft.dropShadow( Color=#999999,offX=2,offY=2); WIDTH: 1px;  HEIGHT: 1px'>";

var folderviewfooter="       ";
    folderviewfooter+="</span>";
    folderviewfooter+="</body></html>";

var loadlevelfile="not implimented here.";

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//Dont touch these ones.
var timershow='';
var timerhide='';
var rightarrow="<font face=Marlett size=2>4</font>";  //a text character for a right arrow
var layover=5;        //number of pixes to have menus layover each other.
var topalignment=15;  // child menus vertical alignment.  lesser numbers move down.. greater move up.
var loadeditems;  //initialized to an array from the buildmenu routines.
var tempmenu;
var tempmenuid;
var templevel;   

function addloaded(inid){
  loadeditems[loadeditems.length]=inid;
}

function isloaded(inid){
  if (loadeditems){
    for (var i=0; i <loadeditems.length;i++){
      if (loadeditems[i]==inid){
         return false;
      }
    }
    addloaded(inid);
  }
  return true;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// start()

function start(){
  drawTree(1,0);
}

function firewin(inaction,intarget){
    if (inaction=='') return false;
    if (intarget=='N'){
        window.location=inaction;
    }else{
        openpage(inaction,intarget);
    }
    top.hidemenu();
}

function openpage(inaction,target)
{
    top.window.open(inaction,target);
}

function startTrans(inobj,inhtml, instyle)
{
	if (inobj.getElementById('menusholder')){
		  inobj.getElementById('menusholder').innerHTML = inhtml;
	//	 inobj.open();
	//	 inobj.write(inhtml);	
	//	 inobj.close();
	}
}


var currentNode = 0;
var mouseovercolor = 'navy';
var timerhide;
var drawTimer;
var activeCollection;

function getActiveCollection(){
	if (activeCollection == 'V'){
		return VmenuData;
	}else{
		return HmenuData;
	}
}


function sTimer(level,startNode,x,y,inmouseovercolor,activeCollectionName){
	drawTree(level,startNode,x,y,inmouseovercolor,activeCollectionName);
	//clearTimeout(drawTimer);	
	//drawTimer = null;
	//drawTimer=setTimeout("drawTree(" +level +"," +startNode +","  +x    +","   +y   +",'"   +inmouseovercolor    +"','"  +activeCollectionName+"');",500);	
}

function endTimer(){
	clearTimeout(drawTimer);	
	drawTimer = null;

}

function ClearAllTimers(){
	clearTimeout(timerhide);
	timerhide = null;
	clearTimeout(drawTimer);	
	drawTimer = null;
}

function drawTree(level,startNode,x,y,inmouseovercolor,activeCollectionName)
{
   var drawto = null;
   ClearAllTimers();
   mouseovercolor = inmouseovercolor;

   if (startNode != currentNode || activeCollection != activeCollectionName){
		activeCollection = activeCollectionName;
		if (document.all.item('menuFrame'+level)) {
			document.all.item('menuFrame'+level).style.display='';  
			document.all.item('menuFrame'+level).style.width=1;
			document.all.item('menuFrame'+level).style.height=1;                      
			drawto=window['menuFrame' +level].window.document;
		}
		if (drawto){
			var html=buildBranch(level,startNode);
			startTrans(drawto,html,"fade");						//used to draw the html and fade the item in and out.
			window['menuFrame' +level].parentid=startNode;		//parentid=ID of the children this folder represents
			syncFrametoContent(level);
	  		setPosition(level,x,y);
		}
   }
   currentNode = startNode;
}


function starthidetimer(){
	timerhide=setTimeout("hidemenu();",250);
}

function setPosition(levelin,xpos,ypos){
	document.all.item('menuFrame'+levelin).style.top=ypos;  //sets the height of the new folder views layer.
	document.all.item('menuFrame'+levelin).style.left=xpos;  //sets the height of the new folder views layer.	
}

function buildBranch(levelin,startNode) {
  var children=null;
  if (startNode==0){ //its root..
  	children = getActiveCollection().extractRootChildren();
  }else{
   	children = getActiveCollection().extractChildrenOf(startNode);
  }
  		var currentIndex = 1;
		var html='';  //folderviewheader;
		html+="<table id=tablenum"+levelin +" border=0 cellpadding=2 cellspacing=0 bgcolor=cccccc class=table_winMenu>";
		levelin=parseInt(levelin)+1;
		while (currentIndex <= children.length) {
			html+=children[currentIndex].buildhtml(levelin) +'\n';
			currentIndex++;
		}
		if (children.length==0){
		/*	var nodeIndex = indexOfNode(startNode);
			if (menuData[nodeIndex].gotchild==1){
    			html+="<tr class=tr_winMenu_off '><td></td><td>(Empty)</td><td></td></tr>";
			}else{
			sqlframe.window.location=loadlevelfile +'?id=' +startNode +'&level='+levelin;
			html+="<tr class=tr_winMenu_off ' onmouseover='clearTimeout(window.parent.timerhide);'><td></td><td>Loading...&nbsp;</td><td>&nbsp;</td></tr>";
			}*/
		}
		html+="</table>";
		//html+=folderviewfooter;
  
  return html;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// set_child() - GENERAL FUNCTION - opens/closes folder nodes after they are loaded.
function set_child(level,id) {
        var inlevel=parseInt(level)-1;
    	var nodeIndex = indexOfNode(id);
    	getActiveCollection()[nodeIndex].open = 1;
    	getActiveCollection()[nodeIndex].gotchild = 1;
     	drawto=window['menuFrame' +inlevel].window.document;
        var html=buildBranch(inlevel,id);
    	startTrans(drawto,html,"fade");			 //used to draw the html and fade the item in and out.
    	syncFrametoContent((level-1));
}



function hidemenu()
{
	currentNode =0;
	document.all.item('menuFrame1').style.display='none';  //different for current rollover style
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// displaylevels() - MENU - < lev are displayed.. > lev are hidden 
function displaylevels(inlevel,objectin){
	lev=1;
	while (document.all.item(objectin +lev)){
		if (lev>inlevel){
			document.all.item(objectin +lev).style.display='none';
			document.all.item(objectin +lev).parentid='';
			currentNode = 0;			//Reset the current display
		}else{
			document.all.item(objectin +lev).style.display='';
		}
		lev++;
	}
}

//takes the content of the information within the menuFrame and makes the frame the same size.
function syncFrametoContent(inlevel)
{
	templevel=inlevel;
	setTimeout("document.all.item('menuFrame' +templevel).style.width=getXYPos(window['menuFrame' +templevel].document.all.item('tablenum'+templevel)).right+2;",1);
	setTimeout("document.all.item('menuFrame' +templevel).style.height=getXYPos(window['menuFrame' +templevel].document.all.item('tablenum'+templevel)).bottom+2;",1);
}

function showfolder(idin,levelin,x,y){
	document.all.item('menuFrame'+levelin).style.top=y-topalignment +'px';  //sets the height of the new folder views layer.
	document.all.item('menuFrame'+levelin).style.left=x-layover +'px';  //sets the height of the new folder views layer.	
	displaylevels(levelin,'menuFrame');
	drawTree(levelin,idin);
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// hlink() - MENU - checks to see if it should hide the menu that is displayed.. then highlights accordingly.
function hlink(inobj,inout,idin,levelin){
	levelin=parseInt(levelin)-1;
	clearTimeout(timershow);
	clearTimeout(timerhide);
	timershow=setTimeout("displaylevels(" +levelin +",'menuFrame');",500);
	h(inobj,inout,idin);
}



// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// highlight() - MENU - onmouseover highlights the menu and displays the children after delayed amount of time.  
function highlight(inobj,idin,levelin,x,y){
	clearTimeout(timershow);
	clearTimeout(timerhide);
	var PNUM=parseInt(levelin)-1;  
	var objlayer=window['menuFrame'+PNUM];	
	if (PNUM>0){
		if (window['menuFrame' +PNUM].parentid!=idin)
		{
			tempmenu=objlayer;
			tempmenuid=levelin;
			tempobj=inobj;
			timershow=setTimeout("tempmenu.selected=tempobj;showfolder('"+idin +"','" +levelin +"','" +x +"','" +y +"')",250);
		}
	}
	h(inobj,1,idin);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// h(inobj,inorout) - Highlights or unhighlights a given <table>||<TR>||<TD> object.	

function h(inobj,inorout,idin){
	var objlayer=getLayer(inobj);  //just grabs the current locations id. not a reference like in divs
	var ONUM=parseInt(objlayer.id.substring(8));  //get the next layer.	
	objlayer=window['menuFrame' +ONUM];  //reference to the menuFrame you are viewing.
	var PARENTNUM=ONUM-1;
	var CHILDNUM=ONUM+1; 
	if (inorout==0){
	//	if (window['menuFrame' +CHILDNUM].parentid!=idin){		
			inobj.className='tr_winMenu_off';
			clearTimeout(timershow);
	//	}	
	}else{
			if (objlayer.selected){
				objlayer.selected.className='tr_winMenu_off';
			}	
			inobj.className='tr_winMenu_on' +mouseovercolor;  //
	}
	if (PARENTNUM>0){  //make sure the correct parent has the right row highlighted.
		window['menuFrame'+PARENTNUM].selected.className='tr_winMenu_on';
	}
	
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Recurses a reference to the table of the menu.
function getLayer(domObj) 
{	
		var reg1str ="tablenum";
		var menuexp=new RegExp(reg1str);
		for (var id=0; !menuexp.test(domObj.id); domObj=domObj.offsetParent);
 		return domObj;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// getOffset() - function - returns an object with the left and top of a given object.
function getOffset(domObj) {
	var sumRight=domObj.offsetWidth; 
	var sumBottom=domObj.offsetHeight;	
	var sumTop=0;
	var sumLeft=0;
	var reg1str ="tablenum";
	var menuexp=new RegExp(reg1str);
	var frameid=0;
	
	while (domObj && domObj!=document.body)
	{
		if (menuexp.test(domObj.id)){
			frameid=domObj.id.substring(8);
		}
		sumTop+=domObj.offsetTop;
		sumLeft+=domObj.offsetLeft;	
		domObj=domObj.offsetParent;	
	}
	
	//add the xy to the menu frame.
	domObj=document.all.item('menuFrame'+frameid);
	while (domObj && domObj!=document.body)
	{
		sumTop+=domObj.offsetTop;
		sumLeft+=domObj.offsetLeft;	
		domObj=domObj.offsetParent;	
	}	
		
	sumRight+=sumLeft;
	sumBottom+=sumTop;
	//alert('right:' +sumRight  +'   bottom:'  +sumBottom);
 	return {left:sumLeft,top:sumTop,right:sumRight,bottom:sumBottom}
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// getXYPos() - function - returns an object with the position of corners of an object
function getXYPos(domObj) {
	var sumRight=0; 
	var sumBottom=0;	
	try {
		var sumRight=domObj.offsetWidth; 
		var sumBottom=domObj.offsetHeight;	
		var sumTop=0;
		var sumLeft=0;
		
		while (domObj && domObj!=document.body)
		{
			sumTop+=domObj.offsetTop;
			sumLeft+=domObj.offsetLeft;	
			domObj=domObj.offsetParent;	
		}
		sumRight+=sumLeft;
		sumBottom+=sumTop;
	}catch(Exception){}
 	return {left:sumLeft,top:sumTop,right:sumRight,bottom:sumBottom}
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Collection() - OBJECT - a dynamic storage structure similar to an Array.

function Collection() 
{
	this.length = 0; 
	this.add = add; 
	this.extractChildrenOf = extractChildrenOf;
    this.extractRootChildren = extractRootChildren;
    this.indexOfNode = indexOfNode;
	return this;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// extractChildrenOf(node)  -METHOD of Collection returns a collection of menus.

function extractChildrenOf(node) {
	var children = new Collection();
	var currentIndex = 1;
	while (currentIndex <= this.length) {
		if (this[currentIndex].parent == node){
			children.add(this[currentIndex])
		}
		currentIndex++
	}
	return children;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// indexOfNode(id)  -METHOD of Collection returns the index of an item..

function indexOfNode(id) {
	var currentIndex = 1;
	while (currentIndex <= getActiveCollection().length) {
			if (getActiveCollection()[currentIndex].id == id) {return currentIndex;}
		currentIndex++;}
  alert('tree index not found:' +id);
	return -1}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// extractRootChildren(node)  -METHOD of Collection returns all the root items + 1st level..

function extractRootChildren() {
	var children = new Collection();
  var breakloop=false;
  var currentIndex=1;
	while (currentIndex <= this.length && breakloop==false) {
		if (this[currentIndex].parent == 0){
			children.add(this[currentIndex]);
      breakloop=true;
		}
		currentIndex++;
	}
  currentIndex = 1;
	while (currentIndex <= this.length) {
		if (this[currentIndex].parent == 1){
     		children.add(this[currentIndex]);
    }
		currentIndex++;
	}
	return children;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// add() - METHOD of Collection - adds an object to a Collection.

function add(object)
{
 	  this.length++;
   	this[this.length] = object;
}


// Menu OBJECTS...~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function Menu(type,inid,inparent,inname,inurl,intarget,inicon)	//subclass of menu
{
	this.type=type;
	this.id=inid;
	this.parent=inparent;
	this.name=inname;
	this.action=inurl;
	this.target=intarget;
	this.icon=inicon;
	this.gotchild=0;
	this.buildhtml=Menu_Buildrow;  //overloads the menu objects
	return this;
}

function Menu_Buildrow(levelin){
	var html="";
		var imageString = iconpath + this.icon;
		html+="\n<tr class=tr_winMenu_off style='FONT-SIZE: 9pt; FONT-FAMILY: arial, verdana, helvetica, san-serif;' ";
   		if (this.action.indexOf("javascript")==-1){
      			html+="onclick=\"window.parent.firewin('" +this.action +"','" +this.target +"');\" ";
    		}else{
      			html+="onclick='" +this.action +"'";
    		}
		if (this.type == 'folder'){
			html+=" onmouseout=window.parent.hlink(this,0," +this.id  +"," +levelin +"); onmouseover=\"window.parent.highlight(this,'"+this.id +"','"+levelin+"',window.parent.getOffset(this).right,window.parent.getOffset(this).bottom);\"";
		}else{
			html+=" onmouseover=window.parent.hlink(this,1," +this.id +"," +levelin +"); onmouseout=window.parent.hlink(this,0," +this.id  +"," +levelin +"); ";
		}
		html+=">";  //closeing > of  the <tr>.

		html+="<td NOWRAP>" + this.name + "</td>\n";  //<IMG SRC='" + imageString + "' WIDTH=16 HEIGHT=16 ALIGN=TEXTTOP BORDER=0>&nbsp

		if (this.type == 'folder') {
			html+="<td >" +rightarrow +"</td>";
		}else{
			html+="<td>&nbsp;</td>";
		}
		html+="</tr>";
	return html;
}
