var DEBUG = true;
// dialogBox
// new DialogBox(String: id, HTML Element Object: element | String: fullscreen , String: titulo, String: tipo, Function: func, Boolean: useShadow [ , Width (px): w, String: botoes])
// tipo: ok, confirm|yescancel, yesno, yesnocancel, custom (sem botoes pré-definidos)
// func:
//	a referencia tem de ser global
//	o this dentro da função vai referir-se ao botao.
//	no caso de caixas de confirm, a função deve ler o valor do atributo "confirm" (this.getAttribute('confirm')) que diz true/false, conforme o botao carregado
var isIE6 = (Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6);
var DIALOGS = {
	dialogs:new Array(),
	add:function(dlg) {
		this.dialogs.push(dlg);
	},
	del:function(dlg) {
		for (i=0;i<this.dialogs.length;i++) {
			if (this.dialogs[i]==dlg) {
				this.dialogs.splice(i,1);
				break;
			}
		}
	},
	canHideShadow:function(dlg) {
		for (i=0;i<this.dialogs.length;i++) {
			if (this.dialogs[i] != dlg && this.dialogs[i].isModal()) {
				return false;
			}
		}
		return true;
	},
	allowCreation:function(dlgID) {
		for (i=0;i<this.dialogs.length;i++) {
			if (this.dialogs[i].getID()==dlgID) {
				return false;
			}
		}
		return true;
	},
	get:function(id) {
		for (i=0;i<this.dialogs.length;i++) {
			if (this.dialogs[i].getID()=="dialogBox"+id) {
				return this.dialogs[i];
			}
		}
		return false;
	},
	idIsActive:function(id) {
		for (i=0;i<this.dialogs.length;i++) {
			if (this.dialogs[i].getID()=="dialogBox"+id) {
				return true;
			}
		}
		return false;
	},
	isActive:function(dlg) {
		for (i=0;i<this.dialogs.length;i++) {
			if (this.dialogs[i]==dlg) {
				return true;
			}
		}
		return false;
	},
	getTotal:function() {
		return this.dialogs.length;	
	},
	destroyAll:function() {
		while (this.dialogs.length>0) { // depende do destroy invocar o delete desta classe.
			this.dialogs[0].destroy();
		}
	},
	destroy:function(dlgName) {
		this.get(dlgName).destroy();
	}
}
var DialogBox = Class.create({
	initialize: function(id,el,titulo,conteudo,tipo,func,useShadow,w,botoes,specialClass) {
		this.id = id;
		if (DIALOGS.allowCreation(this.getID())) {
			DIALOGS.add(this);
		} else {
			if (DEBUG) alert('Já existe uma caixa de dialogo com esse nome!');
			return false;
		}
		this.createContainer();
		this.container = $('dialogContainer');
		this.titulo = titulo;
		this.conteudo = conteudo;
		this.tipo = tipo;
		this.eventFunction = ((func)?func:"function(){}");
		this.width = (w)?w:false;
		this.useShadow = (useShadow===true);
		this.fullscreen = (el == "fullscreen");
		this.specialClass = specialClass?specialClass:false;
		this.createShadow();
		this.draw();
		// calcular a area a cobrir
		if (typeof(el) == "string") {
			if (this.fullscreen) {
				var newW = ((this.width)?(parseInt(this.width)/2):130);
				this.getContainerElement().style.top = "50%";
				this.getContainerElement().style.left = "50%";
				this.show();
				var newH = parseInt(this.getContainerElement().clientHeight)/2;
				this.hide();
				this.getContainerElement().style.margin = "-" + newH + "px 0px 0px -" + newW + "px";
				if (!isIE6) { /* IE6 n suporta fixed position... */
					this.getContainerElement().style.position = "fixed";
				} else {
					this.getContainerElement().style.position = "absolute";
					scroll(0,0); // forçar a pessoa a ver a dialog...
				}
			}
		} else {
			this.getContainerElement().style.margin = "0px 0px 0px 0px";
			this.getContainerElement().style.position = "absolute";
			var top = 0,left = 0,elp = el;
			while (elp.offsetParent) {
				top += elp.offsetTop;
				left += elp.offsetLeft;
				elp = elp.offsetParent;
			}
			el.style.zoom = 1; // IE FIX
			this.getContainerElement().style.top = top+"px";
			this.getContainerElement().style.left = left+"px";
		}

	},
	setWidth:function(w) {
		this.width = w;
		if (this.width) {
			this.getContainerElement().style.width = this.width+"px";
		}
	},
	recalculatePosition:function() {
		if (this.fullscreen) {
			var newW = ((this.width)?(parseInt(this.width)/2):130);
			var vis = this.isVisible();
			if (!vis) this.show();
			var newH = parseInt(this.getContainerElement().clientHeight)/2;
			if (!vis) this.hide();
			this.getContainerElement().style.margin = "-" + newH + "px 0px 0px -" + newW + "px";
		}
	},
	isModal:function() {
		return this.useShadow;
	},
	createContainer:function() {
		if (!$('dialogContainer')) {
			var tempcontainer = document.createElement("div");
			tempcontainer.setAttribute("id","dialogContainer");
			document.body.appendChild(tempcontainer);
		}
	},
	changeContent:function(content) {
		this.getElement().innerHTML = ""; // limpar tudo
		if (typeof(content)=="string") {
			this.getElement().innerHTML = content;
		} else {
			this.getElement().appendChild(content);
		}
		this.recalculatePosition();
	},
	createShadow:function() {
		if (!$("shadowDiv")) {
			this.shadowElement = document.createElement("div");
			this.shadowElement.setAttribute("id","shadowDiv");
			this.container.appendChild(this.shadowElement);
		} else {
			this.shadowElement = $("shadowDiv");
		}
		if (this.useShadow) {
			this.showShadow();
		}
	},
	showShadow:function() {
		if (this.useShadow) {
			try { this.shadowElement.style.display="block"; } catch(ex) {};
		}
	},
	hideShadow:function() {
		if (DIALOGS.canHideShadow(this)) {
			try { this.shadowElement.style.display="none"; } catch(ex) {};
		}
	},
	getID:function() {
		return "dialogBox"+this.id;
	},
	getContainerElement:function() {
		return $(this.getID());
	},
	getTitleElement:function() {
		return $(this.getID()+"Title");
	},
	setTitle:function(t) {
		this.getTitleElement().innerHTML = t;
	},
	getTitleButtonsElement:function() {
		return $(this.getID()+"Buttons");
	},
	getElement:function() {
		return $(this.getID()+"Content");
	},
	getElementID:function() {
		return this.getID()+"Content";
	},
	getCloseButton:function() {
		var btn = document.createElement("div");
		btn.className = "dialogTitleButton";
		Event.observe(btn, 'click', this.destroy.bindAsEventListener(this));
		var img = document.createElement("img");
		img.src = "http://www.sfm.pt/imagens/btn_close.png";
		img.setAttribute("width","9");
		img.setAttribute("height","9");
		btn.appendChild(img);
		return btn;
	},
	getButtonSkin:function() {
		var skin = document.createElement("div");
		skin.className = "dialogButtonContainer";
		return skin;
	},
	getButtonTemplate:function() {
		var template = document.createElement("input");
		template.className = "dialogButton";
		template.setAttribute("type","button");
		return template;
	},
	getDefaultButton:function(text) {
		var skin = this.getButtonSkin();
		var template = this.getButtonTemplate();
		template.setAttribute("value",text);
		skin.appendChild(template);
		Event.observe(template, 'click', this.destroy.bindAsEventListener(this));
		return skin;
	},
	getExtraButtons:function() {
		var buttonHolder = document.createElement("div");
		buttonHolder.className = "dialogContentButtons";
		switch (this.tipo) {
			case "ok":
				buttonHolder.appendChild(this.getDefaultButton("ok"));
				break;
			case "yesno":
				break;
			case "yesnocancel":
				break;
			case "confirm":
			case "yescancel":
				break;
			case "custom":
			default:
				buttonHolder = false;
				break;
		}
		return buttonHolder
	},
	draw:function() {

		//top
		var bt = document.createElement("div");
		bt.className = "bt";
		var emptyDiv = document.createElement("div");
		bt.appendChild(emptyDiv);
		//---

		//content
		var i1 = document.createElement("div");
		var i2 = document.createElement("div");
		var i3 = document.createElement("div");
		var conteudoContainer = document.createElement("div");
		var titleContainer = document.createElement("div");
		var titleButtons = document.createElement("div");
		var titleText = document.createElement("div");
		i1.className = "i1";
		i2.className = "i2";
		i3.className = "i3";
		conteudoContainer.className = "dialogContent";
		conteudoContainer.setAttribute("id",this.getID()+"Content");
		conteudoContainer.innerHTML = this.conteudo;
		var extraButtons=this.getExtraButtons();
		if (extraButtons!==false) {
			conteudoContainer.appendChild(extraButtons);
		}
		titleContainer.className = "dialogTitle";
		titleText.className = "dialogTitleText";
		titleText.setAttribute("id",this.getID()+"Title");
		titleText.innerHTML = this.titulo;
		titleButtons.className = "dialogTitleButtons";
		titleButtons.setAttribute("id",this.getID()+"Buttons");
		titleButtons.appendChild(this.getCloseButton());
		titleContainer.appendChild(titleText);
		titleContainer.appendChild(titleButtons);
		i3.appendChild(titleContainer);
		i3.appendChild(conteudoContainer);
		i2.appendChild(i3);
		i1.appendChild(i2);

		//bottom
		var bb = document.createElement("div");
		bb.className = "bb";
		var emptyDiv = document.createElement("div");
		bb.appendChild(emptyDiv);
		//------

		//custombox
		var cb = document.createElement("div");
		cb.className = "cb" + (this.fullscreen?" cbfullscreen":"") + (this.specialClass?' '+this.specialClass:'');
		cb.style.width= this.width+"px";
		cb.id = this.getID();
		cb.setAttribute("id",this.getID());
		cb.appendChild(bt);
		cb.appendChild(i1);
		cb.appendChild(bb);
		//---------
		this.container.appendChild(cb);
		if (!this.fullscreen) Drag.init(titleContainer,cb);
	},
	destroy:function() {
		this.hide();
		this.container.removeChild(this.getContainerElement());
		DIALOGS.del(this);
		try {
			eval(this.eventFunction);
		} catch(ex) {};
	},
	setShadowOpacity: function(val){
		this.shadowElement.style.zoom = 1; // IE FIX
		this.shadowElement.style.opacity = ((val>0)?(val/100):0);
		this.shadowElement.style.filter = "alpha(opacity='"+parseInt(val)+"')";
	},
	show: function() {
		this.getContainerElement().style.display = "block";
		this.showShadow();
	},
	hide: function() {
		this.getContainerElement().style.display = "none";
		this.hideShadow();
	},
	isVisible:function() {
		return !(this.getContainerElement().style.display == "none");
	}
});
