﻿var CurrentApplication;
var arrStack = new Array();
function DialogForm_ShowFrame(headerText, view, qs, event, w, h) {
    //debugger;
    w = w ? w : DialogForm_GetWidth(view);
    h = h ? h : DialogForm_GetHeight(view);
    var _window = this;
    var _src = 'PopUpPage.aspx?view=' + view + '&CurrentApplication=' + CurrentApplication;
    if (!isNullOrEmpty(qs))
        _src += '&' + qs;
    while (_window.parent != _window) {
        _window = _window.parent
    }
    var arr = new Array(3);
    arr[0] = event;
    _window.arrStack[_window.arrStack.length] = arr;

    var frameId = "modalDialog_" + _window.arrStack.length;
    var _frame = $('<iframe id="' + frameId + '" style="overflow: hidden" width="' + w + '" height="' + h + '" scrolling="no" frameborder="0" src="' + _src + '" />');
    //arr[1] = _window.$('<iframe id="' + frameId + '" class="externalSite" scrolling="no" frameborder="0" src="' + _src + '" />').dialog({
    _frame.attr('scrolling', 'no');
    arr[1] = _window.$('<div />').dialog({
        title: headerText,
        autoOpen: true,
        width: w,
        height: h,
        modal: true,
        resizable: false,
        autoResize: false
    }).width(w).height(h). //;
    //append('<iframe id="' + frameId + '" class="externalSite" width="' + w + '" height="' + h + '" scrolling="auto" frameborder="0" src="' + _src + '" />');
    append(_frame);

    ////////////////////////////////////////////////////////////
    /*
    iframe.load(function(){ 07.    
        // The Iframe's child page BODY element. 08.    
        var iframe_content = iframe.contents().find('body'); 09.     
         10.    
        // Bind the resize event. When the iframe's size changes, update its height as 11.    
        // well as the corresponding info div. 12.    
        iframe_content.resize(function(){ 13.      
        var elem = $(this); 14.       
         15.      
        // Resize the IFrame. 16.      
        iframe.css({ height: elem.outerHeight( true ) }); 17.       
         18.      
        // Update the info div width and height. 19.      
        $('#iframe-info').text( 'IFRAME width: ' + elem.width() + ', height: ' + elem.height() ); 20.    
        });
    */
    ////////////////////////////////////////////////////////////
    arr[1].dialog().parent().find('.ui-dialog-titlebar-close').click(function() {
        _window.RemoveLastWindow();
    });
    
    
    arr[2] = frameId;
}

function RemoveLastWindow() {
    //debugger;
    var __lastWindow = arrStack[arrStack.length - 1][1];
    __lastWindow.children('#divIFrame').unbind('resize'); 
    __lastWindow[0].removeChild(__lastWindow[0].lastChild);
    arrStack.pop();
    //_lastWindow.remove();
   
    __lastWindow.dialog('destroy').remove(); // error: здесь происходит постбек в FF
        
}
function DialogForm_SizeFrame(_width, _height) {
    //debugger;
    if (arrStack && arrStack.length > 0) {
        var _lastWindow = arrStack[arrStack.length - 1][1];
        _height = _height ? _height : _lastWindow.dialog("option", "height");
        _width = _width ? _width : _lastWindow.dialog("option", "width");
        _lastWindow.dialog("option", "height", (_height + 43).toString());
        _lastWindow.dialog("option", "width", _width.toString());
        _lastWindow.children('iframe').width(_width).height(_height);
        _lastWindow.dialog("option", "position", "center");

        //_lastWindow.children('iframe').width(_width).height(_height);
        //_lastWindow.dialog("option", "width", _width).width(_width);
        //_lastWindow.height(_height);

        /*if (_width != null) {
        //
        _lastWindow.width(_width);
        
        } else {
        _lastWindow.dialog("option", "width", _lastWindow.dialog("option", "width"));   
        
        }*/
    }
}

function HideDialogFrame() {
    //debugger;
    var _lastWindow = arrStack[arrStack.length - 1][1];
    _lastWindow.children('#divIFrame').unbind('resize'); 
    var _id = _lastWindow[0].id;
    arrStack[arrStack.length - 1][1].dialog("close");
    arrStack.pop();
    //
    //_lastWindow.remove();
     //.remove();
    _lastWindow[0].removeChild(_lastWindow[0].lastChild);
    _lastWindow.dialog('destroy');
}
function CloseDialogFrame(_val) {
    //debugger;
    var _lastWindow = arrStack[arrStack.length - 1][1];
    _lastWindow.children('#divIFrame').unbind('resize'); 
    if (arrStack.length > 1) {
        arrStack[arrStack.length - 1][1].dialog("close");
        var _fun = arrStack[arrStack.length - 1][0];
        arrStack.pop();
        if (window.execScript) {
            execScript('document.getElementById(arrStack[arrStack.length - 1][2]).contentWindow.' + _fun + '(\'' + _val + '\');');
        } else {
            eval('document.getElementById(arrStack[arrStack.length - 1][2]).contentWindow.' + _fun + '(\'' + _val + '\');');
        }
       
    } else {
        arrStack[arrStack.length - 1][1].dialog("close");
        if (window.execScript) {
            execScript(arrStack[arrStack.length - 1][0] + '(\'' + _val + '\');');
        } else {
            eval(arrStack[arrStack.length - 1][0] + '(\'' + _val + '\');');
        }
        arrStack.pop();
    }
    //

    //_lastWindow.remove();
    
    //.remove();
    //_lastWindow[0].src = '';
    _lastWindow[0].removeChild(_lastWindow[0].lastChild);
    _lastWindow.dialog('destroy');
}
