﻿var QuickModal = 
{
    open: function(tag, properties, fn)
    { 
        var control = $(tag);
        var properties = this.center(control, properties);
        if(properties.overlay)
        {
            this.addOverlay(tag, properties, fn);
        }
        
        $(window).resize(function() {
            QuickModal.center(control, properties);
        });
        $(window).scroll(function() {
            QuickModal.center(control, properties);
        });
    },
    getProperties: function() 
    {
        return  {
            'height'        : 0,
            'width'         : 0,
            'topOffSet'     : 0,
            'position'      : 'absolute',
            'display'       : 'block',
            'zIndex'		: 1000,
            'colorOverlay'  : '#000',
            'overlay' : true,
            'closeOverlay' : true,
            'opacity' : 0.2
        };
    },
    setProperties: function(destination, source) 
    {
        for (var property in source) 
        {
            destination[property] = source[property];
        }
        return destination;
    },
    center : function(control, properties) 
    {
        var property = this.setProperties(this.getProperties(), properties || {});
                
        var height = property.height;
        if(height == 0)
            height = $(control).height();
         
        var pageSize = this.getPageSize();
        var pos = this.realOffset(document.body);
        
        $(control).css('position', property.position);
		$(control).css('z-index',property.zIndex);
        
        var newTopOffSet = (pageSize.windowHeight / 2 - height / 2 + pos[1] + property.topOffSet);
		if (newTopOffSet < 0)
		{
		    $(control).css('top', property.topOffSet);
		}
		else
		{
			$(control).css('top', newTopOffSet);
		}
		
        $(control).css('left', (pageSize.windowWidth / 2 - ((property.width == 0 ? $(control).width() : property.width) / 2) + pos[0]));
        
        if(property.height != 0)
            $(control).height(property.height);
            
        if(property.width != 0)
            $(control).width(property.width);
		
        $(control).css('display', property.display);
        
        $(control).prependTo('form');
		
		return property;
    },
    realOffset: function(element) 
    {
        var top = 0, left = 0;
        do 
        {
            top += element.scrollTop  || 0;
            left += element.scrollLeft || 0;
            element = element.parentNode;
        } while (element);
        
        return [left, top];
    },
    hide: function(control) 
    {
        var overlay = $('html > div:first');
        if(overlay != null)
        {
            $(overlay).remove();
        }
        
        $(control).css('display','none');
        $(window).unbind('resize');
        $(window).unbind('scroll');
    },
    addOverlay: function(control, properties, fn)
    {
        var overlay = document.createElement('div');
        
        $(overlay).css({
            top : 0,
            left : 0,
            position : 'absolute',
            backgroundColor : properties.colorOverlay,
            opacity : properties.opacity,
            height : $(document).height(),
            width : '100%',
            zIndex : properties.zIndex - 1
        });
        
        if(properties.closeOverlay)
        {
            $(overlay).click(function(){
            QuickModal.hide(control);
                if(fn != undefined)
                {
                    fn.call(this);
                }
            });
        }
        
        $(overlay).prependTo('html');
        return overlay;
    },
	setOpacity: function(element, value)
    {
        if (typeof element == 'string')
        {
            element= $(element);
        }
        if (value == 1)
        {
            element.style.opacity = (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 0.999999 : 1.0 ;
            if(/MSIE/.test(navigator.userAgent) && !window.opera)
            {
                element.style.filter = element.style.filter.replace(/alpha\([^\)]*\)/gi,'');
            }
        } 
        else 
        {
            if(value < 0.00001) value = 0;
            {
                element.style.opacity = value;
            }
            
            if(/MSIE/.test(navigator.userAgent) && !window.opera)
            {
                element.style.filter = element.style.filter.replace(/alpha\([^\)]*\)/gi,'') + 'alpha(opacity='+value*100+')';
            }
        }
        return element;
    },
    getPageSize: function()
    {
        var xScroll, yScroll;
        if (window.innerHeight && window.scrollMaxY) 
        {
            xScroll = document.body.scrollWidth;
            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
            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 = windowWidth;
        } 
        else 
        {
          pageWidth = xScroll;
        }
        
        return {
          'pageWidth':pageWidth,
          'pageHeight':pageHeight,
          'windowWidth':windowWidth,
          'windowHeight':windowHeight,
          'yScroll':yScroll,
          'xScroll':xScroll
        };
    }
}
