
woosh=null

function go() {
   applyeventhandlers();
}

function loadbigimage(obj) {
document.getElementById("bigimgholder").src="transparent.gif";
  var ob=obj.firstChild;
   var ttop=parseInt(ob.offsetTop,10);
   var tleft=parseInt(ob.offsetLeft,10);
   var thumbstop=parseInt(document.getElementById("thumbs").offsetTop,10); 
   var thumbsleft=parseInt(document.getElementById("thumbs").offsetLeft,10);
   var st=parseInt(document.getElementById("thumbs").scrollTop,10); 
   var sl=parseInt(document.getElementById("thumbs").scrollLeft,10);
  // debug("thumbs:"+document.getElementById("thumbs").scrollTop);
   //debug (document.getElementById("thumbtop").offsetTop);
     // debug ("thumb0:"+document.getElementById("a_thumb0").offsetTop);
    //  debug (document.getElementById("a_thumb2").offsetTop);
    // debug (document.getElementById("a_thumb4").offsetTop); 
 // debug("tt:"+tt+":ttop:"+ttop+":-:"+(ttop-tt)+":"+":tl:"+tl+":tleft:"+tleft+":-:"+(tleft-tl));
// debug("st:"+st+":ttop:"+ttop+":-:"+(ttop-st)+":"+":sl:"+sl+":tleft:"+tleft+":-:"+(tleft-sl)+":thumbstop:"+thumbstop+":thumbsleft:"+thumbsleft);
 // nned to take borde in to account in positioning
   if (parseInt(document.getElementById("a_thumb0").offsetTop,10)>thumbstop) {
      tpos=(ttop-st); 
      lpos=(tleft-sl);
   } else {
      tpos=ttop+thumbstop; 
      lpos=tleft+thumbsleft;
   }
  //  if (tleft<thumbsleft) tleft+=thumbsleft;// opera adjustment
  //if (ttop<thumbstop) ttop+=thumbstop;
  //var tpos=ttop+thumbstop;
 // var lpos=(tleft-tl);
 // if (ttop>(ttop-tt)) toppos=(ttop-tt);
//  if (tleft>(tleft-tl)) leftpos=(tleft-tl);
   document.getElementById("bigimgholder").style.top=tpos+"px";
   document.getElementById("bigimgholder").style.left=lpos+"px";
   document.getElementById("bigimgholder").style.width=ob.width+"px";
   document.getElementById("bigimgholder").style.height=ob.height+"px";
   bigimg=new Image();
  // debug ("foo");
  //debug(typeof bigimg.onload);
   bigimg.onload=function() { expandimginit()  }
   //  debug(typeof bigimg.onload);
  // debug ("foo2");
   bigimg.onerror=function() { alert("couldn't find") }
   //debug ("foo3");
   bigimg.src=ob.src.replace("thumbs/","");
  // debug ("foo4");
 return false;
}

function expandimginit() {
   clearTimeout(woosh); // anti-breakage for mad clickers
  // document.getElementById("bigimgholder").style.visibility="visible";
   var bt=parseInt(document.getElementById("thebigimg").offsetTop,10);
   var bl=parseInt(document.getElementById("thebigimg").offsetLeft,10); 
   bt+=10;bl+=10; // padding correction
   var st=0;
   var sl=Math.ceil((bl-lpos)/10);
   if (bt-tpos<0) {
      st=0-((bt-tpos)/10);
      st=Math.ceil(st);
      st=0-st;
   } else {
      st=Math.ceil((bt-tpos)/10)
   }
   var tbw=parseInt(document.getElementById("thebigimg").style.width,10);
   var tbh=parseInt(document.getElementById("thebigimg").style.height,10);
   tbw=isNaN(tbw) ? 0 : tbw;
   tbh=isNaN(tbh) ? 0 : tbh;
   var w=parseInt(document.getElementById("bigimgholder").style.width,10);
   var h=parseInt(document.getElementById("bigimgholder").style.height,10);
   var bw=parseInt(bigimg.width,10);
   var bh=parseInt(bigimg.height,10);
   //debug ("bw:"+bw+";w:"+tbw+";bw-w:"+(bw-w));
   var sw=Math.ceil((bw-w)/10);
   var sh=Math.ceil((bh-h)/10);
  // debug ("bw:"+bw+";tbw:"+tbw+";bw-tbw:"+(bw-tbw));
   var stbw=Math.ceil((bw-tbw)/10);
   var stbh=Math.ceil((bh-tbh)/10);
   if (stbw==0 && bw-tbw<0) stbw=-1; // Match.ceil((15-10)/10)=0
   else if (stbw==0 && bw-tbw>0) stbw=1; // 0 is no good because there is 
   if (stbh==0 && bh-tbh<0) stbh=-1; // a style.width difference so detect such 
   else if (stbh==0 && bh-tbh>0) stbh=1; // cases and compensate
   document.getElementById("bigimgholder").style.opacity="0.1";
   document.getElementById("bigimgholder").src=bigimg.src;
   expandimg(sw,sh,bw,bh,st,sl,bt,bl,stbw,stbh);
  // setTimeout(function() {
  //   expandimg(sw,sh,bw,bh,st,sl,bt,bl,stbw,stbh);
  // }, 1000);
}

