/**
 * @version built on Prototype 1.5
 * @author Maxime Cowez
 * @copyright <a href="http://www.edge.be">Edge.be</a>
 */
if (!Edge) var Edge = {};
Edge.LightBox = Class.create();
Edge.LightBox.prototype = {
	
	initialize: function(trigger, cfc, method) {
		this.trigger = $(trigger) || Edge.Tools.Debug.error("You need to pass in a trigger elm as 1st parameter");;
		this.cfc = cfc || Edge.Tools.Debug.error("You need to pass in an ajax url as 2nd parameter");;
		this.method = method || Edge.Tools.Debug.error("You need to pass in an ajax method as 3d parameter");;
		this.prefix = 'lb_';
				
		if (!$('lightbox')) {
			new Insertion.Top(document.body, '<div id="lightbox" style="display:none"></div>'); //1.5
			this.lightbox = $('lightbox');
		}
		if (!$('overlay')) {
			new Insertion.Top(document.body, '<div id="overlay"><div id="loading"></div></div>'); //1.5
			this.overlay = $('overlay');			
			Edge.Tools.Gfx.makeTransparent(this.overlay, 0.6);
		}
		this.overlay = $('overlay');
		this.lightbox = $('lightbox');
		this.loadingImg = $('loading');
		
		this.flashDivs = Prototype.Browser.IE ? $A(document.getElementsByTagName('object')) : $A(document.getElementsByTagName('embed'));
		
		this.triggerListener = this.doAction.bind(this);
		this.overlayListener = this.close.bind(this);
		this.trigger.observe('click', this.triggerListener);
		
		var form = this.getForm();
		if (form) 
			form.getInputs().each(function(input) {
				Event.observe(input, 'keypress', this.checkKey.bind(this)); //1.5
			}.bind(this)); //1.5
	},
	
	doAction: function(evt) {
		Event.stop(evt);
		var param = {method: this.method};
		
		//build parameters from input fields
		var form = this.getForm();
		if (form) {
			form.getInputs().each(function(input) {
				if (input.type == "text" || input.type == "password" || input.type == "hidden" || 
					(input.type == "checkbox" && input.checked)) 
					param[input.name] = input.value;
				});
		}
		
		this.overlay.setStyle({
			height:		$(document.body).getHeight() + 'px',
			display:	'block'
		});
		
		this.center(this.loadingImg);
		
		//hide all flash
		this.flashDivs.each(function(div) {div.style.visibility = 'hidden';});
		
		//make ajax call
		new Ajax.Request(this.cfc, 
			{method: 'post', parameters: param, 
			onComplete: this.show.bind(this), 
			onFailure: Edge.Tools.Debug.report});
	},
	
	show: function(resp) {
		resp = Edge.Tools.Ajax.get(resp, this.cfc);
		this.lightbox.update('<div id="'+this.prefix+'closeButton"></div>' + resp.html);
		if ($(this.prefix + 'loginbutton')) this.setTrigger(this.prefix, 'loginbutton');
		$(this.prefix+'closeButton').observe('click', this.close.bind(this));
		this.overlay.observe('click', this.overlayListener);	

		if (!this.lightbox.getElementsByTagName('form').length) {
			$('md_loginbox_resp').update(resp.html_leftnav);
			$('md_status').up(0).update(resp.html_top);
		}
		else {
			var form = this.lightbox.getElementsByTagName('form')[0]; 
			if (Prototype.Browser.IE && Edge.Tools.IErange(7, 7)) Form.focusFirstElement(form);
			form.getInputs().each(function(input) {
				Event.observe(input, 'keypress', this.checkKey.bind(this)); //1.5
			}.bind(this)); //1.5
		}
		
		this.center(this.lightbox);
	},

	close: function() {
		if (!this.lightbox.getElementsByTagName('form').length) {
			if (this.target) window.location = this.target;
			else if (this.isExcluded(window.location)) window.location = this.exclusionTarget; 
		}
		this.unSetTrigger(this.trigger);
		this.flashDivs.each(function(div) {div.style.visibility = 'visible';});
		this.hide();
	},	
	
	hide: function() {
		this.overlay.hide();
		this.lightbox.hide();		
	},
	
	isExcluded: function(url) {
		if (!this.exclude) return true;
		
		url = new String(url).split("#")[0].replace(/&.*/ig, "");
		for (var i in this.exclude) 
			if (this.exclude[i] == url) 
				return true;
		return false;
	},

	setTrigger: function(prefix, trigger) {
		this.lbTrigger = $(prefix + trigger);
		this.lbTrigger.observe('click', this.triggerListener);

		this.getForm().getInputs().each(function(input) {
			Event.observe(input, 'keypress', this.checkKey.bind(this)); //1.5
		}.bind(this)); //1.5
	},
	
	unSetTrigger: function(prefix, trigger) {
		//Event.stopObserving(this.trigger, 'click', this.triggerListener);
		this.overlay.stopObserving('click', this.overlayListener);
		this.lbTrigger = null;
	},

	getForm: function() {
		var element = this.lbTrigger || this.trigger;
		
		while (element.tagName != "FORM") {
			element = element.up(0);
			if (element.tagName == "BODY") return null;
		}
		return element;
	},

	checkKey: function(evt) {
		if (evt.keyCode && evt.keyCode == Event.KEY_RETURN) this.doAction(evt);
	},
	
	center: function(elm) {
		var elmDim = elm.getDimensions();
		var browserDim = this.getBrowserDimensions();
		elm.setStyle({
			top:		(browserDim.yScroll + (browserDim.height - elmDim.height)/2 || 0) + 'px',
			left:		((browserDim.width - elmDim.width)/2 || 0) + 'px',
			display:	'block'
		});
	},

	getBrowserDimensions: function() {
		var de = document.documentElement;
		return {
			width: self.innerWidth || (de && de.clientWidth) || document.body.clientWidth,
			height: self.innerHeight || (de && de.clientHeight) || document.body.clientHeight,
			yScroll: self.pageYOffset || (de && de.scrollTop) || document.body.scrollTop};
	}
	
}