// JavaScript Document

//Based on an idea from JavaScriptKit.com (Simple Image Trail script)
//Uses alternative document width/height detection method
//This width result is wider in Opera, FF, Camino, Safari.  Height result is same
//Uses alternative get scroll position method.  (Original doesn't work in Opera)

// trailimagediv is set in body of page (otherwise Opera complains)

function imageOn(s,a,w,h)
{
src=s;
imgWidth=w;
imgHeight=h;
document.onmousemove=trackMouse;
loadImage(a);
}

function clickImageOn(here,s,a,w,h)
{
src=s;
imgWidth=w;
imgHeight=h;
posXYL=findPosXY(document.getElementById(here));
posXL=posXYL[0]*1;
posYL=posXYL[1]*1;
loadImage(a);

var posx = 15;
var posy = 15;
var scrollTop=getScrollXY()[1];

cury=posYL-scrollTop;
posx = posx + posXL;

if (docheight - posYL < (imgHeight + 100))
{
 posy += posYL - Math.max(0,(100 + imgHeight + posYL - docheight - scrollTop));
}
else
{
 posy += posYL;
}

if (docheight-cury<30)
{
 imageOff();
 return;
}
positionImage(posx,posy);
}

function loadImage(a)
{
loadimage=1;
theHTML = '<div><h1>' + a + '</h1>';
theHTML=theHTML+'<img src="images/'+src+'" alt="'+a+'" style="width:'+imgWidth+'px; height:'+imgHeight+'px;"></div>';
document.getElementById("trailimagediv").innerHTML=theHTML;
document.getElementById("trailimagediv").style.display="inline";
docwidth=docSize()[0];
docheight=docSize()[1];
}


function imageOff(){
loadimage=0;
document.getElementById("trailimagediv").style.display="none";
if (typeof docwidth!="undefined")
{
 document.getElementById("trailimagediv").style.left=docwidth+"px";
}
//document.getElementById("wideflash").style.display="block";
//document.getElementById("wideflash").style.visibility="visible";
document.getElementById("tallflash").style.display="block";
document.getElementById("tallflash").style.visibility="visible";
document.onmousemove=null;
}

   
function trackMouse(e) {
var posx = 15;
var posy = 15;
var scrollTop=getScrollXY()[1];
if (!e) var e = window.event;
if (e.pageX || e.pageY) //e.g. moz
{
 cury=e.pageY-scrollTop;
 posx = posx + e.pageX;
 if (docheight - e.pageY < (imgHeight + 100))
 {
  posy += e.pageY - Math.max(0,(100 + imgHeight + e.pageY - docheight - scrollTop));
 }
 else
 {
  posy += e.pageY;
 }
}
else if (e.clientX || e.clientY) //e.g. ie
{
 cury=e.clientY-scrollTop;
 posx = posx + e.clientX;
 if (docheight - e.clientY < (imgHeight + 100))
 {
  posy += e.clientY + scrollTop - Math.max(0,(100 + imgHeight + e.clientY - docheight));
 }
 else
 {
  posy += scrollTop + e.clientY;
 }
}

if (docheight-cury<30)
{
 imageOff();
 return;
}
positionImage(posx,posy)
}

function positionImage(posx,posy){
//if (posy< 300)
//{
// document.getElementById("wideflash").style.display="none";
// document.getElementById("wideflash").style.visibility="hidden"; 
//}
rpos=posx-((docwidth-midwidth-225)/2)-112;
if (midwidth-rpos<imgWidth+50)
{
 document.getElementById("tallflash").style.display="none";
 document.getElementById("tallflash").style.visibility="hidden";
}
document.getElementById("trailimagediv").style.left=posx+"px";
document.getElementById("trailimagediv").style.top=posy+"px";
}

//From http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant (SAD: Opera, FF, Safari, Camino) 
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant  (SAD: ??)
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode (SAD: IE6, IE7)
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
function docSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

function findPosXY(obj)
{
 pY=findPos(obj);
 
 pX=0;
 if (obj.offsetParent)
 {
  do
  {
	 pX += obj.offsetLeft;
  }while (obj = obj.offsetParent);
 }
 return[pX,pY];
}
