/*
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/

angepasst von Kai-Henning Ramke
www.khrmedia.de
25.01.2008
*/


//Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', addLightboxMarkup, false);

var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 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
	activate: function(){
		this.displayLightbox("block");
	},
	
	displayLightbox: function(display){
		//$('overlay').style.display = display;
		if(display != "none") {
			Overlay.start( lightbox );
		} else {
			Overlay.stop();
		}
		$('lightbox').style.display = display;
		showLoading (display);
		if(display == 'none') {
			$('lightbox').style.zIndex = "-1";
			//$('loading').style.zIndex = "10000";
		}
		if(display != 'none') this.loadInfo();
	},
	
	// 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();
		showLightbox();	
	},
	
	// Search through new links within the lightbox, and attach click event
    /*actions: function(){
        var that = this;
        lbActions = $('lbContent').select('.lbAction').each(function(obj) {
                Event.observe(obj, 'click', that[obj.readAttribute('rel')].bindAsEventListener(that), false);
        });
    },*/
    // Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = document.getElementsByClassName('lbAction');

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

	},
	
	// Example of creating your own functionality once lightbox is initiated
	insert: function(e){
	   //link = Event.element(e).parentNode;
       link = Event.element(e);
	   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'));
		
		this.displayLightbox("none");
	}
}

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

// Onload, make all links that need to trigger a lightbox active
//var valid;
var count = 0;
function initialize(){
	//addLightboxMarkup();
	lbox = document.getElementsByClassName('lbOn');
	for(i = 0; i < lbox.length; i++) {
		valid = new lightbox(lbox[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 addLightboxMarkup() {
	initialize();
	bod 				= document.getElementsByTagName('body')[0];
	lb					= document.createElement('div');
	lb.id				= 'lightbox';
	lb.className 	= 'loading';
	lb.innerHTML	= '<div id="lbLoadMessage">' +
						  '<p></p>' +
						  '</div>';
	bod.appendChild(lb);
}*/
function addLightboxMarkup() {
	initialize();
	bod 				= document.getElementsByTagName('body')[0];
	loading 			= document.createElement('div');
	loading.id		= 'loading';
	loading.innerHTML	= '&nbsp;';
	lb					= document.createElement('div');
	lb.id				= 'lightbox';
	lb.className 	= 'loading';
	lb.innerHTML	= '<div id="lbLoadMessage">' +
						  '<p></p>' +
						  '</div>';
	bod.appendChild(loading);
	bod.appendChild(lb);
}