var dialogHandler = {

    shift_x      : null, shift_y      : null,
    popupWidth   : null, popupHeight  : null,
    windowWidth  : null, windowHeight : null,

    FloatingItem : null, OverlayItem  : null,
    hasElement   : null, hasInner     : null,

    defaultDialogWidth : 400,
    UglyElements : new Array(),

    nextFunc : null,

    initialize: function()
    {
        bod             = document.getElementsByTagName('body')[0];
        overlay         = document.createElement('div');
        overlay.id      = 'overlay';
        lb              = document.createElement('div');
        lb.id           = 'Dialog';
        lb.innerHTML    = '<div id="dialog-middle-background"><table cellspacing="0" cellpadding="0" border="0" class="dialogOuter" id="dialogOuter" width="100%">' +
            '<tr>' +
                '<td width="11" height="12" valign="top"><img src="/layout/gfx/dialog/top_left.gif" alt="" width="11" height="12"></td>' +
                '<td class="top-border"></td>' + 
                '<td width="11" height="12" valign="top"><img src="/layout/gfx/dialog/top_right.gif" alt="" width="11" height="12"></td>' +
            '</tr>' + 
            '<tr>' +
                '<td class="right-border"></td>' +
                '<td class="center-background" id="dialog-middle-center-dummy"></td>' +
                '<td class="left-border"></td>' +
            '</tr>' +
            '<tr>' +
                '<td width="11" height="12"><img src="/layout/gfx/dialog/bottom_left.gif" alt="" width="11" height="12"></td>' +
                '<td class="bottom-border"></td>' +
                '<td width="11" height="12"><img src="/layout/gfx/dialog/bottom_right.gif" alt="" width="11" height="12"></td>' +
            '</tr>' +
        '</table></div>' + 
        '<div id="dialog-middle-foreground">' +
        '<table cellspacing="0" cellpadding="0" border="0" class="dialogInner" id="dialogInner" width="100%">' +
            '<tr>' +
                '<td width="8" height="8"><img src="/layout/gfx/dialog/inner_top_left.gif" alt="" width="8" height="8"></td>' +
                '<td class="top-border"></td>' +
                '<td width="8" height="8"><img src="/layout/gfx/dialog/inner_top_right.gif" alt="" width="8" height="8"></td>' +
            '</tr>' +
            '<tr>' +
                '<td class="right-border"></td>' +
                '<td class="center-background" id="dialog-middle-center">'+
                    '<table cellspacing="0" cellpadding="0" border="0" width="100%">' +
                        '<tr>' +
                            '<td align="right"><span id="dialog-top-center"></span><img src="/layout/gfx/dialog/close_button.gif" alt="" width="28" height="28" border="0" onclick="dialogHandler.deactivate();" id="dialog-close-button" /></td>' +
                        '</tr>' +
                        '<tr>' +
                            '<td id="dialog-middle-value"></td>' + 
                        '</tr>' +
                        '<tr>' +
                    '</table>' + 
                '<td class="left-border"></td>' +
            '</tr>' +
            '<tr>' +
                '<td width="8" height="8" valign="bottom"><img src="/layout/gfx/dialog/inner_bottom_left.gif" alt="" width="8" height="8"></td>' +
                '<td class="bottom-border"></td>' +
                '<td width="8" height="8" valign="bottom"><img src="/layout/gfx/dialog/inner_bottom_right.gif" alt="" width="8" height="8"></td>' +
            '</tr>' +
        '</table>';

        bod.appendChild(overlay);
        bod.appendChild(lb);

        dialogHandler.FloatingItem = $('Dialog');
        dialogHandler.OverlayItem  = $('overlay');

        dialogHandler.hasElement   = document.documentElement && document.documentElement.clientWidth;
        dialogHandler.hasInner     = typeof(window.innerWidth) == 'number';

        dialogHandler.windowHeight = dialogHandler.getPageSize().windowHeight;
        dialogHandler.windowWidth  = dialogHandler.getPageSize().windowWidth;

        Event.observe(window, 'resize', function(){

            dialogHandler.windowHeight = dialogHandler.getPageSize().windowHeight;
            dialogHandler.windowWidth = dialogHandler.getPageSize().windowWidth;

            dialogHandler.OverlayItem.setStyle({
                height: dialogHandler.getPageSize().pageHeight +'px',
                width: dialogHandler.getPageSize().pageWidth +'px'
            });

        }, false);
    },

    wait : function (){

        var title        = 'Betöltés folyamatban';
        var html         = '<img src="/layout/gfx/loading.gif" alt="" width="32" height="32" />';
        var dialogWidth  = '400';
        var nonClosable  = 'none';

        dialogHandler.disableEscapeKey(true);

        $('dialog-close-button').setStyle({
          display: nonClosable
        });

        $('dialog-top-center').innerHTML = title;

        dialogHandler.showDialogBox(html, dialogWidth);
    },

    addWait : function ()
    {
        $('dialog-close-button').src = '/layout/gfx/loading.gif';
    },

    removeWait : function ()
    {
        $('dialog-close-button').src = '/layout/gfx/dialog/close_button.gif';
    },

    popup : function (popupOptions){

        var title        = (popupOptions.Title) ? popupOptions.Title : '&nbsp;';
        var html         = (popupOptions.Body) ? popupOptions.Body : '&nbsp;';
        var dialogWidth  = (popupOptions.Width) ? popupOptions.Width : dialogHandler.defaultDialogWidth;
        var nonClosable  = (popupOptions.nonClosable) ? 'none' : 'block';

        dialogHandler.disableEscapeKey( (popupOptions.disableEscapeKey) ? true : false );

        $('dialog-close-button').setStyle({
          display: nonClosable
        });

        $('dialog-top-center').innerHTML = title;

        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.defaultDialogWidth;
        var nonClosable = (alertOptions.nonClosable) ? 'none' : 'block';

        dialogHandler.disableEscapeKey( (alertOptions.disableEscapeKey) ? true : false );

        $('dialog-close-button').setStyle({
          display: nonClosable
        });

        if (alertOptions.Func)
        {
            dialogHandler.nextFunc = alertOptions.Func;
        }

        $('dialog-top-center').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();
        }
    },

    /*    	
     * dialogHandler.dialog({
     *    Title       : 'Ez itt a title',
     *    Body        : 'Kovács Tamás egy nagyon szép szál gyerek \'he :)',
     *    Buttons     : {yes : 'igen', no: 'Nem'},
     *    nonClosable : true,
     *    Func        : function(a){
     *        console.log(a);
     *    }
     *});
     */

    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.defaultDialogWidth;
        var nonClosable     = (alertOptions.nonClosable) ? 'none' : 'block';

        dialogHandler.nextFunc = (alertOptions.Func) ? alertOptions.Func : null;

        dialogHandler.disableEscapeKey( (alertOptions.disableEscapeKey) ? true : false );

        $('dialog-close-button').setStyle({
            display: nonClosable
        });

        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-top-center').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.OverlayItem.style.display = display;
        dialogHandler.FloatingItem.style.display = display;

    },

    showDialogBox: function(message, width)
    {
        $('dialogOuter').setStyle({
            opacity : '.4',
            filter  : 'alpha(opacity=40)'
        });

        $('dialog-middle-value').innerHTML = message;

        dialogHandler.popupHeight  = $('Dialog').getDimensions().height;
        dialogHandler.popupWidth   = $('Dialog').getDimensions().width;

        dialogHandler.windowHeight = dialogHandler.getPageSize().windowHeight;
        dialogHandler.windowWidth  = dialogHandler.getPageSize().windowWidth;

        dialogHandler.OverlayItem.setStyle({
            display : 'block',
            height  : dialogHandler.getPageSize().pageHeight +'px',
            width   : dialogHandler.getPageSize().pageWidth +'px',
            backgroundColor : '#000000',
            opacity : '.2',
            filter  : 'alpha(opacity=20)'

        });

        dialogHandler.computePositon();

        dialogHandler.FloatingItem.setStyle({
            width   : width+'px',
            display : 'block'
        });
	
        $('dialogInner').setStyle({
            width  : (width-22)+'px'
        });
        var dims = $('dialogInner').getDimensions();	

        $('dialog-middle-center-dummy').setStyle({
            width  : dims.width+'px',
            height : dims.height+'px'
        });

        dialogHandler.hideUglyElements();
        dialogHandler.computePositon();
    },

    hideUglyElements : function()
    {
        dialogHandler.UglyElements = new Array();

        var flashez  = document.getElementsByTagName('iframe');
        var selectez = document.getElementsByTagName('select');

        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';
            }
        }

        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()
    {
        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-middle-center') )
            return false;

        $('dialog-middle-value').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);
        }
    },

    computePositon : function()
    {
        PS   = dialogHandler.getPageSize();
        POPS = $('dialogOuter').getDimensions();

        if ( navigator.appVersion.search(/MSIE 6.0/) != -1 )
        {
            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.FloatingItem.setStyle({
            left : dialogHandler.shift_x + 'px',
            top  : dialogHandler.shift_y + 'px'
        });

        setTimeout('dialogHandler.computePositon()', 2);
    },

    getPageSize : 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){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
        
        var windowWidth, windowHeight;

        if (self.innerHeight) {	// all except Explorer
            if(document.documentElement.clientWidth){
                windowWidth = document.documentElement.clientWidth; 
            } else {
                windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;

        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }	
        
        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
            pageHeight = windowHeight;
        } else { 
            pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){	
            pageWidth = xScroll;		
        } else {
            pageWidth = windowWidth;
        }

        return {
            'windowHeight' : windowHeight,
            'pageWidth'    : pageWidth,
            'pageHeight'   : pageHeight,
            'windowWidth'  : windowWidth
        };
    }
};

Event.observe(window, 'load', dialogHandler.initialize);