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,

    initialize: function(type)
    {
        bod             = document.getElementsByTagName('body')[0];
        overlay         = document.createElement('div');
        overlay.id      = 'overlay';
        lb              = document.createElement('div');
        lb.id           = 'Dialog';
        
        if (type == 'home')
        {
            lb.innerHTML    = '<div class="popup-container1">' +
		    '<div class="popup-closebtn1"><a href="javascript:void(0);" onclick="dialogHandler.deactivate();" class="close1"></a></div>' + 
		    '<a href="http://www.profonte.hu/akcio" class="close2"></a>'+
            '</div>';
        }
        else
        {
            lb.innerHTML    = '<div class="popup-container">' +
		    '<div class="popup-closebtn">Enneagram tréning vélemények<a href="javascript:void(0);" onclick="dialogHandler.deactivate();" class="close">X Bezár</a></div>' + 
							'<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/MUNo18m16nY&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/MUNo18m16nY&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>'+
            '</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);
    },

    displayBox: function(display)
    {
        dialogHandler.Overlay.style.display = display;
        dialogHandler.popupItem.style.display = display;
    },

    showDialogBox: function()
    {
        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 : '.7',
            filter  : 'alpha(opacity=60)'

        });

        dialogHandler.popupItem.setStyle({
            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()
    {
        dialogHandler.displayBox("none");
        dialogHandler.showUglyElements();
    },

    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
        };
    }
};