// Product Selector
//December 2004 version 5:32pm
var mouseOverImg;
var normalImg;
var charsPerLine;
var selectorWidth = 0;
var menu_items = new Array();
var PS_layer_list = new Array();
PS_layer_list[0] = "level-1";
var selectorTimerID = null; 
var selectorTimerOn = false; 
var selectorTimeCount = 700; 
var what = null; 
var newbrowser = true; 
var check = false;
var netscapeCheck = false;
initMenus();

function initMenus() {

	if (document.getElementById && !document.all) {
		layerRef="document.getElementByID";
		styleSwitch=".style";
		what="netscape";
	} else if(document.getElementById) {
		layerRef="document.getElementByID";
		styleSwitch=".style";
		what="dom1";
	} else if (document.layers) {
		layerRef="document.layers";
		styleSwitch="";
		what ="ns4";
	} else if(document.all) {
		layerRef="document.all";
		styleSwitch=".style";
		what ="ie4";
	}  else {
		what="none";
		newbrowser = false;
	}
	check = true;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
curleft += obj.offsetLeft
obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
curtop += obj.offsetTop;
obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }
function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt,"Top"); }
function getAbsPos(elt,which) {
 iPos = 0;
 while (elt != null) {
  iPos += elt["offset" + which];
  elt = elt.offsetParent;
 }
 return iPos;
}

function layerWrapper(name)
{
	
	if (what=="dom1")
	{
	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
	}
	else if (what =="ie4")
	{
	this.obj = document.all[name];
	this.style = document.all[name].style;
	}
	else if (what =="ns4")
	{
	
	
	this.obj = layerWrapperNN4(document,name);
	this.style = this.obj;
	} 
	else if (what=="netscape")
	{
	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
	}
	
}

function layerWrapperNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
foundLayer = x[i];
		else if (x[i].layers.length)
var tmp = layerWrapperNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}
function get_anchor(name)
{
	if (what=="dom1")
	{
		elem = document.getElementById(name);
	}
	else if (what=="netscape")
	{
		elem = document.getElementById(name);
	}
	else if (what =="ie4")
	{
		elem = document.all[name];
	}
	else if (what =="ns4")
	{
//if we've overloaded the func and passed in the name of the proximate (containing) div
		if(arguments.length == 2)
		{
x = new layerWrapper(arguments[1]);
elem = (x.obj.document.anchors[name]);

		}
		else
		{
elem = document.anchors[name];
		}
		
	}
	return elem;
}
function router (id,ev) {
	/* accepts event 'routing' from <div>'s under NS4 */
	var msg = '';
	
	if(ev.type == 'mouseover')
	{
		selectorStopTime();
		
	}
	else
	{
		selectorStartTime();
	}
	
		
}
function initEventHandlers () {
  
  if (document.layers)
  {
	
	for(var i=0; i <= PS_layer_list.length - 1; i++)
	{
		
		elem = layerWrapperNN4(document,PS_layer_list[i]);
		var prefix = 'window.document.' + PS_layer_list[i] + '.';
		var dynam_function = 'elem' + '.onmouseover = ' + 'elem' + '.onmouseout=' + '	function (evt){ router("' + PS_layer_list[i] +'",evt);};';
		eval(dynam_function);
		
	}
	  
  } 
}
function showSelectorLayer(layerToShow, positioningAnchorName) {
	if(check)
	{
		if (what =="none")
		{
return;
		}
		else 
		{

//it's assumed that we're dealing with a layer here
wrapper = new layerWrapper(layerToShow);
offsetTop = 0;
offsetLeft = 0;
//if this was overloaded, overload the call to get_anchor:
if(arguments.length == 3 && what =="ns4")
{
	anchorer = get_anchor(positioningAnchorName, arguments[2]);//arguments[2] is the div that contains the anchor
	parentLayer = new layerWrapper(arguments[2]);
	offsetTop = (layerToShow == PS_layer_list[0]) ? parentLayer.style.top + 17 : parentLayer.style.top - 4;
	offsetLeft = (layerToShow == PS_layer_list[0]) ? parentLayer.style.left - 4 : parentLayer.style.left + (selectorWidth - 5);
	
} 
else if (what =="netscape") 
{
	netscapeCheck = true;
	parentLayer = new layerWrapper(arguments[2]);
	offsetTop = (layerToShow == PS_layer_list[0]) ? 1 : 0;
	offsetLeft = (layerToShow == PS_layer_list[0]) ? parentLayer.style.left - 1 : parentLayer.style.left + (selectorWidth-5);
	anchorer = get_anchor(positioningAnchorName);
	wrapper.style.left= getAbsX(anchorer) + 0 + 'px';

}
else if(what !="ns4")
{
	offsetTop = (layerToShow == PS_layer_list[0]) ? 1 : 0;
	offsetLeft = (layerToShow == PS_layer_list[0]) ? (0) : (selectorWidth);
	anchorer = get_anchor(positioningAnchorName);
}


wrapper.style.display="block";
wrapper.style.left= getAbsX(anchorer) + offsetLeft + 'px';
//wrapper.style.top= getAbsY(anchorer) + offsetTop + 'px';
wrapper.style.visibility="visible";

		}
		
	} 
	else
	{
		return;
	}
}
function selectorStartTime() {
	
	if (selectorTimerOn == false) {
		selectorTimerID = setTimeout("hideAllSelectorLayers()", selectorTimeCount);
		selectorTimerOn = true;
	}
}

