// Effects.js - (C) 2007 Panic Inc. / Cabel Sasser
// V2.0
//
// Used for fading / zooming effects, such as image zooming.
// Requires: EffectsHTML.js
//
// License: Not For Public Use, Yet. (Public release planned. Stay tuned.)

////////////////////////////
//
// ZOOM IMAGE functions

// Settings

var includeCaption = 1;   // Turn on the "caption" feature, and write out the caption HTML
var zoomTime       = 5;   // Milliseconds between frames of zoom animation
var zoomSteps      = 15;  // Number of zoom animation frames
var fade           = 1;   // Fade images in / out
var minBorder      = 90;  // Amount of padding between large, scaled down images, and the window edges

var zoomImageURI   = '/zoom/'; // Location of the zoom images

// Init. Do not add anything below this line, unless it's something awesome.

var myWidth = 0, myHeight = 0, myScroll = 0; myScrollWidth = 0; myScrollHeight = 0;
var zoomOpen = false, preloadFrame = 1, preloadActive = false, preloadTime = 0, imgPreload = new Image();

var zoomActive = new Array(); var zoomTimer  = new Array(); 
var zoomOrigW  = new Array(); var zoomOrigH  = new Array();
var zoomOrigX  = new Array(); var zoomOrigY  = new Array();

var zoomID    = "ZoomBox";
var theID     = "ZoomImage";
var theCap    = "ZoomCaption";
var theCapDiv = "ZoomCapDiv";

// Zoom: Setup The Page! Called onLoad();

function setupZoom() {
	prepZooms();
	insertZoomHTML();
	zoomdiv = document.getElementById(zoomID);  
	zoomimg = document.getElementById(theID);
}

// Zoom: Inject Javascript functions into zoomable href's, one by one.
// This is done at page load time via an onLoad() handler.

function prepZooms() {
	if (! document.getElementsByTagName) {
		return;
	}
	var links = document.getElementsByTagName("a");
	for (i = 0; i < links.length; i++) {
		if (links[i].getAttribute("href") && (links[i].getAttribute("rel"))) {
			if (links[i].getAttribute("rel").indexOf("zoom:") == 0) {
				links[i].onclick = function () { zoomClick(this); return false; };
				links[i].onmouseover = function () { zoomPreload(this); };
			}
		}
	}
}

// Zoom: Preload a zoom image when hovering over the thumbnail, then set the image once the preload is complete.
// Preloaded image is stored in imgPreload() and swapped out in the zoom function.

function zoomPreload(from) {

	var theimage = from.getAttribute("href");
	// console.log("PRELOAD START: "+theimage);

	// Only preload if we have to, i.e. the image isn't this image already

	if (imgPreload.src.indexOf(from.getAttribute("href").substr(from.getAttribute("href").lastIndexOf("/"))) == -1) {
		preloadActive = true;
		imgPreload = new Image();
      
		// Set a function to fire when the preload is complete, setting flags along the way.
   
		imgPreload.onload = function() {
			// console.log("PRELOAD END");
			preloadActive = false;
		}

		// Load it!
		imgPreload.src = theimage;
	}
}

// Zoom: Start the preloading animation cycle.

function preloadAnimStart() {
	preloadTime = new Date();
	document.getElementById("ZoomSpin").style.left = (myWidth / 2) + 'px';
	document.getElementById("ZoomSpin").style.top = ((myHeight / 2) + myScroll) + 'px';
	document.getElementById("ZoomSpin").style.visibility = "visible";	
	preloadFrame = 1;
	document.getElementById("SpinImage").src = zoomImageURI+'zoom-spin-'+preloadFrame+'.png';  
	preloadAnimTimer = setInterval("preloadAnim()", 100);
}

// Until we've been preloading for one second, just chill out in here.
// 
// function preloadAnimPending(from) {
// 	if (preloadActive != false) {
// 		if ((new Date() - preloadTime) > 1000) {
// 			document.getElementById("ZoomSpin").style.visibility = "visible";
// 			clearInterval(preloadAnimTimer);
// 			preloadAnimTimer = setInterval("preloadAnim()", 100);
// 		}
// 		else {
// 			// Stay in this loop and don't do anything while we wait one second
// 		}
// 	} else {
// 		clearInterval(preloadAnimTimer);
// 		zoomIn(preloadFrom);
// 	}
// } 

// Zoom: Display and ANIMATE the jibber jabber widget. Once preloadActive is false, bail and zoom it up!

function preloadAnim(from) {
	if (preloadActive != false) {
		document.getElementById("SpinImage").src = zoomImageURI+'zoom-spin-'+preloadFrame+'.png';
		preloadFrame++;
		if (preloadFrame > 12) preloadFrame = 1;
	} else {
		document.getElementById("ZoomSpin").style.visibility = "hidden";    
		clearInterval(preloadAnimTimer);
		zoomIn(preloadFrom);
	}
}

// Zoom: We got a click! Should we do the zoom? Or wait for the preload to complete?

function zoomClick(from) {

	// TODO: Double check that imgPreload src = clicked src

	// Get browser dimensions
	getSize();

	if (preloadActive == true) {
		// Preloading is otherwise still going on. So wait.
		preloadFrom = from;
		preloadAnimStart();
	} else {
		// Otherwise, we're loaded: do the zoom!
		zoomIn(from);
	}
}

