// Heavily modified for mootools PTH
/*
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/
*/

function isIE() {
	var detect = navigator.userAgent.toLowerCase();
	return detect.indexOf('msie') + 1;
}

var currentLightboxGW;
var lgw = new Class({
	yPos : 0,
	xPos : 0,

	initialize: function(href) {
		this.href = href;
        window.addEvent('unload', function() { this.href = null; }.bind(this) );
        this.activate();
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (typeof lightboxWillOpen == 'function')
			lightboxWillOpen();
				
		if (!$('lgw_overlay')) {
			overlay = new Element('div');
			overlay.id = 'lgw_overlay';
			overlay.injectInside(document.body);
			overlay = null;
			lb = new Element('div');
			lb.id = 'lightbox';
			lb.className = 'loading';
			lb.innerHTML = '<div id="lbLoadMessage"><p>Loading</p></div>';
			lb.injectInside(document.body);
			lb = null;
		}

		if (isIE()){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");

		if (typeof lightboxDidOpen == 'function')
			lightboxDidOpen();
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		$$('body').setStyles({height: height, overflow: overflow});
		$$('html').setStyles({height: height, overflow: overflow});
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		$$('select').each(function(select) { select.setStyle('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){
		$('lgw_overlay').style.display = display;
		$('lightbox').setStyle('top', parseInt(window.getScrollTop()) + 40 + 'px');
		$('lightbox').style.display = display;
		if(display != 'none') this.loadInfo();
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
		currentLightboxGW = this; // Hack because bindWithEvent doesn't seem to work with Ajax
		new Request.HTML({ method: 'get', onComplete: this.processInfo, evalScripts: true, update: 'lbLoadMessage' }).send({url: this.href});
	},
	
	// Display Ajax response
	processInfo: function(response){
    $('lbLoadMessage').id = 'lbContent';
		$('lightbox').className = 'done';
		$$('.lbAction').each(function(lbAction) {
			lbAction.onclick = currentLightboxGW[lbAction.rel].bindWithEvent(currentLightboxGW);
		});
	},
			
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(event){
		if (typeof lightboxWillClose == 'function')
			lightboxWillClose();
	//	event.stop();
		
		if (isIE()){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");

		}
		
		this.displayLightbox("none");
		if (typeof lightboxDidClose == 'function')
			lightboxDidClose();
		
		$('lightbox').dispose();
		$('lgw_overlay').dispose();
		
		return false;
	}
});

window.addEvent('domready', function() {
	$$('.lbOn' ).each(function(link) { link.onclick = function() { new lgw(this); return false; }; } );
}
);