function selectorStopTime() {
	
	if (selectorTimerOn) {
		clearTimeout(selectorTimerID);
		selectorTimerID = null;
		selectorTimerOn = false;
	}
}

function hideAllSelectorLayers() {
	
	if(arguments.length != 0)
	{
		
		var exclude;
		
		for (var i = 0; i < PS_layer_list.length; i++)
		{
exclude = false;

for(var ii = 0; ii < arguments.length; ii++)
{
	
	if(PS_layer_list[i] == arguments[ii])
	{
		exclude = true;
		break;
	}
}
//if the current layer we're vetting wasn't in the exclusion list, then hide it:
if(!(exclude))
{
  hideSelectorLayer(PS_layer_list[i]);
}
		}
	}
	else
	{
		for (var i = 0; i < PS_layer_list.length; i++)
		{
hideSelectorLayer(PS_layer_list[i]);
		}
	}
} 
function hideSelectorLayer(layerName)
{
	if(check)
	{
		if (what =="none")
		{
return;
		}
		else 
		{
//it's assumed that we're dealing with a layer here
wrapper = new layerWrapper(layerName);
wrapper.style.visibility="hidden";
wrapper.style.display="none";
		}
		
	} 
	else
	{
		return;
	}
}
function hovering(ID, href, value)
	{
		/* a function which changes the background color of a div, in order to signify rolling-over */
		if(document.getElementById)
		{
window.status = href; //NB: when !(value) href=''
document.getElementById((ID)).style.backgroundColor = (value) ? "#f27f1a" : ""; //TBD: color should presumably be stored in a variable
document.getElementById(('td-' + ID)).style.color = (value) ? "#ffffff" : ""; //TBD: color should presumably be stored in a variable
if(document.images[('img-' + ID)]) document.images[('img-' + ID)].src = (value) ?  mouseOverImg : normalImg;
		} 
		 
	}



