var g_pageIsReady = false;
Event.observe( window, 'load', function(e){
	g_pageIsReady = true;
});

// Something we want to do on page load
// If the page is already loaded, do it now
// Otherwise, add an event listener to the page, then do it
function DoOrWaitForLoad( func )
{
	if (g_pageIsReady == true) {
		func();
	}
	else {
		Event.observe( window, 'load', func );
	}
}

var g_imagesToLoad = 0;

function debugPrint( text )
{
	$('debug').innerHTML = $('debug').innerHTML + "<br />" + text;
}

//
// Add some methods to the DOM elements
//
Element.addMethods( {

	// Set positions depending on what's specified
	setPosition: function( element, data )
	{
		['left','top','right','bottom'].each(function(p){
			if (data[p] != undefined)
			{
				element.setStyle(p+':'+data[p]+'px');
				
			}
		});
	},
	
	// Set dimensions of element
	setDimensions: function( element, width, height )
	{
		element.setStyle({ width: Math.floor(width) + 'px', height: Math.floor(height) + 'px' });
	}
});

function newImage( src, style, options )
{
	// default options
	options = Object.extend( {
		hide: false
	}, options || {} );
	
	var isPng = src.toLowerCase().match(/\.png$/i) == ".png";
	//(src.length >= 4 && src.substring( src.length - 4 ).toLowerCase() == '.png');
	var isIE  = Prototype.Browser.IE;
	
	var node = null;
	if (isPng && isIE)
	{
		// create a span
		node = $(Builder.node('div'));
		
		// We need to load in the image to get the width and height info
		var tempImg = $(Builder.node('img'));
		
		// If we don't set visibility to hidden then IE won't actually load the image
		// and report dimensions as zero
		tempImg.setStyle({visibility: 'hidden'});
		document.body.appendChild(tempImg);
		
		Event.observe( tempImg, 'load', function(){
			node.setStyle({width: this.width+'px', height: this.height+'px'});
			tempImg.removeNode();
		});
		tempImg.src = src;
		
		// This crazy ie-specific effect allows transparent PNGs as backgrounds of spans
		node.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
	}
	else
	{
		// All other browsers handle PNG transparency just fine.
		// Create a normal image
		node = $(Builder.node('img'));
		node.src = src;
	}
	
	// Set style and apply options
	if (style)
		node.setStyle( style );
		
	if (options)
	{
		if (options.hide == true)
		{
			node.hide();
		}
	}

	return node;
}

// Simple image loader
var ImageLoader = Class.create({

	imgList: [],
	imagesToLoad: 0,
	fireCallback: false,
	options: null,
	loadingSemaphore:0,
	
	initialize: function( options ){
		this.options = options;
	},
	
	add: function( src ){
		img = Builder.node('img');
		var that = this;
		++this.imagesToLoad;
		
		//debugPrint('adding image ' + src + this.imagesToLoad);
		
		// Register event listeners
		Event.observe( img, 'load', function(){
			++that.loadingSemaphore;
			that.imgOnLoad(this);
			--that.loadingSemaphore;
		});
		Event.observe( img, 'error', function(){
			that.imgOnError(this);
		});
				
		// Add to image array to keep img from going out of scope
		this.imgList.push( img );
		
		// Begin load
		img.src = src;
	},
	
	preload: function(){
		//debugPrint( "Preload called" );
		while (this.loadingSemaphore > 0)
		{
			// do nothing
		}
		//debugPrint( "Preload semaphore passed" );
		
		this.fireCallback = true;
		
		//debugPrint( "Images to load = " + this.imagesToLoad);
		if (this.imagesToLoad == 0){
			// Images already loaded, fire finishing callback now
			//debugPrint( "All images ready; finishing" );
			this.onFinish();
		}
		else
		{
			//debugPrint("Images remain; deferring load" );
		}
	},
	
	imgOnLoad: function(img){
		if (this.options.onLoad){
			this.options.onLoad(img);
		}
		this.imgDone(img);
	},
	
	imgOnError: function(img){
		if (this.options.onError){
			this.options.onError(img);
		}
		this.imgDone(img);
	},
	
	imgDone: function(img){
		if (--this.imagesToLoad == 0 && this.fireCallback == true)
		{
			this.onFinish();
		}
	},
	
	onFinish: function(){	
		if (this.options.onFinish){
			this.options.onFinish();
		}
	}


});

// -- From lightbox:	
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}