// JavaScript Document

function selection(obj, set_text_default){   
    /* @author Bernardo Rufino  
     * @license Creative Commons Developing Nations: http://creativecommons.org/licenses/devnations/2.0/  
     * @description Function to manipulate the actual selection  
     * @returns It returns an object with an attribute *text* with the current selection text and  
     *        a method *setText()* that receives one argument, the text to be set in the selection.  
     * @parameters The first is the object where you want the selection (DOM or String with id), and  
     *           the second (optional), a boolean, if is true then the *setText()* method will  
     *           append the text if there's no way to replace the selection.  
     *  
     *     !Nao retire essas informacoes!  
     *     !Do not drop this information!  
    */  
    if(obj.constructor == String){
	
		obj = document.getElementById(obj);
	
	}   
	
    var set_text = (set_text_default) ? function(text){obj.value += text;} : function(){return false;};   
    var selection = {text: null, setText: set_text};   
    if(document.selection){   
        
		var range = document.selection.createRange();   
		if (range.text !=""){
		
		selection.text = range.text;   
		selection.setText = function(text){   
		range.text = text.replace(/\\r?\\n/g, "\\r\\n");   
			
		}
		}
    } else if(typeof(obj.selectionStart) != "undefined"){   
        
		selection.text = obj.value.substring(obj.selectionStart, obj.selectionEnd);   
        selection.setText = function(text){   
			if(text != null && text != ""){
            obj.value = obj.value.substring(0, obj.selectionStart) + text + obj.value.substring(obj.selectionEnd);   
			}
        }   
    } else if(window.getSelection){   
        selection.text = window.getSelection().toString();   
		 
    }   
	
    return selection;   
}  

function handleEnter (field, event) {
        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        if (keyCode == 13) {
            field.value += "<br>"
        } 
    }      


function SetBold(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<b>" + selected.text + "</b>");  
	}
}

function SetItalic(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<i>" + selected.text + "</i>");  
	}
}

function SetUnderline(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<u>" + selected.text + "</u>");  
	}
}
function SetCenter(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<center>" + selected.text + "</center>");  
	}
}
function SetRight(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<div align='right'>" + selected.text + "</div>");  
	}
}
function SetLeft(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<Div align='left'>" + selected.text + "</div>");  
	}
}




function ajaxInit() {
	var req;
	try {
		 req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
		 try {
			  req = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch(ex) {
		 try {
		   req = new XMLHttpRequest();
	     } catch(exc) {
	 		  alert("Esse browser não tem recursos para uso do Ajax");
	  		 req = null;
	     }
	 }
}
return req;
}

function PreviewPanel(texto) {
	ajax = ajaxInit();
	
	if(ajax) {
			ajax.open("GET","PreviewPanel.asp?texto=" + texto, true);
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {
					if(ajax.status == 200) {
						
						document.getElementById("PreviewPanel").innerHTML = ajax.responseText;
						
					}else{	
						alert(ajax.statusText)
					}
				}
			}
			ajax.send(null);
		
	}
}

function verificaEmail(email) {
	ajax = ajaxInit();
	if(ajax) {
		ajax.open("GET","verifica_email.asp?email=" + email, true);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					
					document.getElementById("alerta").value = ajax.responseText;
					if(document.getElementById("alerta").value == 'N'){
						document.getElementById("textoAlerta").style.display = "";
					}
					
				}else{	
					alert(ajax.statusText)
				}
			}
		}
		ajax.send(null);		
	}
}

function Confirm(text, url) {
	var answer = confirm(text)
	if (answer){
		location.href = url;
		
	}
	else{
		
	}
}

function ConfirmForm(text) {
	var answer = confirm(text)
	if (answer){
		return true;
		
	}
	else{
		return false;
	}
}

var win = null;
function NewWindowPanel(valor){
	
	LeftPosition = (screen.width) ? (screen.width-400)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-300)/2 : 0;
	settings =
	'height=300,width=400,top='+TopPosition+',left='+LeftPosition+',scrollbars=Yes,resizable'
	win = window.open("PreviewPanel.asp?texto="+valor,"",settings)
}



var win = null;
function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
	win = window.open(mypage,myname,settings)
}

function foco(){
	document.forms[0].elements[0].focus();
}


function carregaCombo(id,url,codigo) {
	ajax = ajaxInit();
	url_combo = url + "codigo=" + codigo
	if(ajax) {
		
		ajax.open("GET",url_combo, true);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					document.getElementById(id).innerHTML = ajax.responseText;					
				}else{	
					alert(ajax.statusText)
				}
			}
		}
		ajax.send(null);		
	}
}