function copyArray(arrToCopy)
{
	var temp = new Array();
	for(var i=0; i < arrToCopy.length; i++)
	{
		temp[i] = arrToCopy[i];
	}
	return temp;
}
function buildProductSelector(array_serializer)
{
	/* A function which dynamically generates elements (<div>'s) for the product selector 'widget' */
	
	var markup, item_markup;
	var styleOverride = '';
	var arrArg;
	var xclusion;
	var containingLayer;
	var width;
	
	if(arguments.length == 1) //first time in, building the 'main' menu/div ('level-1')
	{
		width = selectorWidth ;
		containingLayer = 'level-1' ;
		xclusion = "'" + containingLayer + "'";
		arrArg = new Array(containingLayer);
		
	}
	else //overloaded/called recursively
	{
		width = 240; //TBD: needs to be tweaked, or perhaps controllable dynamically;
		containingLayer = array_serializer[0].parent + '-sub';
		
		var surr =  arguments[1];
//build the exclusion list: 
		for(x=0; x < surr.length; x++)
		{
xclusion = (x==0) ? ("'" + surr[x] + "'") : (xclusion + ",'" + surr[x] + "'");

		}
		xclusion += ",'" + containingLayer + "'";
		arrArg = copyArray(arguments[1]); //copy because we're going to augment it
		arrArg[arrArg.length] = containingLayer;
		PS_layer_list[PS_layer_list.length] = containingLayer; //add to Master List
	}
	
	styleOverride = ' style="width:' + width + 'px;"';
	var divHeight = array_serializer.length * 18;
	width=width+1;
	markup = '<div id="' + containingLayer + '" style="position:absolute; visibility:hidden; width:'+width+'px; height:189px; overflow:auto; overflow-y:auto; overflow-x:visible; border-right:1px solid black; border-bottom:1px solid black; color:#E67F1A; scrollbar-3dlight-color: #FCB06E; scrollbar-arrow-color: #ffffff; scrollbar-base-color: #F07D18; scrollbar-darkshadow-color: #BA661C; scrollbar-face-color: #F07D18; scrollbar-highlight-color: #F07D18; scrollbar-shadow-color: #F07D18; scrollbar-track-color: #EBEBEB;" onMouseOver="selectorStopTime();" onMouseOut="selectorStartTime();">';
	selectorStartTime();
	width=width-15;
	if (what=="netscape") {
		width=width-2;
	}
	markup += '\n<table class="tabula" width="' + width + '" border="0" cellspacing="0" cellpadding="0">\n';
	item_markup = '';
	//for each menu item:
	for(var i=0; i < array_serializer.length; i++)
	{

		var thisExclusion = '';
		var obj = array_serializer[i];
		var hasChildren = (obj.sub_nav && obj.sub_nav.length >= 1);
		item_markup += '<tr>';
		item_markup += '<td  class="selector-td"';
		
//if we're on the last item, we need to add the bottom border style:
		if(i == array_serializer.length - 1)
		{
item_markup += ' style="border-bottom:black solid 1px;"';
		}
		item_markup += '><div id="' + obj.id + '" onClick="window.location=' + "'" + obj.link + "'" + ';"';
		item_markup += ' onMouseOver="hovering(' + "'" + obj.id + "'" + ', ' + "'" + obj.link + "'" + ', true); ';
		/* extra mouseover code here: */
		thisExclusion = ' hideAllSelectorLayers(';		
		if(hasChildren) //if there are children, we have to *show* specific layers, as well as hide others
		{
thisExclusion += xclusion + ",'" +obj.id + "-sub'" + "); showSelectorLayer('" + obj.id + "-sub', " + "'" + obj.id + "', '" + containingLayer + "');" + ' selectorStopTime();" ';
//item_markup += thisExclusion;// + "); showSelectorLayer('" + obj.id + "-sub', " + "'" + obj.id + "', '" + containingLayer + "'); selectorStopTime();";
		}
		else //just hide the others
		{
thisExclusion += xclusion + ');" ';
//item_markup += xclusion + ");";

		}
		item_markup += thisExclusion;
		item_markup += 'onMouseOut="hovering(' + "'" + obj.id + "'" + ', ' + "''," + ' false);" ';
		item_markup += ' style="width:' + width + 'px;">\n';;  
		item_markup += '<table class="" border="0" cellspacing="0" cellpadding="0">\n';
		var dynamWidth = width - 5; //5 is the width of the arrow graphic
		item_markup += '<tr><td id="td-' + obj.id + '" class="selector-cell" >' + obj.text + '</td>';
//do we need to add the arrow, which signifies that the menu item has children?
		var rightMostCell = (hasChildren) ? '<img id="img-' + obj.id + '" src="' + normalImg + '" width="5" height="9" border="0">' : '';
		item_markup += '<td class="arrow-cell" >' + rightMostCell + '</td>';
		item_markup += '</tr></table>\n</div></td></tr>\n';
		
		if(hasChildren) //recurse
		{
buildProductSelector(obj.sub_nav, arrArg)
		}
	}

	markup += item_markup;
	markup += '</table>\n</div>';
	
	document.write(markup);
}


function fetchNode(id, arrToSearch)
{
	var found = null;
	
	for(var i=0; i <= arrToSearch.length - 1; i++)
	{
		
		var current = arrToSearch[i];
		
		if(current['id'])
		{
if(current['id'] == id)
{
	found = current;
	break;
}
		}
		if(current['sub_nav'] && current['sub_nav'].length >= 1)
		{
found = fetchNode(id, current['sub_nav']);
if(found) break;
		}
	}
	return found;
}
function buildNode(parentID, id, txt, href, hasSubMenu)
{
	
	//is this on the top-level menu or not?
	if(parentID == 'topmost-div')
	{
		
//build top-level item:
		menu_items[menu_items.length] = new Object();
		var node = menu_items[menu_items.length - 1];
		node['id'] = id;
		node['text'] = txt;
		node['link'] = href;
//does this have children?
		if(hasSubMenu) node['sub_nav'] = new Array();
	}
	else //build subordinate item:
	{
		var parent = fetchNode(parentID, menu_items);
		if(parent)
		{
//Does sub_nav array exist for parent?
if(!parent['sub_nav']) parent['sub_nav'] = new Array();
var subNode = parent['sub_nav'][parent['sub_nav'].length] = new Object();
subNode['parent'] = parent.id;
subNode['id'] = id;
subNode['text'] = txt;
subNode['link'] = href;
	
//Does this have a sub nav?
if(hasSubMenu) subNode['sub_nav'] = new Array();
		}
	}
}



//-->