var tm = 0;
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: 510,
    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 class="dialog-close"><a href="javascript:void(0);" onclick="dialogHandler.deactivate();" class="textdel-button">X</a></div>'+
		'<div id="dialog-title"><span>&nbsp;</span><div></div><div id="popup-message"></div></div>' +
        '<div id="dialog-content">&nbsp;</div>' +
        '<div class="dialog-footer"></div>';
        
        bod.appendChild(overlay);
        bod.appendChild(lb);
        
        dialogHandler.FloatingItem = $('#Dialog');
        dialogHandler.OverlayItem = $('#overlay');
        
		$(dialogHandler.OverlayItem).click( function () {
		//	dialogHandler.deactivate();
		});
        dialogHandler.hasElement = document.documentElement && document.documentElement.clientWidth;
        dialogHandler.hasInner = typeof(window.innerWidth) == 'number';
        
        dialogHandler.windowHeight = dialogHandler.getPageSize().windowHeight;
        dialogHandler.windowWidth = dialogHandler.getPageSize().windowWidth;
        
        $(window).resize(function()
        {
        
            dialogHandler.windowHeight = dialogHandler.getPageSize().windowHeight;
            dialogHandler.windowWidth = dialogHandler.getPageSize().windowWidth;
            
            $(dialogHandler.OverlayItem).css({
                height: dialogHandler.getPageSize().pageHeight + 'px',
                width: dialogHandler.getPageSize().pageWidth + 'px'
            });
        });
    },
    
    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;
        
        $('#dialog-title > span').html(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;
        
        if (alertOptions.Func) 
        {
            dialogHandler.nextFunc = alertOptions.Func;
        }
        
        $('#dialog-title > span').html(title);
        $('#dialog-footer').html('<input type="button" value="Rendben" onclick="dialogHandler.alertClose();" />');
        
        dialogHandler.showDialogBox(html, dialogWidth);
    },
    
    alertClose: function()
    {
        dialogHandler.deactivate();
        
        if (dialogHandler.nextFunc != null) 
        {
            dialogHandler.nextFunc.apply();
        }
    },
    
    activate: function()
    {
        dialogHandler.displayBox("block");
    },
    
    showDialogBox: function(message, width)
    {
        $('#dialog-content').html(message);

		
        $(dialogHandler.OverlayItem).css({
            display: 'block',
            height: dialogHandler.getPageSize().pageHeight + 'px',
            width: dialogHandler.getPageSize().pageWidth + 'px',
            backgroundColor: '#000000',
            opacity: '.8',
            filter: 'alpha(opacity=80)'
        });
        
        $(dialogHandler.FloatingItem).css({
            width: width + 'px',
            display: 'block'
        });
        
        dialogHandler.computePositon();
    },
    
    deactivate: function()
    {
        dialogHandler.displayBox('none');
		clearTimeout(tm);
    },
    
    computePositon: function()
    {
        PS = dialogHandler.getPageSize();
        POPS = {
            width: $('#Dialog').outerWidth(),
            height: $('#Dialog').outerHeight()
        };
        
        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).css({
            left: dialogHandler.shift_x + 'px',
            top: dialogHandler.shift_y + 'px'
        });

		tm = 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
        };
    },
    
    displayBox: function(display)
    {
        $(dialogHandler.OverlayItem).css('display', display);
        $(dialogHandler.FloatingItem).css('display', display);
    }
};

$(document).ready(function()
{
    dialogHandler.initialize();
});
