var interImgs;
var nToDivs = 0;
var arrTot = new Array();
var arrTot_pag = new Array();
var dbanner_count = 0;
var intTime = 0;
function allOnload(time, loop){
	if(document.getElementById('dbanner_box')){
		intTime = time;
		if(loop){
			dbanner_count = document.getElementById('dbanner_box').getElementsByTagName('div').length;
			for(i=0;i<dbanner_count;i++){
				arrTot[i] = document.getElementById('dbanner_box').getElementsByTagName('div')[i].id;
				arrTot_pag[i] = document.getElementById('dbanner_pag').getElementsByTagName('li')[i].id;
			}
			assistInterval()
			interImgs = setInterval(assistInterval, intTime);
		}
	}
	if(document.getElementById('data_publicacao')){
		putcalender()
	}
	if(document.getElementsByClassName('mceEditor').length){
		tinyMCE.execCommand('mceAddControl', false, 'conteudo_post');
		//tinyMCE.execCommand('mceRemoveControl', true, 'conteudo_post');
	}
	if(document.getElementById('geral_move')){
		Sortable.create("geral_move");
		var especifico_ul = document.getElementsByClassName('especifico_ul');
		for(i=0;i<especifico_ul.length;i++){
			Sortable.create(especifico_ul[i].id);
		
		
		}
	}
}
function assistInterval(){
	if(nToDivs>=dbanner_count){
		nToDivs = 0;		
	}
	if(nToDivs == 0){
		unOpes(arrTot[dbanner_count-1]);
	}else{
		unOpes(arrTot[nToDivs-1]);
	}
	opes(arrTot[nToDivs], arrTot_pag[nToDivs]);
	nToDivs++;
}
function unOpes(id){
	for(n=0;n<arrTot.length;n++){
		document.getElementById(arrTot[n]).style.zIndex = 4;
		document.getElementById(arrTot_pag[n]).className = 'pag';
	}
	unDivRef = document.getElementById(id);
	unDivRef.style.zIndex = 5;
}	
function opes(id, id_pag){
	divRef = document.getElementById(id);
	divRef_pag = document.getElementById(id_pag);
	
	divRef.style.left = "715px";
	divRef.style.zIndex = 10;
	divRef_pag.className = 'paghover';
	
	new Effect.Morph(id, {
	  style: 'left:0px;', // CSS Properties
	  duration: 0.8 // Core Effect properties
	});
	return false;
}
function bClickpage(id, num){
	var stopInter = window.clearInterval(interImgs);
	unOpes(arrTot[nToDivs]);
	opes(arrTot[num], id);
	nToDivs = num;
	interImgs = setInterval(assistInterval, intTime);
}


var marcaPage = 0;
var toleft = 0;
function marcaMove(side, size){
	if(size>710){
		var min = 710-size;
		if(side == 'r'){
			//direita r
			if(toleft>min){
				marcaPage--;
				toleft = 710*marcaPage;

				new Effect.Morph('dmarcas_box', {
				  style: 'left:'+toleft+'px;', // CSS Properties
				  duration: 0.8 // Core Effect properties
				});
			}
		}else{
			//esquerda l
			if(toleft<0){
				marcaPage++;
				toleft = 710*marcaPage;
				
				new Effect.Morph('dmarcas_box', {
				  style: 'left:'+toleft+'px;', // CSS Properties
				  duration: 0.8 // Core Effect properties
				});
			}
		}
	}
}
var controleCont_n = new Array(0, 0);
function nomeRees(tagRef, part, value, n){
	if(part=='click'){
		if(controleCont_n[n]==0){
			tagRef.value = "";
			controleCont_n[n] = 1;
			tagRef.style.color = '#282828'
		}
	}else{
		if(tagRef.value == ""){
			tagRef.value = value;
			controleCont_n[n] = 0;
			tagRef.style.color = '#585858'
		}
	}
}


/* WORLD FUNCTIONS */
function remChild(tag){
	tag.parentNode.removeChild(tag);
	return false;
}
function is_array(input){
	return typeof(input)=='object'&&(input instanceof Array);
}