function isInt(id)
{
   	element = document.getElementById(id)
	var str = /^\d+$/;
	return str.test(valor_text);
}


function formatar(id, mask)
{
	src = document.getElementById(id);
	var i = src.value.length;
  	var saida = mask.substring(0,1);
  	var texto = mask.substring(i)
	if (texto.substring(0,1) != saida)
  	{
    	src.value += texto.substring(0,1);
  	}
}
function formatarFone(id)
{
	src = document.getElementById(id);
	var i = src.value.length;
	if(i==1)
	{
		src.value = "(" + src.value;
	}
	if(i==3)
	{
		src.value = src.value + ") " 
	}
	if(i==9)
	{
		src.value = src.value + "-" 
	}
	
	
}

function insNumeric(obj,event)
{
    var tecla = event.charCode;
    var ie = event.keyCode;
    if (!event) event = window.event;
    var code;
    if (event.keyCode) code = event.keyCode;
    else if (event.which) code = event.which; // Netscape 4.?
//se nao for número nem parentesis ou espaço
    if ((code < 48 || code > 59) && (code != 8 ) ){
      event.returnValue = false;
      if (event.which){
        event.preventDefault();
      }
      return false;
    }else{
      event.returnValue = true;
      return true;
    }
 }
function insFloat(obj,event)
{
    var tecla = event.charCode;
    var ie = event.keyCode;
    if (!event) event = window.event;
    var code;
    if (event.keyCode) code = event.keyCode;
    else if (event.which) code = event.which; // Netscape 4.?
//se nao for número nem parentesis ou espaço
    if ((code < 48 || code > 59) && (code != 8 ) & (code != 44)){
      event.returnValue = false;
      if (event.which){
        event.preventDefault();
      }
      return false;
    }else{
      event.returnValue = true;
      return true;
    }
 } 

function isFloat(valor)
{
	var valor_text = valor;
	var str = /^[+-]?((\d+|\d{1,3}(\.\d{3})+)(\,\d*)?|\,\d+)$/;
	return str.test(valor_text);
}
function ocultaMenu(Id)
{
	
	img = document.getElementById(Id.replace("vit_","img_"));
	div_modulo = document.getElementById(Id);
	var aux = div_modulo.style.display;
	if (aux == "none")
	{
		div_modulo.style.display = "block";
		if (img)
		{
		    img.src = "imagens/-.gif";
		    img.alt = "Ocultar"
		}
	}
	if (aux == "block")
	{
		div_modulo.style.display = "none";
		if (img)
		{
		    img.src = "imagens/+.gif";
		    img.alt = "Mostrar"
		}
	}
}
function janelaUpload()
{
	winDt = window.open('conteudo_upload_arquivo.asp','','top=350,left=400,width=550,height=150,scrollbars=1,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1');
	
	winDt.focus();
}   
function janelaTexto(wurl)
{
	winDt = window.open(wurl,'','top=100,left=200,width=800,height=500,scrollbars=1,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1');
	winDt.focus();

}

