var mes, anio=0;
var menuActual = -1;
var subMenuActual = -1;
var SMclick = false;

var estamosActualizando = new Array();
estamosActualizando['es'] = '<h3 align="center">Estamos actualizando la informaci&oacute;n</h3>';
estamosActualizando['en'] = '<h3 align="center">We are updating the information</h3>';
estamosActualizando['it'] = '<h3 align="center">Stiamo aggiornando le informazioni</h3>';




function fObj(obj){
	try{
		o=document.getElementById(obj);
		return o;
	}catch (e){
		return null;
	}
}


function verdocumento(id){
	$.post( "documentos_ver.php", {id: id},
		function (data){
			if(data!='' || data != 'ERROR')
			{
				$('#contenido').html(data);
				iniciaHistoryPag();
			}else{ 
				alert("No tiene permisos para visualizar esta página");
			}
		}
	);
}

function vernoticia(id){
	$.post( "noticias_ver.php", {id: id},
		function (data){
			if(data!='' || data != 'ERROR')
			{
				$('#contenido').html(data);
			}else{ 
			}
		}
	);
}

function AJAX_sess(){

	$.post( "../libs/sess.php", 
		function (data){
		}
	);
	setTimeout("AJAX_sess()", 600000);
}

function conerror(cad){
	if ( cad.substr(0,14) == '<!-- ERROR -->' ){
		return true;
	}else{
		return false;
	}
}

function desplegarmenu(menu){
	$("#"+menu).next('div').slideToggle('fast').siblings('div:visible').slideUp('fast');
}