function GetXMLHttp() {
    if(navigator.appName == "Microsoft Internet Explorer"){
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}
var xmlRequest = new Array();
var url = new String();
var divDest = new String();
function abrirPag(valor, divRef, numDivRef, value){
	
	if(document.getElementsByClassName('mceEditor').length){
		tinyMCE.execCommand('mceRemoveControl', true, 'conteudo_post');
	}
	
	if(numDivRef == ""){
		numDivRef = 0;	
	}
	xmlRequest[numDivRef] = GetXMLHttp();
    url = valor+"?valexp="+value;
	divDest[numDivRef] = divRef;

    xmlRequest[numDivRef].open("POST",url,true);   
	xmlRequest[numDivRef].setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
    xmlRequest[numDivRef].onreadystatechange = function(){
	
		if (xmlRequest[numDivRef].readyState == 4){
			document.getElementById(divDest[numDivRef]).innerHTML = xmlRequest[numDivRef].responseText;
			allOnload(5000, true)
		}
	};
    xmlRequest[numDivRef].send(null);

        if (xmlRequest[numDivRef].readyState == 1) {
            document.getElementById(divDest[numDivRef]).innerHTML = "Carregando...";
        }

    return url;
}
function insertPag(valor, divRef, numDivRef, value){
	if(numDivRef == ""){
		numDivRef = 0;	
	}
	xmlRequest[numDivRef] = GetXMLHttp();
    url = valor+"?valexp="+value;
	divDest[numDivRef] = divRef;

    xmlRequest[numDivRef].open("POST",url,true);   
	xmlRequest[numDivRef].setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
    xmlRequest[numDivRef].onreadystatechange = function(){
	
		if (xmlRequest[numDivRef].readyState == 4){
			document.getElementById(divDest[numDivRef]).innerHTML += xmlRequest[numDivRef].responseText;
		}
	};
    xmlRequest[numDivRef].send(null);

        if (xmlRequest[numDivRef].readyState == 1) {
           //document.getElementById(divDest[numDivRef]).innerHTML = "Carregando...";
        }
    return url;
}
function td_criaXMLHttp() {
 if (typeof XMLHttpRequest != "undefined")
 	return new XMLHttpRequest();
 else if (window.ActiveXObject){
 	var versoes = ["MSXML2.XMLHttp.5.0",
 	"MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0",
 	"MSXML2.XMLHttp", "Microsoft.XMLHttp"
 	];
 }
 for (var i = 0; i < versoes.length; i++){
 	try{
 		return new ActiveXObject(versoes[i]);
 	}catch (e) {}
 }
 throw new Error("Seu browser nao suporta AJAX");
}
function criaReqStr(oForm){
	var aPostStr = new Array();
 
	for(var i = 0; i < oForm.elements.length; i++){
		
		if(oForm.elements[i].type == "checkbox" || oForm.elements[i].type == "radio"){
			if(oForm.elements[i].checked == true){
				var sValor = encodeURIComponent(oForm.elements[i].name);
				sValor += "=";
				sValor += encodeURIComponent(oForm.elements[i].value);
				aPostStr.push(sValor);
			}
		}else if(oForm.elements[i].type == "select-multiple"){
		
			el = oForm.elements[i];
			
			for(n = 0; n < el.options.length; n++){
				if(el.options[n].selected){
					var itens = encodeURIComponent(oForm.elements[i].name)+"=";
					itens += encodeURIComponent(el.options[n].value);
					aPostStr.push(itens);
					//itens += ";";				
				}
			}
			//itens = itens.substring(0,itens.length - 1);
			//aPostStr.push(itens);
		}else{
			var sValor = encodeURIComponent(oForm.elements[i].name);
			sValor += "=";
			sValor += encodeURIComponent(oForm.elements[i].value);
			aPostStr.push(sValor);
		}
		
	}
	return aPostStr.join("&");
}


var numArrDiv = 0;
function enviaReq(formRef, divRef, pageOpen){
	var oForm = formRef;
	var sBody = criaReqStr(oForm);
	var oDiv = document.getElementById(divRef);
	
	oDiv.style.display = 'block';
	oDiv.innerHTML = '<img src="/admlv/css/i/ajax_loader.gif">'
	
	var oXMLHttp = td_criaXMLHttp();
	oXMLHttp.open("post", oForm.action, true);
	oXMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 
	oXMLHttp.onreadystatechange = function (){
		if(oXMLHttp.readyState == 4)
			exibeResult(oXMLHttp.responseText, divRef, pageOpen);
		else
			exibeResult_err('<img src="/admlv/css/i/ajax_loader.gif">', divRef);
	};
	oXMLHttp.send(sBody);
}
function exibeResult(sTxt, divRef, pageOpen){
	var criaArr = sTxt.split(';')
	if(is_array(criaArr)){
		if(criaArr[0] == 'comentarios'){
			insertPag(pageOpen, criaArr[3], numArrDiv++, "&id="+criaArr[2]);
			var th_atua = setTimeout("highlightDiv('"+divRef+"', '#ffff99', '#ffffff')", 200);
			var fix_sTxt = criaArr[1];

		}
		else if(criaArr[0] == 'modanews'){
			alert(criaArr[1]);
		}
		else if(criaArr[0] == 'creapostagem'){
			var fix_sTxt = criaArr[1];
			var inputHidden = document.createElement('input');
			inputHidden.type = 'hidden';
			inputHidden.id = 'post_ID';
			inputHidden.name = 'post_ID';
			inputHidden.value = criaArr[2];
			document.forms[0].appendChild(inputHidden);
			
		}
		else if(criaArr[0] == 'atuapostagem'){
			var fix_sTxt = criaArr[1];
		}
		else if(criaArr[0] == 'expostagem'){
			var fix_sTxt = criaArr[1];
			var lm = criaArr[2].split('.')
			for(i=0;i<(lm.length);i++){
				var divToExc = document.getElementById(lm[i]);
				hideDiv(divToExc)
				var th_exc = setTimeout(function(){divToExc.parentNode.removeChild(divToExc)}, 2000);
			}
		}
		else if(criaArr[0] == 'excoments'){
			var fix_sTxt = criaArr[1];
			var lm = criaArr[2].split('.')
			for(i=0;i<(lm.length);i++){
				var divToExc = document.getElementById(lm[i]);
				hideDiv(divToExc)
				var th_exc = setTimeout(function(){divToExc.parentNode.removeChild(divToExc)}, 2000);
			}
		}
		else if(criaArr[0] == 'pubpostagem'){
			var fix_sTxt = criaArr[1];
			abrirPag('../admlv/blog/'+criaArr[2]+'?page=1', criaArr[3], 0, '&click=1');
		}
		else if(criaArr[0] == 'err'){
			var quantforms = document.forms;
			for(f=0;f<quantforms.length;f++){
				for(el=0;el<quantforms[f].elements.length;el++){
					quantforms[f].elements[el].style.border = "";
					quantforms[f].elements[el].style.backgroundColor = "";				
				
				}
			}
			for(i=2;i<(criaArr.length);i++){
				document.getElementsByName(criaArr[i])[0].style.border = "solid 2px #CF0000"
				document.getElementsByName(criaArr[i])[0].style.backgroundColor = "#ffE6E6"
			}
			var fix_sTxt = criaArr[1];
		}
		else if(criaArr[0] == 'generalsave'){
			var fix_sTxt = criaArr[1];
			abrirPag('../admlv/blog/adm/'+criaArr[2]+'?click=1', criaArr[3], 0, '&'+criaArr[4]);
		}
		else if(criaArr[0] == 'justtext'){
			var fix_sTxt = criaArr[1];
		}
	}else{
		var fix_sTxt = sTxt;
	}
	var divStatus = document.getElementById(divRef);
	divStatus.innerHTML = fix_sTxt;
	var t = setTimeout("hideDiv('"+divRef+"')", 2000)
}
function exibeResult_err(err, divRef){
	var divStatus = document.getElementById(divRef);
	divStatus.innerHTML = err;
}


function highlightDiv(divRef, iniColor, outColor){
	new Effect.Highlight(divRef, {startcolor: iniColor, endcolor: outColor});
}
function hideDiv(divRef){
	new Effect.Opacity(divRef, { from: 1, to: 0, afterFinish: function(){
			document.getElementById(divRef).style.display = 'none';
			new Effect.Opacity(divRef, { from: 0, to: 1});
		}})
}
function slide(tipo, num, ano){
	if(tipo == 'ano'){
		var divrel = document.getElementById('archano_'+num)
		if(divrel.style.display == 'none'){
			new Effect.BlindDown(divrel.id, { duration: 0.3 });
		}else{
			new Effect.BlindUp(divrel.id, { duration: 0.3 });
		}	
	}else{
		var divrel = document.getElementById('archmes_'+ano+'_'+num)
		if(divrel.style.display == 'none'){
			new Effect.BlindDown(divrel.id, { duration: 0.3 });
		}else{
			new Effect.BlindUp(divrel.id, { duration: 0.3 });
		}	
	
	}

}
function putcalender() {
	Calendar.setup({
		dateField      : 'data_publicacao',
		triggerElement : 'data_publicacao'
		})
}
var stateObj = { foo: "bar" };
function change_my_url(change){
   history.pushState(stateObj, "page 2", change);
}
function clicksel(iddiv, input){
	var ln = document.getElementById(iddiv).getElementsByTagName('input').length
	var at = document.getElementById(iddiv).getElementsByTagName('input')

	if(input.checked){
		for(i=0;i<ln;i++){
			at[i].checked = true
		}
	}else{
		for(i=0;i<ln;i++){
			at[i].checked = false
		}
	}
}
function upndown(divrel_id, img){
	var divrel = document.getElementById(divrel_id)
	if(divrel.style.display == 'none'){
		Effect.SlideDown(divrel_id, { duration: 0.5 });
		img.className = 'dv_line_up';
	}else{
		Effect.SlideUp(divrel_id, { duration: 0.5 });
		img.className = 'dv_line_down';
	}
}
function save_form(formName){
	if(confirm('Tem certeza que deseja continuar com a operação?')){
		enviaReq(document.getElementById(formName), 'div_procs_results', false); return false
	}
}
function putSerial(formName, input, serialContent, save){
	document.getElementById(input).value = Sortable.serialize(serialContent);
	if(save){
		save_form(formName)
	}
}
function toex_menus(tp, id){
		if(confirm('Deseja realmente excluir?')){
			switch(tp){
				case 'ex_menu':
					//dv_move_2
					remChild(document.getElementById('dv_move_'+id))
					abrirPag('../admlv/blog/adm/adm_general_procs.php', 'div_procs_results', 1, '&tp='+tp+'&id='+id);
				break;
				case 'ex_submenu':
					//document.getElementById('dv_list_line_'+id).parentNode.removeChild(document.getElementById('dv_list_line_'+id))
					remChild(document.getElementById('dv_list_line_'+id))
					abrirPag('../admlv/blog/adm/adm_general_procs.php', 'div_procs_results', 1, '&tp='+tp+'&id='+id);
				break;
			}
		}
	}












