/*
 * Copyright 2006, Peter Rowntree. All Rights Reserved.
 * http://www.hdyn.com/wr/common/contact.php?addr=pr
 * needs: bars.js, flow.js
 */

function playLoad(barsBox)
{
   if(barsBox == null)
      barsBox=document.getElementById("expl");
   barsBox.onmousedown=doFlowMouseDown;
   setAllFlowBars(barsBox,"div");
}

function doFlowMouseDown(e)
{
   if(!e)
      e=window.event;
	var x=getEvtReceiverRelX(e,this);
	var i=Math.floor(x/g_fBoxWidth);
   var fOb=indImmediateChild(this,i,"div",null);
   if(fOb == null)
   	return false;
  	var fbHeight=fOb.offsetHeight;
	var y=getEvtReceiverRelY(e,this)-g_fBoxTop;
	if(y < 0)
		y=0;
	else if(y > fbHeight)
		y=fbHeight;
   var mid=fbHeight/2;
   var delta=mid-y;
   var h=delta >= 0 ? delta : -delta;
   var barOb=indImmediateChild(fOb,0,"span",null);
   if(barOb == null)
   	return true;
   barOb.style.height=h+"px";
   barOb.style.top=(y > mid ? mid : mid-h)+"px";
   barOb.style.backgroundColor=flowToColor(delta/mid);
   return false;
}

//if className == null it's ignored
//only works with HTML nodes because n.className is only defined for html nodes
function indImmediateChild(n,i,tagName,className)
{
	tagName=tagName.toLowerCase();
   n=n.firstChild;
   while(n != null)
   {
   	if(n.nodeName.toLowerCase() == tagName 
         && (className == null || n.className == className) && i-- <= 0)
   		return n;
   	n=n.nextSibling;
   }
   return n;
}

function setObClass(ob,oc,set)
{
   oc=" "+oc;
   var regex=new RegExp(oc);
   var c=ob.className;
	if(set)
   {
      if(c.indexOf(oc) >= 0)
         return false;
      c+=oc;
   }
   else
   {
      var oldC=c;
      c=c.replace(regex,"");
      if(c == oldC)
         return false;
   }
   ob.className=c;
   return true;
}