window.LightBox = Class.create();

Object.extend(window.LightBox.prototype, {
	id: null,
	body: "",
	content: "",
	divAlertID: null,
	
	initialize: function(html, options){
		this.content = html;
		this.setOptions(options);
		this.divAlertID = $("divAlertID");
		this.setBody();
	},

	setOptions: function(options){
		this.options = Object.extend({
			transform: false,
			position: "scroll", /* scroll, top */
			title: "",
			footer: "",
			justContent: 0,
			onClose: Prototype.emptyFunction
		}, options || {} );
	},
	
	setBody: function(){
		var html = [];
		
		if(this.options.justContent == 0){
			html.push("<table id='LB' class='lightbox' cellpadding='0' cellspacing='0' border='0'>");
			html.push("	 <tr>");
	    	html.push("     <td class='corner_esq'></td>");
			html.push("     <td class='bg_title'></td>");
			html.push("     <td class='corner_dir'></td>");
		 	html.push("  </tr>");
			html.push("  <tr id='LB_header'>");
			html.push("    <td colspan='3' class='bg_title'>");
			html.push("      <div class='imagem'><img id='LB_closeButton' src='#SRC_CLOSE_BUTTON#' title='#HINT#'/></div>");
			html.push("      <div class='titulo'>#TITLE#</div>");
			html.push("    </td>");
			html.push("  </tr>");
			html.push("  <tr class='bg_content'>");
			html.push("    <td><img src='"+window.contextPath+"/imagem/agendamento/space.gif' height='5' width='5'></td>");
			html.push("    <td id='LB_content'>#CONTENT#</td>");
			html.push("    <td><img src='"+window.contextPath+"/imagem/agendamento/space.gif' height='5' width='5'></td>");
			html.push("  </tr>");
			html.push("  <tr id='LB_footer' class='bg_content'>");
			html.push("    <td colspan='3'>#FOOTER#</td>");
			html.push("  </tr>");
			html.push("</table>");
	
			this.body = html.join("");
			this.body = this.body.replace("#TITLE#", this.options.title);
			this.body = this.body.replace("#SRC_CLOSE_BUTTON#", window.contextPath + "/imagem/icone/fechar.gif");
			this.body = this.body.replace("#HINT#", Bundle.getMessage("label.close"));
			this.body = this.body.replace("#CONTENT#", this.content);
			this.body = this.body.replace("#FOOTER#", this.options.footer);
		}else{
			html.push("<table id='LB' cellpadding='0' cellspacing='0' border='0'>");
			html.push("  <tr>");
			html.push("   <td id='LB_content'>");
			html.push("	 		<div style='position:absolute; margin-top:3px; margin-left: 520px;'><img id='LB_closeButton' src='#SRC_CLOSE_BUTTON#' title='#HINT#' /></div>");
			html.push(" 		 #CONTENT#");
			html.push("   </td>");
			html.push(" </tr>");
			html.push("</table>");
	
			this.body = html.join("");
			this.body = this.body.replace("#SRC_CLOSE_BUTTON#", window.contextPath + "/imagem/icone/fechar.gif");
			this.body = this.body.replace("#HINT#", Bundle.getMessage("label.close"));
			this.body = this.body.replace("#CONTENT#", this.content);
		}
	},
	
	transform: function(func, id){
		id = (id == null) ? "LB_content" : id;

		$(id).select(".removeOnPrint").each(Element.remove);
		$(id).select("a").each(function(element){
			var span = "<span class='link' style='cursor:auto;'>"+element.innerHTML+"</span>";
			element.insert({after:span});
			element.remove();
		});

		if(Object.isFunction(func)){
			$(id).descendants().each(func);
		}
	},

	show: function(){
		Consulte.toggleDimmer("dimmerAlert", true);
		this.divAlertID.innerHTML = this.body;
		if(this.options.transform){
			this.transform();
		}
		this.divAlertID.show();
		this.divAlertID.style.top = ((this.options.position == "scroll") ? document.documentElement.scrollTop + 100 : $("content").positionedOffset().top) + "px";
		this.divAlertID.style.left = ((document.documentElement.scrollWidth - this.divAlertID.offsetWidth) / 2) + "px";
		Event.observe($("LB_closeButton"), "click", this.close.bind(this));
	},
	
	hide: function(){
		this.divAlertID.hide();
		Consulte.toggleDimmer("dimmerAlert", false);
	},
	
	close: function(){
		this.divAlertID.innerHTML = "";
		this.divAlertID.hide();
		this.divAlertID = null;
		Consulte.toggleDimmer("dimmerAlert", false);
		this.options.onClose();
	}
});
