/*
 * Copyright 2008, Peter Rowntree. All Rights Reserved.
 * http://www.hdyn.com/mail/contact.php?addr=pr
 * needs blocks.js, but should be loaded first;
 * preload might conflict with other implementations of setImgSrc & isPNG
 */
 
var g_gs=[-1,-1,""];
var g_blocks=[];

var g_thisPage=location.pathname+location.search;
var g_hideTimer=new GPTimer();
var g_treeRoots=[];
var g_currentHolder=null;
var g_horizontals=[];
var g_imgArr=[];	//preloaded images
var g_filterPNGs=true;	//filter preloads?
var g_openRegex=new RegExp("Open$");
var g_closedRegex=new RegExp("Closed$");

window.onbeforeunload=hideAllSubs;
 
//roots: array of [holderID,iBlock]
//thisPage,depth no longer used; we now require root-relative URL's for nav page hrefs
function navLoad(roots,thisPage)	//thisPage, if specified, will override
{
	var hrz=g_gs[2];
	if(typeof hrz == "number")
		g_horizontals["h"+hrz]=1;
	else if(hrz != "")
	{
		hrz=hrz.split(",");
	  	for(var i=0; i<hrz.length; ++i)
	  		g_horizontals["h"+hrz[i]]=1;
	}
	if(thisPage != null)
		g_thisPage=thisPage;
	loadRoots(roots);
}

function loadRoots(roots)
{
	if(roots == null)
		return;
   for(var i=0; i < roots.length; i++)
   {
   	var root=roots[i];
   	if(root.length < 2)
   		continue;
   	var holder=document.getElementById(root[0]);
		if(holder == null)
   		continue;
		var block=buildBlock(root[1],"");
		if(block == null)
   		continue;
		g_treeRoots.push(block);
		holder.appendChild(block);
   }
}

//currently, no block items may be or contain divs (see tgtBlock())
function buildBlock(i,itemName)
{
	var itemOb;
	var items=g_blocks[i];
	if(items == null)
		return null;
	var isHorizontal=typeof g_horizontals["h"+i] != "undefined";
  	var blockOb=document.createElement('div');
  	blockOb.className="navBlock navBlock"+i;
  	blockOb.name=itemName;	//block name and item name match
  	for(var j=0; j<items.length; ++j)
  	{
  		var item=items[j];
  		var label=item[0];
  		var tgt=item[1];
   	var cssClass=item[2];
   	var title=item[3];
   	itemOb=document.createElement('a');
   	itemOb.name=j;
  		if(typeof tgt == "number")
  		{
  			itemOb.setAttribute("sub",tgt);
   		itemOb.onmouseover=isHorizontal ? downSub : rightSub;
  		}
  		else
  		{
	   	if(tgt == g_thisPage || tgt == "")
	   	{
  				if(tgt == g_thisPage)
	   			cssClass+=" currentItem";
	   	}
	   	else
	   	{
   			itemOb.setAttribute("href",tgt);
	   	}
  			itemOb.onmouseover=hideSibSub;
  		}
  		if(label != "")
  			itemOb.innerHTML=label;
  		if(title != "")
  			itemOb.title=title;
   	itemOb.onmouseout=startHideTimer;
  		blockOb.appendChild(itemOb);
  		itemOb.className=cssClass;
  	}
  	return blockOb;
}

function downSub(e)
{
	showSub(e,true);
}

function rightSub(e)
{
	showSub(e,false);
}

function cleanHolder(blockOb,itemOb)
{
	if(blockOb == null)
		return null;
	var holder=blockOb.parentNode;
	var hidAll;
	if(g_currentHolder != null && g_currentHolder != holder)
		hidAll=hideBlockSubs(g_currentHolder.firstChild,null);
	else
		hidAll=hideBlockSubs(blockOb,itemOb);
   g_currentHolder=holder;
   return hidAll;
}

