var mes, anio=0;

$(function(){
	
	if ($(".art-page-footer").length > 0){
		$(".art-page-footer").append('<div style="text-align:center; margin-top:10px"><a href="http://www.webline.es" style="text-decoration: none;" title="powered by WEBLINE.es" alt="powered by WEBLINE.es"><img src="http://www.webline.es/imagenes/poweredByWebline.gif" border="0" /></a></div>');
	}else{
		$('body').html($('body').html()+'<div style="text-align:center; margin-top:10px;"><a href="http://www.webline.es" style="text-decoration: none;" title="powered by WEBLINE.es" alt="powered by WEBLINE.es"><img src="http://www.webline.es/imagenes/poweredByWebline.gif" border="0" /></a></div>');
	}	

	
	loadScript("http://www.arx2.com/libs/JSON.js", function(){
	});
	$("#BTNcontacto").css("cursor", "pointer");
	$("#BTNcontacto").click(function (){
		verdocumento(idContacto, "M1_"+idContactoIDMENU);
	});
	$("#BTNcontacto").hover(
			function(){
				$(this).removeClass("BTNcontactoOff").addClass("BTNcontactoOn");
			},
			function(){
				$(this).removeClass("BTNcontactoOn").addClass("BTNcontactoOff");
			});
	$("#BTNhome").css("cursor", "pointer");
	$("#BTNhome").click(function (){
		verdocumento(idHome, "M1_"+idHomeIDMENU);
	});
	$("#BTNhome").hover(
			function(){
				$(this).removeClass("BTNhomeOff").addClass("BTNhomeOn");
			},
			function(){
				$(this).removeClass("BTNhomeOn").addClass("BTNhomeOff");
			});

	$('<div id="ICNvacio" style="width:15px;height:19px; float:left;"></div>').insertAfter("#DIVtwitter");
	$('<div id="ICNshop" style="display:none; background: url(../PlantillA/carro.gif) no-repeat; width:19px;height:19px; margin-left: 5px;float:left;cursor:pointer"></div>').insertAfter("#ICNvacio");
	$('<div id="ICNinfo" style="display:none; background: url(../PlantillA/mas_info.gif) no-repeat; width:19px;height:19px; margin-left: 5px;float:left;cursor:pointer"></div>').insertAfter("#ICNshop");

	$.getJSON("procesos_ajax.php",{accion: "hayMasInfo", method: "GET", args: new Array()},
			function (data){
				if (data.OK) {
					if(data.hayMasInfo) {
						$("#ICNinfo").show();
					}
				}
			}
		);	
	
	
	$("#ICNinfo").click(function(){
		$.getJSON("procesos_ajax.php",{accion: "getMasInfo", method: "GET", args: new Array()},
				function (data){
					if (data.OK) {
						try {
							antesDelLoad();
						}catch(err) {
						}
						verdocumento(idContacto, "M1_"+idContactoIDMENU, 'masInfo', data.texto);
						$.getJSON( "procesos_ajax.php",{accion: "borrarMasInfo", method: "GET", args: new Array()} );
						$("#ICNinfo").hide();
					}
				}
			);
	});
	
	$.getJSON("procesos_ajax.php",{accion: "hayCarro", method: "GET", args: new Array()},
			function (data){
				if (data.OK) {
					if(data.hayCarro) {
						$("#ICNshop").show();
					}
				}
			}
		);	
	
	$("#ICNshop").click(function(){
		$.getJSON("procesos_ajax.php",{accion: "getCarro", method: "GET", args: new Array()},
				function (data){
					if (data.OK) {
						try {
							antesDelLoad();
						}catch(err) {
						}
						verdocumento(idContacto, "M1_"+idContactoIDMENU, 'masInfo', data.texto);
						$.getJSON( "procesos_ajax.php",{accion: "vaciarCarro", method: "GET", args: new Array()} );
						$("#ICNshop").hide();
					}
				}
			);
	});
});





