var tipos = {
        1: ["deptoCentralId"], // Perfil central
        2: ["hotId", "deptoHotelId", "fax"], // Perfil hotel
        3: ["empresa", "tipoExternoId", "fax", "motivo"] // Perfil externo
    };

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 pte()
{
    alert(TXT_PTE);
}

function confirmarSubmit(f, pregunta, accion)
{
    if (confirm("¿" + TXT_CONFIRM + pregunta + "?")) {
        f.action = accion;
        f.submit();
    }
}

function submitTo(f, accion)
{
    f.attr('action', accion);
    f.submit();
}

function olvidoPassword()
{
    $('#olvido').val(1);
    document.guardar.submit();
}

function enviarMail(tipoId, id)
{
    var texto = TXT_ENVIARMAIL + '<BR/><BR/><INPUT type="text" id="email" name="email"/>';
    var botones = {
        'Enviar': function() { procesarMailTo($('#email').val(), tipoId, id); $(this).dialog("close"); },
        'Cancelar': function() { $(this).dialog("close"); }
    }

    modal('Barceló', texto, 'modalFlecha', botones);
}

function probarComunicado(novId)
{
    var texto = TXT_PROBARCOMUNICADO + '<BR/><BR/><INPUT type="text" id="email" name="email"/>';
    var botones = {
        'Enviar': function() { procesarComunicadoTo($('#email').val(), novId); $(this).dialog("close"); },
        'Cancelar': function() { $(this).dialog("close"); }
    }

    modal('Barceló', texto, 'modalFlecha', botones);
}

function avisoElementosCom()
{
    var botones = {
        'Aceptar': function() { $(this).dialog("close"); }
    }

    modal('Barceló', TXT_AVISOELEMENTOSCOM, 'modalAviso', botones);
}

function procesarMailTo(mailTo, tipoId, id)
{
    // TODO: validar mail
    if (mailTo != '' && mailTo != null)
    {
        ir('enviarMail.php?' + tipoId + '=' + id + '&mail=' + mailTo);
    }
}

function procesarComunicadoTo(mailsTo, novId)
{
    // TODO: validar mail
    if (mailsTo != '' && mailsTo != null)
    {
        ir('probar.php?novId=' + novId + '&mails=' + mailsTo);
    }
}

function descargarOriginal()
{
    document.guardar.downFlg.value = '1';
    document.guardar.submit();
}

function statusMsg(msg) {
    window.status = msg;
    return true
}

function verPagina(pg)
{
    $('#pgNum').val(pg);
    $('#paginador').submit();
}

function muestraPopUp(num)
{
    var pos = $('#thumb' + num).offset({ border: true, padding: true });
    $('#popup' + num).css('display', 'block').css('top', pos['top'] - 58).css('left', pos['left'] - 90);
}

function ocultaPopUp(num)
{
    $('#popup' + num).css('display', 'none');
}

function ocultarTodos()
{
    var todos;

    ocultos = ['deptoCentralId', 'hotId', 'deptoHotelId', 'empresa', 'tipoExternoId', 'fax', 'motivo'];

    // Ocultamos todos
    jQuery.each(ocultos, function(i) {
        $('#guardar #' + this).parents('div.lineaForm').hide();
    });
}

function ordenar(destino, id, orden, tipo)
{
    ir(destino + '?id=' + id + '&orden=' + orden + '&tipo=' + tipo);
}

function muestraTipo()
{
    // Ocultamos los no comunes
    ocultarTodos();

    // Tipo a mostrar
    var tipo = $('#guardar #tipo').val();
    if (tipo > 1000) { tipo = 3; }

    // Mostramos los correspondientes al perfil
    $.each(tipos[tipo], function () { $('#guardar #' + this).parents('div.lineaForm').show(); });
}

function initComboCategorias()
{
    var banId = $('#banId').val();
    vaciarCombo('catId');
    $.each(categorias, function()
    {
        if (banId == -1 || banId == this['banId'] || this['banId'] == -1 ) {
            $('#catId').addOption(this['value'], this['caption'], this['value'] == catId );
        }
    });
    $('#catId').val(catId);
}

function cambiarCategorias()
{
    catId = $('#catId').val();
    initComboCategorias();
    recargarCombo('catId');
}

function initCombosHoteles()
{
    vaciarCombo('paiId');
    $.each(paises, function() { $('#paiId').addOption(this['value'], this['caption'], this['value'] == paiId ); } );
    vaciarCombo('desId');
    $.each(destinos, function()
    {
        if (paiId == -1 || paiId == this['paiId']) {
            $('#desId').addOption(this['value'], this['caption'], this['value'] == desId );
        }
    });
    vaciarCombo('hotId');
    $.each(hoteles, function()
    {
        if ((paiId == -1 || paiId == this['paiId']) && (desId == -1 || desId == this['desId'])) {
            $('#hotId').addOption(this['value'], this['caption'], this['value'] == hotId );
        }
    });
    $('#paiId').val(paiId); $('#desId').val(desId); $('#hotId').val(hotId);
}