// Zoom: Move an element in to endH endW, using zoomHost as a starting point.
// "from" is an object reference to the href that spawned the zoom.

function zoomIn(from) {

	zoomimg.src = from.getAttribute("href");

	// Determine the zoom settings from where we came from, the element in the <a>.
	// If there's no element in the <a>, or we can't get the width, make stuff up

	if (from.childNodes[0].width) {
		startW = from.childNodes[0].width;
		startH = from.childNodes[0].height;
		startPos = findElementPos(from.childNodes[0]);
	} else {
		startW = 50;
		startH = 12;
		startPos = findElementPos(from);
	}
		    
	hostX = startPos[0];
	hostY = startPos[1];

	// Make up for a scrolled containing div.
	// TODO: This HAS to move into findElementPos.
	
	if (document.getElementById('scroller')) {
		hostX = hostX - document.getElementById('scroller').scrollLeft;
	}

	// Determine the target zoom settings

	endW = imgPreload.width;
	endH = imgPreload.height;
	
	// TODO: need to get "rollover" setting somehow.
  
	// Don't act if we're already doing something.
  
	if (zoomActive[theID] != true) {
  
		// Clear everything out just in case something is already open
     
		document.getElementById("ShadowBox").style.visibility = "hidden";
		document.getElementById("ZoomClose").style.visibility = "hidden";     
     
		// Set the CAPTION if turned on
  
		if (includeCaption == 1) {
			zoomcap  = document.getElementById(theCap);
			zoomcapd = document.getElementById(theCapDiv);
       
			if (from.getAttribute('title') && includeCaption == 1) {
				zoomcapd.style.display = 'block';
				zoomcap.innerHTML = from.getAttribute('title');
			} else {
				zoomcapd.style.display = 'none';
			}
       
		}   
     
		// Store original position in an array for future zoomOut.

		zoomOrigW[theID] = startW;
		zoomOrigH[theID] = startH;
		zoomOrigX[theID] = hostX;
		zoomOrigY[theID] = hostY;
     
		// Now set the starting dimensions
     
		zoomimg.style.width = startW + 'px';
		zoomimg.style.height = startH + 'px';
		zoomdiv.style.left = hostX + 'px';
		zoomdiv.style.top = hostY + 'px';
     
		// Show the zoom box, make it invisible
     
		if (fade == 1) {
			setOpacity(0, zoomID);
		}
		zoomdiv.style.visibility = "visible";
  
		// If it's too big to fit in the window, shrink the width and height to fit (with ratio).
  
		sizeRatio = endW / endH;
		if (endW > myWidth - minBorder) {
			endW = myWidth - minBorder;
			endH = endW / sizeRatio;
		}
		if (endH > myHeight - minBorder) {
			endH = myHeight - minBorder;
			endW = endH * sizeRatio;
		}

		zoomChangeX = ((myWidth / 2) - (endW / 2) - hostX);
		zoomChangeY = (((myHeight / 2) - (endH / 2) - hostY) + myScroll);
		zoomChangeW = (endW - startW);
		zoomChangeH = (endH - startH);
		
		// console.log("X: "+zoomChangeX+" Y: "+zoomChangeY+" W: "+zoomChangeW+" H: "+zoomChangeH);

		// Setup Zoom
     
		zoomCurrent = 0;
     
		// Setup Fade with Zoom, If Requested
     
		if (fade == 1) {
			fadeCurrent = 0;
			fadeAmount = (0 - 100) / zoomSteps;
		} else {
			fadeAmount = 0;
		}
       
		// Do It!
		
		zoomTimer[theID] = setInterval("zoomElement('"+zoomID+"', '"+theID+"', "+zoomCurrent+", "+startW+", "+zoomChangeW+", "+startH+", "+zoomChangeH+", "+hostX+", "+zoomChangeX+", "+hostY+", "+zoomChangeY+", "+zoomSteps+", "+fade+", "+fadeAmount+", 'zoomDoneIn(zoomID)')", zoomTime);		
		zoomActive[theID] = true; 
	}
}

// Zoom it back out.

function zoomOut() {

	// Check to see if something is happening/open
  
	if (zoomActive[theID] != true) {
     
		// First, get rid of the shadow if necessary
     
		document.getElementById("ShadowBox").style.visibility = "hidden";
		document.getElementById("ZoomClose").style.visibility = "hidden";
     
		// Now, figure out where we came from, to get back there

		startX = parseInt(zoomdiv.style.left);
		startY = parseInt(zoomdiv.style.top);
		startW = zoomimg.width;
		startH = zoomimg.height;
		zoomChangeX = zoomOrigX[theID] - startX;
		zoomChangeY = zoomOrigY[theID] - startY;
		zoomChangeW = zoomOrigW[theID] - startW;
		zoomChangeH = zoomOrigH[theID] - startH;
    
		// Setup Zoom
	   
		zoomCurrent = 0;
     
		// Setup Fade with Zoom, If Requested
     
		if (fade == 1) {
			fadeCurrent = 0;
			fadeAmount = (100 - 0) / zoomSteps;
		} else {
			fadeAmount = 0;
		}
     
		// Do It!

		zoomTimer[theID] = setInterval("zoomElement('"+zoomID+"', '"+theID+"', "+zoomCurrent+", "+startW+", "+zoomChangeW+", "+startH+", "+zoomChangeH+", "+startX+", "+zoomChangeX+", "+startY+", "+zoomChangeY+", "+zoomSteps+", "+fade+", "+fadeAmount+", 'zoomDone(zoomID, theID)')", zoomTime);	
		zoomActive[theID] = true;
   }
}