function listarConteudo(tipo,codigo,id) {
	ajax = ajaxInit();
	url = "combo_conteudos_ordena.asp?"
	url_combo = url + "codigo=" + codigo + "&tipo=" + tipo
	if(ajax) {
		
		document.getElementById(id).className = "fundoCarregando"
		ajax.open("GET",url_combo, true);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					document.getElementById(id).innerHTML = ajax.responseText;	
					document.getElementById(id).className = "fundoNormal"				
				}else{	
					alert(ajax.statusText)
					document.getElementById(id).className = "fundoNormal"
				}
			}
		}
		ajax.send(null);		
	}
}
function ordenar(tipo)
{
	var pos = 0
	var valorAux = ""
	var textAux = ""
	objSelect = document.getElementById("lista")
	for(i=0;i<objSelect.options.length;i++)
	{
		if(objSelect.options[i].selected == true)
		{
			pos = i;
			break
		}
	}
	if(tipo == "s")
	{
		if(parseInt(pos) - 1 >= 0)
		{
			valorAux = 	objSelect.options[parseInt(pos) - 1].value;
			textAux = objSelect.options[parseInt(pos) - 1].text;
			
			objSelect.options[parseInt(pos) - 1].value = objSelect.options[pos].value
			objSelect.options[parseInt(pos) - 1].text = objSelect.options[pos].text
			
			objSelect.options[pos].value = valorAux
			objSelect.options[pos].text = textAux
			
			objSelect.options[parseInt(pos) - 1].selected = true
			objSelect.options[pos].selected = false
		}
	}
	else
	{
		if(parseInt(pos) + 1 < objSelect.options.length)
		{
			valorAux = 	objSelect.options[parseInt(pos) + 1].value;
			textAux = objSelect.options[parseInt(pos) + 1].text;
			
			objSelect.options[parseInt(pos) + 1].value = objSelect.options[pos].value
			objSelect.options[parseInt(pos) + 1].text = objSelect.options[pos].text
			
			objSelect.options[pos].value = valorAux
			objSelect.options[pos].text = textAux
			
			objSelect.options[parseInt(pos) + 1].selected = true
			objSelect.options[pos].selected = false
			
		}		
	}
	
}
function salvarOrdem()
{
	var url = ""
	objSelect = document.getElementById("lista")
	for(i=0;i<objSelect.options.length;i++)
	{
		url = url + "&codigo_"+ i + "=" + objSelect.options[i].value + "&pos_" + i + "=" + i
	}	
	url = url + "&total="+objSelect.options.length + "&tipo="+ document.getElementById("tipoLista").value
	ajax = ajaxInit();
	url_combo = "combo_conteudos_ordena.asp?local=ordenar" + url
	
	if(ajax) {
		
		
		ajax.open("GET",url_combo, true);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					alert("Alteração realizada com sucesso")				
				}else{	
					alert(ajax.statusText)
				}
			}
		}
		ajax.send(null);		
	}
	
}
function incluirFundoOpaco(idFundo)
{
	$("#"+idFundo).remove()
	$("<div id='" + idFundo + "' class='fundoOpaco'>&nbsp;</div>").appendTo("body")	
	objF = document.getElementById(idFundo)
	objF.style.width = dimensoesTela().split("x")[0] + "px"
	objF.style.height = dimensoesTela().split("x")[1] + "px"
		

}
function dimensoesTela()
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;


	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	scrollPage = false
	if(yScroll <= windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
		scrollPage = true
	}

	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	if( scrollPage )
	{
		pageWidth = parseInt(pageWidth)
	}
	
	dimensao = pageWidth + "x" + pageHeight
	return dimensao
}
function validaConteudo()
{
	if(validaFormGeral() == false)
	{
		return false
	}
	if (document.getElementById("categoria").value == "0")
	{
		alert("Selecione uma categoria");
		document.getElementById("categoria").focus;
		return false;
	}
	
	return true

}
function limparTema()
{
	str = "<select name='cod_subcategoria' id='cod_subcategoria' ><option  value='0'>Selecione</option></select>"
	document.getElementById("combo_tema").innerHTML = str
}
function fecharJanelaForm()
{
	window.close()		
	$("#fundoLista").remove()
}
function moveItemAcima(nivel)
{
	
	if( $("#" + nivel + " li[title=' ']").length > 0 )
	{
		
		pos = $("#" + nivel + " li").index($("#" + nivel + " li[title=' ']"))
		
		if( parseInt(pos)-1 >=0 )
		{
			conteudoAnterior = $("#" + nivel + " li:eq("+ (parseInt(pos)-1) +")").html()
			$("#" + nivel + " li:eq("+ (parseInt(pos)-1) +")").empty().html($("#" + nivel + " li[title=' ']").html())
			$("#" + nivel + " li[title=' ']").empty().html(conteudoAnterior)
			
			$(".listNivel").css("background","#FFF").css("color","#000")
			$(".listNivel").attr("title","")
			$("#" + nivel + " li:eq("+ (parseInt(pos)-1) +")").attr("title"," ")
			$("#" + nivel + " li:eq("+ (parseInt(pos)-1) +")").css("background","#F39814").css("color","#FFFFFF")
			posItem = parseInt(posItem) - 1
			iniciaFuncoesIcons(nivel)
			
			
		}
		
	}
	else
	{
		alert("Selecione o item para ordenar")	
	}	
	
}
function moveItemBaixo(nivel)
{
	if( $("#" + nivel + " li[title=' ']").length > 0 )
	{
		
		pos = $("#" + nivel + " li").index($("#" + nivel + " li[title=' ']"))
		
		if( parseInt(pos)+1 < $("#" + nivel + " li").length )
		{
						
			conteudoAnterior = $("#" + nivel + " li:eq("+ (parseInt(pos)+1) +")").html()
			$("#" + nivel + " li:eq("+ (parseInt(pos)+1) +")").empty().html($("#" + nivel + " li[title=' ']").html())
			$("#" + nivel + " li[title=' ']").empty().html(conteudoAnterior)
			
			$(".listNivel").css("background","#FFF").css("color","#000")
			$(".listNivel").attr("title","")
			$("#" + nivel + " li:eq("+ (parseInt(pos)+1) +")").attr("title"," ")
			$("#" + nivel + " li:eq("+ (parseInt(pos)+1) +")").css("background","#F39814").css("color","#FFFFFF")												
			iniciaFuncoesIcons(nivel)
			posItem = parseInt(posItem) - 1
			
		}
		
	}
	else
	{
		alert("Selecione o item para ordenar")	
	}	
	
}
function salvarOrdemItens(nivel)
{
	if( $("#" + nivel + " .codigos").length > 0 )
	{
		wurl = "carrega_conteudos.asp?funcao=ordenar&tipo=" + $("#" + nivel + " .codigos:eq(0)").attr("id").split("_")[0]
		for(i=0;i<$("#" + nivel + " .codigos").length;i++)
		{
			wurl += "&codigo"+ i +"=" + $("#" + nivel + " .codigos:eq("+ i +")").attr("id").split("_")[2] + "&ordem"+ i +"=" + i	
		}
		wurl += "&itens=" + $("#" + nivel + " .codigos").length					
		$("#caixaCarregando").remove()
		$("#fundoLista").remove()
		incluirFundoOpaco("fundoLista")
		$("<div id='caixaCarregando' class='caixaCarregando'><img src='imagens_adm/loading2.gif'><label>Aguarde...</label></div>").appendTo("#ins" + nivel).show()
		$.post(wurl, function(retornoPost){
			
			if( retornoPost.length > 0 )
			{
				
				$("#caixaCarregando img").hide()
				$("#caixaCarregando label").text("Alteração realizada com sucesso")
				$("<br><label><br><button type='button' alt='Fechar' id='fecharCaixa' title='Fechar' style='background:url(imagens_adm/bt_fechar.jpg);width:50px;height:25px;'></button></label>").appendTo("#caixaCarregando")
				$("#fecharCaixa").focus()
				$("#fecharCaixa,#fundoLista").click(function(){							
						
					$("#fundoLista").remove()
					$("#caixaCarregando").remove()
					
					
				})		
											
			}
							  
		})
	}
	else
	{
		alert("Nenhum item na lista")	
	}
	
}