function trabajando(idBoton) {
	try {
		antesDelLoad();
	}catch(err) {
	}
	if (tipoMenu == "H") {
		sombra(".cuerpoCont img");
	}else {
		mostrarEstadoBoton(idBoton);
		try {
			mostrarEstadoBotonP(idBoton);
		}catch(err) {
		}
	}
	barraNavegacion(idBoton); 
	$('#contenido').html("");
	$.post( "documentos_ver_XML.php", {id: -1},
		function (data){
			datas = data.split("***<>***");
			$('#contenido').html(datas[0]);
			try {
				despuesDelLoad();
			}catch(err) {
			}
			if (tipoMenu == "H") {
				sombra(".cuerpoCont img");
			}
			$('#DIVlinks').html('');
		}
	);
}

function tituloMenu(id) {
	return $("#PM_"+id).find(" a:first").text();
}
function linkMenu(id) {
	return $("#PM_"+id).find(" a:first").attr("href");
}

function barraNavegacion(idBoton) {
	if (tipoMenu == 'A' || tipoMenu == 'B' && $('#DIVnavega').length != 0) {
		if (tipoMenu == 'A' ) {
			var claseMenu = '.art-vmenu';
		}else {
			var claseMenu = '.art-menu';
		}
		var pos = idBoton.split("_");
		var aSMenu = new Array();
		var aSSMenu = new Array();
		var m1 = m2 = m3 = 0;
		$(claseMenu+" >li").find("a").each(function(){
		   // console.log("->"+this.id);
		   var pos = this.id.split("_");
		   if (pos[0]=="M1"){
		        m1=pos[1];
		        m2=0;
		        m3=0;
		   }else if (pos[0]=="M2"){
		        m2=pos[1];
		        aSMenu[m2] = m1;
		   }else{
		        m3=pos[1];
		        aSSMenu[m3] = m2;
		   }
		});
		if (pos[0] == 'M1') {
			var titulo = '<a href="'+linkMenu(pos[1])+'">'+tituloMenu(pos[1])+'</a>'; 	
			$("#DIVnavegaM").html(titulo);
			$("#DIVnavegaSSM").html('');
			$("#DIVnavegaSM").html('');
		}else if (pos[0] == 'M2') {
			var titulo = '<a href="'+linkMenu(pos[1])+'">'+tituloMenu(pos[1])+'</a>'; 	
			$("#DIVnavegaSM").html(titulo);

			var titulo = '<a href="'+linkMenu(aSMenu[pos[1]])+'">'+tituloMenu(aSMenu[pos[1]])+'</a>'; 	
			$("#DIVnavegaM").html(titulo+"&nbsp;&gt;&gt;");
			$("#DIVnavegaSSM").html('');
		}else {
			var titulo = '<a href="'+linkMenu(pos[1])+'">'+tituloMenu(pos[1])+'</a>'; 	
			$("#DIVnavegaSSM").html(titulo);
			var titulo = '<a href="'+linkMenu(aSSMenu[pos[1]])+'">'+tituloMenu(aSSMenu[pos[1]])+'</a>'; 	
			$("#DIVnavegaSM").html(titulo+"&nbsp;&gt;&gt;");
			var titulo = '<a href="'+linkMenu(aSMenu[aSSMenu[pos[1]]])+'">'+tituloMenu(aSMenu[aSSMenu[pos[1]]])+'</a>'; 	
			$("#DIVnavegaM").html(titulo+"&nbsp;&gt;&gt;");
		}
	}
	
}

