function irNueva(pagina) {
    var win = window.open(pagina, 'Barcelo', "toolbar=no,location=no,direction=no,status=no,scrollbars=yes,resizable=no,copyhistory=no,width=1024,height=768");
    win.focus();
    return false;
}

function ir(pagina) {
    if (pagina && pagina != "") { self.location = pagina; }
}

function irHome() {
    self.location = homeUrl;
}

function confirmarSubmit(f, pregunta, accion)
{
    if (confirm("¿Está seguro/a de que desea" + pregunta + "?")) {
        f.action = accion;
        f.submit();
    }
}

var cromusSeleccionados = [], descargaSeleccionada = -1, necesarioSeleccionado = false;

function seleccionarCromu(id, necesario)
{
    var quedan = parseInt( $("#numCromus_" + id).html() );
    if (necesario && !necesarioSeleccionado)
    {
        necesarioSeleccionado = true;
        $('#cromuNecesario').css('background-image', $('#cromuNecesario').css('background-image').replace('_sep.jpg', '.jpg') );
    }
    else
    {
        if ($.inArray(id, [0, 2, 4, 5, 7]) != -1) { if (!confirm("¿Estás seguro de que quieres usar ese e-cromuuu?")) { return; } }
        var num = parseInt( $("#cromusRestantes").html() );
        if (num == 0) { $.modal('Ya tienes todos los e-cromuuus extra que necesitas'); return; }
        if (quedan <= 0) { return; }
        $("#cromusRestantes").html(num - 1); cromusSeleccionados[cromusSeleccionados.length] = id;
    }

    $("#numCromus_" + id).html(quedan - 1);
    if (quedan == 1) { $("#botonCromu_" + id).remove(); $("#numCromus_" + id).remove(); $("#cromu" + id).css('background-image', $("#cromu" + id).css('background-image').replace('.jpg', '_off.jpg') ); }
}

function seleccionarDescarga(id)
{
    $('.candado').removeClass('candadoSeleccionado'); $('#candado_' + id).addClass('candadoSeleccionado');
    descargaSeleccionada = id;
}

function canjearPremio()
{
    var num = parseInt($("#cromusRestantes").html()), premio = $("#cromusRestantes").attr('premio');
    if (num > 0) { $.modal('Aún no has seleccionado e-cromuuus suficientes para canjear el premio'); return; }

    $.post('canjear.php', { 'premio': premio, 'cromuNecesario': necesarioSeleccionado ? '1' : '0', 'cromus': cromusSeleccionados, 'descarga': descargaSeleccionada }, function(data) {
        if (data == 'OK') {
            var texto = 'Enhorabuena, has canjeado correctamente el premio.';
            if ($("#cromusRestantes").attr('premio') != '1') { texto += ' Lo recibirás en el domicilio que nos has indicado en tu perfil en el plazo de un mes.'; }
            $.modal(texto, { onClose: function() { top.location = 'album.php'; } }); }
        else { alert(data); }
    });
}

function submitTo(f, accion)
{
    f.attr('action', accion);
    f.submit();
}

function statusMsg(msg) {
    window.status = msg;
    return true
}

function elegirAvatar(dir)
{
    var src = $('#avatar').attr('src'), avatarId = parseInt($('#avatarId').val());

    avatar = (9 + dir + avatarId) % 9; $('#avatarId').val(avatar);
    var temp = src.split('_'); $('#avatar').attr('src', temp[0] + '_' + avatar + '.png');
}

function popupNIF(menor)
{
    var texto = menor ? 'Pedimos el NIF/NIE de tus padres para que en caso de que te toque un premio, podamos contactar con ellos y enviártelo.' : 'Te pedimos tu NIF/NIE para verificar tu identidad si ganas un premio.';
    $.modal(texto);
}

