var ventanas = new Array();
nVentanas = 0;

document.onmousemove = window.onmousemove = function () {
    for (i = 0; i < nVentanas; i++) {
        try {
            if (ventanas[i] != null && !ventanas[i].closed) { ventanas[i].focus(); }
        } catch (e) { }
    };
}

var DIALOGO = function () {
    var _this = this;
    this.countDialog = 0;
    this.ventanaModal = new Array();
    this.open = function (titulo, url, width, height, maximizable, modal) {
        if (width == undefined || height == undefined) draggable = false;
        if (width == undefined) width = vwWidth() - 20; //Maximizo
        if (height == undefined) height = vwHeight() - 60; //Maximizo
        if (maximizable == undefined) maximizable = false;
        if (modal == undefined) modal = true;
        var identificador = "Dialog" + _this.countDialog;
        titulo = "CSMS: " + titulo;
        var div = '<div id="' + identificador + '" class="easyui-window"><iframe scrolling="auto" width="100%" height="100%" src="' + url + '" style="padding:0px; border:0px;"></iframe></div>';
        _this.countDialog++;
        $("body").prepend(div);
        $('#' + identificador).window({
            title: titulo,
            width: width,
            modal: modal,
            shadow: false,
            closed: false,
            height: height,
            resizable: false,
            //resizable: false,
            collapsible: false,
            minimizable: false,
            maximizable: maximizable,
            onClose: function () {
                _this.ventanaModal.pop();
                $(this).panel('destroy');
            }
        });
        $('#' + identificador).window('open');
        _this.ventanaModal.push(identificador);
    }
    this.openDoc = function (url, width, height) {
        var wf = '';
        wf = wf + 'left=' + (screen.width - width) / 2 + 'px';
        wf = wf + ',top=' + (screen.height - height) / 2 + 'px';
        wf = wf + ',screenX=' + (screen.width - width) / 2 + 'px';
        wf = wf + ',screenY=' + (screen.height - height) / 2 + 'px';
        wf = wf + ',width=' + width + 'px';
        wf = wf + ',height=' + height + 'px';
        wf = wf + ',resizable=yes';
        wf = wf + ',scrollbars=no';
        wf = wf + ',menubar=no';
        wf = wf + ',toolbar=no';
        wf = wf + ',directories=no';
        wf = wf + ',location=no';
        wf = wf + ',status=no';
        wf = wf + ',statusbar=no';
        return window.open(url, '_blank', wf);
    }
    this.close = function () {
        if (window.top == window) {
            try {
                self.close();
            } catch (ex) { }
        } else {
            if (window.parent.dialogo.ventanaModal != undefined) {
                var identificador = window.parent.dialogo.ventanaModal.pop();
                window.parent.$('#' + identificador).window('destroy');
            }
        }
    }
    this.resize = function (ancho, alto) {
        if (window.top == window) {
            window.resizeTo(ancho, alto);
        } else {
            var pos = window.parent.dialogo.ventanaModal.length - 1;
            var identificador = window.parent.dialogo.ventanaModal[pos];
            var $item = $('#' + identificador, parent.window.document).parent("div");
            $item.width(ancho).height(alto);
            $item.position({
                my: "center",
                at: "center",
                of: $item.parent()
            });

            $item.find(".panel-header").setWidth("100%");
            $item.find(".panel-body").setWidth("100%").setHeight($item.height() - $item.find(".panel-header").alto());
        }
    }
    this.maximize = function () {
        if (window.top == window) {
            try {
                window.moveTo(0, 0);
            } catch (e) {
            }
            try {
                window.resizeTo(screen.availWidth, screen.availHeight);
            } catch (e) {
            }
        } else {
            if (_this.ventanaModal != undefined) {
                var pos = window.parent.dialogo.ventanaModal.length - 1;
                window.parent.$('#' +  window.parent.dialogo.ventanaModal[pos]).window('maximize');
            }
        }
    }
}
var dialogo = new DIALOGO();

//INICIO: Ventanas de diálogo
(function ($) {
    $.fn.openDialog = function () {
        $(this).show();
        $(this).window({
            modal: true,
            shadow: false,
            closed: false,
            resizable: false,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            onClose: function () {
                $(this).panel('destroy');
            }
        });
    };
})(jQuery);

function extraeDialog(url) {
    var v = windowOpen(url, "_blank", 800, 600, 'yes', 'yes', 'no', 'no', 'no', 'no', 'no', 'no');
    v.moveTo(0, 0);
    v.resizeTo(screen.availWidth, screen.availHeight);
}

function downloadFile(archivo) {
    if ($('#downloadFile').length == 0) { $('<iframe id="downloadFile" name="downloadFile" style="display:none">').appendTo('body'); }
    $('#downloadFile').attr('src', archivo);
}

