/**
 * jQuery InfoBox
 * Version 0.4 - 09/17/2007
 * @author Warren Krewenki
 *
 * Based on InfoBox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
 * Originally written to make use of the Prototype framework, and Script.acalo.us, now altered to use jQuery.
 *
 **/

var InfoBox = {
	overlayOpacity : 0.8,
	borderSize : 10,
	activeImage : null,
	inprogress : false,
	resizeSpeed : 350,
	destroyElement: function(id) {
		if(el = document.getElementById(id)){
			el.parentNode.removeChild(el);
		}
	},
	initialize: function() {
		$("a").each(function(){
			if(this.rel.toLowerCase().match('infobox')){
				$(this).click(function(){
					InfoBox.start(this);
					return false;
				});
			}
		});
		InfoBox.destroyElement('overlay');
		$('body').append($('<div>').attr('id', 'overlay').hide());
		// $("body").append('<div id="overlay" style="display: none"></div>');
		$("#overlay").click(function(){ InfoBox.end(); }).hide();
		$("#infobox").click(function(){ InfoBox.end();}).hide();
		// $('#infobox_outter_container').css({width: '485px', height: '300px;'});
	},
	
	start: function(imageLink) {	
		$("select, embed, object").hide();
		var arrayPageSize = InfoBox.getPageSize();
		$("#overlay").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : InfoBox.overlayOpacity}).fadeIn();
		InfoBox.imageArray = [];
		imageNum = 0;		
		var anchors = document.getElementsByTagName( imageLink.tagName);

		var arrayPageScroll = InfoBox.getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		var lightboxLeft = arrayPageScroll[0];
		$('#infobox').css({top: lightboxTop+'px', left: lightboxLeft+'px'}).show();
		
		$('#infobox p').css({
								width: '435px',
								position: 'relative',
								overflow: 'visible',
								padding: '0px 20px 10px 20px',
								margin: '0px',
								lineHeight: '18px',
								fontSize: '12px',
								textAlign: 'left',
								display: 'block'});

		$('#infobox p.close').css({textAlign: 'center'});
		
		this.changeImage(imageNum);
	},
	changeImage: function(imageNum) {	
		if(this.inprogress == false){
			this.inprogress = true;
			InfoBox.activeImage = imageNum;
			$('#loading').show();
			$('#lightboxImage').hide();
			$('#hoverNav').hide();
			$('#prevLink').hide();
			$('#nextLink').hide();
			$('#numberDisplay').hide();		
		}
	},

	end: function() {
		$('#infobox').hide();
		$("#overlay").fadeOut();
		$("select, object, embed").show();
	},
	
	getPageSize : function(){
		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;

		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;
		}


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

		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	},
	getPageScroll : function(){
		
		var xScroll, yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}

		arrayPageScroll = new Array(xScroll,yScroll) 
		return arrayPageScroll;
	},
	pause : function(ms){
		var date = new Date();
		curDate = null;
		do{var curDate = new Date();}
		while( curDate - date < ms);
	}
};

$(document).ready(function(){
	InfoBox.initialize();
});