/************************************************\
| Totally sweet image thumbnail expanding script |
|                 By Nate True                   |
|                                                |
|     Feel free to use the script if you can     |
|          understand how it works.              |
\************************************************/
var curImage;
var curThumb;
var curZoomer;
var curDirection;
var curOpacity;
var curZoomage;
var imageLoaded;
var curOldSpot;
var curOldX, curOldY;
var noUnzoom = false;
var lastRZtime;
var isPortrait = false;

var xheight = 480;
var xwidth = 640;

function getAbsoluteLeft(obj) {
	// Get an object left position from the upper left viewport corner
	// Tested with relative and nested objects
	oLeft = obj.offsetLeft;            // Get left position from the parent object
	while(obj.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = obj.offsetParent;    // Get parent object reference
		oLeft += oParent.offsetLeft; // Add parent left position
		obj = oParent;
	}
	// Return left postion
	return oLeft;
}

function getAbsoluteTop(obj) {
	// Get an object top position from the upper left viewport corner
	// Tested with relative and nested objects
	oTop = obj.offsetTop;            // Get top position from the parent object
	while(obj.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = obj.offsetParent;  // Get parent object reference
		oTop += oParent.offsetTop; // Add parent top position
		obj = oParent;
	}
	// Return top position
	return oTop;
}

function dozoom(num, nom, portrait) {
  var d = document.getElementById("zoomdiv" + num);
  if (!d) location.href = nom;
  if (curZoomer == d.firstChild) { unzoom(); return;}
  else if (curImage || curZoomer) closezoom();
  noUnzoom = true;
  curThumb = document.getElementById("image" + num);

  isPortrait = portrait;

  curZoomage = d;
  curOldSpot = d.nextSibling;
  var x = getAbsoluteLeft(curThumb);
  var y = getAbsoluteTop(curThumb);
  var p = d;



  d.parentNode.removeChild(d);
  var f = document.getElementById('first');
//  d.style.left = curOldX = x - 5;
//  d.style.top = curOldY = y - 59;
  d.style.left = curOldX = x;
  d.style.top = curOldY = y;
  d.style.position = 'absolute';
  d = f.parentNode.insertBefore(d, f);
  curZoomer = document.getElementById("zoomspan" + num);
  if (!curZoomer) location.href = nom;
  imageLoaded = false;
  curZoomer.innerHTML = "<img src='" + nom + "' id='secretImage' class='hidden' onLoad='imageLoaded=true;'>";
  curImage = document.getElementById("secretImage");
  curImage.style.MozOpacity = 0;

  curOpacity = 0;
  curThumb.parentNode.focus();
  noUnzoom = false;
  lastRZtime = (new Date()).getTime();
  setTimeout("resumezoom()", 10);
  curDirection = 1;
}
function resumezoom() {
  if (curDirection != 1) return;
  if (!curThumb) return;
  var frac, ofrac;
  var nw = (new Date()).getTime();
  var diff = nw - lastRZtime;
  frac = 0.7;
  ofrac = 0.95;
  if (diff > 20) {frac = 0.6; ofrac = 0.9;}
  if (diff > 100) {frac = 0.4; ofrac = 0.6;}
  if (diff > 200) {frac = 0; ofrac = 0;}
  lastRZtime = nw;
  var omfrac = 1 - frac;
  var oomfrac = 1 - ofrac;

  if (isPortrait) {
    curThumb.width = Math.ceil(curThumb.width * frac + 576 * omfrac);
    curThumb.height = Math.ceil(curThumb.height * frac + xheight * omfrac);
  } else {
    curThumb.width = Math.ceil(curThumb.width * frac + xwidth * omfrac);
    curThumb.height = Math.ceil(curThumb.height * frac + xheight * omfrac);
  }
  d = curZoomer.parentNode;
  d.style.width = curThumb.width;
  var targX, targY;
  var wH,wW,wX,wY;
  if (window.innerWidth == undefined) {
    wW = document.body.clientWidth;
    wH = document.body.clientHeight;
    wX = document.body.scrollLeft;
    wY = document.body.scrollTop;
  }
  else {
    wW = window.innerWidth;
    wH = window.innerHeight;
    wX = window.scrollX;
    wY = window.scrollY;
    }
  targX = (wW - curThumb.width) / 2 + wX;
  targY = (wH - curThumb.height) / 2 + wY;
  d.style.left = Math.ceil(parseInt(d.style.left) * frac + targX * omfrac);
  d.style.top = Math.ceil(parseInt(d.style.top) * frac + targY * omfrac);
  
  curImage = document.getElementById("secretImage");
  if (curImage && imageLoaded) {
    curImage.width = curThumb.width;
    curImage.height = curThumb.height;
    curImage.className = 'nothidden';
    curOpacity = curOpacity * ofrac + 100 * oomfrac;
    curImage.setAttribute("style", "-moz-opacity: "
      + curOpacity/100 + "; "
      + "filter: alpha(Opacity=" + parseInt(curOpacity) + "); "
      + "opacity: " + (curOpacity / 100) + ";");
    }
  if (isPortrait) {
    if (curThumb.width >= 576 && curOpacity > 95) return;
  } else {
    if (curThumb.width >= xwidth && curOpacity > 95) return;
  }
  setTimeout('resumezoom()', 10);
}
var shrinkThumb, shrinkZoomer, shrinkImage, shrinkOldSpot, shrinkOldX, shrinkOldY;
function unzoom() { 
  if (noUnzoom) return;
  shrinkThumb = curThumb;
  shrinkZoomer = curZoomer;
  shrinkImage = curImage;
  shrinkOldSpot = curOldSpot;
  shrinkOldX = curOldX;
  shrinkOldY = curOldY;
  curThumb = false;
  curImage = false;
  curZoomer = false;
  curOldSpot = false;
  setTimeout('reducezoom()', 10);
  }
function reducezoom() {
  if (!shrinkThumb) return;
  shrinkThumb.width = shrinkThumb.width * 0.7 + 128 * 0.3;
  shrinkThumb.height = shrinkThumb.height * 0.7 + 96 * 0.3;
  d = shrinkZoomer.parentNode;
  d.style.width = shrinkThumb.width;
  d.style.left = Math.ceil(parseInt(d.style.left) * 0.7 + shrinkOldX * 0.3);
  d.style.top = Math.ceil(parseInt(d.style.top) * 0.7 + shrinkOldY * 0.3);
  shrinkZoomer.innerHTML = "";
  if (shrinkThumb.width < 129) {
    shrinkThumb.width = 128;
    shrinkThumb.height = 96;
    closeshrink();
  }
  else setTimeout('reducezoom()', 10);
}
function closeshrink() {
  if (!shrinkThumb || !shrinkZoomer || !shrinkOldSpot) return;
  shrinkZoomer.innerHTML = "";
  var d = shrinkZoomer.parentNode;
  d.parentNode.removeChild(d);
  shrinkOldSpot.parentNode.insertBefore(d, shrinkOldSpot);
  d.style.position = 'static';
  shrinkImage = false;
  shrinkZoomer = false;
  shrinkThumb = false;
  }
function closezoom() {
  curZoomer.innerHTML = "";
  var d = curZoomer.parentNode;
  d.parentNode.removeChild(d);
  curOldSpot.parentNode.insertBefore(d, curOldSpot);
  curImage = false;
  curZoomer = false;
  curThumb = false;
}