
var LightBox = Class.Create();
LightBox.AddMethods({
	
	id: null,
	box: null,
	boxele: null,
	
	maincontent: null,
	objcontent: null,
	contentParent:null,
	
	width: null,
	height: null,
	
	_constructor: function(id, width, height) {
		this.id = id;
		this.width = width;
		this.height = height;
	},
	
	Close: function() {
		this.RemoveContent();
		UI.Document.RemoveFromBody(this.id);
	},
	
	CenterBox: function() {
		var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		this.box.style.top = ((windowHeight/2) + document.documentElement.scrollTop) - (this.box.offsetHeight/2) + "px";
		this.box.style.left = ($("pageBody").offsetWidth/2) - (this.box.offsetWidth/2) + "px";
	},
	
	CloseBox: function() {
		this.RemoveContent();
		this.Close();
	},
	
	CreateBox: function(width, height) {
		if($(this.id)!=false) {
			UI.Document.RemoveFromBody(this.id);
		}
		UI.AlphaBg.IncBgLevel();
		var html_lb = "";
		//html_lb += "<div class=\"lightbox\">";
			html_lb += "<div class=\"top\" id=\""+this.id+"_top\"><div class=\"title lightcolor\" id=\""+this.id+"_title\"></div><div class=\"action\"><input type=\"button\" class=\"button bsmall bfechar\" onclick=\"LightBoxFunctions.CloseBox('"+this.id+"');\" value=\"Fechar\" /></div></div>";
			html_lb += "<div class=\"content\" id=\""+this.id+"_content\"></div>";
		//html_lb += "</div>";
		this.boxele = UI.Document.AddToBody("div", this.id);
		this.box = $(this.id);
		this.box.Hide();
		this.box.AddClass("lightbox");
		this.box.SetContent(html_lb);
		if(this.width != null) {
			this.box.style.width = this.width + "px";
		}
		if(this.height != null) {
			this.box.style.height = this.height + "px";
		}
	},
	
	MoveToFront: function() {
		this.box.style.zIndex = UI.GetZIndex();
	},
	
	RemoveContent: function() {
		if(this.objcontent!=null) {
			this.objcontent.Hide();
			this.contentParent.appendChild(this.objcontent);
		}
	},
	
	SetMainContent: function(content) {
		if(typeof(content)=="string") {
			this.maincontent = content;
			$(this.id+"_content").SetContent(content);
		} else {
			content.Show();
			this.contentParent = content.parentNode;
			$(this.id+"_content").appendChild(content);
			this.objcontent = content;
		}
	},
	
	Show: function() {
		this.box.style.display = "";
		this.box.style.zIndex = UI.GetZIndex();
	},
	
	SetTitle: function(title) {
		$(this.id+"_title").SetContent(title);
	}
	
});

LightBoxFunctions = {
	boxes: [],
	
	CloseBox: function(id) {
		/*if(id.indexOf("_lb")>=0) {
			id = id.substring(0, id.length-"_lb".length);
		}*/
		this.boxes[id].CloseBox();
		UI.AlphaBg.DecBgLevel();
		this.boxes[id] = null;
	},
	createBox: function(id, width, height) {
		if(this.boxes[id] != null) {
			this.CloseBox(id);
		}
		var box = new LightBox(id, width, height);
		box.CreateBox(50, 50);
		this.boxes[id] = box;
		return box;
	},
	
	ShowBoxFromElement: function(id, title, elementid, width, height, callback) {
		var box = this.createBox(id, width, height);
		box.SetTitle(title);
		//$(id + "_content").innerHTML = $(elementid).innerHTML;
		box.contentParent = $(elementid).parentNode;
		$(id + "_content").appendChild($(elementid));
		box.objcontent = $(elementid);
		$(elementid).Show();
		this.showBoxFromElementCallback(id);
		if(callback!=null) {
			callback();	
		}
	},
	showBoxFromElementCallback: function(boxid) {
		LightBoxFunctions.boxes[boxid].Show();
		LightBoxFunctions.boxes[boxid].CenterBox();
	},
	
	ShowBox: function(id, title, xmlReq, xslRequest, params, callback, width, height) {
		var box = this.createBox(id, width, height);
		//box.Show();
		box.SetTitle(title);
		xslRequest.SDBCallback = callback;
		xslRequest.SDBId = id;
		var xslParams = (params==null) ? new XslParams() : params;
		XSLBinder.Bind(xmlReq, xslRequest, xslParams, this.showBoxCallback, id + "_content");
	},
	showBoxCallback: function(req) {
		var boxid = req.XSLRequest.SDBId;
		LightBoxFunctions.boxes[boxid].Show();
		LightBoxFunctions.boxes[boxid].CenterBox();
	}
	
};
