/**
 * @version built on Prototype 1.5
 * @author Maxime Cowez
 * @copyright <a href="http://www.edge.be">Edge.be</a>
 */
if (!Edge) var Edge = {};
Edge.MessagentLightBox = Class.create();
Edge.MessagentLightBox.prototype = {
	
	initialize: function(trigger, page, hash, complex) {
		this.trigger = $(trigger) || null;
		this.page = page;
		this.hash = hash;
		this.complex = complex;
		this.prefix = 'lb_';
				
		if (!$('messagentLightbox')) {
			new Insertion.Top(document.body, '<div id="messagentLightbox" style="display:none"></div>'); //1.5
			this.lightbox = $('messagentLightbox');
		}
		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 = $('messagentLightbox');
		this.loadingImg = $('loading');
		
		this.flashDivs = Prototype.Browser.IE ? $A(document.getElementsByTagName('object')) : $A(document.getElementsByTagName('embed'));
		
		this.overlayListener = this.close.bind(this);	
		if (this.trigger) {
			this.triggerListener = this.doAction.bind(this);
			this.trigger.observe('click', this.triggerListener);
		}
		else this.doAction();
	},
	
	doAction: function(evt) {
		if (evt) Event.stop(evt);
		
		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';});
		
		//insert iFrame
		var url = this.page + '?messagentID=' + this.hash;
		if (this.complex) url += '&cid=' + this.complex;
		this.show('<iframe src="' + url + '" width="450px" height="100%" frameborder="0" />');
	},
	
	show: function(resp) {
		this.lightbox.update('<div id="'+this.prefix+'closeButton"></div>' + resp);
		$(this.prefix+'closeButton').observe('click', this.close.bind(this));
		this.overlay.observe('click', this.overlayListener);	

		this.center(this.lightbox);
	},

	close: function() {
		this.flashDivs.each(function(div) {div.style.visibility = 'visible';});
		this.hide();
	},	
	
	hide: function() {
		this.overlay.hide();
		this.lightbox.hide();			

		// when the light box closes a redirect to the ticketing page
		// alert(this.complex);
				
		switch(this.complex)
		{
			case "KGRAN":
				// granada
				PopUpwindow = window.open("http://www.servicaixa.com/nav/es/cine/pulianas/recintos.html?CODIUSU=P009KI02#recinto_08OE","KineTicket","left=50,top=50,screenX=50,screenY=50,width=800,height=600,toolbar=1,location=1,directories=1,status=1,menuBar=1,scrollBars=1,resizable=1");
				break;
			case "KMAD":
				// madrid
				PopUpwindow = window.open("http://www.servicaixa.com/nav/es/cine/pozuelo/recintos.html?CODIUSU=P009KI02#recinto_08R7","KineTicket","left=50,top=50,screenX=50,screenY=50,width=800,height=600,toolbar=1,location=1,directories=1,status=1,menuBar=1,scrollBars=1,resizable=1");
				break;
			case "KVAL":
				// valencia
				PopUpwindow = window.open("http://www.servicaixa.com/nav/es/cine/paterna/recintos.html?CODIUSU=P009KI02#recinto_08V7","KineTicket","left=50,top=50,screenX=50,screenY=50,width=800,height=600,toolbar=1,location=1,directories=1,status=1,menuBar=1,scrollBars=1,resizable=1");
				break;
			default:
				window.location = 'http://www.kinepolis.com/es/index.cfm?PageID=107957';	
				break;
		} 
	},

	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};
	}
	
}