function mostrarMenu(menu, id, idDoc){
	if ( menu != menuActual ){
		menuActual = menu;
		subMenuActual = -1;
		$.post("menuHA_ajax.php", { menu: menu, id: id },
			function (data){
				$("#DIVmenu").html(data);

		        $('div.css_menu> div').hide(); 

 		        $('div.css_menu> h3').click(function() {
		          $('div.css_menu> div').hide(); 
		          $(this).next('div').slideToggle('fast').siblings('div:visible').slideUp('fast');
		        });
		
		        $('div.css_submenu> h3').click(function() {
		          $('div.css_submenu> div').hide(); 
		          $(this).next('div').slideToggle('fast').siblings('div:visible').slideUp('fast');
		        });
				
		        $('div.css_menu> p').click(function() {
			        $('div.css_menu> div').hide(); 
				});


				$(".menuCabecera").removeClass("menuOn");
				$(".menuOn").addClass("menuCabecera").removeClass("menuOn");
				$("#divMNU_"+menu).addClass("menuOn").removeClass("menuCabecera");

				
				document.title = EMPRESA + " - " + titMNU[menu];
				iniciaHistory2();
				if (idDoc == 0){
					var hash = $(".css_linkmenu").eq(0).attr('href');
					hash = hash.replace(/^.*#/, '');
					pageload(hash);
					var onclick = $(".css_linkmenu").eq(0).attr('onclick');
					eval(onclick);
				}
			}
		);
	}
	if (idDoc >0){
		verdocumento(idDoc);
	}
}

function mostrarNoticias(menu, id, idDoc){
	subMenuActual = -1;

	
	$.post( "noticias_listado.php", {filtro: menu},
	function (data){
		if(data!='' || data != 'ERROR')
		{
			$('#contenido').html(data);
			iniciaHistory3();


				document.title = EMPRESA + " - " + titMNU[menuActual];
		}else{ 
		} 
	}
	);
}


function FNestamosActualizando(){
	var href = document.location.href.split("/");
	var pagina = href[href.length-1];
	//console.log(pagina);
	if (pagina != "enviar_form.php"){
		$('#contenido').html('<p>&nbsp;</p><p>&nbsp;</p><p></p><p></p><p></p>'+estamosActualizando[SESidioma]);
	}	
}


function pageload(hash) {
	// hash doesn't contain the first # character.
	//console.log(hash); 
	//console.log(arguments.length);
	if (hash=='' || hash == "#" ){ 
		FNestamosActualizando();
	}
	
	
	var variables=hash.split("&");
	
	try {
		var funcion = variables[0].split("=");
		var menu = variables[1].split("=");
		var id = variables[2].split("=");
		var doc = variables[3].split("=");
	}catch (ex){	
	}
	//alert(funcion[1]);
	//console.log("*******************************");
	//console.log("FUNCION = " + funcion[1]);
	switch ( funcion[1]){
		case 'mostrarMenu' :
			mostrarMenu(menu[1], id[1], doc[1] );
			break;
		case 'mostrarNoticias':
	//		menuActual = menu[1];
			mostrarNoticias(menu[1]);
//			mostrarNoticias(menu[1], id[1], doc[1] );
			break;
		case 'verdocumento':

			if ( documentos[menu[1]].MNU != menuActual && !SMclick){
				mostrarMenu(documentos[menu[1]].MNU, idMNU[documentos[menu[1]].MNU], menu[1] );
			}
			
			//console.log(subMenuActual +"!="+ documentos[menu[1]].SMNU);
			//console.log(SMclick);
			//console.log('#H3_' + documentos[menu[1]].SMNU);
			if (subMenuActual != documentos[menu[1]].SMNU){
				subMenuActual = documentos[menu[1]].SMNU;
				if (!SMclick || documentos[menu[1]].SSMNU == 0) {
					$('div.css_menu> div').hide();
				} 
			}
			//$('#H3_' + documentos[menu[1]].SMNU).next('div').slideToggle('fast').siblings('div:visible').slideUp('fast');
			if ((documentos[menu[1]].SSMNU != 0 && !SMclick) ||  (SMclick && documentos[menu[1]].SSMNU == 0) ){
				//console.log("MUESTRA #H3_" + documentos[menu[1]].SMNU);
				$('#H3_' + documentos[menu[1]].SMNU).next('div').slideToggle('fast').siblings('div:visible').slideUp('fast');
			} 

			

			//$("#H3_" + documentos[menu[1]].SMNU).find("a").css("color", "#FF0000");
			if (documentos[menu[1]].SSMNU != 0 ){
				//$("#H3_" + documentos[menu[1]].SMNU + "_" + documentos[menu[1]].SSMNU).find("a").css("color", "#FF0000");
			}
			SMclick = false;
			verdocumento(menu[1]);
			break;
		case 'vernoticia':
			vernoticia(menu[1]);
			break;
	}
}

function CSSmenu(cual, link){
	//alert("entra");
	if (cual == 2){
		$(".css_Pmenu").find("a").removeClass("menu2On");
		$(".css_Psubmenu").find("a").removeClass("menu3On");
		$("#H3_"+link).find("a").addClass("menu2On");
	}else{
		$(".css_Psubmenu").find("a").removeClass("menu3On");
		$("#H3_"+link).find("a").addClass("menu3On");
	}
}



	
$(function(){
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload);
	
	$(".history").click(function(){
			// 
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			//console.log("*******************************" + hash);
			SMclick=true;
			// moves to a new page. 
			// pageload is called at once. 
			// hash don't contain "#", "?"
			$.historyLoad(hash);
			return false;
		});

});


function iniciaHistoryPag(){
	$(".historyPag").click(function(){
		// 
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		//console.log("*******************************" + hash);
		SMclick=true;
		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
		return false;
	});
}


function iniciaHistory2(){
	$(".history2").click(function(){
			// 
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			//console.log("*******************************" + hash);
			SMclick=true;
			// moves to a new page. 
			// pageload is called at once. 
			// hash don't contain "#", "?"
			$.historyLoad(hash);
			return false;
		});
}
function iniciaHistory3(){
	$(".history3").click(function(){
			// 
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			//console.log("*******************************" + hash);
			SMclick=true;
			// moves to a new page. 
			// pageload is called at once. 
			// hash don't contain "#", "?"
			$.historyLoad(hash);
			return false;
		});
}