// Finished Zooming In

function zoomDoneIn(zoomdiv, theID) {

	// Note that it's open
  
	zoomOpen = true;

	// Make sure they are gone

	setOpacity(0, "ShadowBox");
	setOpacity(0, "ZoomClose");

	// Position the shadow behind the zoomed in image.

	zoomdiv = document.getElementById(zoomdiv);
	shadowdiv = document.getElementById("ShadowBox");
 
	shadowLeft = parseInt(zoomdiv.style.left) - 13;
	shadowTop = parseInt(zoomdiv.style.top) - 8;
	shadowWidth = zoomdiv.offsetWidth + 26;
	shadowHeight = zoomdiv.offsetHeight + 26; 

	shadowdiv.style.width = shadowWidth + 'px';
	shadowdiv.style.height = shadowHeight + 'px';
	shadowdiv.style.left = shadowLeft + 'px';
	shadowdiv.style.top = shadowTop + 'px';
  
	// Display Shadow and Zoom
  
	document.getElementById("ShadowBox").style.visibility = "visible";
	fadeElementSetup("ShadowBox", 0, 100, 5);
	document.getElementById("ZoomClose").style.visibility = "visible";
	fadeElementSetup("ZoomClose", 0, 100, 5);
  
}

// Finished Zooming Out

function zoomDone(zoomdiv, theID) {

	// No longer open
  
	zoomOpen = false;

	// Clear stuff out, clean up

	zoomOrigH[theID] = "";
	zoomOrigW[theID] = "";
	document.getElementById(zoomdiv).style.visibility = "hidden";
	zoomActive[theID] == false;
}

// Actually zoom the element

function zoomElement(zoomdiv, theID, zoomCurrent, zoomStartW, zoomChangeW, zoomStartH, zoomChangeH, zoomStartX, zoomChangeX, zoomStartY, zoomChangeY, zoomSteps, fade, fadeAmount, execWhenDone) {

	// console.log("Zooming Step #"+zoomCurrent+ " of "+zoomSteps+" (zoom " + zoomStartW + "/" + zoomChangeW + ") (zoom " + zoomStartH + "/" + zoomChangeH + ")  (zoom " + zoomStartX + "/" + zoomChangeX + ")  (zoom " + zoomStartY + "/" + zoomChangeY + ") Fade: "+fadeAmount);
    
	// Test if we're done, or if we continue

	if (zoomCurrent == (zoomSteps + 1)) {
		zoomActive[theID] = false;
		clearInterval(zoomTimer[theID]);

		if (execWhenDone != "") {
			eval(execWhenDone);
		}
	} else {
	
		// Do the Fade!
	  
		if (fade != 0) {
			if (fadeAmount < 0) {
				setOpacity(Math.abs(zoomCurrent * fadeAmount), zoomdiv);
			} else {
				setOpacity(100 - (zoomCurrent * fadeAmount), zoomdiv);
			}
		}
	  
		// Calculate this step's difference, and move it!
		
		moveW = cubicInOut(zoomCurrent, zoomStartW, zoomChangeW, zoomSteps);
		moveH = cubicInOut(zoomCurrent, zoomStartH, zoomChangeH, zoomSteps);
		moveX = cubicInOut(zoomCurrent, zoomStartX, zoomChangeX, zoomSteps);
		moveY = cubicInOut(zoomCurrent, zoomStartY, zoomChangeY, zoomSteps);
	
		document.getElementById(zoomdiv).style.left = moveX + 'px';
		document.getElementById(zoomdiv).style.top = moveY + 'px';
		zoomimg.style.width = moveW + 'px';
		zoomimg.style.height = moveH + 'px';
	
		zoomCurrent++;
		
		clearInterval(zoomTimer[theID]);
		zoomTimer[theID] = setInterval("zoomElement('"+zoomdiv+"', '"+theID+"', "+zoomCurrent+", "+zoomStartW+", "+zoomChangeW+", "+zoomStartH+", "+zoomChangeH+", "+zoomStartX+", "+zoomChangeX+", "+zoomStartY+", "+zoomChangeY+", "+zoomSteps+", "+fade+", "+fadeAmount+", '"+execWhenDone+"')", zoomTime);
	}
}

// Zoom Rollover Functions
// To be re-added

function zoomMouseOver() {
//  if (rollOverImg) {
//     if (document.getElementById("ZoomImage").src != rollOverImg) {
//        document.getElementById("ZoomImage").src = rollOverImg;
//     }
//  }
}

function zoomMouseOut() {
//  if (rollOverImg) {
//     if (document.getElementById("ZoomImage").src != image) {
//        document.getElementById("ZoomImage").src = image;
//     }
//  }
}

