/*
Created By: Chris Campbell
Modified By: Noah Winecoff (http://www.findmotive.com)
Website: http://particletree.com
Date: 2/1/2006

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

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

var detect = navigator.userAgent.toLowerCase();
var lbiOS,lbiBrowser,lbiVersion,total,thestring;

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

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

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

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

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

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

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

Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', getlbiBrowserInfo, false);
//Event.observe(window, 'unload', Event.unloadCache, false);

var lightboxi = Class.create();

lightboxi.prototype = {

	yPlbiOS : 0,
	xPlbiOS : 0,

	initialize: function(ctrl) {
		this.content = ctrl.href;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (lbiBrowser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displaylightboxi("block");
	},
	
	// 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.yPlbiOS = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPlbiOS = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPlbiOS = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	displaylightboxi: function(display){
		$('overlayi').style.display = display;
		$('lightboxi').style.display = display;
		if(display != 'none') this.loadInfo();
	},
	
	// Write an iFrame instead of using an AJAX call to pull the content
	loadInfo: function() {
		
		info = "<div id='lbiContent'><div class='lbiCllbiOSe'><a href='#' class='lbiAction' rel='deactivate'>St&#228;ng</a></div><iframe frameborder=\"0\" width=\"900\" height=\"665\" src=\"" + this.content + "\"</iframe></div>";
		new Insertion.Before($('lbiLoadMessage'), info)
		$('lightboxi').className = "done";	
		this.actions();			
		
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbiActions = document.getElementsByClassName('lbiAction');

		for(i = 0; i < lbiActions.length; i++) {
			Event.observe(lbiActions[i], 'click', this[lbiActions[i].rel].bindAsEventListener(this), false);
			lbiActions[i].onclick = function(){return false;};
		}

	},
	
	// Example of creating your own functionality once lightbox is initiated
	insert: function(e){
	   link = Event.element(e).parentNode;
	   Element.remove($('lbiContent'));
	 
	   var myAjax = new Ajax.Request(
			  link.href,
			  {method: 'plbiOSt', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
	   );
	 
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		Element.remove($('lbiContent'));
		
		if (lbiBrowser == "Internet Explorer"){
			this.setScroll(0,this.yPlbiOS);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		
		this.displaylightboxi("none");
	}
}

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

// Onload, make all links that need to trigger a lightbox active
function initialize(){
	addlightboxiMarkup();
	lbiox = document.getElementsByClassName('lbiOn');
	for(i = 0; i < lbiox.length; i++) {
		valid = new lightboxi(lbiox[i]);
	}
}

// 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 addlightboxiMarkup() {
	bod 				= document.getElementsByTagName('body')[0];
	overlayi 			= document.createElement('div');
	overlayi.id		= 'overlayi';
	lbi					= document.createElement('div');
	lbi.id				= 'lightboxi';
	lbi.className 	= 'loading';
	lbi.innerHTML	= '<div id="lbiLoadMessage">' +
						  'loading' +
						  '</div>';
	bod.appendChild(overlayi);
	bod.appendChild(lbi);
}