function popupImagen(img, w, h)
{
    var size = { width: window.innerWidth || document.body.clientWidth, height: window.innerHeight || document.body.clientHeight }
    var ratio = 1;

    if (size['height'] < h + 138) { ratio = (size['height'] - 138.0) / h; h *= ratio; w *= ratio; }
    if (size['width'] < w + 111) { ratio = (size['width'] - 111.0) / w; h *= ratio; w *= ratio; }

    var html = '<TABLE border="0" cellspacing="0" cellpadding="0"> ' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR>' +
            '<TR><TD class="bVertical"> </TD>' +
                '<TD><IMG src="' + img + '" style="display:block; width: ' + w + 'px; height: ' + h + 'px;" height="' + h + '" width="' + w + '" /></TD>' +
                '<TD class="bVertical"> </TD></TR>' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR></TABLE>';


    $.modal(html, { containerCss: { height: h + 92, width: w + 74 } });
    $('#simplemodal-container a.modalCloseImg').css('right', (w / 2) - 20);
}

function mostrarPreviewTonos()
{
    var w = 377, h = 100;
    var html = '<TABLE border="0" cellspacing="0" cellpadding="0"> ' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR>' +
            '<TR><TD class="bVertical"> </TD>' +
                '<TD style="width: ' + w + 'px; height: ' + h + 'px; overflow: hidden">' +
                    '<IFRAME frameborder="0" width="' + w + '" height="' + h + '" src="' + homeUrl + '/tonos.php" scrolling="no" style="display: block"></IFRAME>' +
                '</TD>' +
                '<TD class="bVertical"> </TD></TR>' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR></TABLE>';


    $.modal(html, { containerCss: { height: h + 92, width: w + 74 } });
    $('#simplemodal-container a.modalCloseImg').css('right', (w / 2) - 20);

}

function mostrarJuegoAjax(i)
{
    var w = 900, h = 500, juego = '';
    switch (i)
    {
        case 1: juego = 'patinete'; break;
        case 2: juego = 'marmitas'; break;
        case 3: juego = 'baile'; break;
        case 4: juego = 'vackart'; break;
    }

    var html = '<TABLE border="0" cellspacing="0" cellpadding="0"> ' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR>' +
            '<TR><TD class="bVertical"> </TD>' +
                '<TD style="width: ' + w + 'px; height: ' + h + 'px; overflow: hidden">' +
                    '<IFRAME frameborder="0" width="' + w + '" height="' + h + '" src="' + homeUrl + '/juegos/' + juego + '.php" scrolling="no" style="display: block"></IFRAME>' +
                '</TD>' +
                '<TD class="bVertical"> </TD></TR>' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR></TABLE>';


    $.modal(html, { containerCss: { height: h + 92, width: w + 74 } });
    $('#simplemodal-container a.modalCloseImg').css('right', (w / 2) - 20);
}

function mostrarConsejos(w, h, ruta)
{
    var html = '<TABLE border="0" cellspacing="0" cellpadding="0"> ' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR>' +
            '<TR><TD class="bVertical"> </TD>' +
                '<TD style="width: ' + w + 'px; height: ' + h + 'px; overflow: hidden">' +
                    '<IFRAME frameborder="0" width="' + w + '" height="' + h + '" src="' + homeUrl + ruta + '" scrolling="no" style="display: block"></IFRAME>' +
                '</TD>' +
                '<TD class="bVertical"> </TD></TR>' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR></TABLE>';


    $.modal(html, { containerCss: { height: h + 92, width: w + 74 } });
    $('#simplemodal-container a.modalCloseImg').css('right', (w / 2) - 20);
}

function mostrarVideo(w, h, ruta)
{
    var html = '<TABLE border="0" cellspacing="0" cellpadding="0"> ' +
            '<TR><TD colspan="3" class="bHorizontalV"> </TD></TR>' +
            '<TR><TD class="bVerticalV"> </TD>' +
                '<TD style="width: ' + w + 'px; height: ' + h + 'px; overflow: hidden">' +
                    '<IFRAME frameborder="0" width="' + w + '" height="' + h + '" src="' + homeUrl + ruta + '" scrolling="no" style="display: block"></IFRAME>' +
                '</TD>' +
                '<TD class="bVerticalV"> </TD></TR>' +
            '<TR><TD colspan="3" class="bHorizontalV"> </TD></TR></TABLE>';


    $.modal(html, { containerCss: { height: h + 52, width: w + 19 } });
    $('#simplemodal-container a.modalCloseImg').css('right', (w / 2) - 60).addClass('modalCloseImgV');
}

function mostrarRegistrateVackart()
{
    ir(homeUrl + '/vackart.php');
}