////////////////////////////
//
// FADE Functions
//

function fadeOut(elem) {
	if (elem.id) {
		fadeElementSetup(elem.id, 100, 0, 10);
	}
}

function fadeIn(elem) {
	if (elem.id) {
		fadeElementSetup(elem.id, 0, 100, 10);	
	}
}

// Fade: Initialize the fade function

var fadeActive = new Array();
var fadeQueue  = new Array();
var fadeTimer  = new Array();
var fadeClose  = new Array();

function fadeElementSetup(theID, fdStart, fdEnd, fdSteps, fdClose) {

	if (fadeActive[theID] == true) {
		// Already animating, queue up this command
		fadeQueue[theID] = new Array(theID, fdStart, fdEnd, fdSteps);
	} else {
		fadeSteps = fdSteps;
		fadeCurrent = 0;
		fadeAmount = (fdStart - fdEnd) / fadeSteps;
		fadeTimer[theID] = setInterval("fadeElement('"+theID+"', '"+fadeCurrent+"', '"+fadeAmount+"', '"+fadeSteps+"')", 15);
		fadeActive[theID] = true;
		if (fdClose == 1) {
			fadeClose[theID] = true;
		} else {
			fadeClose[theID] = false;
		}
	}
}

// Fade: Do the fade. This function will call itself, modifying the parameters, so
// many instances can run concurrently.

function fadeElement(theID, fadeCurrent, fadeAmount, fadeSteps) {
  
	if (fadeCurrent == fadeSteps) {

	    // We're done, so clear.
	    clearInterval(fadeTimer[theID]);
	    fadeActive[theID] = false;
	    
	    // Should we close it?
	    
	    if (fadeClose[theID] == true) {
	    	document.getElementById(theID).style.visibility = "hidden";
	    }
	    
	    // Hang on.. did a command queue while we were working? If so, make it happen now
	    
	    if (fadeQueue[theID] && fadeQueue[theID] != false) {
	    	fadeElementSetup(fadeQueue[theID][0], fadeQueue[theID][1], fadeQueue[theID][2], fadeQueue[theID][3]);
	    	fadeQueue[theID] = false;
	    }  
  
	} else {
  
		fadeCurrent++;
		
		// Set the opacity depending on if we're adding or subtracting (pos or neg)
		if (fadeAmount < 0) {
			setOpacity(Math.abs(fadeCurrent * fadeAmount), theID);
		} else {
			setOpacity(100 - (fadeCurrent * fadeAmount), theID);
		}
		
		// Keep going, and send myself the updated variables
		clearInterval(fadeTimer[theID]);
		fadeTimer[theID] = setInterval("fadeElement('"+theID+"', '"+fadeCurrent+"', '"+fadeAmount+"', '"+fadeSteps+"')", 15);
	}
}

////////////////////////////
//
// UTILITY functions
//

// Utility: Set the opacity, compatible with a number of browsers. Value from 0 to 100.

function setOpacity(opacity, theID) {

	var object = document.getElementById(theID).style;

	// If it's 100, set it to 99 for Firefox.

	if (navigator.userAgent.indexOf("Firefox") != -1) {
		if (opacity == 100) { opacity = 99.9999; } // This is majorly retarded
	}

	// Multi-browser opacity setting

	object.filter = "alpha(opacity=" + opacity + ")"; // IE/Win
	//object.KhtmlOpacity = (opacity / 100);            // Safari 1.1 or lower, Konqueror
	//object.MozOpacity = (opacity / 100);              // Older Mozilla+Firefox
	object.opacity = (opacity / 100);                 // Safari 1.2, Firefox+Mozilla

}

// Utility: Math functions for animation calucations - From http://www.robertpenner.com/easing/
//
// t = time, b = begin, c = change, d = duration
// time = current frame, begin is fixed, change is basically finish - begin, duration is fixed (frames),

function linear(t, b, c, d)
{
	return c*t/d + b;
}

function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

function cubicIn(t, b, c, d) {
	return c*(t/=d)*t*t + b;
}

function cubicOut(t, b, c, d) {
	return c*((t=t/d-1)*t*t + 1) + b;
}

function cubicInOut(t, b, c, d)
{
	if ((t/=d/2) < 1) return c/2*t*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;
}

function bounceOut(t, b, c, d)
{
	if ((t/=d) < (1/2.75)){
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)){
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)){
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
}


// Utility: Get the size of the window, and set myWidth and myHeight

