var FLDialog = {

	srcs: {},

	show: function(id, target){
		var win = FLDialog.get(id);
		win.show(target);
		win.toFront();
	},
	
	register: function(config){
		Ext.onReady(function(){FLDialog._register(config);});
	},
	
	_register: function(config){
		config["id"] = config["id"] || "win01";
        config["shadow"] = (config["shadow"] != null) ? config["shadow"] : true;
        config["modal"] = (config["modal"] != null) ? config["modal"] : true;
        config["draggable"] = (config["draggable"] != null) ? config["draggable"] : true;
        config["fixedcenter"] = (config["fixedcenter"] != null) ? config["fixedcenter"] : false;
        config["proxyDrag"] = (config["proxyDrag"] != null) ? config["proxyDrag"] : true;
        config["resizable"] = (config["resizable"] != null) ? config["resizable"] : true;
        config["height"] = (config["height"]==null || config["height"]==0) ? 350 : config["height"];
        config["width"] = (config["width"]==null || config["width"]==0) ? 600 : config["width"];
        config["minHeight"] = (config["minHeight"]==null || config["minHeight"]==0) ? 150 : config["minHeight"];
        config["minWidth"] = (config["minWidth"]==null || config["minWidth"]==0) ? 200 : config["minWidth"];
		config["textBtnClose"] = (config["textBtnClose"] != null) ? config["textBtnClose"] : "Fechar";
	
		var myDialog = Ext.DialogManager.get(config["id"]);
		if (myDialog != null)
			return myDialog;
		
		var el = null;
		if (config["src"] == null || config["src"] == ''){
			el = document.getElementById(config["html"]);
			if (el != null)
				el.parentNode.removeChild(el);
		}else{
			this.srcs[config["id"]] = config["src"];
		}
			
		if (typeof(config["animateTarget"]) == 'string')
			config["animateTarget"] = document.getElementById(config["animateTarget"]);
		
		config["autoCreate"] = {
			tag:'div',
			cls:'x-dlg',
			children:[ {
				tag:'div',
				id: 'my-dlg-hd',
				cls:'x-dlg-hd',
				html:config["title"]
			},{
				tag:(el == null) ? 'iframe' : 'div',
				cls:'x-dlg-bd',
				frameborder: 0,
				id: config["id"] + '-iframe',
				name: config["id"] + '-iframe',
				src: "about:blank",
				html: (el == null) ? null : el.innerHTML
			},{
				tag:'div',
				id: 'my-dlg-ft',
				cls:'x-dlg-ft'
			}]
		};

		myDialog = new Ext.BasicDialog(config["id"], config);
		config["functionBtnClose"] = (config["functionBtnClose"] != null) ? config["functionBtnClose"] : myDialog.hide;
		if (config["visibleButtons"]["buttons"] != "none"){
			for (var b in config["visibleButtons"]){
				var fn = config["visibleButtons"][b]["fn"];
				var scope = config["visibleButtons"][b]["scope"];
				myDialog.addButton(b, fn, scope);
			}
			myDialog.addButton(config["textBtnClose"], config["functionBtnClose"], myDialog);
		}
		myDialog.addKeyListener(27, myDialog.hide, myDialog);
		var winFrame = null;
		if (config["src"] != null && config["src"] != ""){
			winFrame = window.frames[myDialog.id + "-iframe"];
            myDialog.on('hide', function(){
				winFrame.location = "about:blank";
            });
            myDialog.on('beforeshow', function(){
				winFrame.location = FLDialog.srcs[myDialog.id];
            });
		}
		myDialog.setElementValue = function(elementId, property, value){
			var cmd = "window.frames['" + myDialog.id + "-iframe'].document.getElementById('" + elementId + "')." + property + " = " + value + ";";
			eval(cmd);
		};		
	},

	get: function(id){
		return Ext.DialogManager.get(id);
	},
	
	hide: function(id){
		Ext.DialogManager.get(id).hide();
	}
};
