var PopupHandler = {

    shift_x      : null, shift_y      : null,
    popupWidth   : null, popupHeight  : null,
    windowWidth  : null, windowHeight : null,
    itemWidth    : null, itemHeight   : null,

    FloatingItem : null, OverlayItem  : null,
    hasElement   : null, hasInner     : null,

    UglyElements : new Array(),

    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></td>' +
					                '<td id="dialog-middle-center-dummy"></td>' +
					                '<td></td>' +
					            '</tr>' +
					        '</table>' +
					        '</div>' + 
					        '<div id="dialog-middle-foreground">' +
					        '<table cellspacing="0" cellpadding="0" border="0" class="dialogInner" id="dialogInner" width="100%">' +
					            '<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"><a href="javascript:void(0)" class="bezar" onclick="PopupHandler.deactivate();">[X]</td>' +
					                        '</tr>' +
					                        '<tr>' +
					                            '<td id="dialog-middle-value"></td>' + 
					                        '</tr>' +
					                        '<tr>' +
					                    '</table></td>' + 
					                '<td class="left-border"></td>' +
					            '</tr>' +
					        '</table></div>';

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

        PopupHandler.FloatingItem = $('Dialog');
        PopupHandler.OverlayItem  = $('overlay');
		
        PopupHandler.hasElement   = document.documentElement && document.documentElement.clientWidth;
        PopupHandler.hasInner     = typeof(window.innerWidth) == 'number';

        PopupHandler.windowHeight = PopupHandler.getPageSize().windowHeight;
        PopupHandler.windowWidth  = PopupHandler.getPageSize().windowWidth;

    },

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

    displayBox: function(display)
    {
        PopupHandler.OverlayItem.style.display = display;
        PopupHandler.FloatingItem.style.display = display;

    },

    showDialogBox: function(message, width, height)
    {
        $('overlay').observe('click', function(){
			PopupHandler.deactivate();
        }, false);
    	
        $('dialog-middle-value').innerHTML = message;
        PopupHandler.popupHeight  = $('Dialog').getDimensions().height;
        PopupHandler.popupWidth   = $('Dialog').getDimensions().width;
        
        PopupHandler.windowHeight = PopupHandler.getPageSize().windowHeight;
        PopupHandler.windowWidth  = PopupHandler.getPageSize().windowWidth;
        PopupHandler.OverlayItem.setStyle({
            display : 'block',
            height  : PopupHandler.getPageSize().pageHeight +'px',
            width   : PopupHandler.getPageSize().pageWidth +'px',
            backgroundColor : '#000000',
            opacity : '.5',
            filter  : 'alpha(opacity=50)'
        });

        PopupHandler.FloatingItem.setStyle({
            width   : width+'px',
            height  : height+'px',
            display : 'block'
        });

        /*
        var dims = $('dialogInner').getDimensions();		

        $('dialog-middle-center-dummy').setStyle({
            width  : dims.width+'px',
            height : dims.height+'px'
        });
        */

        /*
        $('dialogInner').setStyle({
            width  : (width-22)+'px'
        });
        */
			
        PopupHandler.hideUglyElements();
        PopupHandler.computePositon();
    },

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

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

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

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

    showUglyElements : function()
    {
        for (var i=0;i<PopupHandler.UglyElements.length;i++)
        {
            PopupHandler.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 = '';
        PopupHandler.displayBox("none");
        PopupHandler.showUglyElements();
    },

	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
		};
	},
    
    computePositon : function()
    {
        PS   = PopupHandler.getPageSize();
        Pwidth = $('dialog-middle-value').offsetWidth;
        Pheight = ($('dialog-middle-value').offsetHeight)+35;
        /*
        if ((typeof $('dialog-middle-value').offsetHeight == 'number') && ($('dialog-middle-value').offsetHeight > 1)) {
        	Pheight = $('dialog-middle-value').offsetHeight;
        }
        */

        if ( navigator.appVersion.search(/MSIE 6.0/) != -1 )
        {	
            PopupHandler.shift_x = PopupHandler.hasInner ? pageXOffset : PopupHandler.hasElement ? document.documentElement.scrollLeft : document.body.scrollLeft;
            PopupHandler.shift_x += ( (PS.windowWidth - Pwidth) / 2);

            PopupHandler.shift_y = PopupHandler.hasInner ? pageYOffset : PopupHandler.hasElement ? document.documentElement.scrollTop : document.body.scrollTop;
            PopupHandler.shift_y += ( (PS.windowHeight - Pheight) / 2);
        }
        else
        {
            PopupHandler.shift_x = ( (PS.windowWidth - Pwidth) / 2);
            PopupHandler.shift_y = ( (PS.windowHeight - Pheight) / 2);
        }

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

        setTimeout('PopupHandler.computePositon()', 2);
    }
};

Event.observe(window, 'load', PopupHandler.initialize);