function getSize() {
	if (document.all) {
		// IE4+ or IE6+ in standards compliant 
		myWidth  = (document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth;
		myHeight = (document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight;
		myScroll = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	} else {
		// Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		myScroll = window.pageYOffset;
	}
	
	// Core code from - quirksmode.org
    if (window.innerHeight && window.scrollMaxY) {	
        myScrollWidth = document.body.scrollWidth;
		myScrollHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
		myScrollWidth = document.body.scrollWidth;
		myScrollHeight = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		myScrollWidth = document.body.offsetWidth;
		myScrollHeight = document.body.offsetHeight;
	}
}

// Utility: Find the Y position of an element on a page. Return Y and X as an array

function findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	//console.log("Found element "+elemFind+" at "+elemY+"/"+elemX);

	return Array(elemX, elemY);
}




//
// ----- DISPLAY CODE -----
//

function insertZoomHTML() {

  // All of this junk creates the three <div>'s used to hold the closebox, image, and zoom shadow.
  // It's a lot of Javascript for a little HTML. But, hey, this must be the "right way", though, yeah?
  // I'd also like to ask forgiveness for the large amounts of tables in use. Sometimes, CSS just doesn't cut it... I think.

  var inBody = document.getElementsByTagName("body").item(0);

  // WAIT SPINNER
  
  var inSpinbox = document.createElement("div");
  inSpinbox.setAttribute('id', 'ZoomSpin');
  inSpinbox.style.position = 'absolute';
  inSpinbox.style.left = '10px';
  inSpinbox.style.top = '10px';
  inSpinbox.style.visibility = 'hidden';
  inSpinbox.style.zIndex = '5000';
  inBody.insertBefore(inSpinbox, inBody.firstChild);
  
  var inSpinImage = document.createElement("img");
  inSpinImage.setAttribute('id', 'SpinImage');
  inSpinImage.setAttribute('src', zoomImageURI+'zoom-spin-1.png');
  inSpinbox.appendChild(inSpinImage);

  // ZOOM IMAGE
  //
  // <div id="ZoomBox">
  //   <a href="javascript:zoomOut();"><img src="/images/spacer.gif" id="ZoomImage" border="0"></a> <!-- THE IMAGE -->
  //   <div id="ZoomClose">
  //     <a href="javascript:zoomOut();"><img src="/images/closebox.png" width="30" height="30" border="0"></a>
  //   </div>
  // </div>

  var inZoombox = document.createElement("div");
  inZoombox.setAttribute('id', 'ZoomBox');
  inZoombox.style.position = 'absolute'; 
  inZoombox.style.left = '10px';
  inZoombox.style.top = '10px';
  inZoombox.style.visibility = 'hidden';
  inZoombox.style.zIndex = '499';
  // inZoombox.style.background = 'white';           // DEBUG
  inBody.insertBefore(inZoombox, inSpinbox.nextSibling);

  var inLink1 = document.createElement("a");
  inLink1.setAttribute('href','javascript:zoomOut();');
  inZoombox.appendChild(inLink1);
  
  var inImage1 = document.createElement("img");
  inImage1.setAttribute('src',zoomImageURI+'spacer.gif');
  inImage1.setAttribute('id','ZoomImage');
  inImage1.setAttribute('border', '0');
  inImage1.setAttribute('onMouseOver', 'zoomMouseOver();')
  inImage1.setAttribute('onMouseOut', 'zoomMouseOut();')
  inImage1.style.display = 'block';
  inImage1.style.width = '10px';
  inImage1.style.height = '10px';
  inLink1.appendChild(inImage1);

  var inClosebox = document.createElement("div");
  inClosebox.setAttribute('id', 'ZoomClose');
  inClosebox.style.position = 'absolute';
  inClosebox.style.left = '-15px';
  inClosebox.style.top = '-15px';
  inClosebox.style.filter = 'alpha(opacity=0)';
  inClosebox.style.MozOpacity = '0';
  inClosebox.style.opacity = '0';
  inClosebox.style.visibility = 'hidden';
  inZoombox.appendChild(inClosebox);
  
  var inLink2 = document.createElement("a");
  inLink2.setAttribute('href','javascript:zoomOut(1);');
  inClosebox.appendChild(inLink2);
  var inImage2 = document.createElement("img");
  inImage2.setAttribute('src',zoomImageURI+'closebox.png');
  inImage2.setAttribute('width','30');
  inImage2.setAttribute('height','30');
  inImage2.setAttribute('border','0');
  inLink2.appendChild(inImage2);
  
  // SHADOW
  // Now, the.. shudder.. shadow table.
  
  // <div id="ShadowBox"><table border="0" width="100%" height="100%" cellpadding="0" cellspacing="0"> X
  //   <tr height="25">
  //   <td width="27"><img src="/images/zoom-shadow1.png" width="27" height="25"></td>
  //   <td background="/images/zoom-shadow2.png">&nbsp;</td>
  //   <td width="27"><img src="/images/zoom-shadow3.png" width="27" height="25"></td>
  //   </tr>

  var inShadowbox = document.createElement("div");
  inShadowbox.setAttribute('id', 'ShadowBox');
  inShadowbox.style.position = 'absolute'; 
  inShadowbox.style.left = '50px';
  inShadowbox.style.top = '50px';
  inShadowbox.style.width = '100px';
  inShadowbox.style.height = '100px';
  inShadowbox.style.visibility = 'hidden';
  inShadowbox.style.zIndex = '45';
  inBody.insertBefore(inShadowbox, inZoombox.nextSibling);
 
  var inTable = document.createElement("table");
  inTable.setAttribute('border', '0');
  inTable.setAttribute('width', '100%');
  inTable.setAttribute('height', '100%');
  inTable.setAttribute('cellpadding', '0');
  inTable.setAttribute('cellspacing', '0');
  inShadowbox.appendChild(inTable);
  
  var inRow1 = document.createElement("tr");
  inRow1.style.height = '25px';
  inTable.appendChild(inRow1);
  
  var inCol1 = document.createElement("td");
  inCol1.style.width = '27px';
  inRow1.appendChild(inCol1);  
  var inShadowImg1 = document.createElement("img");
  inShadowImg1.setAttribute('src', zoomImageURI+'zoom-shadow1.png');
  inShadowImg1.setAttribute('width', '27');
  inShadowImg1.setAttribute('height', '25');
  inShadowImg1.style.display = 'block';
  inCol1.appendChild(inShadowImg1);

  var inCol2 = document.createElement("td");
  inCol2.setAttribute('background', zoomImageURI+'zoom-shadow2.png');
  inRow1.appendChild(inCol2);
  // inCol2.innerHTML = '<img src=';
  var inSpacer1 = document.createElement("img");
  inSpacer1.setAttribute('src',zoomImageURI+'spacer.gif');
  inSpacer1.setAttribute('height', '1');
  inSpacer1.setAttribute('width', '1');
  inSpacer1.style.display = 'block';
  inCol2.appendChild(inSpacer1);

  var inCol3 = document.createElement("td");
  inCol3.style.width = '27px';
  inRow1.appendChild(inCol3);  
  var inShadowImg3 = document.createElement("img");
  inShadowImg3.setAttribute('src', zoomImageURI+'zoom-shadow3.png');
  inShadowImg3.setAttribute('width', '27');
  inShadowImg3.setAttribute('height', '25');
  inShadowImg3.style.display = 'block';
  inCol3.appendChild(inShadowImg3);

  //   <tr>
  //   <td background="/images/zoom-shadow4.png">&nbsp;</td>
  //   <td bgcolor="#ffffff">&nbsp;</td>
  //   <td background="/images/zoom-shadow5.png">&nbsp;</td>
  //   </tr>

  inRow2 = document.createElement("tr");
  inTable.appendChild(inRow2);
  
  var inCol4 = document.createElement("td");
  inCol4.setAttribute('background', zoomImageURI+'zoom-shadow4.png');
  inRow2.appendChild(inCol4);
  // inCol4.innerHTML = '&nbsp;';
  var inSpacer2 = document.createElement("img");
  inSpacer2.setAttribute('src',zoomImageURI+'spacer.gif');
  inSpacer2.setAttribute('height', '1');
  inSpacer2.setAttribute('width', '1');
  inSpacer2.style.display = 'block';
  inCol4.appendChild(inSpacer2);
  
  var inCol5 = document.createElement("td");
  inCol5.setAttribute('bgcolor', '#ffffff');
  inRow2.appendChild(inCol5);
  // inCol5.innerHTML = '&nbsp;';
  var inSpacer3 = document.createElement("img");
  inSpacer3.setAttribute('src',zoomImageURI+'spacer.gif');
  inSpacer3.setAttribute('height', '1');
  inSpacer3.setAttribute('width', '1');
  inSpacer3.style.display = 'block';
  inCol5.appendChild(inSpacer3);
  
  var inCol6 = document.createElement("td");
  inCol6.setAttribute('background', zoomImageURI+'zoom-shadow5.png');
  inRow2.appendChild(inCol6);
  // inCol6.innerHTML = '&nbsp;';
  var inSpacer4 = document.createElement("img");
  inSpacer4.setAttribute('src',zoomImageURI+'spacer.gif');
  inSpacer4.setAttribute('height', '1');
  inSpacer4.setAttribute('width', '1');
  inSpacer4.style.display = 'block';
  inCol6.appendChild(inSpacer4);

  //   <tr height="26">
  //   <td width="27"><img src="/images/zoom-shadow6.png" width="27" height="26"</td>
  //   <td background="/images/zoom-shadow7.png">&nbsp;</td>
  //   <td width="27"><img src="/images/zoom-shadow8.png" width="27" height="26"></td>
  //   </tr>  
  // </table>

  var inRow3 = document.createElement("tr");
  inRow3.style.height = '26px';
  inTable.appendChild(inRow3);
  
  var inCol7 = document.createElement("td");
  inCol7.style.width = '27px';
  inRow3.appendChild(inCol7);
  var inShadowImg7 = document.createElement("img");
  inShadowImg7.setAttribute('src', zoomImageURI+'zoom-shadow6.png');
  inShadowImg7.setAttribute('width', '27');
  inShadowImg7.setAttribute('height', '26');
  inShadowImg7.style.display = 'block';
  inCol7.appendChild(inShadowImg7);

  var inCol8 = document.createElement("td");
  inCol8.setAttribute('background', zoomImageURI+'zoom-shadow7.png');
  inRow3.appendChild(inCol8);  
  // inCol8.innerHTML = '&nbsp;';
  var inSpacer5 = document.createElement("img");
  inSpacer5.setAttribute('src',zoomImageURI+'spacer.gif');
  inSpacer5.setAttribute('height', '1');
  inSpacer5.setAttribute('width', '1');
  inSpacer5.style.display = 'block';
  inCol8.appendChild(inSpacer5);

  var inCol9 = document.createElement("td");
  inCol9.style.width = '27px';
  inRow3.appendChild(inCol9);  
  var inShadowImg9 = document.createElement("img");
  inShadowImg9.setAttribute('src', zoomImageURI+'zoom-shadow8.png');
  inShadowImg9.setAttribute('width', '27');
  inShadowImg9.setAttribute('height', '26');
  inShadowImg9.style.display = 'block';
  inCol9.appendChild(inShadowImg9);

  if (includeCaption == 1) {

    // CAPTION
    //
    // <table border="1" cellpadding="0" cellspacing="0">
    // <tr height="26">
    // <td><img src="zoom-caption-l.png" width="13" height="26"></td>
    // <td rowspan="3" background="zoom-caption-fill.png"><div id="ZoomCaption"></div></td>
    // <td><img src="zoom-caption-r.png" width="13" height="26"></td>
    // </tr>
    // </table>
    
    var inCapDiv = document.createElement("div");
    inCapDiv.setAttribute('id', 'ZoomCapDiv');
    inCapDiv.style.display = 'none';
    inCapDiv.style.marginLeft = '13px';
    inCapDiv.style.marginRight = '13px';
    inShadowbox.appendChild(inCapDiv);
    
    var inCapTable = document.createElement("table");
    inCapTable.setAttribute('border', '0');
    inCapTable.setAttribute('cellpadding', '0');
    inCapTable.setAttribute('cellspacing', '0');
    inCapTable.setAttribute('align', 'center');
    inCapDiv.appendChild(inCapTable);
    
    var inCapRow1 = document.createElement("tr");
    inCapTable.appendChild(inCapRow1);
  
    var inCapCol1 = document.createElement("td");
    inCapRow1.appendChild(inCapCol1);
    var inCapImg1 = document.createElement("img");
    inCapImg1.setAttribute('src', zoomImageURI+'zoom-caption-l.png');
    inCapImg1.setAttribute('width', '13');
    inCapImg1.setAttribute('height', '26');
    inCapImg1.style.display = 'block';
    inCapCol1.appendChild(inCapImg1);
  
    var inCapCol2 = document.createElement("td");
    inCapCol2.setAttribute('background', zoomImageURI+'zoom-caption-fill.png');
    inCapCol2.setAttribute('id', 'ZoomCaption');
    inCapCol2.setAttribute('valign', 'middle');
    
    inCapCol2.style.fontSize = '14px';
    inCapCol2.style.fontFamily = 'Helvetica';
    inCapCol2.style.fontWeight = 'bold';
    inCapCol2.style.color = '#ffffff';
    inCapCol2.style.textShadow = '0px 2px 4px #000000';
    inCapCol2.style.whiteSpace = 'nowrap';
    
    inCapRow1.appendChild(inCapCol2);
  
    var inCapCol3 = document.createElement("td");
    inCapRow1.appendChild(inCapCol3);
    var inCapImg2 = document.createElement("img");
    inCapImg2.setAttribute('src', zoomImageURI+'zoom-caption-r.png');
    inCapImg2.setAttribute('width', '13');
    inCapImg2.setAttribute('height', '26');
    inCapImg2.style.display = 'block';
    inCapCol3.appendChild(inCapImg2);

  }
}


//
// CodaEffects.js - (C) 2007 Panic, Inc.
//
// Used to scroll our tabbed view, display our download popup, and display the giant dialog.
// Requires: Effects.js
//
// Not for redistribution.

// Scroll: Setup Scrolling Stuff

var currentSection = "sites-pane"; // The default loaded section on the page
var tabTag = "-tab";
var paneTag = "-pane";

// Scroll the page manually to the position of element "link", passed to us.

function ScrollSection(link, scrollArea, offset)
{

	// Store the last section, and update the current section

	if (currentSection == link) {
		return;
	}
	lastSection = currentSection;
	currentSection = link;
	
	// Change the section highlight.
	// Extract the root section name, and use that to change the background image to 'top', revealing the alt. state

    sectionTab = currentSection.split("-")[0] + tabTag;
    document.getElementById(sectionTab).className = "active";
    if (lastSection) {
	    lastTab = lastSection.split("-")[0] + tabTag;
	    document.getElementById(lastTab).className = "inactive";
	}
    
	// Get the element we want to scroll, get the position of the element to scroll to
	
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(link));

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
	// return false;
}

