﻿var categoriaCero = 45;

$(document).ready(function () {
    //Cargamos botón Google+ cabecera
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    setTimeout(function () {
        s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
    }, 100);

    //Evitamos la propagación de eventos en los links con clase "noev"
    $("a.noev").click(function (event) {
        event.stopPropagation();
    });

    $(".fondoThumb > img").error(function () {
        //alert('Error"');
        $(this).unbind("error").attr("src", "http://www.ganga.es/imgWeb/NoFoto.gif");
    });
});

function cargarComboCategorias(combo,valor,nivelUrl,soloGrupos) {
    var preUrl = '';
    if (nivelUrl == 2) preUrl = '../';
    if (nivelUrl == 3) preUrl = '../../';

    $.ajax({
        type: "POST",
        url: preUrl + "webServices/wsNav.asmx/getCategoriasCombos",
        contentType: "application/json; charset=utf-8",
        data: "{idCategoriaPadre:'" + categoriaCero + "',soloGrupos:'" + soloGrupos +"'}",
        dataType: "json",
        success: function (msg) {
            llenaComboCat(combo, msg.d, valor);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            //alert(xhr.status);
            //alert(thrownError);
        }
    });
}

function cargarComboProvincias(combo, valor, nivelUrl) {
    var preUrl = '';
    if (nivelUrl == 2) preUrl = '../';
    if (nivelUrl == 3) preUrl = '../../';

    $.ajax({
        type: "POST",
        url: preUrl + "webServices/wsNav.asmx/getProvinciasCombos",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            llenaCombo(combo, msg.d, valor);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            //alert(xhr.status);
            //alert(thrownError);
        }
    });
}

function llenaComboCat(combo,texto,valor) {
    var optionsValues = '';
    $.each(texto, function () {
        optionsValues += '<option value="' + this.Value + '"';
        if (($.trim(this.Text).charAt(0) == "[")) {
            optionsValues += ' class="Grupo"'
        } else if (($.trim(this.Text).charAt(0) != "-") && (this.Value != 0)) {
            optionsValues += ' class="Supercategoria"'
        };
        if (this.Value == valor) optionsValues += ' Selected';
        optionsValues += '>' + this.Text + '</option>';
    });
    $(combo).html(optionsValues)
}

function llenaCombo(combo, texto, valor) {
    var optionsValues = '';
    $.each(texto, function () {
        optionsValues += '<option value="' + this.Value + '"';
        if (this.Value == valor) optionsValues += ' Selected';
        optionsValues += '>' + this.Text + '</option>';
    });
    $(combo).html(optionsValues)
}

//Mueve la publicidad de un Div a otro para carga al final de página
function moveAds(divSrcId, divDestId) {
    //$("#" + divDestId + "").append($("#" + divSrcId + ""));

    $("#" + divSrcId + " script").remove();
    $("#" + divSrcId + "").appendTo("#" + divDestId + "");
}

function ves(urlcod) {
    var url = urlcod.replace('#', '/');
    url = 'http://www.ganga.es/' + url + '.html';
    location.href = url;
}

function alerta(texto) {
    var $dialog = $('<div class="alerta"></div>')
		.html(texto.replace( /\n/g, '<br />\n' ))
		.dialog({
		    autoOpen: false,
		    title: 'ganga.es',
		    buttons: { "Ok": function () { $(this).dialog("close"); } },
		    modal: true,
		    resizable: false
		});
    $dialog.dialog('open');
}

$.datepicker.setDefaults({
    dateFormat: 'dd/mm/yy',
    dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'],
    monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo',
                    'Junio', 'Julio', 'Agosto', 'Septiembre',
                    'Octubre', 'Noviembre', 'Diciembre'],
    monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr',
                    'May', 'Jun', 'Jul', 'Ago',
                    'Sep', 'Oct', 'Nov', 'Dic']
});

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function mostrar18() {
    $("#msgAdulto").hide();
    $("#cntAnuncio").show();
    $("#cntLateralAnuncio").show();
    createCookie("adulto", "true", 1);
}

function abrirNuevaVentana(url) {
    url_to_open = $(this).attr("href");
    window.open(url, '_blank');
    return false;
}

function abrirCondiciones() {
    abrirNuevaVentana('http://www.ganga.es/Docs/condiciones.htm');
}
function abrirProteccion() {
    abrirNuevaVentana('http://www.ganga.es/Docs/proteccion.htm');
}

function getBanner(zoneId,idCat) {
    var m3_u = (location.protocol == 'https:' ? 'https://adserver.ganga.es/www/delivery/ajs.php' : 'http://adserver.ganga.es/www/delivery/ajs.php');
    var m3_r = Math.floor(Math.random() * 99999999999);
    if (!document.MAX_used) document.MAX_used = ',';
    document.write("<scr" + "ipt type='text/javascript' src='" + m3_u);
    document.write("?zoneid=" + zoneId);
    document.write("&amp;categoria=" + idCat);
    document.write('&amp;cb=' + m3_r);
    if (document.MAX_used != ',') document.write("&amp;exclude=" + document.MAX_used);
    document.write(document.charset ? '&amp;charset=' + document.charset : (document.characterSet ? '&amp;charset=' + document.characterSet : ''));
    document.write("&amp;loc=" + escape(window.location));
    if (document.referrer) document.write("&amp;referer=" + escape(document.referrer));
    if (document.context) document.write("&context=" + escape(document.context));
    if (document.mmm_fo) document.write("&amp;mmm_fo=1");
    document.write("'><\/scr" + "ipt>");
}

