/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

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

Extended by: echternacht new media (http://www.echternacht.com)
Extension: it is posible to load Lightbox content via iframe
Requirement: requires Prototype JavaScript framework >= 1.5.0 (http://prototype.conio.net/)
Requirement: requires QueryParser  >= 0.2 (http://www.echternacht.com)
*/
if((typeof Prototype=='undefined') || parseFloat(Prototype.Version.split(".")[0] + "." + 
        	Prototype.Version.split(".")[1]) < 1.5)
	throw("Lightbox requires the Prototype JavaScript framework >= 1.5.0");
if(typeof QueryParser=='undefined' || parseFloat(QueryParser.version) < 0.2)
	throw("Lightbox requires QueryParser >= 0.2");
/*-------------------------------GLOBAL VARIABLES------------------------------------*/
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring,abox;

var Lightbox = Class.create();
/*-----------------------------------------------------------------------------------------------*/
//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/
Lightbox.getBrowserInfo = function () {
	if (Lightbox.checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (Lightbox.checkIt('safari')) browser 	= "Safari"
	else if (Lightbox.checkIt('omniweb')) browser 	= "OmniWeb"
	else if (Lightbox.checkIt('opera')) browser 		= "Opera"
	else if (Lightbox.checkIt('webtv')) browser 		= "WebTV";
	else if (Lightbox.checkIt('icab')) browser 		= "iCab"
	else if (Lightbox.checkIt('msie')) browser 		= "Internet Explorer"
	else if (!Lightbox.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 (Lightbox.checkIt('linux')) OS 		= "Linux";
		else if (Lightbox.checkIt('x11')) OS 	= "Unix";
		else if (Lightbox.checkIt('mac')) OS 	= "Mac"
		else if (Lightbox.checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}
Lightbox.checkIt = function (string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
/*-----------------------------------------------------------------------------------------------*/
Lightbox.prototype = {
	yPos : 0,
	xPos : 0,
	height : 0,
	width : 0,
	type : 'ajax',
	content : '',
	
	initialize: function(ctrl, type) {
		this.yPos	 = 0;
		this.xPos	 = 0;
		this.type	 = (type != null) ? type : 'ajax';
		this.content = ctrl.href;
		
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
		
		qp = new QueryParser(this.content);
		this.height	 = (qp.getValue('height') != null) ? qp.getValue('height') : 0;
		this.width	 = (qp.getValue('width') != null) ? qp.getValue('width') : 0;
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");
		Lightbox.setActiveBox(this);
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the 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; 
	},
	
	// 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;
		}
	},
	
	// 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){
		this.adjustBox();
		$('overlay').style.display = display;
		$('lightbox').style.display = display;
		if(display != 'none') {
			switch(this.type){
				case 'ajax': this.loadInfo(); break;
				case 'iframe': this.loadIframe(); break;
			}
		}
	},
	
	adjustBox: function(){
		if(this.height > 0 && this.width > 0){
			wd = this.getWindowDimensions();
			$('lightbox').style.height = this.height + "px";
			$('lightbox').style.width  = this.width + "px";
			$('lightbox').style.top    =  (wd['height']-this.height)/2 + "px";
			$('lightbox').style.left   =  (wd['width']-this.width)/2 + "px";
			$('lightbox').style.margin = 0;
		}
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
		var myAjax = new Ajax.Request(
			this.content,
			{method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
		);
	},
		
	// Display Ajax response
	processInfo: function(response){
		info = "<div id='lbContent'>" + response.responseText + "</div>";
		new Insertion.Before($('lbLoadMessage'), info)
		$('lightbox').className = "done";	
		this.actions();			
	},
	
	//load iframe src based off of the href of the clicked linked
	loadIframe: function(){
		info = "<iframe id='lbContent' name='lbContent' src='"+this.content+"' frameborder='0' scrolling='no'></iframe>";
		new Insertion.Before($('lbLoadMessage'), info);
		$('lightbox').className = "done";
		this.actions();
	},
	
	// Example of creating your own functionality once Lightbox is initiated
	insert: function(e){
	   link = Event.element(e).parentNode;
	   Element.remove($('lbContent'));
	 
	   var myAjax = new Ajax.Request(
			  link.href,
			  {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
	   );
	 
	},
	
	// Example of creating your own functionality once Lightbox is initiated
	deactivate: function(){
		Element.remove($('lbContent'));
		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		this.displayLightbox("none");
	},
	
	getWindowDimensions: function() {
		var w = 0, h = 0, d = document;
		if( typeof( window.innerWidth ) == 'number' ) { //Non-IE
			w = window.innerWidth;
			h = window.innerHeight;
		} else if( d.documentElement && ( d.documentElement.clientWidth || d.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode'
			w = d.documentElement.clientWidth;
			h = d.documentElement.clientHeight;
		} else if( d.body && ( d.body.clientWidth || d.body.clientHeight ) ) { //IE 4 compatible
			w = d.body.clientWidth;
			h = d.body.clientHeight;
		}
		return {width: w, height: h};
	}
}

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

Lightbox.setActiveBox = function (box) {
	abox = box;
}
Lightbox.deactivateBox = function (){
	if(abox != null) abox.deactivate();
}

// Onload, make all links that need to trigger a Lightbox active
Lightbox.initialize = function(){
	Lightbox.addMarkup();
	var lbox = document.getElementsByClassName('lbOn');
	for(var i = 0; i < lbox.length; i++) {
		lbox[i] = new Lightbox(lbox[i]);
	}
	var fbox = document.getElementsByClassName('fbOn');
	for(var i = 0; i < fbox.length; i++) {
		fbox[i] = new Lightbox(fbox[i], 'iframe');
	}
}

// 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.addMarkup = function() {
	bod 			= document.getElementsByTagName('body')[0];
	overlay 		= document.createElement('div');
	overlay.id		= 'overlay';
	lb				= document.createElement('div');
	lb.id			= 'lightbox';
	lb.className 	= 'loading';
	lb.innerHTML	= '<div id="lbLoadMessage">' +
						  '<p>Loading</p>' +
						  '</div>';
	bod.appendChild(overlay);
	bod.appendChild(lb);
}

Event.observe(window, 'load', Lightbox.initialize, false);
Event.observe(window, 'load', Lightbox.getBrowserInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);