function initCombosSenalizacion()
{
    vaciarCombo('marId');
    $.each(marcas, function() { $('#marId').addOption(this['value'], this['caption'], this['value'] == marId ); } );
    vaciarCombo('secId');
    $.each(secciones, function()
    {
        if (marId == this['marId']) {
            $('#secId').addOption(this['value'], this['caption'], this['value'] == secId );
        }
    });
    $('#marId').val(marId);
    $('#secId').val(secId);
}

function cambiarValoresSenalizacion()
{
    marId = $('#marId').val();
    secId = $('#secId').val();
    initCombosSenalizacion();
    recargarCombo('marId');
    recargarCombo('secId');
}

function todosLogosCarrito(todosDescargables)
{
    var ext = $('input:radio[@checked]')[0].value;
    if (ext == 'ai' || todosDescargables) { submitTo($('#descarga'), 'carrito.php'); }
    else { dialogoNoDescargables(); }
}

function dialogoNoDescargables()
{
    var texto = TXT_AVISONODESCARGABLE;
    var botones = {
        'Añadir el vectorial': function() { noDescargables('ai'); },
        'Añadir sólo los disponibles': function() { noDescargables(''); },
        'Cancelar': function() { $(this).dialog("close"); }
    }

    modal('Barceló', texto, 'modalAviso', botones);
}

function noDescargables(ext)
{
    $('#noDescargables').val(ext);
    submitTo($('#descarga'), 'carrito.php');
}

function modal(titulo, texto, tipo, botones)
{
    var div = '<DIV id="divModal"><DIV class="' + tipo + '">&nbsp;</DIV>' + texto + '</DIV>';
    var opts = { modal: true, title: titulo, overlay: { opacity: 0.2, background: 'black' }, width: '470px', height: '210px', buttons: botones };

    $(div).dialog(opts);
}

function modalIframe(href)
{
    var iframe = '<div id="divModalIframe"><iframe id="modal_iframe" width="430px" height="250px" frameborder="0" src="' + href + '" /></div>';
    $(iframe).dialog({ modal: true, title: 'Barceló', overlay: { opacity: 0.6, background: 'black' }, width: '415px', height: '250px' });
}

function cerrarModalIframe()
{
    $('#divModalIframe').dialog('destroy').remove();
}

function cambiarValoresCombos()
{
    paiId = $('#paiId').val(); desId = $('#desId').val(); hotId = $('#hotId').val();
    initCombosHoteles();
    recargarCombo('paiId'); recargarCombo('desId'); recargarCombo('hotId');
}

function vaciarCombo(id)
{
    var combo = $('#' + id);
    for (var i = combo[0].options.length - 1 ; i >= 1; i--) { combo[0].remove(i); }
}

function recargarCombo(id)
{
    $('#' + id + '_input').remove(); $('#' + id + '_container').remove();
    $('#' + id).selectbox();
}

function quitarImagenElemento(tipo, ideId, tipoF, idioma)
{
    if (confirm(TXT_CONFIRMQUITARIMG)) {
        ir('borrarFichero.php?' + tipo + '=' + ideId + '&tipo=' + tipoF + '&idioma=' + idioma);
    }
}

function enviarComunicado()
{
    if (confirm("¿Está seguro de enviar este comunicado?"))
    {
        document.comunicado.cargarUsuarios.value = 1;
        document.comunicado.submit();
    }
}

function borrarFoto()
{
    if (confirm(TXT_CONFIRMQUITARFOTO)) {
        document.guardar.accion.value = 'borrar';
        document.guardar.submit();
    }
}

var totalUsrs = 0;
function actualizaNumeroUsuarios(tipo, num)
{
    if($("#segmentos_" + tipo).attr('checked')) { totalUsrs = totalUsrs + num; }
    else { totalUsrs = totalUsrs - num; }

    $('#totalUsrs').html(totalUsrs);
}

function inicializaNumeroUsuarios(total)
{
    totalUsrs = total;
    $('#totalUsrs').html(totalUsrs);
}

function init()
{
    var opsCheckbox = { empty: homeUrl + '/images/empty.png'};
    $('input[@type=checkbox]').checkbox(opsCheckbox);
    $('input[@type=radio]').checkbox(opsCheckbox);
    $('select').selectbox();
}

$(document).ready( init );
