// var

var initCalled = false;


// init

if (! initCalled)
	init();

function init() {
	zindexStart = 10;
	initCalled  = true;
}


// internal functions

/**
 *  Out of:
 *  Lightbox v2.02
 *	by Lokesh Dhakar - http://www.huddletogether.com
 *	3/31/06
 * @access public
 * @return array
 **/
function getPageSize() {
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
	return arrayPageSize;
}


// external called functions

/**
 * Set background height (IE does not support CSS property "height" to be set to 100%.
 * @access public
 * @return void
 **/
function setFadeContainer() {
	var pageSize = getPageSize();
	window.document.getElementById('fadeContainer').style.height = pageSize[1] + 'px';
}


// effect functions

// -----------------------------------
/**
 Toggle Original
 */
function setup_toggleOriginal(element, effect) {
	window.document.getElementById('fadeContainer').style.display = 'block';
	window.document.getElementById('fadeBox').style.display = 'block';
	window.document.getElementById('fadeAll').style.display = 'block';
 }

function reset_toggleOriginal(element, effect) {
	window.document.getElementById('fadeContainer').style.display = 'none';
	window.document.getElementById('fadeBox').style.display = 'none';
	window.document.getElementById('fadeAll').style.display = 'none';
}

function toggleOriginal(originalHref) {
	// variables

	var duration = 800;

	var pixelSettings = new Array(
							85,		// absolute height of bottom line and offset
							0		// image border x 2
						);

	// body

	if (originalHref != false) {
		var imgLoader = new Image();

		imgLoader.onload = function() {
			var fadeAll 	= window.document.getElementById('fadeAll');
			var fadeFile 	= window.document.getElementById('fadeFile');
			var fadeBox		= window.document.getElementById('fadeBox');

			fadeFile.src = originalHref;
			fadeFile.style.height = imgLoader.height + 'px';
			fadeFile.style.width = imgLoader.width + 'px';

			var offHeight = imgLoader.height + pixelSettings[0] /*px*/;
			var offWidth  = imgLoader.width + pixelSettings[1] /*px*/;

			fadeAll.style.height = offHeight + 'px';
			fadeAll.style.width = offWidth + 'px';
						
			if (document.documentElement && document.documentElement.clientHeight > 0) {				
				if (navigator.userAgent.search(/Safari/i) == -1)
					fadeBox.style.top = (document.documentElement.clientHeight / 2 + document.documentElement.scrollTop) + 'px';
				else
					fadeBox.style.top = (document.documentElement.clientHeight / 2 + document.body.scrollTop) + 'px';
			} else {
				fadeBox.style.top = (document.body.clientHeight / 2 + document.body.scrollTop) + 'px';
			}
						
			fadeBox.style.left = '50%';

			fadeBox.style.marginTop = '-' + Math.floor(offHeight / 2) + 'px';
			fadeBox.style.marginLeft = '-' + Math.floor(offWidth / 2) + 'px';

			fadeObj = new Spry.Effect.Fade( 'fadeContainer', { duration: duration, from: 0, to: 70, toggle: false, setup: void(0), finish:
			function finish() {
				fadeObj = new Spry.Effect.Fade('fadeAll', { duration: 10, from: 0, to: 100, toggle: false});
				fadeObj.start();

				window.document.getElementById('fadeContainer').style.visibility = 'visible';
				window.document.getElementById('fadeAll').style.visibility = 'hidden';

				fadeObj = new Spry.Effect.Grow('fadeAll', { duration: duration, from: '0%', to: '100%', toggle: true, setup: setup_toggleOriginal()});
				fadeObj.start();
			}});
			fadeObj.start();
		}

		imgLoader.src = originalHref;
	} else {
		fadeObj = new Spry.Effect.Fade('fadeContainer', { duration: duration, from: 70, to: 0, toggle: false, finish:
		function finish() {
		}});
		fadeObj.start();

		fadeObj = new Spry.Effect.Fade('fadeAll', { duration: duration, from: 100, to: 0, toggle: false, finish:
		function finish() {
			window.document.getElementById('fadeAll').style.visibility = 'hidden';
			window.document.getElementById('fadeContainer').style.visibility = 'hidden';
			reset_toggleOriginal();
		}});
		fadeObj.start();

		/*
			//
			// shrink and fade out
			//
			fadeObj = new Spry.Effect.Grow( 'fadeAll', { duration: duration, from: '100%', to: '0%', toggle: true, setup: void(0), finish:
			function finish() {
				window.document.getElementById('fadeAll').style.visibility = 'hidden';

				fadeObj = new Spry.Effect.Fade('fadeContainer', { duration: duration, from: 70, to: 0, toggle: false, finish:
				function finish() {
					window.document.getElementById('fadeContainer').style.visibility = 'hidden';
					reset_toggleOriginal();
				}});
				fadeObj.start();
			}});
			fadeObj.start();
		*/
	}

	return false;
}