function mostrarDescarga(i)
{
    var w = 400, h = 300;
    var html = '<TABLE border="0" cellspacing="0" cellpadding="0"> ' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR>' +
            '<TR><TD class="bVertical"> </TD>' +
                '<TD style="width: ' + w + 'px; height: ' + h + 'px; overflow: hidden"><IMG src="' + homeUrl + '/images/descarga_' + i + '.jpg" style="display:block;"/></TD>' +
                '<TD class="bVertical"> </TD></TR>' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR></TABLE>';


    $.modal(html, { containerCss: { height: h + 92, width: w + 74 } });
    $('#simplemodal-container a.modalCloseImg').css('right', (w / 2) - 20);
}

function disableTextSelection()
{
    $.extend($.fn.disableTextSelect = function()
    {
        return this.each(function() {
            if ($.browser.mozilla) { $(this).css('MozUserSelect', 'none'); }
            else if ($.browser.msie) { $(this).bind('selectstart', function() { return false; } ); }
            else{ $(this).mousedown( function() { return false; } ); }
        } );
    } );
    $('.noSelect').disableTextSelect();
}

function mostrarPopupPremio(num)
{
    var html = '<DIV style="background: white url(' + homeUrl + '/images/premio_' + num + '.png) center center no-repeat; height: 500px; width: 500px;"></DIV>';
    $.modal(html, { containerCss: { height: 500, width: 500 } });
    $('#simplemodal-data').css('padding', 0);
    $('#simplemodal-container a.modalCloseImg').css('right', 180);
}

var datosRankings = [], pagRankings = 1;
function mostrarRankings(num)
{
    $.post(homeUrl + '/flash/dispatch.php?service=classement_jeu', { 'id_jeu': num }, function(data) {
        var w = 491, h = 354;
        datosRankings = $.parseJSON(data); pagRankings = 1;

        var html = '<TABLE border="0" cellspacing="0" cellpadding="0"> ' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR>' +
            '<TR><TD class="bVertical"> </TD>' +
                '<TD style="width: ' + w + 'px; height: ' + h + 'px; overflow: hidden">' +
                    '<DIV id="divRankings" style="background: white url(' + homeUrl + '/images/fondoRankings.jpg) center center no-repeat; width: 491px; height: 354px;"></DIV>' +
                '</TD>' +
                '<TD class="bVertical"> </TD></TR>' +
            '<TR><TD colspan="3" class="bHorizontal"> </TD></TR></TABLE>' +
            '<DIV id="rankingsAnterior" class="noSelect" onClick="pintarPaginaRankings(-1)"></DIV>' +
            '<DIV id="rankingsSiguiente" class="noSelect" onClick="pintarPaginaRankings(1)"></DIV>';

        $.modal(html, { containerCss: { height: h + 92, width: w + 74 } });
        $('#simplemodal-container a.modalCloseImg').css('right', (w / 2) - 20);
        pintarPaginaRankings(0);
    });
}

function pintarPaginaRankings(mover)
{
    pagRankings += mover;
    if (pagRankings < 1) { pagRankings = 1; }
    if (pagRankings > 7) { pagRankings = 7; }

    var porPg = 15, inicio = (pagRankings - 1) * porPg, html = '<TABLE cellspacing="0" cellpadding="0" border="0">';
    for (var i = inicio; i < inicio + porPg && i < 100; i++)
    {
        var datos = datosRankings[i];
        html += '<TR>' +
                    '<TD width="25" class="pos">' + datos[0] + '</TD><TD width="85"> </TD>' +
                    '<TD width="200" class="nick">' + datos[1] + '</TD width="15"> </TD>' +
                    '<TD width="70" class="puntos">' + datos[2] + '</TD></TR>';
    }
    html += '</TABLE>';

    $('#divRankings').html(html);
}

function abreMenu(id)
{
    $('.menu2').hide();
    $('.menu' + id + ' .menu2').show();
}

function fadeIn(url)
{
    $('<img />').attr('src', url).load(function()
    {
        $('#cargando').fadeOut(1000);
        $('#central').css({'backgroundImage': 'url('+url+')'}).hide().css({visibility: "visible"}).fadeIn(1000);
    });
}

function init()
{
    disableTextSelection();
}

$(document).ready( init );

