/* fixedBox : fixed box jQuery plugin
 * Copyright (C) 2007 Jean-Francois Hovinne - http://www.hovinne.com/
 * Dual licensed under the MIT and GPL licenses.
 */

jQuery.fn.fixedBox = function(options) {

    options = jQuery.extend({
    
        x: false,
        y: false
    
    }, options);

    var overlayBgColor = '#000';
	var overlayOpacity	= 0.3;    
    
    return(this.each(function() {
    
        var block_obj = this;
        var x = y = 0;
    
        if(!options.x || !options.y) {
        
            //compute top and left positions
            
            var ww = document.documentElement.clientWidth
                     || document.body.clientWidth;
            var hh = document.documentElement.clientHeight
                     || document.body.clientHeight;
            
            //Opera
            if(jQuery.browser.opera) hh = document.body.clientHeight;
    
            var w = jQuery(this).width();
            var h = jQuery(this).height();
       
            var padx = jQuery(this).css('padding-left');
            padx = parseInt(padx.substring(0, padx.length - 2));
            var pady = jQuery(this).css('padding-top');
            pady = parseInt(pady.substring(0, pady.length - 2));
            
            var borx = jQuery(this).css('border-left-width');
            borx = parseInt(borx.substring(0, borx.length - 2));
            if(!borx) borx = 0;
            var bory = jQuery(this).css('border-top-width');
            bory = parseInt(bory.substring(0, bory.length - 2));
            if(!bory) bory = 0;
            
            x = Math.round((ww - w - 2*padx - 2*borx)/2);
            y = Math.round((hh - h - 2*pady - 2*bory)/2);
            
            if(y < 0){
                h = Math.round(hh - 2*pady - 2*bory - 80);
                y = Math.round((hh - h)/2);
                jQuery(this).css({
                    height: h + 'px',
                    overflow: 'auto'
                });
            }
        }
        
        
        
        if(options.x) x = parseInt(options.x);
        if(options.y) y = parseInt(options.y);

        //handle MSIE 6.0
        if(jQuery.browser.msie && jQuery.browser.version == '6.0') {
           
            //see http://www.howtocreate.co.uk/fixedPosition.html
            var expression = "( " + y + " + ( ignoreMe = "
                           + "document.documentElement.scrollTop ? "
                           + "document.documentElement.scrollTop : "
                           + "document.body.scrollTop ) ) + 'px'";
                         
            jQuery(this).get(0).style.setExpression("top", expression);
        
            jQuery(this).css({
                position: 'absolute',
                zIndex: '1000',
                left: x + 'px'            
            });
        
        } else{
    
            jQuery(this).css({
            
                position: 'fixed',
                zIndex: '1000',
                top: y + 'px',
                left: x + 'px'            
            });
        }
        
        function ___getPageSize() {
			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;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
        
        function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
        
        function _finish(){
			$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
            $(block_obj).fadeOut('slow');
			// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, select').css({ 'visibility' : 'visible' });
		}

        
        $('body').append('<div id="jquery-overlay"></div>');
        var arrPageSizes = ___getPageSize();
        $('#jquery-overlay').css({
            backgroundColor:	overlayBgColor,
            opacity:			overlayOpacity,
            width:				arrPageSizes[0],
            height:				arrPageSizes[1]
        }).fadeIn();
        $('#jquery-overlay,#fixed_block').click(function() { _finish(); });
    }));
};
