FivecomUtil = {
	
	showLoading:
		function() {
			if (typeof loadingQt == "undefined")
				loadingQt=0;

			loadingQt++;
				
			$('#loading').show();
		},
	hideLoading:
		function() {
			//Ext.onReady(function() {
				if (typeof loadingQt == "undefined")
					loadingQt=0;
	
				loadingQt--;
				if (loadingQt<0)
					loadingQt=0;
				if (loadingQt == 0)
					$('#loading').hide();
			//});
		},
	include:
		function(url, div) {
			$(document).ready(function() {
				FivecomUtil.loadHtml(url, div);
			});
		},
	buildScriptSize: 
		function(div) {
			var script = "";
			script += "<script type=\"text/javascript\">\n";
			script += "Ext.onReady(";
			script += "function() { \n";
			script += "var jqScroll = $('" + div + " #scrolled'); \n";
			script += "if (jqScroll.size() > 0) { \n";
			script += "var jqFixed = $('" + div + " #fixed'); \n";
			script += "var heightFixed = jqFixed.height(); \n";
			script += "heightFixed = heightFixed.replace('px', ''); \n";
			script += "heightFixed = parseInt(heightFixed); \n";
			script += "var extCmp = jqScroll.parent().attr('id'); \n";
			script += "var cmp = Ext.getCmp('ext_' + extCmp); \n";
			script += "if (typeof cmp != 'undefined') { \n";
			script += "var height = cmp.getInnerHeight(); \n";
			script += "var width = cmp.getInnerWidth(); \n";
			script += "jqScroll.css('height', (height-heightFixed-2) + 'px'); \n";
			//script += "jqScroll.css('width', (width-2) + 'px'); \n";
			script += "var jqScrollDivGrid = $('" + div + " #divconteudoTopGrid'); \n";
			script += "if(jqScrollDivGrid) \n";
			script += "jqScrollDivGrid.height(height-heightFixed-2 + 'px'); \n";
			//script += "jqScrollDivGrid.width((width) + 'px'); \n";
			script += "} \n";
			script += "} \n";
			script += "}\n";
			script += ");\n";
			script += "</script>\n";
	
			return script;
		},	
	splitV:
		function (enable) {
			var menu = Ext.getCmp('ext_menu');
			if (menu) {
				if (enable == false)
					menu.hide();
				else
					menu.show();
				Ext.getCmp('viewport').render();
			}
		},
	splitH:
		function (enable) {
			var bottom = Ext.getCmp('ext_conteudoBottom');
			if (bottom) {
				if (enable == false)
					bottom.hide();
				else
					bottom.show();
				Ext.getCmp('viewport').render();
			}
		},
	submitForm:
		function(form, div) {

			var frm = $(form).get(0);
			
			if (frm.e)
				if (frm.e.value == 'E')
					if (confirm('Confirma a exlusão?') == false)
						return;

			var data = $(form).serialize();// + "&div=" + div;
			data = data.replace("%E2%80%93", "-");
			data = data.replace("%E2%80%9D", "%22");
			data = data.replace("%E2%80%9C", "%22");
			$.ajax( {
				url: $(form).attr("action"),
				type: $(form).attr("method"),
				data: data,
				cache: true,
				async: true,
				beforeSend: function(xmlhttprequest){
					FivecomUtil.showLoading();
				},
				complete: function(xmlhttprequest) {
					FivecomUtil.hideLoading();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					FivecomUtil.hideLoading();
				},
				success: function(html){
					FivecomUtil.hideLoading();
					if (div == "#MsgBox")
						FivecomUtil.createMsgBox("MsgBox", 400, 300);

					var jq = $(div);
					var script = "";
					if (typeof scriptOnReady != "undefined")
						script = scriptOnReady;
					else 
						script = FivecomUtil.buildScriptSize(div);
					jq.html(script + html);

					//$(div).html(html);
				}
			});
		},
	openModule:
		function(id, moduleUrl, topUrl, bottomUrl) {
			$("#menu").html("");
			$("#conteudoTop").html("");
			$("#conteudoBottom").html("");
			if (moduleUrl != "")
				this.splitV(true);
			else
				this.splitV(false);
				
			$('#navBar .ativo').removeClass();
			$('#navBar #' + id).addClass('ativo');
			
			if (moduleUrl != "") {
				this.loadHtml(moduleUrl, '#menu');
			}
			this.openFunction(topUrl, bottomUrl);
		},
	openSubModule:
		function(id, description, image, treeViewUrl, topUrl, bottomUrl) {
			$("#conteudoTop").html("");
			$("#conteudoBottom").html("");
			$('#frameCenterLeftBoxModuloTreeview').hide();
			$('#frameCenterLeftBoxModuloList').show();
			$('#frameCenterLeftBoxModuloToolBar').show();
			if (treeViewUrl != "") {
				$('#frameCenterLeftBoxModuloToolBar li').show();
				$('#frameCenterLeftBoxModuloToolBar #' + id).hide();
				$('#frameCenterLeftBoxModuloList').hide();
				$('#frameCenterLeftBoxModuloTreeview').show();
				this.loadHtml(treeViewUrl, '#frameCenterLeftBoxModuloTreeviewBox');
			}
			else
				$('#frameCenterLeftBoxModuloToolBar').hide();
			$('#frameCenterLeftBox .ativo').removeClass();
			$('#frameCenterLeftBox .cabecalho').html('<img src="' + image + '" alt="' + description + '" width="26" height="26" align="absmiddle" /> ' + description);
			$('#frameCenterLeftBoxModuloToolBar #' + id).addClass('ativo');
			$('#frameCenterLeftBoxModuloList #' + id).addClass('ativo');
			this.openFunction(topUrl, bottomUrl);
		},
	openFunction:
		function(topUrl, bottomUrl) {
			$("#conteudoTop").html("");
			$("#conteudoBottom").html("");
			if (bottomUrl != "")
				this.splitH(true);
			else
				this.splitH(false);

			if (topUrl != "")
				this.loadHtml(topUrl, '#conteudoTop');
			else
				$('#conteudoTop').html('');

			this.openFunctionBottom(bottomUrl);
		},
	openFunctionBottom:
		function(bottomUrl) {
			
			if (bottomUrl != "")
				this.splitH(true);
			else
				this.splitH(false);

			if (bottomUrl != "")
				this.loadHtml(bottomUrl, '#conteudoBottom');
		},
	getBrowser:
		function() {
			if( typeof( window.innerWidth ) == 'number' )
				return 0; //Firefox
			else
				return 1; //IE
		},
	clickToolBar:
		function (idForm, evento) {

			eventoOld = $(idForm).get(0).e.value;
			if (evento != "")
				$(idForm).get(0).e.value=evento;
			
			if((evento == "EG") || (evento == "E"))
				if(!confirm("Deseja Excluir o(s) Registro(s) Selecionado(s)?"))
					return false;
			this.submitForm(idForm, "#MsgBox");

			if (evento != "")
				$(idForm).get(0).e.value=eventoOld;

		},
	setMidiaToHtmlEdit:
		function (field, value) {
			window.opener.setMidiaToHtmlEdit(field, value);
				//window.close();
		},
	initEditor:
		function (cd_sisma, field, width, height, openFiveMidia) {
			
			//$(document).ready ( function() {
			Ext.onReady( function() {
					if (typeof openFiveMidia == "undefined")
						openFiveMidia = true;

					if (typeof height == "undefined")
						height = 400;

					if (typeof width == "undefined")
						width = 700;

					if ((typeof cd_sisma == "undefined") || (typeof field == "undefined"))
						return;
		
					//Ext.onReady( function() {  
										  
						Ext.form.HtmlEditorFivecom = Ext.extend(Ext.form.HtmlEditor, {

							//essa funcao foi necessaria refaze-la (herdar), pois o on fivemidia estava com bug.
							//foi trocado as chamadas dos medodos execCmd para relayCmd
							insertAtCursor : function(text){
								if(!this.activated){
									return;
								}
								if(Ext.isIE){
									this.win.focus();
									var r = this.doc.selection.createRange();
									if(r){
										r.collapse(false);
										r.pasteHTML(text);
										(function(){
											this.syncValue();
											this.deferFocus();
										}).defer(10, this);

									}
								}else if(Ext.isGecko || Ext.isOpera){
									this.win.focus();
									this.relayCmd('InsertHTML', text);
									this.deferFocus();
								}else if(Ext.isSafari){
									this.relayCmd('InsertText', text);
									this.deferFocus();
								}
							},
																
							createToolbar : function(editor) {
								Ext.form.HtmlEditorFivecom.superclass.createToolbar.call(this, editor);
								
								if (openFiveMidia == true) {
									this.tb.add(
										'-',
										{
											itemId : 'insertMidia',
											cls : 'x-btn-icon x-edit-'+'insertMidia',
											enableToggle:false !== false,
											scope: editor,
											handler:function() { FivecomUtil.openFivemidia(cd_sisma, field) }||editor.relayBtnCmd,
											clickEvent:'mousedown',
											tooltip: this.tipsEnabled ? editor.buttonTips[id] || undefined : undefined,
											tabIndex:-1,
											icon: 'imagens/toolbar/picture.gif',
											tooltip: 'inserir Midias (imagens/videos/embed)'
										}
									);
								}
							}
							
						});
						
						Ext.reg('htmleditorfivecom', Ext.form.HtmlEditorFivecom);
		
						var ed = new Ext.form.HtmlEditorFivecom({
							height: height,
							width: width,
							//autoWidth : true,
							id:"htmledit_" + field,
							enableFormat:true,
							enableAlignments:true,
							enableFont:true,
							enableFontSize:true,
							enableLinks:true,
							enableLists:true,
							enableSourceEdit:true,
							applyTo: field,
							layout: "fit",
							listeners: { 
								'sync':function(htmlEditor, e) { 
									//alert('sync');
								},
								'push':function(htmlEditor, e) { 
									//alert('push');
								}								
							}
						});
						
		
					//}); 

			});
			
		},
	createMsgBox:
		function (id, width, height) {
			
			width = parseInt(width);
			height = parseInt(height);
			
			if (typeof _zindex == "undefined")
				_zindex=500000;
			else
				_zindex++;
				
			$('#div' + id).html('');
			$('#div' + id).remove();
			
			var html = '';
			html += '<div id="div' + id + '" style="display:none;z-index:' + (_zindex) + ';">\n';
			html += '        <div style="border:solid 1px #0f6189;z-index:' + (_zindex + 3) + ';position:absolute;background-color:#FFFFFF;overflow:auto;padding: 10px 10px 10px 10px;top:50%;left:50%;height:' + parseInt((parseInt(height) + 0)) + 'px;width:' + width + 'px;margin-left:-' + width/2 + 'px; margin-top:-' + parseInt(height + 0)/2 + 'px;">\n';
			html += '           <div style="position:absolute; width:' + width + 'px; text-align:center; padding:4px 0 4px 0;text-align:right;"><a href="javascript:;" onclick="javascript:$(\'#div' + id + '\').remove();return false;"><img src="imagens/ics_exit.gif" alt="" title="Fechar janela"></a></div>\n';
			html += '        	<div id="' + id + '" style="position:absolute;left:10px;height:' + (height-32) + 'px;width:' + width + 'px; top:43px; overflow:auto;"></div>\n';
			html += '        </div>\n';
			html += '    <div style="z-index:' + (_zindex) + ';width:100%;position:absolute;height:5000px;background-color:#CCCCCC;opacity: .5;filter: Alpha(Opacity=50);-moz-opacity: 0,5;top:0;left:0;" onclick="javascript:$(\'#div' + id + '\').remove();"></div>\n';
			html += '</div>\n';
			html += '<script>\n';
			html += 'window.document.onkeydown = function (e) {\n';
			html += '	var code;\n';
			html += '	if (!e) var e = window.event;\n';
			html += '	if (e.keyCode) code = e.keyCode;\n';
			html += '	else if (e.which) code = e.which;\n';
			html += '	if (code == 27) {\n';
			html += '		$("#div' + id + '").remove();\n';
			html += '		window.document.onkeydown = void(0);\n';
			html += '	}\n';
			html += '}\n';
			html += '</script>\n';
			var div = jQuery(html);
			div.prependTo(document.body);
			
			$('#div' + id).show();
			//$('#div' + id).bgiframe();
		},
	closeMsgBox:
		function (id) {
			id = id.replace("#", "");
			FivecomUtil.showLoading();
			$('#div' + id).html('');
			$('#div' + id).remove();
			FivecomUtil.hideLoading();
		},    
	openMsgBox:
		function (id, url, width, height) {
			FivecomUtil.createMsgBox(id, width, height);
			FivecomUtil.loadHtml(url, "#" + id, "");
		},    
	openPopup:
		function (name, title, url, height, width, extra) {
			if (typeof extra == "undefined")
				extra = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no';
			
			winLeft = (screen.width-width)/2;
			winTop = (screen.height-(height+110))/2;
			
			extra = extra + ',left=' + winLeft + ',top=' + winTop + ',width=' + width + ',height=' + height;
			window.open(url,name,extra);  
		},
	openSite:
		function (idCd_site, idDs_site) {
			var lnk = "";
			lnk = "index.php?id=/apps/_commons/buscarSite.php";
			
			if (typeof idCd_site == "undefined")
				idCd_site = "";

			if (typeof idDs_site == "undefined")
				idDs_site = "";

			if ((idCd_site == "") && (idDs_site == "")) {
				alert("passagem de parametros incorretos: idCd_site, idDs_site");
				return;
			}
				
			lnk = lnk + "&idcd_site=" + idCd_site;
			lnk = lnk + "&idds_site=" + idDs_site;
			FivecomUtil.openPopup("site", "site", lnk, 400, 600);
		},		
	openFivemidia:
		function (cd_sisma, field, cd_matia) {
			var lnk = "";
			lnk = "index.php?id=/apps/fivemidia/index.php&cd_sisma=" + cd_sisma;
			
			if (typeof field == "undefined")
				field = "";
			if (field != "")
				lnk = lnk + "&field=" + field;

			if (typeof cd_matia == "undefined")
				cd_matia = "";
			if (cd_matia != "")
				lnk = lnk + "&cd_matia=" + cd_matia;
				
			if (!(typeof funcOnClose == "undefined"))
				functionOnClose = funcOnClose;

			FivecomUtil.openPopup("midia", "midia", lnk, 600, 800);
		},
	openFivemidia2:
		function (cd_sisma, field, cd_matia, id_tpmid_tipo) {
			var lnk = "";
			lnk = "index.php?id=/apps/fivemidia/template2/midia.grid.php&cd_sisma=" + cd_sisma;
			
			if (typeof field == "undefined")
				field = "";
			if (field != "")
				lnk = lnk + "&field=" + field;

			if (typeof cd_matia == "undefined")
				cd_matia = "";
				
			if (cd_matia != "")
				lnk = lnk + "&cd_matia=" + cd_matia;
				
			if (cd_matia != "")
				lnk = lnk + "&id_tpmid_tipo=" + id_tpmid_tipo;				
				
			if (!(typeof funcOnClose == "undefined"))
				functionOnClose = funcOnClose;

			FivecomUtil.openMsgBox("midia", lnk, 850, 550);
		},
	openFunctionPopup:
		function(Url, title, width, height) {
			var div = "function" + Math.random()*32767;
			div = div.replace(".", "");
			
			if (typeof width == "undefined")
				width = 800;

			if (typeof height == "undefined")
				height = 500;
			
			var win = new Ext.Window({
				id: 'ext_' + div,
				title    : title,
				closable : true,
				constrainHeader : true,
				autoSize : true,
				//autoHeight: true,
				resizable: true,
				maximizable : true,					
				minimizable : true,
				autoScroll: true,
				onEsc: Ext.emptyFn,
				width    : width,
				height   : height,
				plain    : false, //transparent
				layout   : 'fit',
				listeners: { 
					'show':function() { 
						FivecomUtil.loadHtml(Url, "#" + div);
					},
					'show':function() { 
						FivecomUtil.loadHtml(Url, "#" + div);
					},
					'resize':function() { 
							var jq = $("#" + div + " #scrolled");
							var jqFixed = $("#" + div + " #fixed");
							if (jq.size() > 0) { 
								var height = Ext.getCmp('ext_' + div).getInnerHeight();
								var heightFixed = jqFixed.height();
								heightFixed = heightFixed.replace('px', '');
								heightFixed = parseInt(heightFixed);
								jq.height(height-heightFixed-1);
							}
					}
				},
				items: {
					html: '<div id="' + div + '"></div>',
					border: false
				}
			});
			win.show();
			var vetPos = win.getPosition();
			var str = String(Math.random()*32767);
			var deslocamento = parseInt(str.substr(0,2));
			deslocamento = deslocamento + 20;
			win.setPosition(vetPos[0] + deslocamento, vetPos[1] + deslocamento);
		},
	closeFunctionPopup:
		function(div) {
			var idpop = div.replace("#", "");
			idpop = "ext_" + idpop;
			var popWindow = Ext.getCmp(idpop);
			if (popWindow)
				popWindow.close();
		},
	loadHtml:
		function(Url, div, scriptOnReady) {
			$.ajax({
				url: Url,
				type: "GET",
				data: {"div":div},
				cache: true,
				async: true,
				beforeSend: function(xmlhttprequest){
					FivecomUtil.showLoading();
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
				  
				},
				complete: function(xmlhttprequest){
					FivecomUtil.hideLoading();
				},
				success: function(html){
					if (div == "#MsgBox")
						FivecomUtil.createMsgBox("MsgBox", 400, 300);

					var jq = $(div);
					var script = "";
					if (typeof scriptOnReady != "undefined")
						script = scriptOnReady;
					else
						script = FivecomUtil.buildScriptSize(div);
					jq.html(script + html);
					FivecomUtil.hideLoading();
				}
			});
		},
	openMatia:
		function(Url) {
			FivecomUtil.openFunctionPopup(Url, 'Matia', 900, 500);
		},
	buildOptionsByAjax:
		function(comboId, table, field, fieldList, where) {
			FivecomUtil.loadHtml('index.php?id=/buildOptions.php&table=' + table + '&where=' + where + '&field=' + field + '&fieldList=' + fieldList, comboId);
		}
};