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 : 819,

    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 class="popup-header">' +
                '<span id="dialog-title"></span>' +
                '<span id="dialog-url"></span>' +
                '<a id="dialog-button" href="javascript:void(0);" onclick="dialogHandler.deactivate();">&nbsp;</a>' +
            '</div>' +
            '<div class="popup-middle"><div id="dialog-content"></div></div>' +
            '<div class="popup-bottom" id="dialog-footer"></div>' +
            '</div>';

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

        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 url         = (popupOptions.Url)   ? popupOptions.Url   : '&nbsp;';
        var title       = (popupOptions.Title) ? popupOptions.Title : '&nbsp;';
        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 );

        $('dialog-title').innerHTML = title;
        $('dialog-url').innerHTML   = url;

        dialogHandler.showDialogBox(html, dialogWidth);
    },

    /*
     * Using
     * dialogHandler.alert({
     *	 Title : 'Title text',
     *	 Body  : 'Message body',
     *   Func  : Function()
     * });
     */

    alert : function(alertOptions) {
        
        var title       = (alertOptions.Title) ? alertOptions.Title   : 'Figyelmeztetés';
        var html        = (alertOptions.Body)  ? alertOptions.Body    : '&nbsp;';
        var dialogWidth = (alertOptions.Width) ? (alertOptions.Width) : dialogHandler.defaultWidth;
        var nonClosable = (alertOptions.nonClosable) ? 'none' : 'block';

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

        if (alertOptions.Func)
        {
            dialogHandler.nextFunc = alertOptions.Func;
        }

        $('dialog-title').innerHTML = title;
        var alertBody = '<div class="alert-body">'+html+'</div>' +
            '<div class="alert-buttons"><input type="button" value="Rendben" id="dialog-ok-button" onclick="dialogHandler.alertClose();" class="button100" /></div>';

        dialogHandler.showDialogBox(alertBody, dialogWidth);
    },

    alertClose : function() {

        dialogHandler.deactivate();

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

    dialog :function(alertOptions) {

        var title           = (alertOptions.Title)   ? alertOptions.Title   : 'Figyelmeztetés';
        var html            = (alertOptions.Body)    ? alertOptions.Body    : '&nbsp;';
        var buttons         = (alertOptions.Buttons) ? alertOptions.Buttons : {yes : '  Igen  ', no : '  Nem  '};
        var dialogWidth     = (alertOptions.Width)   ? (alertOptions.Width) : dialogHandler.defaultWidth;
        var nonClosable     = (alertOptions.nonClosable) ? 'none' : 'block';

        dialogHandler.nextFunc = (alertOptions.Func) ? alertOptions.Func : null;

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

        var buttonLine = '';
        var keys = Object.keys(buttons);
        var vals = Object.values(buttons);
    
        for (var i=0;i<keys.length;i++)
        {
            buttonLine += '<input type="button" value="'+vals[i]+'" onclick="dialogHandler.dialogClose(\''+keys[i]+'\');" class="button100" style="margin-right: 6px;" />';
        }

        $('dialog-title').innerHTML = title;
        var dialogBody = '<div class="alert-body">'+html+'</div>' +
            '<div class="alert-buttons">'+buttonLine+'</div>';

        dialogHandler.showDialogBox(dialogBody, dialogWidth);
    },

    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.Overlay.setStyle({
            display : 'block',
            height  : dialogHandler.getPS().pageHeight +'px',
            width   : dialogHandler.getPS().pageWidth +'px',
            backgroundColor : '#000000',
            opacity : '.4',
            filter  : 'alpha(opacity=40)'

        });

        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);
    },

    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);

            dialogHandler.shift_y = dialogHandler.hasInner ? pageYOffset : dialogHandler.hasElement ? document.documentElement.scrollTop : document.body.scrollTop;
            dialogHandler.shift_y += ( (PS.windowHeight - POPS.height) / 2);
        }
        else
        {
            dialogHandler.shift_x = ( (PS.windowWidth - POPS.width) / 2);
            dialogHandler.shift_y = ( (PS.windowHeight - POPS.height) / 2);
        }

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

    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();

});
