
var skin;
$(document).ready(leerCookie);

// capturo el evento
function leerCookie(){

    var ckArr = document.cookie.split("; ");
    var userInfo = null;
    var horoscopo_y_tiempo = null;
    var userType = 0;
    var ipUser = false;
    for (var nBucle=0;nBucle<ckArr.length;nBucle++){
        var aCookie = ckArr[nBucle].split("=");
        if ( (aCookie[0].substring(0,5)) == 'cUser'){
            userInfo = unescape(aCookie[1]);
        }
        if ( (aCookie[0].substring(0,5)) == 'Iuser'){
		  	if(userInfo == 'nouser' || userInfo == null || userInfo == '&nbsp;'){	
            	ipUser = true;
			}
        }
        if ( (aCookie[0].substring(0,9)) == 'elPaisXXX'){
            var userInfo2;
            userInfo2 = unescape(aCookie[1]).split("#");
            userType = userInfo2[0];
        }
        if ( (aCookie[0].substring(0,14)) == 'elPaisColorXXX'){
            skin = unescape(aCookie[1]);
        }
        if ( (aCookie[0].substring(0,18)) == 'horoscopo_y_tiempo'){
            horoscopo_y_tiempo = unescape(aCookie[1]);
        }
    }//end for


    if (userInfo == null){
        var date = new Date();
        date.setTime((date.getTime() + (8760*60*60*1000)));
        document.cookie = "cUser=nouser; PATH=/; DOMAIN=.infometeo.com; expires=" + date.toGMTString();
        userInfo = "nouser";
    }

    // Si está el usuario
    var cadRegistro = "";
    if (userInfo == "nouser"){
        if (horoscopo_y_tiempo != null){

            cadRegistro = "<div id='personalizacion'><div class='cont_personalizacion estirar'><ul class='acciones'><li><a href='http://www.infometeo.com/clientes2/RegistroBasico.html?backURL=http%3A%2F%2Fwww.infometeo.com%2Fclientes2%2Fmipais%2Fmiperfil.html' title='Registrar'>registrar</a></li><li><a href='http://www.infometeo.com/clientes2/mipais/mipais_nologueado.html' title='Conectar'><strong>conectar</strong></a></li></ul><div class='mod_personalizacion' id='mod_personalizacion'><dl id='horoscopo'></dl><dl class='tiempo' id='tiempo'></dl></div></div></div>";

            $("#info_suscriptor").html(cadRegistro);

            arr = horoscopo_y_tiempo.split(":");
            var horoscopo = arr[0];
            var tiempo = arr[1];
            var  arr_horos = horoscopo.split(",");
            var  arr_tiempo = tiempo.split(",");

            if (arr_horos[0]!=0){
                $.get("/mod_perso.php?u=hor&p=" + arr_horos[1], function(data){
			$("#horoscopo").html(data);
		});
            }
            if (arr_tiempo[0]!=0){
                $.get("/mod_perso.php?u=tie&p=" + horoscopo_y_tiempo, function(data){
			$("#tiempo").html(data);
		});
            }
        }else{

            cadRegistro = "<div id='personalizacion'><div class='cont_personalizacion estirar'><ul class='acciones'><li><a href='http://www.infometeo.com/clientes2/RegistroBasico.html?backURL=http%3A%2F%2Fwww.infometeo.com%2Fclientes2%2Fmipais%2Fmiperfil.html' title='Registrar'>registrar</a></li><li><a href='http://www.infometeo.com/clientes2/mipais/mipais_nologueado.html' title='Conectar'><strong>conectar</strong></a></li></ul></div></div>";

            $("#info_suscriptor").html(cadRegistro);
        }
    }else{

        // se pone la cabecera
        if (ipUser){

            cadRegistro = "<div id='personalizacion'><div class='cont_personalizacion estirar'><ul class='acciones'><a href='/clientes2/desconectar.html?backURL=http://www.infometeo.com' title='Desconectar'>desconectar</a></li></ul><div class='mod_personalizacion' id='mod_personalizacion'></div><p>Hola <a href='#' title='Ver perfil'><strong></strong></a></p>";

        }else{

            cadRegistro = "<div id='personalizacion'><div class='cont_personalizacion estirar'><ul class='acciones'><li><a href='/clientes2/mipais/miperfil.html' title='Configuración y personalización. Colores, estilos, Newsletters, Widtgets'>mi país</a></li><li><a href='/clientes2/desconectar.html?backURL=http://www.infometeo.com' title='Desconectar'>desconectar</a></li></ul><div class='mod_personalizacion' id='mod_personalizacion'><dl id='horoscopo'></dl><dl class='tiempo' id='tiempo'></dl></div><p>Hola <a href='https://plus.infometeo.com/clientes2/mipais/modificacionDatos/editarPerfilUsuario.html' title='Ver perfil'><strong>"+ userInfo +"</strong></a></p></div></div>";

        }
        cadRegistro += "</div></div>";

        if (cadRegistro!=""){
            $("#info_suscriptor").html(cadRegistro);
        }

        if (horoscopo_y_tiempo != null){
            arr = horoscopo_y_tiempo.split(":");
            var horoscopo = arr[0];
            var tiempo = arr[1];
            var arr_horos = horoscopo.split(",");
            var arr_tiempo = tiempo.split(",");

            if (arr_horos[0]!=0){
                $.get("/mod_perso.php?u=hor&p=" + arr_horos[1], function(data){
			$("#horoscopo").html(data);
		});
            }
            if (arr_tiempo[0]!=0){
                $.get("/mod_perso.php?u=tie&p=" + horoscopo_y_tiempo, function(data){
			$("#tiempo").html(data);
		});
            }

        }
    }
}


