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: 400,
    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-header">'+
		'<div id="dialog-title"><span class="title">&nbsp;</span><span class="subtitle">&nbsp;</span></div>'+
		'<a href="javascript:void(0);" onclick="dialogHandler.deactivate();" id="dialog-close">×</a>'+
		'</div>' +
        '<div id="dialog-content">&nbsp;</div>' +
        '<div id="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'
            });
        });
    },
	
	wait : function (){

        var title        = 'Folyamatban....';
        var html         = '<img src="/layout/gfx/loading.gif" alt="" width="32" height="32" />';
        var dialogWidth  = '400';
        var nonClosable  = 'none';
		
		dialogHandler.OverlayItem = $('#overlay');

        $('#dialog-close').css({
          display: nonClosable
        });
		
		$(dialogHandler.OverlayItem).click( function () {
			dialogHandler.displayBox("block");
		});

		$('#dialog-title > span.subtitle').html(title);

        dialogHandler.showDialogBox(html, dialogWidth);
    },
	
    addWait : function ()
    {
        $('#dialog-close').src = '/layout/gfx/loading.gif';
    },

    removeWait : function ()
    {
        $('#dialog-close').src = '/layout/gfx/close_button.gif';
    },
    
    popup: function(popupOptions)
    {
        var title = (popupOptions.Title) ? popupOptions.Title : '&nbsp;';
		var subtitle = (popupOptions.subTitle) ? popupOptions.subTitle : '&nbsp;';
        var html = (popupOptions.Body) ? popupOptions.Body : '&nbsp;';
		var nonClosable = (popupOptions.Closable) ? popupOptions.Closable : 'block';
        var dialogWidth = (popupOptions.Width) ? (popupOptions.Width) : dialogHandler.defaultDialogWidth;
        
        $('#dialog-title > span.title').html(title);
		$('#dialog-title > span.subtitle').html(subtitle);
		
		$('#dialog-close').css({
          display: nonClosable
        });
        
        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 subtitle = (alertOptions.subTitle) ? alertOptions.subTitle : '&nbsp;';
        var html = (alertOptions.Body) ? alertOptions.Body : '&nbsp;';
        var dialogWidth = (alertOptions.Width) ? (alertOptions.Width) : dialogHandler.defaultDialogWidth;
        
        if (alertOptions.Func) 
        {
            dialogHandler.nextFunc = alertOptions.Func;
        }
		
		$('#dialog-close').css({
          display: 'none'
        });
        
        $('#dialog-title > span.title').html(title);
		$('#dialog-title > span.subtitle').html(subtitle);
		
		html += '<div class="buttons"><input type="button" value="RENDBEN" onclick="dialogHandler.alertClose();" class="dialog-button" style="margin-bottom: 6px;"/></div>';
        
        dialogHandler.showDialogBox(html, dialogWidth);
    },
    
    alertClose: function()
    {
        dialogHandler.deactivate();
        
        if (dialogHandler.nextFunc != null) 
        {
            dialogHandler.nextFunc.apply();
        }
    },

    dialog: function(alertOptions)
    {
        var title = (alertOptions.Title) ? alertOptions.Title : '&nbsp;';
		var subtitle = (alertOptions.subTitle) ? alertOptions.subTitle : '&nbsp;';
        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;
		
		dialogHandler.nextFunc = (alertOptions.Func) ? alertOptions.Func : null;
		
		var buttonLine = '';
		$.each(buttons, function(keys, vals) { 
			buttonLine += '<input type="button" value="'+vals+'" onclick="dialogHandler.dialogClose(\''+keys+'\');" class="dialog-button" />';
		});

        html += '<div class="buttons">'+buttonLine+'</div>';
        $('#dialog-title > span.title').html(title);
		$('#dialog-title > span.subtitle').html(subtitle);
		
		$('#dialog-close').css({
          display: 'none'
        });
        
        dialogHandler.showDialogBox(html, dialogWidth);
    },

    dialogClose : function(answer) {

        dialogHandler.deactivate();

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

	
    
    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: '.3',
            filter: 'alpha(opacity=30)'
        });
        
        $(dialogHandler.FloatingItem).css({
            width: width + 'px',
            display: 'block'
        });
        
        dialogHandler.computePositon();
    },
    
    deactivate: function()
    {
        dialogHandler.displayBox('none');
		clearTimeout(tm);
    },
	
    disableEscapeKey : function(to)
    {
        if (to){
            $(document).keypress(function(e){dialogHandler.getKey(e)});
        }
    },
	
    getKey : function (e)
    {
        var myEvent = (e == null) ? event : e;
        var keyCode = myEvent.keyCode | null;
        alert(keyCode);
        if (keyCode == 27)
            dialogHandler.deactivate();
    },


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

