// -----------------------------------------------------------------------------------
//
//	Lightbox v2.02
//	by Lokesh Dhakar - http://www.huddletogether.com
//	3/31/06
//
//	For more information on this script, visit:
//	http://huddletogether.com/projects/lightbox2/
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//	
//	Credit also due to those who have helped, inspired, and made their code available to the public.
//	Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.org), Thomas Fuchs(mir.aculo.us), and others.
//
//
// -----------------------------------------------------------------------------------
/*

	Table of Contents
	-----------------
	Configuration
	Global Variables

	Extending Built-in Objects	
	- Object.extend(Element)
	- Array.prototype.removeDuplicates()
	- Array.prototype.empty()

	Lightbox Class Declaration
	- initialize()
	- start()
	- changeImage()
	- resizeFormContainer()
	- showImage()
	- updateDetails()
	- updateNav()
	- preloadNeighborImages()
	- end()
	
	Miscellaneous Functions
	- getPageScroll()
	- getPageSize()
	- getKey()
	- listenKey()
	- showSelectBoxes()
	- hideSelectBoxes()
	- pause()
	- initLightbox()
	
	Function Calls
	- addLoadEvent(initLightbox)
	
*/
// -----------------------------------------------------------------------------------

var formResizeSpeed = 7;	// controls the speed of the image resizing (1=slowest and 10=fastest)
var formBorderSize = 3;	//if you adjust the padding in the CSS, you will need to update this variable
var top = 100;
var left = 100;

// -----------------------------------------------------------------------------------

// Global functions
function closeFormBox() { 
	myFormLightbox.end(); 
	return false; 
	}

function showDiv(name){
	divId = document.getElementById(name);
	if (divId.style.display != 'none'){
		return;
	}
	new Effect.Grow(divId, {direction: 'top-left'});
	//new Effect.SlideDown;
	//new Effect.Appear(divId);
}

function hideDiv(name){
	divId = document.getElementById(name);
	if (divId.style.display == 'none'){
		return;
	}
//	new Effect.Shrink(divId, {direction: 'top-left'});
	new Effect.Fade(divId);
}


var FormLightbox = Class.create();


FormLightbox.prototype = {
	initialize: function() {	
		if (!document.getElementsByTagName){ 
			return; 
			}
		var anchors = document.getElementsByTagName('a');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('formbox'))){
				anchor.onclick = function () {myFormLightbox.startForm(this); return false;}
			}
		}

		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','formoverlay');
		objOverlay.setAttribute('class','formoverlay');
		objOverlay.style.display = 'none';
		objBody.appendChild(objOverlay);
		
		var objLightbox = document.createElement("div");
		objLightbox.setAttribute('id','formbox');
		objLightbox.setAttribute('class','formbox');
		objLightbox.style.display = 'none';
		objBody.appendChild(objLightbox);	
	},
	


	// Display Ajax response
	processAjaxResponse: function(response){
		Element.hide('formbox');
		$('formbox').innerHTML = response.responseText;
		showDiv('formbox');
	},

	startForm: function(formLink) {	
		$('formbox').innerHTML = '';		

		// stretch overlay to fill page and fade in
		var arrayPageSize = getPageSize();
		$('formoverlay').style.height = arrayPageSize[1] +"px";
		new Effect.Appear('formoverlay', { duration: 0.1, from: 0.0, to: 0.5 });

		if (!document.getElementsByTagName){ 
			alert('Unable to acquire elements'); 
			}

		// calculate top offset for the lightbox and display 
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);

		$('formbox').style.top = top +"px";
		$('formbox').style.left = left +"px";

		// Begin Ajax request based off of the href of the clicked linked
		var myAjax = new Ajax.Request(formLink.getAttribute('href'),
          	{method: 'POST', parameters: "", onComplete: this.processAjaxResponse.bindAsEventListener(this)}
		);
	},

	end: function() {
		hideDiv('formbox');
		new Effect.Fade('formoverlay', { duration: 0.2});
	}
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

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

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
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){ // 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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}


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

// -----------------------------------------------------------------------------------



function initFormbox() {
	myFormLightbox = new FormLightbox(); 
	}
	
	
Event.observe(window, 'load', initFormbox, false);