function expandimg(sw,sh,bw,bh,st,sl,bt,bl,stbw,stbh) {
//  debug ("sw:"+sw+";sh:"+sh+";bw:"+bw+";bh:"+bh+";st:"+st+";sl:"+sl+";bt:"+bt+";bl:"+bl+";stbw:"+stbw+";stbh:"+stbh)
   var w=parseInt(document.getElementById("bigimgholder").style.width,10);
   var h=parseInt(document.getElementById("bigimgholder").style.height,10);
   w+=sw;h+=sh;
   if (w>bw) w=bw;
   if (h>bh) h=bh;
   document.getElementById("bigimgholder").style.width=w+"px";
   document.getElementById("bigimgholder").style.height=h+"px";
   var l=parseInt(document.getElementById("bigimgholder").style.left,10);
   var t=parseInt(document.getElementById("bigimgholder").style.top,10);
   l+=sl;t+=st;
   //if (l>bl) l=bl;
   if ((sl<0 && l<bl) || (sl>0 && l>bl)) l=bl;
   //if (t>bt) t=bt;
   if ((st<0 && t<bt) || (st>0 && t>bt)) t=bt;
   document.getElementById("bigimgholder").style.top=t+"px";
   document.getElementById("bigimgholder").style.left=l+"px";
   var op=parseFloat(document.getElementById("bigimgholder").style.opacity,10);
   op+=0.1;
   if (op>1) op=1;
   document.getElementById("bigimgholder").style.opacity=op;
   var tbw=parseInt(document.getElementById("thebigimg").style.width,10);
   var tbh=parseInt(document.getElementById("thebigimg").style.height,10);
   tbw=isNaN(tbw) ? 0 : tbw;
   tbh=isNaN(tbh) ? 0 : tbh;
   tbw+=stbw;tbh+=stbh;
   if ((stbw<0 && tbw<bw) || (stbw>0 && tbw>bw)) tbw=bw;
   if ((stbh<0 && tbh<bh) || (stbh>0 && tbh>bh)) tbh=bh;
   document.getElementById("thebigimg").style.width=tbw+"px";
   document.getElementById("thebigimg").style.height=tbh+"px";
  //  debug(w+"!="+bw+" || "+h+"!="+bh+" || "+l+"!="+bl+" || "+t+"!="+bt+" || "+bw+"!="+tbw+" || "+bh+"!="+tbh) ;
   if (w!=bw || h!=bh || l!=bl || t!=bt || bw!=tbw || bh!=tbh ) { 
      woosh=setTimeout(function() {
     expandimg(sw,sh,bw,bh,st,sl,bt,bl,stbw,stbh);
   }, 50);
   } else {
      document.getElementById("thebigimg").src=document.getElementById("bigimgholder").src;
    // document.getElementById("bigimgholder").style.visibility="hidden";
   }
}

function applyeventhandlers() {
   if (document.getElementsByTagName) {
      var imgs=document.getElementsByTagName("a");
      for (i=0;i<imgs.length;i++) {
         if (imgs[i].id.indexOf("thumb")!=-1) {
	    eval("document.getElementById('"+imgs[i].id+"').onclick=function() {var state=loadbigimage(this);return state}");
	  // imgs[i].addEventListener("click",loadbigimage,false);
	}
      }
   }
}

function yo() {
alert("yo!");
}

function scrolltothumb() {
// function to scroll the thumbs div so thumb for current image is showing.
// variable t is used because not all browsers report the position of an element at the top/left of the div as the same thing. some report the distance from edge of viewport, some from edge of the div.
// Attributes used not part of DOM but work in IE, Firefox, Mozilla, Opera, Konqueror and Safari
   if (document.getElementById('marker')) {
      if (document.getElementById('marker').offsetTop) {
         var tt=parseInt(document.getElementById('thumbtop').offsetTop,10); 
         var pt=parseInt(document.getElementById('marker').offsetTop,10);
	 var tl=parseInt(document.getElementById('thumbtop').offsetLeft,10); 
         var pl=parseInt(document.getElementById('marker').offsetLeft,10);
         document.getElementById("thumbs").scrollTop=(pt-tt);
         document.getElementById("thumbs").scrollLeft=(pl-tl);
      }
   }
}


function debug(w) {
   document.getElementById("debug").innerHTML+=w+"<br />";
}