// Scroll the page using the arrows

function ScrollArrow(direction, toolbar, scrollArea, offset) {

	toolbarElem = document.getElementById(toolbar);
	toolbarNames = new Array();
    
	// Find all the <li> elements in the toolbar, and extract their id's into an array.
    
	if (toolbarElem.hasChildNodes())
	{
		var children = toolbarElem.childNodes;
		for (var i = 0; i < children.length; i++) 
		{
			if (toolbarElem.childNodes[i].tagName == "LI") {
				toolbarNames.push(toolbarElem.childNodes[i].id.split("-")[0]);
			}
		}
	}

	// Now iterate through our array of tab names, find matches, and determine where to go.

	for (var i = 0; i < toolbarNames.length; i++) {
		if (toolbarNames[i] == currentSection.split("-")[0]) {
			if (direction == "left") {
				if (i - 1 < 0) {
					gotoTab = toolbarNames[toolbarNames.length - 1];
				} else {
					gotoTab = toolbarNames[i - 1];
				}
			} else {
				if ((i + 1) > (toolbarNames.length - 1)) {
					gotoTab = toolbarNames[0];
				} else {
					gotoTab = toolbarNames[i + 1];
				}
			}
		}
	}
	
	// Go to the section name!
	
	ScrollSection(gotoTab+paneTag, scrollArea, offset);

}

