function __setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function __getCookie(name) {
   var ind = document.cookie.indexOf(name + '=');
   if (ind >= 0 ){
     a = document.cookie.substring(ind + name.length + 1,document.cookie.length);
     if(a.indexOf(';') != -1)a = a.substring(0,a.indexOf(';'))
     return unescape(a); 
   }
   return false;
}

function leerEmail() {
	cookie = __getCookie('ser_session_info');


	if(cookie) {
		a = cookie.split('|')[2].replace('%40','@');
		return a;
	}
	return null;
}

function leerLogin() {
	var cookie = __getCookie('ser_session_info');

	if(cookie && cookie.split('|').length >= 4){
		var trozos=cookie.split('|');
		var nombre=trozos[3];
		mpChar = nombre.substring(0,1).toUpperCase();
		postString = nombre.substring(1,nombre.length);
		nombre = mpChar + postString;
		apellido=trozos[4];
		mpChar = apellido.substring(0,1).toUpperCase();
		postString = apellido.substring(1,apellido.length);
		apellido = mpChar + postString;
		var a = nombre + " " + apellido;
                if ( nombre == '' )
                {
                        a = trozos[5];
                }


		return a;
	}
	return null;
} 

$(document).ready(function(){
    $("input#url_window").attr("value",window.location);
    butt_html1 = "<a class='facebook btn-fb' href='javascript:getUrl(1)'>";
    butt_html2 = "<img width='85' height='18' title='Conectar a Facebook' alt='Conectar a Facebook' src='/img/registro/conectar_facebook.png'>";
    butt_html3 = "</a>"
    butt_html = butt_html1+ butt_html2+ butt_html3;

    $(".conectar_facebook").html(butt_html);

	nombre = leerLogin();
	if(nombre != null){
		$(".sesion span:first-child").remove();	
		$(".sesion a:first-child").html(nombre);
		$(".sesion a:nth-child(2)").html("Desconectar");
		var get="";
		var backurl=$(".sesion a:first-child").attr("href");
		if(backurl && backurl.split('?').length>1) get = "?" + backurl.split('?')[1];
		$(".sesion a:first-child").attr("href","https://usuarios.cadenaser.com/acceso/modificacion.html" + get );
		$(".sesion a:nth-child(2)").attr("href","https://usuarios.cadenaser.com/acceso/desconectar.html" + get );
	}

	$("#pie_bm0").mouseover(function (e){ e.preventDefault(); $("#modulo0").show('slow',5000)});
	$("#pie_bm0").mouseout(function (e){ e.preventDefault(); $("#modulo0").hide()});
	$("#pie_bm1").mouseover(function (e){ e.preventDefault(); $("#modulo1").show()});
	$("#pie_bm1").mouseout(function (e){ e.preventDefault(); $("#modulo1").hide('slow',5000)});
	$("#pie_bm2").mouseover(function (e){ e.preventDefault(); $("#modulo2").show()});
	$("#pie_bm2").mouseout(function (e){ e.preventDefault(); $("#modulo2").hide('slow',5000)});
});

String.prototype.removeAccents = function ()
{
	var __r = { 'À':'a','Á':'a','Â':'a','Ã':'a','Ä':'a','Å':'a','Æ':'e', 'È':'e','É':'e','Ê':'e','Ë':'e', 'Ì':'i','Í':'i','Î':'i', 'Ò':'o','Ó':'o','Ô':'o','Ö':'o', 'Ù':'u','Ú':'u','Û':'u','Ü':'u'};
	return this.replace(/[ÀÁÂÃÄÅÆÈÉÊËÌÍÎÒÓÔÖÙÚÛÜ]/gi, function(m) {
		var ret = __r[m.toUpperCase()];
		return ret;
	});
};

/*
* Recoge el campo de búsqueda del buscador y elimina los acentos
*/
function compVacio(campo) {
	var query=jQuery.trim($('#'+campo).val());
	if (query=="") return false;
	queryBus = query.removeAccents();
	document.location="/buscar/"+queryBus.toLowerCase().replace(/\s/g,"-");
	return false;
}

