/* ***************************************************************************************

$(document).ready
getViewSize
suscribirUsuario
updateLinkSocial

* ***************************************************************************************/

//$(document).ready
$(document).ready(function()
{
    //===============================================
    //RESOLUCION DE PANTALLA Y DISPOSITIVO DE CLIENTE

    //alert('#DISPOSITIVO_CLIENTE ' + $('#DISPOSITIVO_CLIENTE').val());
    if (!$('#DISPOSITIVO_CLIENTE').val())
    {
        var ancho = getViewSize();

        //alert(ancho);
        if (document.location.href.indexOf('dev=') < 0)
        {
            if (ancho < 600)
            {
                var dev = 'dev=hh';
            }
            else if (ancho < 800)
            {
                var dev = 'dev=med';
            }
            else
            {
                var dev = 'dev=normal';
            } //if (ancho...

            var ref = document.location.href;
            
            if (document.location.href.indexOf('?') >= 0)
            {
                document.location.href = ref + '&' + dev;
            }
            else
                document.location.href = ref + '?' + dev;
        } //if (document.location.href.indexOf('dev=')..
    } //if (!$('#DISPOSITIVO_CLIENTE').val()
    
    //============================================================================
    //LOCALIZADORES DE CONTENIDO Y ACTIVACION DE ESTILOS EN FUNCION DEL CONTENIDO
    var clase_a_activar, id_imagen;
    var IGNORAR_RESTO_LOCALIZADORES = false;
    
    //Activa las entradas de menu que tengan la clase indicada por localizador_contenido
    $('input[name=localizador_contenido_exclusivo]').each(function()
    {
        //alert($(this).val());
        clase_a_activar = $(this).val();
        $('.' + clase_a_activar).addClass('on');
        $('.' + clase_a_activar).parents("li").addClass('on');
        IGNORAR_RESTO_LOCALIZADORES = true;
    });
    
    if (!IGNORAR_RESTO_LOCALIZADORES)
    {
        //Activa las entradas de menu que tengan la clase indicada por localizador_contenido
        $('input[name=localizador_contenido]').each(function()
        {
            //alert($(this).val());
            clase_a_activar = $(this).val();
            $('.' + clase_a_activar).addClass('on');
            $('.' + clase_a_activar).parents("li").addClass('on');
        });
    }
        
    //============================================================================
    //ACTIVACION DE IMAGENES EN ENTRADAS DE MENU, EN FUNCION DE LOS LOCALIZADORES 
    //Busca los HIDDEN con nombre "activar_img_X_i" para cambiar 
    //las imagenes "img_X_i" de aquellas entradas de menu que esten en ON
    //y anular sus eventos "onmouseover" y "onmouseout"
    $('input[id^=activar_img]').each(function()
    {
        id_imagen = $(this).attr('id').replace('activar_','');
        
        if ($('#'+id_imagen).parents("li").hasClass('on'))
        {
            $('#'+id_imagen).attr('src', $(this).val());
            $('#'+id_imagen).attr('onmouseover', '');
            $('#'+id_imagen).attr('onmouseout', '');
        }
    });
});

//getViewSize
//Obtiene las dimensiones de la pantalla (de momento, solo el ancho)
function getViewSize()
{
    //alert('getViewSize');

    var viewportwidth;
    var viewportheight;
    
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined')
    {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
    }
    
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    
    else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
    {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
    }
    
    // older versions of IE
    
    else
    {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    //this.ancho = viewportwidth;
    //this.alto = viewportheight;
    
    return viewportwidth;
 }

//Realiza la suscripcion de suscriptores al boletin
function suscribirUsuario(objNombre, pemail, ruta_home)
{
    var pnombre;
    if (objNombre)
        pnombre = objNombre.value;
    else
        pnombre = ' '; //Pone un valor distinto de '' para validarlo
    
    if (!validarMail(pemail) || pnombre=='')
    {
        //Si no hay campo nombre no lo incluye en el mensaje
        if (objNombre)
            alert('Debe proporcionar un nombre y un mail válidos');
        else
            alert('Debe proporcionar un mail válido');
        
        return false;
    }

    if (pemail.length>250 || pnombre.length>50)
    {
        alert('Los valores son demasiado largos');
        return false;
    }

    //Añade una "/" final a ruta_home (si procede)
    if (ruta_home.lastIndexOf('/') < ruta_home.length)
        ruta_home += '/';
        
    if (pnombre == ' ') pnombre=''; //Vuelve a poner cadena vacia en pnombre
    
    $.post(ruta_home + 'xsuscriptor.aspx',
        {
            nombre: pnombre,
            email: pemail
        },
        function(data){
            alert(getXMLParam(data, 'suscripcion'));
        });
}

function updateLinkSocial(new_share_url)
{
    var url = window.location.href;
    var http_pos;
    
    $('a.link-social').each(function(){
        http_pos = Math.max($(this).attr('href').lastIndexOf('http://'), $(this).attr('href').lastIndexOf('https://'));
        if (http_pos>0)
        {
            $(this).attr('href', $(this).attr('href').substring(0, http_pos) + new_share_url);
        }
    });
}