//
// Animated Scroll Functions
// Scrolls are synchronous -- only one at a time.
//

var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(elem, start, end, direction)
{
	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 25;
	scrollanim.element = elem;
	
	if (direction == "horiz") {
		scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	}
	else {
		scrollanim.timer = setInterval("scrollVertAnim();", 15);
	}
}

function scrollVertAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollTop = move; 
		scrollanim.time++;
	}
}

function scrollHorizAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}

//
// LARGE POPUP: Full-Screen Pop-up Functions
//

function showLargePopup(elem) {

    var popFullscreen = document.getElementById('fullscreen');
    var popLarge = document.getElementById('largepopup');

	// Put the correct content in the pop-up

	//if (navigator.platform.indexOf('Mac') != -1) {
	//	document.getElementById('start-download').className = 'show';
	//	document.getElementById('wrong-os').className = 'hide';
	//} else {
	//	document.getElementById('start-download').className = 'hide';
	//	document.getElementById('wrong-os').className = 'show';
	//}
 
 	document.getElementById('start-download').className = 'show';
	// document.getElementById('wrong-os').className = 'hide';
    
    // Make fullscreen thing really full screen, and show it
    getSize();
    popFullscreen.style.height = myScrollHeight + 'px';
    popFullscreen.style.display = 'block';
	
    // Position pop-up
    popLarge.style.left = ((myWidth - popLarge.offsetWidth) / 2) + 'px';
    popLarge.style.top = (((myHeight - popLarge.offsetHeight) / 2) + myScroll) + 'px';
    popLarge.style.visibility = 'visible';
    
    refreshTimer = setTimeout("setLocation('"+elem.getAttribute("href")+"')", 1500);

}

