/* licuit.com */
/* layers.js */
/* Created Jun, 2010 */
/* Modified Jun, 2010
--------------------------------------- */

licuitCore.layers = new Class({
	Extends : licuitCore.licuit,
	
	fx : null,
	activeLayer : null,
	hashInterval : null,
	urlQuery : null,
	
	layerTypes : {
		"showLoginEs" 	: "layers/showlogines.html",
		"showLoginPt" 	: "layers/showloginpt.html",
		"showLoginEn" 	: "layers/showloginen.html"
	},
	
	initialize : function(){
		this.transitions = [];
	},
	
	setup : function(){
		
	},
	
	checkHash : function(windowType){
		if(window.location.hash != "#" + windowType)
			this.close();
	},
	
	open : function(windowType, urlQuery){
		var tClass = this;
		
		if(this.activeLayer != null)
			this.close();
		
		if(urlQuery == undefined)
			this.urlQuery = "";
		else
			this.urlQuery = urlQuery;
		
		this.activeLayer = this.buildLayer();
		this.addContent(windowType);
		
		if (this.fx != null) {
			this.fx.onComplete = function(){};
			this.fx = null;
		}
		
		this.fx = new Fx.Tween(this.activeLayer, {property:"opacity", duration: 600, transition: Fx.Transitions.Quad.easeInOut});
						
		this.fx.set(0);
		this.fx.start(1);
		
		window.location.hash = windowType;
		this.hashInterval = setInterval(function(){tClass.checkHash(windowType);},500);
	},
	
	close : function(){
		
		var tClass = this;
		
		if(this.activeLayer == null)
			return;
		
		clearInterval(this.hashInterval);
		var topScroll = document.documentElement.scrollTop;
		window.location.hash = "";
		document.documentElement.scrollTop = topScroll;
		
		this.fx.onComplete = function(){};
	
		this.fx.onComplete = function(){tClass.activeLayer.dispose();};
		this.fx.start(0);
	},
	
	buildLayer : function(){
		var tClass = this;
		
		var body = $(document.body);
		var layer = new Element("div",{"class":"licuitLayer"});
		
		var bg = new Element("div", {"class":"bg"});
		var contentHolder = new Element("div",{"class":"contentHolder"});
		var content = new Element("div",{"class":"content"});
		
		//layer.setStyle("height",document.documentElement.clientHeight);
		//layer.setStyle("width",document.documentElement.clientWidth);
		
		bg.inject(layer);
		contentHolder.inject(layer);
		content.inject(contentHolder);
		layer.inject(body);
		
		contentHolder.addEvent("click",function(){tClass.close();});
		
		return layer;
	},
	
	addContent : function(windowType){
		
		var tClass = this;
		var layerHeight = 450;
		var layerWidth = 550;
		
		var content = this.activeLayer.getElement(".content");
		
		content.setStyle("width",layerWidth);
		content.setStyle("height",layerHeight);
		content.setStyle("margin-top",-Math.round(layerHeight/2));
		
		var filename = this.layerTypes[windowType];
		
		var myIframe = new Element("iframe",{"src":filename + this.urlQuery});
		myIframe.inject(content);
				
		var closeText = (language == "es") ? "x" : "x";
		var close = new Element("div",{"class":"btnClose","html":"<p>"+closeText+"</p>"});
		
		close.addEvent("click",function(){tClass.close();});
		
		close.inject(content);
	}
	
});

var layers = new licuitCore.layers();
window.addEvent("domready", function(){	layers.setup(); });
