var layer = {
	layerTemplate : '<div class="layerContainer"><div class="layerBox"><div class="layerTop"></div><div class="layerRepeater clearfix"><div class="layerCloseButton"><a href="javascript:layer.close()"><img src="/images/common/layer_close_button.png" alt="Fechar" /></a></div><div class="layerContent" id="layerContent"></div></div><div class="layerBottom"></div></div></div>',
	layerInit : false,
	layerAttach : false,
	ajaxCallback : null,
	
	close : function(){
		$(".layerContainer").hide('slow', function(){
			$("#layerContent").html('');
		});
	},
	
	openLayer : function(html){
		if(layer.layerInit == false){
			layer.init();
		}
		$("#layerContent").html(html);
		$(".layerContainer").show('fast', function(){
	  		layer.positionLayer();
	  	});
		layer.positionLayer();
	},
	
	openAjaxLayer : function(url){
		if(layer.layerInit == false){
			layer.init();
		}
		$.ajax({
		  url: url,
		  cache: false,
		  dataType: "html",
		  success: function(html){
		  	$("#layerContent").html(html);
		  	$(".layerContainer").show('fast', function(){
		  		layer.positionLayer();
		  	});
			layer.positionLayer();
			if(layer.ajaxCallback != null){
				layer.ajaxCallback();
			}
		  }
		});
	},
	
	init : function(){
		$("body").prepend(layer.layerTemplate);
		layer.layerInit = true;
		$(window).scroll(layer.windowScrollHandler);
		$(window).resize(layer.positionLayer);
	},
	
	windowScrollHandler : function(){
		if(layer.layerAttach == true){
			$(".layerContainer").css('top', $(window).scrollTop());
		}
	},
	
	positionLayer : function(){
		if($(window).height() > $(".layerBox").height()){
			var offsetTop = ($(window).height() - $(".layerBox").height())/2;
			$(".layerBox").css('margin-top', offsetTop+'px');
			layer.layerAttach = true;
		}else{
			layer.layerAttach = false;
			if($(".layerBox").height() > $("body").height()){
				$(".layerContainer").css('height', ($(".layerBox").height()+100)+'px');
				$(".layerBox").css('margin-top', 50+'px');
			}else{
				$(".layerContainer").css('height', $("body").height()+'px');
				var offsetTop = ($("body").height() - $(".layerBox").height())/2;
				$(".layerBox").css('margin-top', offsetTop+'px');
			}
		}
		$(".layerContainer").css('top', $(window).scrollTop());
	}
}