function verdocumento(id, idBoton){
	
	var mensaje = '';
	if (arguments.length>3) {
		if (arguments[2]=='masInfo') {
			var mensaje = arguments[3];
		}
	}
	barraNavegacion(idBoton); 
	if (id == 0 || id == undefined || id == '') {
		trabajando();
		return true;
	}
	$.post( "documentos_ver_XML.php", {id: id},
		function (data){
//			$('#contenido').html(data);
			datas = data.split("***<>***")
			$('#contenido').html(datas[0]);
			if (mensaje != '') {
				$('#contenido textarea').val(mensaje);
			}
			try {
				despuesDelLoad();
			}catch(err) {
			}
//			if (datas[1] != '')
			var titulo = empresa;
			if (datas.lenght>2) 
				titulo = empresa+ " | " + datas[2];
			document.title = titulo;
			try {
				pageTracker._trackPageview("/"+titulo );
			}catch(err) {
				
			}
			try {
				var linkFacebook = "http://www.facebook.com/share.php?u="+site+"?opc="+id+"&t="+titulo+"&src=sp";
				var linkTwitter = "http://twitter.com/home?status="+titulo+"+"+site+"?opc="+id;
				$("#DIVfacebook").popupWindow({ 
						windowURL:linkFacebook, 
						windowName:'facebook' 
				}); 
				$("#DIVtwitter").popupWindow({ 
					windowURL:linkTwitter, 
					windowName:'twitter' 
				}); 
			}catch(err) {
			}
			try {
				if (datas.lenght>1)
					$('#DIVlinks').html(datas[1]);
			}catch(err) {
			}
			if (tipoMenu == "H") {
				sombra(".cuerpoCont img");
			}else {
				mostrarEstadoBoton(idBoton);
				try {
					mostrarEstadoBotonP(idBoton);
				}catch(err) {
				}
			}
		}
	);
}


function mostrarEstadoBoton(idBoton) {
	var pos = idBoton.substring(1,2);
	var PM = idBoton.substring(3);
	if (pos==1){
		$(".css_Pmenu").not(".css_PmenuBoff").removeClass("css_PmenuBover").removeClass("css_PmenuBon").addClass("css_PmenuBoff");
		$("#PM_"+PM).removeClass("css_PmenuBoff").addClass("css_PmenuBon");
	}else if (pos==2){
		$(".css_Psubmenu").not(".css_PsubmenuBoff").removeClass("css_PsubmenuBover").removeClass("css_PsubmenuBon").addClass("css_PsubmenuBoff");
		$("#PM_"+PM).removeClass("css_PsubmenuBoff").addClass("css_PsubmenuBon");
	}else{
		$(".css_Psubsubmenu").not(".css_PsubsubmenuBoff").removeClass("css_PsubsubmenuBover").removeClass("css_PsubsubmenuBon").addClass("css_PsubsubmenuBoff");
		$("#PM_"+PM).removeClass("css_PsubsubmenuBoff").addClass("css_PsubsubmenuBon");
	}	
}


function sombra(jqObj) {
	if (tipoMenu == "H") {
		$(jqObj).dropShadow({left: 1, top: 1, blur: 2, opacity: 0.5});
	}
}

function verdocumentoXML(id, idBoton){
	try {
		antesDelLoad();
	}catch(err) {
	}
	
	
	verdocumento(id, idBoton);
//	$.post( "documentos_ver_XML.php", {id: id},
//		function (data){
//			datas = data.split("***ADJLNK***")
//			$('#contenido').html(datas[0]);
////			if (datas[1] != '')
//			$('#DIVlinks').html(datas[1]);
//			sombra($(".cuerpoCont img"));
//		}
//	);
}

function AJAX_sess(){
	$.post( "../libs/sess.php");
	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 loadScript(url, callback){

	 var script = document.createElement("script")
	 script.type = "text/javascript";

	 if (script.readyState){  //IE
	    script.onreadystatechange = function(){
	     if (script.readyState == "loaded" ||
	         script.readyState == "complete"){
	           script.onreadystatechange = null;
	           callback();
	        }
	     };
	 } else {  //Others
	   script.onload = function(){
	     callback();
	   };
	 }

	 script.src = url;
	 document.getElementsByTagName("head")[0].appendChild(script);
}



function masInfo(referencia) {
	
	if (referencia != '') {
		var args = JSON.encode([referencia]);
		$.getJSON("procesos_ajax.php",{accion: "masInfo", method: "GET", args: args},
			function (data){
				if (data.OK) {
					$("#ICNinfo").show();
				}
		
			}
		);
	}
}


function agregarCarro(referencia, detalle, precio) {
	
	if (referencia != '') {
		var args = JSON.encode([referencia, detalle, precio]);
		$.getJSON("procesos_ajax.php",{accion: "agregarAlCarro", method: "GET", args: args},
			function (data){
				if (data.OK) {
					$("#ICNshop").show();
				}
		
			}
		);
	}
}




