/*
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com

	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/

	Extremely modified (read: removed most of the unnecessary code)
	to simply swap images and captions

*/

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis)
{
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true)
	{
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}


//
// showSwap()
// Preloads images. Places new image in container element and displays.
//
function showSwap(objLink)
{
	// prep objects
	var objImage = document.getElementById('imgMain');
	var objCaption = document.getElementById('imgCaption');

	// preload image
	imgPreload = new Image();

	imgPreload.onload=function()
	{
		objImage.src = objLink.href;
		
		if(objLink.getAttribute('title'))
		{
			objCaption.style.display = 'block';
			objCaption.innerHTML = objLink.getAttribute('title');
		}
		
		else
		{
			objCaption.style.display = 'none';
		}
		
		// A small pause between the image loading and displaying is required with IE,
		// this prevents the previous image displaying for a short burst causing flicker.
		if (navigator.appVersion.indexOf("MSIE")!=-1)
		{
			pause(250);
		}

		return false;
	}
	imgPreload.src = objLink.href;
}

//fcn to add class name of Current and remove it from element that has it upon fcn call
function modClassName(justClicked, anchorArray)
{
	var classNameToSwap = 'currentSqr';
	
	var regEx = /\s*currentSqr/;
	
	var currentClassName;
	
	//sort thru anchorArray and remove className of 'current' from element that currently has it
	for(k = 0; k < anchorArray.length; k++)
	{
 		currentClassName = anchorArray[k].className;
		
		if(currentClassName != '')
		{
			anchorArray[k].className = (currentClassName)?currentClassName.replace(regEx,''):'';
		}
	}
	
	//add class name 'current' to clicked name to visually alter it
	//to show it's the currently selected item
	justClicked.className += ' ' + classNameToSwap;
}

//
// initSwap()
// Function runs on window load, going through link tags looking for rel="swapMe".
// These links receive onclick events that enable the image swap.
//
function initSwap()
{
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	var swapAnchors = new Array();
	var counter = 0;

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "swapMe"))
		{
			swapAnchors[counter] = anchor; //store all anchors that pass above if test into new array
			
			swapAnchors[counter].onclick = function () {showSwap(this); modClassName(this, swapAnchors); return false;}
			++counter;
		}
	}
}


//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
    	window.onload = func;
	}
	
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

addLoadEvent(initSwap);	// run initSwap onLoad