var dialogHandler = {

    countall     : false,
    isie6        : (navigator.appVersion.search(/MSIE 6.0/) != -1),
    shift_x      : null, shift_y      : null,
    popupWidth   : null, popupHeight  : null,
    windowWidth  : null, windowHeight : null,
    popupItem    : null, Overlay      : null,
    hasElement   : null, hasInner     : null,
    UglyElements : null, nextFunc     : null,
    defaultWidth : 974,

    initialize: function()
    {
        bod             = document.getElementsByTagName('body')[0];
        overlay         = document.createElement('div');
        overlay.id      = 'overlay';
        lb              = document.createElement('div');
        lb.id           = 'Dialog';
        lb.innerHTML    = '<div class="popup-container">' +
            '<div id="dialog-content"></div>' +
            '</div>';

        bod.appendChild(overlay);
        bod.appendChild(lb);

		$('overlay').addClassName('opacity');

        dialogHandler.popupItem = $('Dialog');
        dialogHandler.Overlay   = $('overlay');

        dialogHandler.hasElement   = document.documentElement && document.documentElement.clientWidth;
        dialogHandler.hasInner     = typeof(window.innerWidth) == 'number';

        dialogHandler.windowHeight = dialogHandler.getPS().windowHeight;
        dialogHandler.windowWidth  = dialogHandler.getPS().windowWidth;

        Event.observe(window, 'resize', function(){

            dialogHandler.windowHeight = dialogHandler.getPS().windowHeight;
            dialogHandler.windowWidth  = dialogHandler.getPS().windowWidth;

            dialogHandler.Overlay.setStyle({
                height: dialogHandler.getPS().pageHeight +'px',
                width: dialogHandler.getPS().pageWidth +'px'
            });

        }, false);
    },

    popup : function (popupOptions)
    {
        var html        = (popupOptions.Body)  ? popupOptions.Body  : '&nbsp;';
        var dialogWidth = (popupOptions.Width) ? popupOptions.Width : dialogHandler.defaultWidth;
        var nonClosable = (popupOptions.nonClosable) ? 'none' : 'block';

        dialogHandler.disableEscapeKey( (popupOptions.disableEscapeKey) ? true : false );

        dialogHandler.showDialogBox(html, dialogWidth);
    },

    alertClose : function() {

        dialogHandler.deactivate();

        if (dialogHandler.nextFunc != null)
        {
            dialogHandler.nextFunc.apply();
        }

		dialogHandler.nextFunc = null;
    },

    dialogClose : function(answer) {

        dialogHandler.deactivate();

        if (dialogHandler.nextFunc != null)
        {
            dialogHandler.nextFunc.apply(dialogHandler.nextFunc, [answer]);
        }
    },

    activate: function()
    {
        dialogHandler.displayBox("block");
    },

    getKey : function (e)
    {
        var myEvent = (e == null) ? event : e;
        var keyCode = myEvent.keyCode | null;
        
        if (keyCode == 27)
        {
             dialogHandler.deactivate();
        }
    },

    displayBox: function(display)
    {
        dialogHandler.Overlay.style.display = display;
        dialogHandler.popupItem.style.display = display;
    },

    showDialogBox: function(message, width)
    {
        $('dialog-content').innerHTML = message;

        dialogHandler.popupHeight  = $('Dialog').getDimensions().height;
        dialogHandler.popupWidth   = $('Dialog').getDimensions().width;

        dialogHandler.windowHeight = dialogHandler.getPS().windowHeight;
        dialogHandler.windowWidth  = dialogHandler.getPS().windowWidth;

		dialogHandler.setOverlay();

        dialogHandler.popupItem.setStyle({
            width   : width+'px',
            display : 'block'
        });

        dialogHandler.setPT();
        dialogHandler.hideUglyElements();


        new PeriodicalExecuter(function(pe) {

            if (dialogHandler.popupItem.style.display != 'block')
            {
                pe.stop();
            }
            else
            {
                dialogHandler.setPT();
            }
        }, 2);
    },

	setOverlay : function ()
	{
        dialogHandler.Overlay.setStyle({
            display : 'block',
            height  : dialogHandler.getPS().pageHeight +'px',
            width   : dialogHandler.getPS().pageWidth +'px',
            backgroundColor : '#000000'
        });
	},

    hideUglyElements : function()
    {
        dialogHandler.UglyElements = new Array();

        var flashez  = document.getElementsByTagName('iframe');

        for (var i=0;i<flashez.length;i++)
        {
            if (dialogHandler.checkUglyElement(flashez[i]) )
            {
                dialogHandler.UglyElements[ dialogHandler.UglyElements.length ] = flashez[i];
                flashez[i].style.visibility = 'hidden';
            }
        }

        if ( dialogHandler.isie6 )
        {
            var selectez = document.getElementsByTagName('select');

            for (var i=0;i<selectez.length;i++)
            {
                if (dialogHandler.checkUglyElement(selectez[i]) )
                {
                    dialogHandler.UglyElements[ dialogHandler.UglyElements.length ] = selectez[i];
                    selectez[i].style.visibility = 'hidden';
                }
            }
        }
    },

    showUglyElements : function()
    {
        if (dialogHandler.UglyElements == null)
        {
            return false;
        }

        for (var i=0;i<dialogHandler.UglyElements.length;i++)
        {
            dialogHandler.UglyElements[i].style.visibility = 'visible';
        }
    },

    checkUglyElement : function(elem)
    {
        var parentz = $(elem).ancestors();
        var isouter = true;

        parentz.each(function(item) {

            if( $(item) == $('Dialog'))
            {
                isouter = false;
            }
        });

        return isouter;
    },

    deactivate: function()
    {
        if ( !$('dialog-content') )
        {
            return false;
        }

        $('dialog-content').innerHTML = '';

        dialogHandler.displayBox("none");
        dialogHandler.showUglyElements();
    },

    disableEscapeKey : function(to)
    {
        if (to)
        {
            Event.stopObserving(document, 'keypress', dialogHandler.getKey);
        }
        else
        {
            Event.observe(document, 'keypress', dialogHandler.getKey, false);
        }
    },

    setPT : function()
    {
        PS   = dialogHandler.getPS();
        POPS = $('Dialog').getDimensions();

        if ( dialogHandler.isie6 )
        {
            dialogHandler.shift_x = dialogHandler.hasInner ? pageXOffset : dialogHandler.hasElement ? document.documentElement.scrollLeft : document.body.scrollLeft;
            dialogHandler.shift_x += ( (PS.windowWidth - POPS.width) / 2);
        }
        else
        {
            dialogHandler.shift_x = ( (PS.windowWidth - POPS.width) / 2);
        }

        dialogHandler.popupItem.setStyle({
            left : dialogHandler.shift_x + 'px',
            top  : '20px'
        });
    },

    getPS : function(){
				
        var xScroll, yScroll;
        
        if (window.innerHeight && window.scrollMaxY) 
        {	
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
        } 
        else if (document.body.scrollHeight > document.body.offsetHeight)
        {
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        }
        else
        {
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
        
        var windowWidth, windowHeight;

        if (self.innerHeight)
        {
            if(document.documentElement.clientWidth)
            {
                windowWidth = document.documentElement.clientWidth; 
            }
            else
            {
                windowWidth = self.innerWidth;
            }
            
            windowHeight = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight) 
        {
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        }
        else if (document.body) 
        {
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }	
        
        if (yScroll < windowHeight)
        {
            pageHeight = windowHeight;
        }
        else
        { 
            pageHeight = yScroll;
        }

        if(xScroll < windowWidth)
        {	
            pageWidth = xScroll;		
        }
        else
        {
            pageWidth = windowWidth;
        }

        return {
            'windowHeight' : windowHeight,
            'pageWidth'    : pageWidth,
            'pageHeight'   : pageHeight,
            'windowWidth'  : windowWidth
        };
    }
};

Event.observe(window, 'load', function () {

    dialogHandler.initialize();

});
