var newimg
var currentthumb
var scrollID

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function findPosLeft(obj) {
	var pos=findPos(obj)
        return pos[0]
}
function findPosTop(obj) {
	var pos=findPos(obj)
        return pos[1]
}

function setpic_()
{
    document.getElementById("mainpic").src=newimg
}

function scrollimgstrip()
{
   var currpos = findPosLeft(document.getElementById("imgthumbstrip")) 
                 + document.getElementById("imgthumbstrip").scrollLeft 

   var newpos  = findPosLeft(document.getElementById(currentthumb)) 
                 - (document.getElementById("imgthumbstrip").clientWidth / 2) 
                 + 65;  // (adjust by half the width of a thumbnail)


   var step=10;
   var slop=(document.getElementById("imgthumbstrip").clientWidth-400) / 2
   if (slop < step)
   {
     slop = step + 1
   }
 
   // have we moved "close enough" to newpos?
   var posdiff=currpos-newpos
   if ((-slop <= posdiff) && (posdiff <= slop))
   {
     clearInterval(scrollID);
     return;
   }

   // move a little bit
   var currscroll=document.getElementById("imgthumbstrip").scrollLeft
   if (currpos > newpos)
   {
     currscroll-=step;
   }
   else
   {
     currscroll+=step;
   }
   document.getElementById("imgthumbstrip").scrollLeft=currscroll;

   // check if scrolled to the edge
   if (document.getElementById("imgthumbstrip").scrollLeft!=currscroll)
   {
     clearInterval(scrollID);
     return;
   }
}

function setpic(pic,cmt,thumbcount)
{
  if (!document.getElementById) {
    return true;
  }

  if ( document.getElementById("mainpic").src.indexOf(pic) == -1)
  { 
    var prevcount = thumbcount - 1;
    var nextcount = thumbcount + 1;
    newimg=pic

    // mark the "active" thumbnail and scroll to it
    if (document.getElementById(currentthumb))
    {
      document.getElementById(currentthumb).style.border="black solid 1px"
    }
    currentthumb="thumbimg" + thumbcount
    document.getElementById(currentthumb).style.border="#e68200 solid 3px"
    scrollID = setInterval('scrollimgstrip()',20)

    // Set the navigation links
    var mainpicnavprev=document.getElementById("mainpicnavprev");
    var mainpicnavnext=document.getElementById("mainpicnavnext");
    document.getElementById("mainpicnavlinkdiv").href=document.getElementById("thumbhref" + thumbcount).href
    document.getElementById("mainpicnavlinkdiv").style.display="inline";
    if (document.getElementById("thumbhref" + prevcount))
    {
      mainpicnavprev.onclick=document.getElementById("thumbhref" + prevcount).onclick;
      mainpicnavprev.style.display="inline";
    } else {
      mainpicnavprev.style.display="none";
    }
    if (document.getElementById("thumbhref" + nextcount))
    {
      mainpicnavnext.onclick=document.getElementById("thumbhref" + nextcount).onclick;
      mainpicnavnext.style.display="inline";
    } else {
      mainpicnavnext.style.display="none";
    }

    // load the animated gif and make sure the img is visible
    document.getElementById("mainpic").src="progresscircle.gif"
    document.getElementById("mainpic").style.display="inline"

    // load the new image and text async
    //setTimeout('setpic_()',100);
    setpic_()
    loadXMLDoc(cmt)
  }
  return false;
}

