/*-------------------------------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;
}

/*-----------------------------------------------------------------------------------------------*/

var Moobox = new Class({
	
	yPos : 0,
	xPos : 0,
	keepSelects: null,

	initialize: function(target) {
		this.content = $(target);
		this.keepSelects = this.content.getElements('select');		
		this.activate();
		this.displayLightbox('block');
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");	
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){		
		bod = $E('body');
		bod.setStyle('height', height);
  
		htm = $E('html');
		htm.setStyle('height', height);
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
		this.keepSelects.each(function (el) {
			el.setStyle('visibility', 'visible');
		});
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	displayLightbox: function(display){
		
		var contentHeight = this.content.getStyle('height').toInt();
		var windowHeight = window.getHeight();
		var margin = 50; //(windowHeight - contentHeight) / 4;
		
		var top = Window.getScrollTop() + margin;
		
		//alert(window.getScrollHeight());
		
		$('overlay').style.display = display;
		this.content.style.display = display;
		
		this.content.setStyle('top', top + 'px');
		if (browser == 'Internet Explorer') $('overlay').setStyle('height', window.getScrollHeight());
				
		if(display != 'none') this.actions();
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = $ES('.lbAction');

		for(i = 0; i < lbActions.length; i++) {
			lbActions[i].addEvent('click', this[lbActions[i].rel].bind(this));
		}

	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(e){
		if (e) Window.stopEvent(e);
		
		if (browser == "Internet Explorer"){
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}

		this.displayLightbox("none");
	}
	
});

// -----------------------------------------------------------------------------------------------

// Onload, make all links that need to trigger a lightbox active
function initialize(){
	addLightboxMarkup();
}

// 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.
function addLightboxMarkup() {

	bod 				= document.getElementsByTagName('body')[0];

	overlay 			= document.createElement('div');
	overlay.id			= 'overlay';

	bod.appendChild(overlay);
}

window.onDomReady(function () {
	initialize();
	getBrowserInfo();
});