/*
 * Copyright 2006, Peter Rowntree. All Rights Reserved.
 * http://www.hdyn.com/wr/common/contact.php?addr=pr
 * needs: queryStrLite, and others
 */
 
preloadImages(["/images/R0/"],["prev.gif","next.gif"]);
var g_searchState=new SearchState();
var g_nHPP=10;

function doLoad()
{
   g_infoOb=document.getElementById('xfc');
   goRandom('mainBox',3);
   handleURI();
   okScroll(false);
}

function handleURI()
{
   var uri=getQueryItem("uri");
   if(uri == null)
      return;
   findAssertions(uri);
}

function findAssertions(uri)
{
   document.getElementById("uri").value=decodeURIComponent(uri);
   sezWho();
}

function sezWho()
{
   var uri=document.getElementById("uri").value;
   var destOb=document.getElementById('xfc2');
   if(uri == "")
      xfrComm("you need to enter a URL beginning with 'http://', or a numeric ID",destOb);
   else
   {
      var reqURI=g_ctx+"send.uin?action=";
      if(isNaN(uri))
         reqURI+="203&uri="+encodeURIComponent(uri);
      else
         reqURI+="115&id="+uri;
      xfrComm("....&loz;....",destOb);
      var req=sendXML("<doc></doc>",reqURI,getInflowsDone,null);
   }
}

function getInflowsDone(req)
{  
   var tempOb=g_infoOb;
   g_infoOb=document.getElementById('xfc2');
   try
   {
      testError(req);
      setInfUI(req.responseXML);
   }
   catch(e)
   {
      xfrComm(typeof e.message == "undefined" ? e : e.message);
   }
   g_infoOb=tempOb;
}

function setInfUI(xmlDoc)
{
   //fsHTML is the doc node:
   var n=xmlDoc.firstChild;
   if(n == null)
      throw "no data!";
   var cdata=n.firstChild;
   var s;
   if(cdata == null || (s=cdata.nodeValue) == null || s == "")
     xfrComm("no assertions!");
   else
   { 
      xfrComm("<div class='mbPos searchBars bgTrans' id='inf'>"+s+"</div>");
      var ob=document.getElementById('inf');
      setAllFlowBars(ob,"a");
   }
	showBottom(g_infoOb);
}

function showBottom(ob)
{
   var scroll=typeof pageYOffset != "undefined" ? pageYOffset : document.documentElement.scrollTop;
   var d=(offsetToReceiverY(ob,document.body)+ob.offsetHeight)-(wHeight()+scroll);
   var scroll=typeof pageYOffset != "undefined" ? pageYOffset : document.documentElement.scrollTop;
   if(d > 0)
		window.scrollBy(0,d);
}

function wHeight()
{
	if(typeof innerHeight == 'number' )
		return innerHeight;
	if(document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	return document.body.clientHeight;
}

function prev()
{
   g_searchState.prev();
}

function next()
{
   g_searchState.next();
}

function jump(a)
{
   g_searchState.jump(a);
}

function searchPeople(i)
{
   g_searchState.search(i);
}

function SearchState()
{
   this.action=2;
   this.alpha=0;
   this.ss=null;
   this.prev=function()
   {
      this.alpha-=g_nHPP;
      if(this.alpha < 0)
         this.alpha=0;
      this.searchPeople();
   }
   this.search=function(action)
   {
      this.action=action;
      this.alpha=0;
      this.ss=document.getElementById("sStr").value;
      this.searchPeople();
   }
   this.next=function()
   {
      this.alpha+=g_nHPP;
      this.searchPeople();
   }
   this.jump=function(alpha)
   {
      this.alpha=alpha;
      this.searchPeople();
   }
   this.searchPeople=function()
   {
      try
      {
         if(this.ss == null || this.ss == "")
            throw "no search terms!";
         var reqURI=g_ctx+"ss.hds?action="
            +this.action+"&a="+this.alpha+"&ss="+encodeURIComponent(this.ss);
         xfrComm("....&loz;....");
         sendXML("<doc></doc>",reqURI,this,null);
      }
      catch(e)
      {
         xfrComm(typeof e.message == "undefined" ? e : e.message);
      }
   }
   this.done=function(req)
   { 
      try
      {
         testError(req);
         var n=req.responseXML == null ? null : req.responseXML.firstChild;
         if(n == null)
            throw "no data!";
         var nDocs=numberAttr(n,"nDocs",0);
         this.alpha=numberAttr(n,"alpha",0);
         var g_nHPP=numberAttr(n,"hpp",10);
         setPrevNextUI(this.alpha,nDocs);
         n=n.firstChild;
         var s=n == null ? null : n.nodeValue;
         if(s == null || s == "")
           xfrComm("no search results");
         else
         { 
            xfrComm("<div id='sr'>"+s+"</div>");
            setAllFlowColors(document.getElementById('sr'),"a");
         }
      }
      catch(e)
      {
         xfrComm(typeof e.message == "undefined" ? e : e.message);
      }
   }
}

function numberAttr(node,attr,dflt)
{
   var x=node.getAttribute(attr);
   if(x == null || x == "" || isNaN(x))
      return dflt;
   return parseInt(x);
}

function setPrevNextUI(alpha,nDocs)
{
   var s="found: "+nDocs+" document"+(nDocs == 1 ? "." : "s.");
   if(nDocs > g_nHPP)
   {
      s+=" go to:";
      var i=alpha-g_nHPP*5+1;
      if(i < 1)
         i=1;
      var omega=i+g_nHPP*10;
      if(omega > nDocs)
         omega=nDocs;
      for(; i<=omega; i+=g_nHPP)
      {
         if(i == alpha+1)
            s+=" "+i;
         else
            s+=" <a href='javascript:jump("+(i-1)+")'>"+i+"</a>";
      }
   }
   document.getElementById("srJump").innerHTML=s;
   document.getElementById("prev").style.display=alpha > 0 ? "block" : "none";
   document.getElementById("next").style.display=alpha+g_nHPP >= nDocs ? "none" : "block";
}

function setAllFlowColors(barsBox,boxType)
{
   if(barsBox == null)
      return;
   var bars=barsBox.getElementsByTagName(boxType);
   if(bars == null)
      return;
   var barsLen=bars.length;
	for(var i=0; i < barsLen; ++i)
   {
      if(!setFlowColor(bars.item(i)))
         break;
   }
}

function setFlowColor(bbox)
{
   var f=bbox.getAttribute("f");
   if(f == null)
      return true;
   if(f == "")
      f=0.0;
   bbox.firstChild.style.backgroundColor=flowToColor(f);
   return true;
}