﻿
/* PopupManager */

function PopupManager(popupControl) {
    if (popupControl == null || popupControl == undefined)
        throw 'Argumento [popupControl] inválido.';
    this.popupControl = popupControl;
    this.listaGridRefresh = new Array();
    this.windowUsadas = 0;
    this.popupControl.CloseUp.AddHandler(Function.createDelegate(this, this.onCloseUp));
}

PopupManager.prototype.openPopup = function(url, width, height, gridRefresh, returnFunction) {
    if (this.windowUsadas == this.popupControl.GetWindowCount())
        throw 'Não há mais popups disponíveis.';

    this.windowUsadas++;

    var wnd = this.popupControl.GetWindow(this.windowUsadas - 1);
    wnd.SetHeaderText('');
    this.popupControl.SetWindowSize(wnd, width, height);
    this.popupControl.SetWindowContentUrl(wnd, 'javascript:void(0)');
    this.popupControl.SetWindowContentUrl(wnd, url);
    this.popupControl.GetWindowContentIFrame(wnd).popupWindow = wnd;
    this.popupControl.GetWindowContentIFrame(wnd).returnFunction = returnFunction;
    this.popupControl.ShowWindow(wnd);
    if (gridRefresh == undefined)
        this.listaGridRefresh[wnd.index] = null;
    else
        this.listaGridRefresh[wnd.index] = gridRefresh;

    return wnd;
}

//PopupManager.instance = null;

PopupManager.getInstance = function() {
    if (window.top.__popupManager == null)
        throw "PopupManager não inicializado.";
    else
        return window.top.__popupManager;
}

PopupManager.inicializar = function(popupControl) {
    if (window.top.__popupManager == null)
        window.top.__popupManager = new PopupManager(popupControl);
    else
        throw "PopupManager já inicializado.";
}

PopupManager.prototype.onCloseUp = function(s, e) {
    this.windowUsadas--;
    this.popupControl.SetWindowContentUrl(e.window, 'javascript:void(0)');
    var grid = this.listaGridRefresh[e.window.index];
    if (grid != null) {
        this.listaGridRefresh[e.window.index] = null;
        grid.Consultar();
    }
}

PopupManager.prototype.getCurrentWindow = function() {
    if (this.windowUsadas > 0)
        return this.popupControl.GetWindow(this.windowUsadas - 1);
    else
        return null;
}

PopupManager.prototype.setTituloWindow = function(titulo) {
    var wnd = this.getCurrentWindow();
    if (wnd != null) {
        wnd.SetHeaderText(titulo);
    }
}

PopupManager.prototype.closePopup = function() {
    var wnd = this.getCurrentWindow();
    if (wnd != null) {
        this.popupControl.HideWindow(wnd);
    }
}

PopupManager.prototype.openPopupSelecao = function(url, width, height, returnFunction) {
    this.openPopup(url, width, height, null, returnFunction);
}

/*
function returnFunction(obj) {
alert('ID_PESSOA_OPONENTE = ' + obj.ID_PESSOA_OPONENTE);
alert('CODIGO = ' + obj.CODIGO);
alert('ABREV = ' + obj.ABREV);
alert('NOME = ' + obj.NOME);
alert('MATRICULA = ' + obj.MATRICULA);
alert('CPF = ' + obj.CPF);
alert('CNPJ = ' + obj.CNPJ);
}
*/