rules_portada = {
evento :
    function() {
        $("#indexPortada").click(rules_portada.ejecutar);
    },
ejecutar :
    function() {
        return leerGlobalCookie();
    }
}
$(document).ready(rules_portada.evento);



function leerGlobalCookie(){

    nombre="elPaisGlobal";
    if (document.cookie.indexOf( nombre )!= -1){
        a = document.cookie.substring(document.cookie.indexOf( nombre + '=') + nombre.length + 1,document.cookie.length);
        if (a.indexOf(';') != -1)
            a = a.substring(0,a.indexOf(';'))
                if (a==1)
            {
                window.location.href="/global/";
                return false;
            }
    }else{
	var html = $.ajax({url: "/geoIp.php?", async: false}).responseText;
        if (html==0)
        {
            window.location.href="/global/";
            return false;
        }
    }
    return true;
}



function isStyleImpresa()
{
    var sheetList = document.styleSheets;
    var hrefSheet;
    var i, j;

    /* look through stylesheets in reverse order that
       they appear in the document */
    for (i=sheetList.length-1; i >= 0; i--)
    {
        hrefSheet = sheetList[i].href;
        if (hrefSheet != undefined)
            if (hrefSheet.indexOf("impresa.css")!= -1)
                return true;
    }
    return false;
}


function leerLaCookie() {
    if (!isStyleImpresa()){
        nombre="personalizacion";
        if (document.cookie.indexOf( nombre )!= -1){
            a = document.cookie.substring(document.cookie.indexOf( nombre + '=') + nombre.length + 1,document.cookie.length);
            if (a.indexOf(';') != -1)
                a = a.substring(0,a.indexOf(';'))
                    procesar(unescape(a));

        }
    }
}



var urlImg="/im/tit_logo.gif";



function getCabecera(){
    html = '<a href="/" title="Volver a la portada de ELPAIS.com"><img src="'+urlImg+'" alt="ELPAIS.COM" id="imgCab" /></a>';
    return html;
}



function procesar(personalizacion){
    arr = personalizacion.split(":");
    imgsCabeceras = arr[0].split(",");
    if (imgsCabeceras[0] != "")
        urlImg = "/mipais/img/"+imgsCabeceras[0];
    titular     = "tit="+arr[1];
    entradilla  = "ent="+arr[2];
    noticia     = "noticia="+arr[3];
    color       = "color="+arr[4];
    parametros= titular + "&" + entradilla + "&" + noticia + "&" + color;
    link = '<link rel="stylesheet" href="/clientes2/mipais/personalizacion.php?'+parametros+'" type="text/css" />';
    document.write(link);

}


function escribirGlobalCookie(valor) {
    nombre="elPaisGlobal";
    if (document.cookie.indexOf( nombre )== -1){
        var date = new Date();
        date.setTime((date.getTime() + (8760*60*60*1000)));
        document.cookie="elPaisGlobal="+valor+"; PATH=/; DOMAIN=.infometeo.com; expires= " + date.toGMTString();
    }
}


leerLaCookie();


function compVacio(campo,urlBase,campoObject) {
	var query=jQuery.trim($('#'+campo).val());

	if (campoObject!=undefined){
		query = jQuery.trim($(campoObject).val());
	}
	if (query=="") return false;
	
	var queryBus = query.removeAccents();
	document.location=urlBase+"/"+queryBus.toLowerCase().replace(/\s/g,"-");
	return false;
}


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;
	});
};


$(document).ready(function(){
	$('form[name=fbusqueda]').submit(function (){
		compVacio('query','/buscar',$('form[name=fbusqueda] input[name=query]'));
		
		return false;
	});
});