function setLocation(loc) {
	window.location = loc;
}

function hideLargePopup() {
    var popFullscreen = document.getElementById('fullscreen');
    var popLarge = document.getElementById('largepopup');
    
    popLarge.style.visibility = 'hidden';
    popFullscreen.style.display = 'none';
}

//
// DOWNLOAD SMALL POPUP: Download Hint Pop-up Functions
//
// An advantage of using a timer to do a hide is that we can ignore
// any spurious mouseOut events that have bubbled up, into <td>'s, etc.

var dpopTimer = "";

function showDownloadPopup(e) {
	var popDownload = document.getElementById('dpop');
	var btnDownload = document.getElementById('download');

	if (moveanim.timer != null) {
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	}

	// Determine where we should pop up in relation to the download button

	position = findElementPos(btnDownload);
	popDownload.style.top = (position[1] - (popDownload.offsetHeight - 40)) +"px";
	popDownload.style.left = "5" + "px";

	// If already trigger a rollover, cancel it because we're back in

	if (dpopTimer != "")
	{
		clearTimeout(dpopTimer);
		dpopTimer = "";
	} else {
		setOpacity(0, 'dpop');
		popDownload.style.visibility = 'visible';
		moveStart(popDownload, parseInt(popDownload.style.left), parseInt(popDownload.style.left), parseInt(popDownload.style.top) + 10, parseInt(popDownload.style.top), 15);
		fadeElementSetup('dpop', 0, 100, 13);
	}
}

function hideDownloadPopup() {
	// Start timer to hide the pop-up and the overlay
	dpopTimer = setTimeout("actuallyHide()", 500);
}

function actuallyHide() {
	var popDownload = document.getElementById('dpop');
	if (dpopTimer != "")
	{
		dpopTimer = "";
		moveStart(popDownload, parseInt(popDownload.style.left), parseInt(popDownload.style.left), parseInt(popDownload.style.top), parseInt(popDownload.style.top) - 10, 15);		
		fadeElementSetup('dpop', 100, 0, 13, 1);
	}
}

//
// MOVE: Animate the move of an element.
//
// Move is also synchronous. One at a time, please.
//

var moveanim = {time:0, beginX:0, changeX:0.0, beginY:0, changeY:0, duration:0.0, element:null, timer:null};

function moveStart(elem, startX, endX, startY, endY, duration)
{
	if (moveanim.timer != null) {
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	}
	moveanim.time = 0;
	moveanim.beginX = startX;
	moveanim.changeX = endX - startX;
	moveanim.beginY = startY;
	moveanim.changeY = endY - startY;
	moveanim.duration = duration;
	moveanim.element = elem;

	moveanim.timer = setInterval("moveAnimDo();", 15);
}

function moveAnimDo()
{
	if (moveanim.time > moveanim.duration) {
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	}
	else {
		moveX = cubicOut(moveanim.time, moveanim.beginX, moveanim.changeX, moveanim.duration);
		moveY = cubicOut(moveanim.time, moveanim.beginY, moveanim.changeY, moveanim.duration);
		moveanim.element.style.left = moveX + "px";
		moveanim.element.style.top = moveY + "px";
		moveanim.time++;
	}
}


//console.log("Initialized");

