var popUpTime = 500; //how long until the popup terminates after rollout. (milliseconds)
var yOffset = 0; //how many pixels the popup should appear below its corresponding thumbnail
var theTimer; //timer variable that controls popup termination
var currentPic = null; //the current img that has been rolled over

//find X position of an object (obtained from: http://blog.firetree.net/2005/07/04/javascript-find-position/)
function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

//find Y position of an object (obtained from: http://blog.firetree.net/2005/07/04/javascript-find-position/)
  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

//returns an array containing the x,y offset of the window scroll amount.
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

//Activates the image rollover and populates it with data.
//"downloadSRC" should only be the file name. The path is added later by download.php.
function imgRollover(thisDiv, imgSrc){
	//stop timer in case the user activated a new rollover before the popup could be hidden
    clearTimeout(theTimer);
	
	if(currentPic != null){
		$(currentPic).fadeOut('fast', 1);
	}
	
    var winWidth = 0, winHeight = 0;
    var elementX = findPosX(document.getElementById('imagesDiv'));
    var elementY = findPosY(thisDiv);
    var popUp = document.getElementById('imgPopup');
    var scrollAmountY = getScrollXY();

    //get the window height/width so we can know if the popup will exceed the bounds (and correct for this).
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        winWidth = window.innerWidth - 18; //-18 because firefox seems to include the scroll bar in the width
        winHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        winWidth = document.documentElement.clientWidth;
        winHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        winWidth = document.body.clientWidth;
        winHeight = document.body.clientHeight;
    }
    
    //set the image
    document.getElementById('imgData').innerHTML = '<img id="bigImg" src="' + imgSrc + '">';
    document.getElementById('imgText').innerHTML = thisDiv.alt;
    //display popup
    popUp.style.display = 'block';
    
    //align to thumbnail x position
    if ((elementX + thisDiv.offsetWidth - popUp.offsetWidth) > winWidth){
        popUp.style.left = winWidth - popUp.offsetWidth + 'px';
    } else {
        popUp.style.left = elementX - document.getElementById('bigImg').offsetWidth - 1 +'px';
    }
    
    //align to thumbnail y position
    if (((elementY - scrollAmountY[1]) + yOffset + popUp.offsetHeight) > winHeight){
        popUp.style.top = winHeight - popUp.offsetHeight + scrollAmountY[1] + 'px';
    } else {
        popUp.style.top = elementY + yOffset + 'px';
    }
}

//hides the popup
function terminatePopUp () {
    document.getElementById('imgPopup').style.display = 'none';
}

//starts the timer to hide the popup
function beginPopUpTermination() {
    theTimer = setTimeout('terminatePopUp();', popUpTime)
}

function cancelTermination() {
	clearTimeout(theTimer);	
}