﻿function Dialog(_window, num) 
{
    // init
    this.LoadingImageURI = ImagesPath + 'loading.gif',
    this.params = null;
    this.Position = new Position(_window, 'divDlg' + num),
    // const
    this.headerHeight = 25;
    this.margin = 0;

    // properties
    this.GetDivDlgOverlay = function() {
    return _window.document.getElementById('divDlgOverlay' + num);
    };
    this.GetDivDlg = function() {
    return _window.document.getElementById('divDlg' + num);
    };
    this.GetSpanDlgTitle = function() {
    return _window.document.getElementById('spanDlgTitle' + num);
    };
    this.GetTdDlgContent = function() {
    return _window.document.getElementById('tdDlgContent' + num);
    };
    this.GetIFrameDlg = function() {
    return _window.document.getElementById('iframeDlgContent' + num);
    };
    this.GetDivDlgLoading = function() {
    return _window.document.getElementById('divDlgLoading' + num);
    };

    // methods
    this.SetDisplay = function(display) {
        this.GetDivDlgOverlay().style.display = display;
        this.GetDivDlg().style.display = display;

        var pageDim = getPageDimensions();
        this.GetDivDlgOverlay().style.left = '0px';
        this.GetDivDlgOverlay().style.top = '0px';
        this.GetDivDlgOverlay().style.width = pageDim[0] + 'px';
        this.GetDivDlgOverlay().style.height = pageDim[1] + 'px';
    };

    this.Resize = function() {
        if (!this.params) return;
        if (this.GetIFrameDlg()) {
            this.GetIFrameDlg().style.left = this.margin + 'px';
            this.GetIFrameDlg().style.top = this.headerHeight + 'px';
            this.GetIFrameDlg().style.width = this.params.width + 'px';
            this.GetIFrameDlg().style.height = this.params.height + 'px';
        }
        this.GetDivDlg().style.width = (this.params.width + (this.margin * 2)) + 'px';
        this.GetDivDlg().style.height = (this.params.height + this.headerHeight) + 'px';
        this.Position.Dialog();
    };

    this.Show = function(params) {
        if (params) {
            this.SetDisplay('block');
            this.Update(params);
        }
    };

    this.Hide = function() {
        this.params = null;
        this.SetDisplay('none');
        this.GetIFrameDlg().src = '';
    };

    this.HideContent = function() {
        if (this.GetIFrameDlg()) {
            this.GetIFrameDlg().style.visibility = 'hidden';
            this.Loading(true);
        }
    };

    this.ShowContent = function() {
        if (this.GetIFrameDlg()) {
            this.Loading(false);
            this.GetIFrameDlg().style.visibility = 'visible';
        }
    };

    this.Update = function(params) {
        if (params == null) return;
        this.params = params;

        this.GetSpanDlgTitle().innerHTML = params.title;

        this.Resize();
        if (params.path) {
            if (this.GetIFrameDlg()) {
                this.GetIFrameDlg().style.top = '10000px';

            }
            else {
                var iframe = document.createElement('iframe');
                iframe.style.zIndex = '1';
                iframe.style.position = 'absolute';
                iframe.setAttribute('id', 'iframeDlgContent' + num);
                iframe.style.border = 'none';
                //iframe.setAttribute('frameborder', '0px');
                iframe.frameBorder = '0px';
                iframe.setAttribute('border', '0px');
                //iframe.setAttribute('cellspacing', '0px');
                iframe.setAttribute('cellSpacing', '0px');
                iframe.setAttribute('cellPadding', '0px');
                iframe.setAttribute('scrolling', 'no');
                iframe.setAttribute('marginwidth', '0px');
                iframe.setAttribute('marginheight', '0px');
                iframe.setAttribute('noresize', 'noresize');
                iframe.style.top = '10000px';
                this.GetTdDlgContent().appendChild(iframe);
            }

            this.Loading(true);
            this.GetIFrameDlg().style.visibility = 'visible';

            //this.GetIFrameDlg().src = SitePath + 'DialogContent.aspx?path=' + params.path + '&' + params.params;
            this.GetIFrameDlg().src = params.path;

        }
    };

    this.Loading = function(process) {
        if (this.GetDivDlgLoading())
            this.GetTdDlgContent().removeChild(this.GetDivDlgLoading());
        if (process) {
            var img = document.createElement('img');
            img.src = this.LoadingImageURI;

            var div = document.createElement('div');
            div.setAttribute('id', 'divDlgLoading' + num);
            div.style.zIndex = '0';
            div.style.position = 'absolute';
            div.style.textAlign = 'center';
            div.style.verticalAlign = 'middle';
            div.style.width = this.params.width + 'px';
            div.style.top = ((this.params.height / 2) + (this.headerHeight / 2)) + 'px';
            div.appendChild(img);

            this.GetTdDlgContent().style.height = this.params.height + 'px';
            this.GetTdDlgContent().appendChild(div);
        }
        else {
            this.Resize();
        }
    };

    this.LoadingComplete = function() {
        this.Loading(false);
    };
}