function showSub(e,dropDown)
{
	stopHideTimer();
	var ob=evtTarget(e);
	if(ob == null)
		return;
	var parentBlock=tgtBlock(ob);
	if(!cleanHolder(parentBlock,ob))
		return;	//sub is already showing
	var holder=g_currentHolder;
	ob.className=ob.className.replace(g_closedRegex,"Open");
	var sub=buildBlock(ob.getAttribute("sub"),ob.name);
	if(sub == null)
		return;
	var x,y;
	if(dropDown)
	{
		x=offsetToRootX(ob,holder);
		y=offsetToRootY(ob,holder)+ob.offsetHeight+g_gs[1];
   }
   else
	{
   	x=offsetToRootX(ob,holder)+ob.offsetWidth+g_gs[0];
   	y=offsetToRootY(ob,holder);
   }
	sub.style.left=x+"px";
	sub.style.top=y+"px";
	holder.appendChild(sub);
}

function offsetToRootX(ob,root)
{
	var x=0;
   do
	{
      x+=ob.offsetLeft;
      ob=ob.offsetParent;
	}
	while(ob != null && ob != root);
   return x;
}

function offsetToRootY(ob,root)
{
	var y=0;
   do
	{
      y+=ob.offsetTop;
      ob=ob.offsetParent;
	}
	while(ob != null && ob != root);
   return y;
}

function evtTarget(e)
{
   if(!e)
      e=window.event;
	if(typeof e.target != "undefined")
		return e.target;
   return e.srcElement;	//IE 6
}

function hideAllSubs(e)
{
   for(var i=0; i < g_treeRoots.length; i++)
		hideBlockSubs(g_treeRoots[i],null);
}

function hideSibSub(e)
{
	stopHideTimer();
	cleanHolder(tgtBlock(evtTarget(e)),null);
}

//returns true if it hides them all, false if it leaves item's sub
function hideBlockSubs(blockOb,itemOb)
{
	var holder=blockOb.parentNode;
	var sub1=blockOb.nextSibling;
	if(sub1 == null)
		return true;
	var stopBlock;
	var hidingAll=itemOb == null || sub1.name != itemOb.name;
	if(hidingAll)
		stopBlock=blockOb;
	else
		stopBlock=sub1;
	while(holder.lastChild != stopBlock)
		holder.removeChild(holder.lastChild);
   var aList=blockOb.getElementsByTagName("a");
   for(var i=0; i < aList.length; ++i)
   {
   	if(aList[i] != itemOb)
			aList[i].className=aList[i].className.replace(g_openRegex,"Closed");
	}
	return hidingAll;
}

//no block items may be or contain divs
function tgtBlock(ob)
{
	while(ob != null && ob.nodeName.toLowerCase() != "div")
		ob=ob.parentNode;
	return ob;
}

//-----------------the hide timer-----------------------------------------------

function doHideTimer()
{
	if(stopHideTimer())
		hideAllSubs(null);
}

function startHideTimer()
{
	g_hideTimer.doAfter("doHideTimer()",50);
}

function stopHideTimer()
{
	var alreadyStopped=g_hideTimer.i_timer == null;
	g_hideTimer.clear();
	return !alreadyStopped;
}

function GPTimer()
{
   this.i_timer=null;
   this.doAfter=function(execStr,delay)
   {
      this.clear();
      this.i_timer=setTimeout(execStr,delay);
   }
   this.clear=function()
   {
      if(this.i_timer != null)
      {
         clearTimeout(this.i_timer);
         this.i_timer=null;
      }
   }
}

//---------------preload--------------------------------------------------------
function preloadImages(pathsArr)
{
   var imgPath;
   var img;
   for(var i=0; i < pathsArr.length; i++)
   {
      imgPath=pathsArr[i];
      img=document.createElement('img');
      setImgSrc(img,imgPath);
      g_imgArr.push(img);
   }
}

function setImgSrc(img,src)
{
   if(g_filterPNGs && typeof img.style.filter != 'undefined' && isPNG(src))
   {
   	img.src="images/trans.gif";
      img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
      	+src+"',sizingMethod='scale')";
   }
   else
     	img.src=src;
}

function isPNG(src)
{
	var arr=src.split(".");
	return arr[arr.length-1].toLowerCase() == "png";
}