function zoom(img) {
    var div = '<div id="zoomImagen" title="Visor" style="display:none;overflow:hidden"><center><img id="imagenDeZoom" src="' + img + '"/></center></div>';
    window.top.$("body").prepend(div);
    window.top.$("#imagenDeZoom").load(function () {
        window.top.$('#zoomImagen').show();
        window.top.$('#zoomImagen').window({
            width: window.top.$("#imagenDeZoom").width() + 16,
            height: window.top.$("#imagenDeZoom").height() + 36,
            noheader: false,
            border: false,
            modal: true,
            shadow: false,
            closed: false,
            resizable: false,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            onClose: function () { window.top.$(this).panel('destroy'); }
        });
    });
}
//FIN: Ventanas de diálogo

function windowOpen(Enlace, nombreVentana, Width, Height, resizable, scrollbars, menubar, toolbar, directories, location, status, statusbar) {
    var wf = '';
    wf = wf + 'left=' + (screen.width - Width) / 2 + 'px';
    wf = wf + ',top=' + (screen.height - Height) / 2 + 'px';
    wf = wf + ',screenX=' + (screen.width - Width) / 2 + 'px';
    wf = wf + ',screenY=' + (screen.height - Height) / 2 + 'px';
    wf = wf + ',width=' + Width + 'px';
    wf = wf + ',height=' + Height + 'px';
    wf = wf + ',resizable=' + resizable;
    wf = wf + ',scrollbars=' + scrollbars;
    wf = wf + ',menubar=' + menubar;
    wf = wf + ',toolbar=' + toolbar;
    wf = wf + ',directories=' + directories;
    wf = wf + ',location=' + location;
    wf = wf + ',status=' + status;
    wf = wf + ',statusbar=' + statusbar;
    return window.open(Enlace, nombreVentana, wf);
}
function windowFocus(vent) {
    try {
        if (vent != null && !vent.closed) { vent.focus(); }
    } catch (E) { }
}
function windowRecargarPadre() {
    if (window.opener != null && !window.opener.closed) window.opener.location.href = window.opener.location.href;
}
function recargarPadreDesdeDialog() {
    if (window.parent != null && !window.parent.closed) window.parent.location.href = window.parent.location.href;
}
function windowCenter(vent) {
    try {
        var x = parseInt((parseInt(screen.width) - parseInt(document.body.offsetWidth)) / 2);
        var y = parseInt((parseInt(screen.height) - parseInt(document.body.offsetHeight)) / 2);
        if (vent != null && !vent.closed) vent.moveTo(x, y);
    } catch (ex) { }
}
function windowSinPadre() {
    if (window.top != window) window.top.location.href = window.location.href;
}
function getForm() {
    return document.forms[0];
}
function stopEvent(e) {
    if (!e) e = window.event;
    if (e.stopPropagation) e.stopPropagation();
    else e.cancelBubble = true;
}
function cancelEvent(e) {
    if (!e) e = window.event;
    if (e.preventDefault) e.preventDefault();
    else e.returnValue = false;
}
function disableFormAndControls() {
    var frm = getForm();
    frm.disabled = true;
    for (i = 0; i < frm.elements.length; i++) frm.elements[i].disabled = true;
}

//Inicio Revista xarxa
function desactivaControles(dom) {
    if (dom.nodeName == 'INPUT' || dom.nodeName == 'A') dom.disabled = true;
    if (dom.nodeName != '#text') dom.onmouseover = dom.onclick = dom.onmouseout = null;
    for (var i = 0; i < dom.childNodes.length; i++) desactivaControles(dom.childNodes.item(i));
}

function desactivaEventos(control) {
    var item = document.getElementById(control);
    if (item == null) return;
    desactivaControles(item);
}
//Fin Revista xarxa

function disableKeyENTER() {
    var codigo, targ;
    if (!e) var e = window.event;
    if (e.keyCode) codigo = e.keyCode;
    else if (e.which) codigo = e.which;
    else return;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    else return;
    if (targ.nodeType == 3) targ = targ.parentNode;
    if (codigo == 13 && targ.nodeName == 'INPUT') {
        if (e.keyCode) e.keyCode = 0;
        else if (e.which) e.which = 0;
    }
}

/*Ratón*/
/*Posición exacta de un objeto*/
function getTagPixels(StartTag, Direction) {
    var PixelAmt = (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
    while ((StartTag.tagName != 'BODY') && (StartTag.tagName != 'HTML')) {
        StartTag = StartTag.offsetParent;
        PixelAmt += (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
    }
    return PixelAmt;
}
String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, '') }

