function irNueva(pagina) {
    var win = window.open(pagina, 'Moma', "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('Funcionalidad pendiente de implementar');
}

function confirmarSubmit(f, pregunta, accion)
{
    if (confirm("¿Está seguro/a de que desea " + pregunta + "?")) {
        f.action = accion;
        f.submit();
    }
}

function confirmarTo(pregunta, pagina)
{
    if (confirm("¿Está seguro/a de que desea " + pregunta + "?")) {
        ir(pagina);
    }
}

function submitTo(f, accion)
{
    f.attr('action', accion);
    f.submit();
}

function olvidoPassword()
{
    $('#olvido').val(1);
    document.guardar.submit();
}

function procesarMailTo(mailTo, tipoId, id)
{
    // TODO: validar mail
    if (mailTo != '' && mailTo != null)
    {
        ir('enviarMail.php?' + tipoId + '=' + id + '&mail=' + mailTo);
    }
}

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 ordenar(destino, id, orden, tipo)
{
    ir(destino + '?id=' + id + '&orden=' + orden + '&tipo=' + tipo);
}

function recenterModal(height)
{
    var windowHeight = $(window).height();

    $('.ui-dialog').css('top', (Math.floor((windowHeight - height) / 2)));
}

function modal(titulo, texto, tipo, botones)
{
    var div = '<DIV class="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);
    recenterModal(210);
}

function modalVideo(href, width, height)
{
    var h, w;

    if (width) { w = width; }
    if (height) { h = height; }

    var opts = {center: true, modal: true, overlay: { opacity: 0.6, background: '#E0E0E0'},  width: w + 'px', dialogClass: 'flv', close: function (evt, ui) { cerrarModalIframe(); }};
    if (!ie) { opts['height'] = h + 'px'; }
    else { opts['min-height'] = (h + 20) + 'px';  }

    pintarIframe(href, opts);
}

function pintarIframe(href, opts)
{
    var h = '320px', w = '620px';

    if (opts['width']) { w = opts['width']; }
    if (opts['height']) { h = opts['height']; }
    if (opts['min-height']) { h = opts['min-height']; }

    var iframe = '<div class="divModal"><iframe id="modal_iframe" width="' +  w + '" height="' +  h + '" frameborder="0" src="' + href + '" /></div>';
    $(iframe).dialog(opts);

    recenterModal(h.substr(0, (h.length - 2)));
}

function modalIframe(href, width, height)
{
    var h = 296, w = 500;

    if (width) { w = width; }
    if (height) { h = height; }

    var iframe = '<div class="divModal"><iframe id="modal_iframe" width="' + w + 'px" height="' + h + 'px" scrolling="no" frameborder="0" src="' + href + '" /></div>';
    $(iframe).dialog({ modal: true, overlay: { opacity: 0.6, background: 'black' }, width: w + 'px' });
    recenterModal(339);
}

function cerrarModalIframe()
{
    $('#divModalIframe').dialog('destroy').remove();
    $('.ui-dialog').remove();
    $('.divModal').remove();
}

/* Un poco chapuza*/
function cerrarModal()
{
    parent.document.location.reload();
}

function descargar(tipo)
{
    var ver = $("#descargar" + tipo);
    var ocultar = $("#infoDescargas");
    ocultar.hide();
    ver.show();
}

function descargaDirecta(docId)
{
    document.bajarZip.docId.value = docId;
    document.bajarZip.submit();
    cerrarModal();
}

function borrarPDF(nombre)
{
    if (confirm("¿Está seguro/a de que desea eliminar el pdf  '" + nombre + "'?"))
    {
        ir('guardar.php?pdf=' + nombre);
    }
}

function buscar()
{
    var tagId;

    tagId = $('select option:selected').val();

    document.buscarTop.tagId.value = tagId;
    document.buscarTop.submit();

    return true;
}

function initCheckbox()
{
    var opsCheckbox = { empty: homeUrl + '/images/empty.png'};
    $('input[@type=checkbox]').checkbox(opsCheckbox);
    $('input[@type=radio]').checkbox(opsCheckbox);
}

function initSelectBox(id)
{
    $('#' + id).selectbox();
}

/* Para el slide de la home */
function initSlide()
{
    $(window).bind("load", function()
    {
        $("div#slider").slideViewerPro({
            thumbsVis: false,
            autoslide: true,
            asTimer: 3500,
            typo: false,
            galBorderWidth: 0,
            buttonsTextColor: "#707070",
            buttonsWidth: 40,
            shuffle: true
        });
    });
}

/* Para el autocomplete */
function extractLast(term)
{
    return split(term).pop();
}

function split(val)
{
    return val.split( /,\s*/ );
}

function initAC(id, destino)
{
    var opts = {
        source: function( request, response ) {
            $.getJSON( destino, { term: extractLast( request.term ) }, response )
        },
        focus: function() { return false; },
        select: function (event, ui) {
            var terms = split( this.value );
            // remove the current input
            terms.pop();
            // add the selected item
            terms.push( ui.item.value );
            // add placeholder to get the comma-and-space at the end
            terms.push( "" );
            this.value = terms.join( ", " );
            return false;
        }
    };

    $(id).autocomplete(opts);
}