function iniciaFuncoesList()
{
	$(".listNivel").click(function(){
		vetOp = $(this).attr("id").split("_")
		posItem = $("#"+ vetOp[1] +" li").index(this)					
		$(".listNivel").css("background","#FFF").css("color","#000")
		$("li").attr("title","")
		$(this).css("background","#F39814").css("color","#FFFFFF")
		$(this).attr("title"," ")
		
		
	})
	
}
idClickReload = ""
function iniciaFuncoesIcons(id)
{
	if( id == "")
	{
		itensLoc = ".imgLista"	
		itensLocEditar = ".imgEditar"
		itensLocExcluir = ".imgExcluir"
	}
	else
	{
		itensLoc = "#" + id + " .imgLista"		
		itensLocEditar = "#" + id + " .imgEditar"
		itensLocExcluir = "#" + id + " .imgExcluir"
	}
	
	$(itensLoc).unbind();
	$(itensLocEditar).unbind();
	$(itensLocExcluir).unbind();
	
	$(itensLoc).click(function(){		
		idClickReload = $(this).attr("id")		
		listarItens(idClickReload)
	})
	
	$(itensLocEditar).click(function(){
		vetOpLoad = $(this).attr("id").split("_")
		idElement = $(this).attr("id")
		wurl = "carrega_conteudos.asp"
		wurl += "?funcao=editar&codigo=" + vetOpLoad[3]	+ "&idClick=" + idClickReload						
		wcodido = vetOpLoad[3]	
			
		if( vetOpLoad[2] == "conteudo" )
		{
			wurl += "&tipo=" + vetOpLoad[2]	
		
			incluirFundoOpaco("fundoLista")
			
			pageWidth = dimensoesTela().split("x")[0]
			pageHeight = dimensoesTela().split("x")[1]	
			$("<div id='caixaCarregando' class='caixaCarregando'><img src='imagens_adm/loading2.gif'><label>Aguarde...</label></div>").appendTo("body")
			wleft = ( parseInt(dimensoesTela().split("x")[0]) - parseInt($("#caixaCarregando").width() ) ) / 2
			if (document.all)
			{
				var wtop = parseInt(document.documentElement.scrollTop) + 20
				
			}
			else
			{
				var wtop = parseInt(window.scrollY) + 20	
			}
			//wtop = ( parseInt(dimensoesTela().split("x")[1]) - parseInt( ) ) / 2	
			$("#caixaCarregando").css("left",parseInt(wleft))
			$("#caixaCarregando").css("top",wtop)
			$("#caixaCarregando").show()
			$("<div id='conteudoForm'/>").appendTo("body").hide()
			wleft = ( parseInt(dimensoesTela().split("x")[0]) - parseInt($("#conteudoForm").width() ) ) / 2
			$("#conteudoForm").css("left",parseInt(wleft))
			$("#conteudoForm").css("top",wtop)
			
			wurl += "&codUsuario=" + $("#codUsuario").val()
			winDt = window.open(wurl,'','top=10,left='+ wleft +',width=800,height='+ $(window).height() +',scrollbars=1,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1');
			winDt.focus();
			$("#caixaCarregando").remove()
			$("#fundoLista").click(function(){	
				$("#fundoLista").remove()
				winDt.close()
				
				
			})
		}
		else
		{
			
			incluirFundoOpaco("fundoLista")
			$("#incluirTitulo").remove()
			str = "<p id='incluirTitulo'>"
			str += "<input type='text' name='titulo' id='titulo'/>"
			str += "<img src='imagens_adm/bt_salvar.jpg' style='left:250px;' id='btnSalvarNovo'/>"
			str += "<img src='imagens_adm/bt_fechar.jpg' alt='Fechar' style='left:305px;' id='btnFecharTitulo' />"
			str += "</p>"
			
			$(str).appendTo("body").css("left",$("#"+idElement).parent("div").parent("li").offset().left).css("top",parseInt($("#"+idElement).parent("div").parent("li").offset().top)-17)
			$("#titulo").focus()
			$("#titulo").val($("#"+idElement).parent("div").parent("li").find(".codigos").text())
			$("#btnSalvarNovo").click(function(){
				if(	$("#titulo").val() != "" )
				{
					wurl = "carrega_conteudos.asp"
					wtitulo = $("#titulo").val()
					$("#titulo").val("Aguarde...")
					wurl += "?funcao=editarCategoriaSubCategoria&tipo="+ vetOpLoad[2] +"&titulo=" + wtitulo + "&codigo=" + vetOpLoad[3];
					wurl += "&codUsuario=" + $("#codUsuario").val()
					alert(wurl)
					$.post(wurl, function(retornoPost){
						if( retornoPost.length > 0 )	
						{
							$("#"+idElement).parent("div").parent("li").find(".codigos").text(wtitulo)
							$("#incluirTitulo").remove()												
							$("#fundoLista").remove()
							
						}
					})
				}
				else
				{
					alert("Preencha o titulo")	
					$("#titulo").focus()
				}
			})
			$("#btnFecharTitulo,#fundoLista").click(function(){
				$("#incluirTitulo").remove()												
				$("#fundoLista").remove()
			})	
		}
		
	})
	
	$(itensLocExcluir).click(function(){
		vetOpLoad = $(this).attr("id").split("_")
		
		wurl = "carrega_conteudos.asp"
		wurl += "?funcao=excluir&codigo=" + vetOpLoad[3] + "&tipo=" + vetOpLoad[2]	
		wurl += "&codUsuario=" + $("#codUsuario").val()
		if( confirm("Ateção, todos os conteúdos com ligação a este conteúdo também serão excluidos.\nDeseja realmente excluir este conteudo?") )
		{
			posNivel = vetOpLoad[1].replace("ulPos","")
			if( posNivel > 0 )
			{
				$("#caixaCarregando").remove()
				$("#fundoLista").remove()
				incluirFundoOpaco("fundoLista")
				$("<div id='caixaCarregando' class='caixaCarregando'><img src='imagens_adm/loading2.gif'><label>Aguarde...</label></div>").appendTo("#ulPos"+posNivel).show()
			}
			idElement = $(this).attr("id")
			$.post(wurl, function(retornoPost){
				if( retornoPost.length > 0 )
			  	{
									
					$(".insulNivel:gt("+ posNivel +")").remove()
					$("#"+idElement).parent("div").parent("li").remove()
					$("#caixaCarregando").remove()
					$("#fundoLista").remove()
			  	}
			})
		}
											  
	})
}
posItem = 0
nivelPos = ""
function listarItens(id)
{
	vetOpLoad = id.split("_")
	wurl = "carrega_conteudos.asp"
						
	
	if( vetOpLoad[2] == "categ" )
	{
		wurl += "?funcao=listar&tipo=Categ&subTipo=conteudo&codigo=" + vetOpLoad[3]
	}
	else
	{
		if( vetOpLoad[2] == "subCateg" )
		{
			wurl += "?funcao=listar&tipo=subCateg&codigo=" + vetOpLoad[3]	
		}
		if( vetOpLoad[2] == "conteudo" )
		{
			wurl += "?funcao=listar&tipo=Conteudo&codigo=" + vetOpLoad[3]
			
		}
	}
	
	posNivel = vetOpLoad[1].replace("ulPos","")					
	$(".insulNivel:gt("+ posNivel +")").remove()
	posNivel = parseInt(posNivel) + 1
	
	wurl += "&posNivel=" + parseInt(posNivel)
	
	widthConteudo = (parseInt(posNivel) + 1) * 400
	$("#insNivel").width(widthConteudo)
	
	str = "<div style='width:330px;float:left;position:relative;' id='insulPos" + posNivel + "' class='insulNivel'>"
	str += "<div style='width:300px;float:left;text-align:right;' class='btnsTopo'>"
	str += "<img src='imagens_adm/bt_seta_ab.jpg'  id='imgBaixo_ulPos" + posNivel + "'>&nbsp;"
	str += "<img src='imagens_adm/beta_seta_ac.jpg'  id='imgAcima_ulPos" + posNivel + "'>&nbsp;"
	str += "<img src='imagens_adm/bt_salvar.jpg' id='imgSalvar_ulPos" + posNivel + "'>&nbsp;"	
	str += "<img src='imagens_adm/bt_novo.jpg' alt='Novo' id='imgNovo_ulPos" + posNivel + "'>"
	str += "</div>"
	str += "<ul id='ulPos" + posNivel + "'></ul>"            	
	str += "</div>"
	$(str).appendTo("#insNivel")
	$("#caixaCarregando").remove()
	$("#fundoLista").remove()
	incluirFundoOpaco("fundoLista")
	$("<div id='caixaCarregando' class='caixaCarregando'><img src='imagens_adm/loading2.gif'><label>Aguarde...</label></div>").appendTo("#ulPos"+posNivel).show()
	
	$("#imgBaixo_ulPos" + posNivel).click( function(){
		wposicao = $(this).attr("id").replace("imgBaixo_","")
		moveItemBaixo(wposicao)
	})
	$("#imgAcima_ulPos" + posNivel).click( function(){
		wposicao = $(this).attr("id").replace("imgAcima_","")
		moveItemAcima(wposicao)
	})
	$("#imgSalvar_ulPos" + posNivel).click( function(){
		wposicao = $(this).attr("id").replace("imgSalvar_","")
		salvarOrdemItens(wposicao)
	})
	$("#imgNovo_ulPos" + posNivel).click( function(){
		wposicao = $(this).attr("id").replace("imgNovo_","")
		novoItem(wposicao)
	})
	
	$.post(wurl, function(retornoPost){
		
		
		$("#ulPos"+posNivel).html(retornoPost)
		$("#caixaCarregando").remove()
		
		
		
		if( $("#ulPos"+posNivel + " li").length > 0)
		{
			if( vetOpLoad[2] == "categ" )
			{
				$("#imgNovo_ulPos" + posNivel).remove()
			}
			posNivel = parseInt(posNivel) + 1
			
			
		}
		
		if( vetOpLoad[2] == "categ" )
		{
			
			wurl = "carrega_conteudos.asp?funcao=listar&tipo=Categ&subTipo=subCateg&codigo=" + vetOpLoad[3]	
			
			
			wurl += "&posNivel=" + parseInt(posNivel)
			$.post(wurl, function(retornoPost){
				
				if( retornoPost.length > 0 )
				{
					
					widthConteudo = (parseInt(posNivel) + 1) * 400
					$("#insNivel").width(widthConteudo)
					
					str = "<div style='width:330px;float:left;position:relative;' id='insulPos" + posNivel + "' class='insulNivel'>"
					str += "<div style='width:300px;float:left;text-align:right;' class='btnsTopo'>"
					str += "<img src='imagens_adm/bt_seta_ab.jpg'  id='imgBaixo_ulPos" + posNivel + "'>&nbsp;"
					str += "<img src='imagens_adm/beta_seta_ac.jpg'  id='imgAcima_ulPos" + posNivel + "'>&nbsp;"
					str += "<img src='imagens_adm/bt_salvar.jpg' id='imgSalvar_ulPos" + posNivel + "'>&nbsp;"
					str += "<img src='imagens_adm/bt_novo.jpg' alt='Novo' id='imgNovo_ulPos" + posNivel + "'>"
					
					str += "</div>"
					str += "<ul id='ulPos" + posNivel + "'></ul>"            	
					str += "</div>"
					$(str).appendTo("#insNivel").hide()
					$("#ulPos"+posNivel).html(retornoPost)	
					
					if( $("#ulPos"+posNivel + " li").length > 0 && $("#erro").length == 0)
					{
						$("#imgBaixo_ulPos" + posNivel).click( function(){
							wposicao = $(this).attr("id").replace("imgBaixo_","")
							moveItemBaixo(wposicao)
						})
						$("#imgAcima_ulPos" + posNivel).click( function(){
							wposicao = $(this).attr("id").replace("imgAcima_","")
							moveItemAcima(wposicao)
						})
						$("#imgSalvar_ulPos" + posNivel).click( function(){
							wposicao = $(this).attr("id").replace("imgSalvar_","")
							salvarOrdemItens(wposicao)
						})
						$("#imgNovo_ulPos" + posNivel).click( function(){
							wposicao = $(this).attr("id").replace("imgNovo_","")											
							novoItem(wposicao)
						})
						
						
						$("#insulPos"+posNivel).show()							
						iniciaFuncoesIcons("ulPos"+ (parseInt(posNivel)-1))
						iniciaFuncoesIcons("ulPos"+posNivel)
						iniciaFuncoesList()
						
					}
					else
					{
						iniciaFuncoesIcons("ulPos"+ (parseInt(posNivel)-1))
						iniciaFuncoesList()
						$("#insulPos"+posNivel).remove()	
					}
					$("#fundoLista").remove()
				}
				
			})
		}
		else
		{
			$("#fundoLista").remove()
			iniciaFuncoesIcons("ulPos"+ (parseInt(posNivel)-1))
			iniciaFuncoesList()	
		}
										
		
						  
	})	
	
	
}
function novoItem(nivel)
{
	
	pos = $("#" + nivel + " li").index($("#" + nivel + " li[title=' ']"))
	
	if( pos >= 0 )
	{
		//codigos
		vetOpLoad = $("#" + nivel + " li:eq("+ pos + ")").find(".codigos").attr("id").split("_")
		idClickReload = "imgLista_"+ nivel +"_"+ vetOpLoad[0] +"_" + vetOpLoad[2]
		wurl = "carrega_conteudos.asp"
		wurl += "?funcao=incluir&codigo=" + vetOpLoad[2] + "&tipo=" + vetOpLoad[0] + "&idClick=" + idClickReload	
		
		incluirFundoOpaco("fundoLista")
		
		pageWidth = dimensoesTela().split("x")[0]
		pageHeight = dimensoesTela().split("x")[1]	
		$("<div id='caixaCarregando' class='caixaCarregando'><img src='imagens_adm/loading2.gif'><label>Aguarde...</label></div>").appendTo("body")
		wleft = ( parseInt(dimensoesTela().split("x")[0]) - parseInt($("#caixaCarregando").width() ) ) / 2
		if (document.all)
		{
			var wtop = parseInt(document.documentElement.scrollTop) + 20
			
		}
		else
		{
			var wtop = parseInt(window.scrollY) + 20	
		}
		//wtop = ( parseInt(dimensoesTela().split("x")[1]) - parseInt( ) ) / 2	
		$("#caixaCarregando").css("left",parseInt(wleft))
		$("#caixaCarregando").css("top",wtop)
		$("#caixaCarregando").show()
		$("<div id='conteudoForm'/>").appendTo("body").hide()
		wleft = ( parseInt(dimensoesTela().split("x")[0]) - parseInt($("#conteudoForm").width() ) ) / 2
		$("#conteudoForm").css("left",parseInt(wleft))
		$("#conteudoForm").css("top",wtop)
		
		wurl += "&codUsuario=" + $("#codUsuario").val()
		winDt = window.open(wurl,'','top=10,left='+ wleft +',width=800,height='+ $(window).height() +',scrollbars=1,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1');
		winDt.focus();
		$("#caixaCarregando").remove()
		$("#fundoLista").click(function(){	
			$("#fundoLista").remove()
			winDt.close()
			$("#" + idClickReload).click(function(){		
				id = $(this).attr("id")		
				listarItens(id)
			})	
			
		})
	}
	else
	{
		alert("Selecione um item")	
	}
}
function atualizaLista(idClick)
{
	
	if( window.opener.$("#" + idClick).length > 0)
	{
		window.opener.$("#" + idClick).click()	
	}
	else
	{
		vetOp = idClick.split("_")		
		if( window.opener.$("#item" + vetOp[3] + "_" + vetOp[1] + " .icons").length > 0)
		{
			$("<img src='imagens_adm/seta.png' class='imgLista' id='"+ idClick +"' style='left:50px;' />").appendTo(window.opener.$("#item" + vetOp[3] + "_" + vetOp[1] + " .icons"))
			
		}
	}
	
}
function incluirCategoria()
{
	
	incluirFundoOpaco("fundoLista")
	$("#incluirTitulo").remove()
	str = "<p id='incluirTitulo'>"
    str += "<input type='text' name='titulo' id='titulo'/>"
   	str += "<img src='imagens_adm/bt_salvar.jpg' style='left:250px;' id='btnSalvarNovo'/>"
    str += "<img src='imagens_adm/bt_fechar.jpg' alt='Fechar' style='left:305px;' id='btnFecharTitulo' />"
    str += "</p>"
	$(str).appendTo("#insulPos0")
	$("#titulo").focus()
	$("#btnSalvarNovo").click(function(){
		if(	$("#titulo").val() != "" )
		{
			wurl = "carrega_conteudos.asp"
			wtitulo = $("#titulo").val()
			$("#titulo").val("Aguarde...")
			wurl += "?funcao=incluirCategoriaSubCategoria&tipo=categ&titulo=" + wtitulo;
			wurl += "&codUsuario=" + $("#codUsuario").val()
			
			$.post(wurl, function(retornoPost){
				if( retornoPost.length > 0 )	
				{
					$("#incluirTitulo").remove()												
					$("#fundoLista").remove()						
					$("#ulPos0").load("carrega_conteudos.asp?funcao=listar&tipo=Categ&subTipo=Categ&codigo=0",function(){
						iniciaFuncoesIcons("ulPos0")
						iniciaFuncoesList()
					})
					
				}
			})
		}
		else
		{
			alert("Preencha o titulo")	
			$("#titulo").focus()
		}
	})
	$("#btnFecharTitulo,#fundoLista").click(function(){
		$("#incluirTitulo").remove()												
		$("#fundoLista").remove()
	})
}
function incluirSubCategoria()
{
	pos = $("#ulPos0 li").index($("#ulPos0 li[title=' ']"))
	
	if( pos >= 0 )
	{
		
		vetOpLoad = $("#ulPos0 li:eq("+ pos + ")").find(".codigos").attr("id").split("_")
			
		incluirFundoOpaco("fundoLista")
		$("#incluirTitulo").remove()
		str = "<p id='incluirTitulo'>"
		str += "<input type='text' name='titulo' id='titulo'/>"
		str += "<img src='imagens_adm/bt_salvar.jpg' style='left:250px;' id='btnSalvarNovo'/>"
		str += "<img src='imagens_adm/bt_fechar.jpg' alt='Fechar' style='left:305px;' id='btnFecharTitulo' />"
		str += "</p>"
		$(str).appendTo("#insulPos0")
		$("#titulo").focus()
		$("#btnSalvarNovo").click(function(){
			if(	$("#titulo").val() != "" )
			{
				wurl = "carrega_conteudos.asp"
				wtitulo = $("#titulo").val()
				$("#titulo").val("Aguarde...")
				wurl += "?funcao=incluirCategoriaSubCategoria&tipo=subCateg&titulo=" + wtitulo + "&codCateg=" + vetOpLoad[2];
				wurl += "&codUsuario=" + $("#codUsuario").val()
				
				$.post(wurl, function(retornoPost){
					if( retornoPost.length > 0 )	
					{
						$("#incluirTitulo").remove()												
						$("#fundoLista").remove()
						if( $("#ulPos0 li:eq("+ pos + ")").find(".imgLista").length > 0 )
						{
							$("#ulPos0 li:eq("+ pos + ")").find(".imgLista").click()	
						}
						else
						{
							idImg = ""
							if( $("#ulPos0 li:eq("+ pos + ")").find(".imgEditar").length > 0 )
							{
								idImg = $("#ulPos0 li:eq("+ pos + ")").find(".imgEditar").attr("id").replace("imgEditar","imgLista")	
							}
							else
							{
								if( $("#ulPos0 li:eq("+ pos + ")").find(".imgExcluir").length > 0 )
								{
									idImg = $("#ulPos0 li:eq("+ pos + ")").find(".imgExcluir").attr("id").replace("imgExcluir","imgLista")
								}
							}
							if( idImg != "" )
							{
								$("<img src='imagens_adm/seta.png' class='imgLista' id='"+ idImg +"' style='left:50px;' />").appendTo($("#ulPos0 li:eq("+ pos + ")").find(".icons"))
								$("#" + idImg).click(function(){		
									id = $(this).attr("id")		
									listarItens(id)
								})	
								listarItens(idImg)
							}
						}
						
					}
				})
			}
			else
			{
				alert("Preencha o titulo")	
				$("#titulo").focus()
			}
		})
		$("#btnFecharTitulo,#fundoLista").click(function(){
			$("#incluirTitulo").remove()												
			$("#fundoLista").remove()
		})	
	}
	else
	{
		alert("Selecione uma categoria")	
	}
	
}
			
