/*
Based on script created by Chris Campbell from http://particletree.com

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
*/

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/

function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*-----------------------------------------------------------------------------------------------*/
LightBox = {};

LightBox.yPos = 0;

// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
LightBox.prepareIE = function(height, overflow){
	bod = document.getElementsByTagName('body')[0];
	bod.style.height = height;
	bod.style.overflow = overflow;

	htm = document.getElementsByTagName('html')[0];
	htm.style.height = height;
	htm.style.overflow = overflow; 
}

LightBox.setScroll = function(x, y){
	window.scrollTo(x, y); 
}

LightBox.getScroll = function(){
  LightBox.yPos = 0;
	if (self.pageYOffset) {
		LightBox.yPos = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		LightBox.yPos = document.documentElement.scrollTop; 
	} else if (document.body) {
		LightBox.yPos = document.body.scrollTop;
	}
}

// In IE, select elements hover on top of the lightbox
LightBox.hideSelects = function(visibility){
	selects = document.getElementsByTagName('select');
	for(i = 0; i < selects.length; i++) {
		selects[i].style.visibility = visibility;
	}
}

// Begin Ajax request based off of the href of the clicked linked
LightBox.loadInfo = function(url) {
  $('lightbox').className = "loading";		
	var myAjax = new Ajax.Request(
      url, {method: 'get', parameters: "", onComplete: LightBox.processInfo}
	);	
}

// Display Ajax response
LightBox.processInfo = function(response){
	info = "<div id='lbContent'>" + response.responseText + "</div>";
	new Insertion.Before($('lbLoading'), info)
	$('lightbox').className = "done";		
}

LightBox.displayLightbox = function(display, url){
	$('overlay').style.display = display;
	$('lightbox').style.display = display;
	if(display != 'none') LightBox.loadInfo(url);
}

LightBox.deactivate = function(){
	
	if (browser == "Internet Explorer"){
		LightBox.setScroll(0, LightBox.yPos);
		LightBox.prepareIE("auto", "auto");
		LightBox.hideSelects("visible");
	}
	
	LightBox.displayLightbox("none");
	Try.these(function() {Element.remove($('lbContent'))});
}

LightBox.activate = function(url){
	if (browser == 'Internet Explorer'){
		LightBox.getScroll();
		LightBox.prepareIE('100%', 'hidden');
		LightBox.setScroll(0,0);
		LightBox.hideSelects('hidden');
	}
	
	Try.these(function() {Element.remove($('lbContent'))});
	LightBox.displayLightbox("block", url);
}

// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
LightBox.addLightboxMarkup = function() {
	bod 				= document.getElementsByTagName('body')[0];
	overlay 			= document.createElement('div');
	overlay.id		= 'overlay';
	overlay.onclick = new Function("LightBox.deactivate();");
	lb					= document.createElement('div');
	lb.id				= 'lightbox';
	lb.className 	= 'loading';
	lb.innerHTML	= '<img id="lbLoading" src="/images/lb-loading.gif" alt="Loading..."/>';
	bod.appendChild(overlay);
	bod.appendChild(lb);
}

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', LightBox.addLightboxMarkup, false);
Event.observe(window, 'load', getBrowserInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);