/*
 * uBox jQuery lightbox plugin
 * Requires jQuery 1.3+
 *
 * Quick HTML usage guide:
 *   <div id="overlay" style="display:none">I'm hidden but will be shown later</div>
 *   <a href="#overlay" rel="ubox">Open div#overlay in a popup</a>
 *   <a href="via_ajax.html" rel="ubox">Load via AJAX</a>
 *
 * Quick JS usage guide:
 *   $.ubox("#overlay");
 *   $("#overlay").ubox();
 *   $.ubox("load_via_ajax.html");
 *   $.ubox.hide();
 */
 
(function($)
{
	$.ubox = function(p) { return $.ubox.show(p); }
	$.fn.ubox = function() { $.ubox(this); }
    $.ubox.options =
    {
        screen_speed:     500,   // Speed of the black screen's fading in/out
        popup_speed:      250,   // Speed of the popup's fading in/out (no effect in IE)
        transition_speed: 400,   // Speed of loading the content, and going from one screen to another
        delay:            300,   // Delay in AJAX loading time (please don't set to 0)
        allow_clickout:   true,  // Allow clicking on the background to close
        screen_opacity:   0.7,   // Opacity of the black screen (0...1)
        screen_color:     "#000" // Color of the black screen
    };
	    
    $.extend($.ubox,
    { 
        // Properties
        _screen:   null,  // The bg screen
        _container: null,
        _subcon:   null,
        _content:  null,  // The popup window (null if not active)
        _ietimer:  null,  // Timer for IE
        _original: null,
        
        _loadWindow: function (target)
        {
            // What is returned will be referred to as _content and
            // appended to this._subcon.
            
            el = $(target);
            if (el.length) {
                this._original = el;
                return $('<div class="ubox-content">').append(this._original.show()).show();
            }
            
            // Make it up
            el = $('<div class="ubox-content ubox-deferred">').hide();
            
            // AJAX
            window.setTimeout(function() { $.get(target, function(data)
            {
                if (!$.ubox._content) { return; }
                // Load the stuff (it's hidden right now btw)
                el.html(data);
                
                // Delete the loader
                loader = $.ubox._subcon.find(".ubox-loader");
                loader.hide();
                
                // Make the content fit the loader, then expand
                $.ubox._subcon
                  .css({
                    "width":  loader.outerWidth(),
                    "height": loader.outerHeight()
                  })
                  .animate(
                    { "width": el.outerWidth(), "height": el.outerHeight() },
                    $.ubox.options.transition_speed,
                    function() { el.fadeIn($.ubox.options.transition_speed); }
                  );
                
                // Animate the repositioning
                if (1) // Err... an IE check was here before
                {
                    c = $.ubox._container;
                    c.animate({
                        "top":  parseInt(c.css('top'))  - (el.outerHeight()- loader.outerHeight())/2,
                        "left": parseInt(c.css('left')) - (el.outerWidth() - loader.outerWidth() )/2
                    }, $.ubox.options.transition_speed,function() { $.ubox._oncomplete(); });
                }
                
            }); }, this.options.delay);
            return el;
        },
        
    	/**
    	 * Loads the popup.
    	 * Called by onclick events.
    	 *
    	 * @par Example
    	 * // Loads the "my-overlay" box.
    	 * $.ubox.show('#my-overlay');
    	 */
	    show: function(target)
    	{
    		// Loads popup only if it is disabled.
    		if (!this._content)
    		{
    		    this._content = this._loadWindow(target);
		    
    		    // Activate the background popup
    			this._screen.css(
    			{
    				"opacity":    this.options.screen_opacity,
    				"position":   "absolute",
    				"height":     $(document).height()+"px",
    				"background": this.options.screen_color,
    				"width":      "100%",
    				"top":        0,
    				"left":       0,
    				"z-index":    10001
    			});
			
    			// IE6 fix for dropdown boxes: Use bgiframe.
    		    if (($.browser.msie) && ($.browser.version < 6.5))
    			    { this._screen.bgiframe(); }

                // Show screen
    			this._screen.fadeIn(this.options.screen_speed, function() {});

                // Dynamicly-generated box
                if (this._content.is(".ubox-deferred")) {
                    this._subcon.append($('<div class="ubox-loader">'));
		            this._subcon.append(this._content);
                }
                
                // Box that's pulled from the content
                else {
                    // Shows, but this will not be shown as it's container
                    // is still invisible.
		            this._subcon.append(this._content);
                }
                
                // Show popup window
                $.ubox._centerPopup(this._content);
    		}
		
    		// If the popup is already shown
    		else
    		{
    			// Abruptly make the old popup disappear
    			if (this._original)
    			    { $(document.body).append(this._original.hide()); }
    			
    			new_content = this._loadWindow(target);
    			
    			this._subcon.empty();
    			if (new_content.is(".ubox-deferred")) {
    			    loader = $('<div class="ubox-loader">').hide();
    			    this._subcon.append(loader);
    			    ox = loader.outerWidth() - loader.width();
    			    oy = loader.outerHeight() - loader.height();
    			    loader.css({ "width": this._subcon.width()-ox,
    			        "height": this._subcon.height()-oy });
    			    loader.fadeIn(this.options.transition_speed);
			    }
			    this._subcon.append(new_content);
			
    			// Quickly switch to the new popup
    			this._content = new_content;
    		}
    	},
    	
    	/**
    	 * Gets rid of the popup.
    	 */
    	
    	kill: function()
    	{    
    		// Disable the popup only if it is enabled
    		if (!$.ubox._content) { return; }
		
    		// Disable the IE timer if it's been turned on.
    		if (this._ietimer)
    		    { window.clearTimeout(this._ietimer); }
		
    		// Fade out the windows.
    		$.ubox._screen.fadeOut(this.options.screen_speed);
    		$.ubox._container.fadeOut(this.options.popup_speed, function() {
    		    $.ubox._content.hide();
    		    // Clear, and clear any dimensions set by the ajax thing
    		    $.ubox._subcon.html('');
    		    $.ubox._subcon.css({"width":"","height":""});
    		    
    		    // If we pulled something from the source, put it back
    		    if ($.ubox._original) {
    		        $(document.body).append($.ubox._original.hide());
    		        $.ubox._original = null;
		        }
		        
		        
    		    $.ubox._content = null;
    		});    		
	    },
	    
	    /**
	     * Called on document ready; initializes the hooks.
	     * @private
	     */
	     
	    _init: function()
	    {
	        if ($.browser.msie)
	            { this.options.popup_speed = 0; }
	            
    	    // Screen
    		$.ubox._screen =
    		    $('<div id="ubox-screen">')
    		        .css({ "display": "none" })
    		        .insertBefore($(document.body.firstChild));
    		        
    	    // Border
    		$.ubox._container =
    		    $('<div id="ubox-container">')
    		        .css({ "display": "none" })
    		        .insertBefore($(document.body.firstChild));
    		
    		this._subcon = $('<div id="ubox-subcontainer">')
    		        .css({ "overflow": "auto" });
    		    
    		$.ubox._container.append(this._subcon);
    		
    		$("a.popup-button, a[rel=ubox]").live("click", function()
    		    { $.ubox($(this).attr('href')); $(this).blur(); return false; });
		
    		$("input.popup-button").click(function()
    		    { $.ubox($(this.form).attr('action')); $(this).blur(); return false; });
		
    		$(".popup-close, a[rel=ubox-close]").live("click", function()
    		    { $(this).blur(); $.ubox.kill(); return false; });
			
    		// temporarily commented to make it modal
    		// Click out  
    		/* 
    		$.ubox._screen.click(function()
    			{ if ($.ubox.options.allow_clickout) $.ubox.kill(); });
				
    		// Escape key
    		$(document).keypress(function(e)
    		{
    			if ((e.keyCode == 27) && ($.ubox._content))
    				{ $.ubox.kill(); }
    		});
    		*/ 
        },
	    
	    /**
	     * Initializes the popup window.
	     * @private
	     */
	     
	    _centerPopup: function (target_id)
    	{
		    
    		popup = this._container;
    		var windowWidth  = document.documentElement.clientWidth;
    		var windowHeight = document.documentElement.clientHeight;
    		var popupHeight  = popup.outerHeight();
    		var popupWidth   = popup.outerWidth();

    		popup.css({
    			"position": "fixed",
    			"top": windowHeight/2-popupHeight/2,
    			"left": windowWidth/2-popupWidth/2
    		});
		
    		// MSIE does not support position: fixed, so we work around it.
    		if ($.browser.msie) {
    			popup.css({
    				  "position": "absolute",
    				  "top": windowHeight/2-popupHeight/2 + $(window).scrollTop()
    			});
    			if (!this._ietimer) this._ietimer = window.setInterval(function()
        		{
    		        var popupHeight  = popup.outerHeight();
    		        var popupWidth   = popup.outerWidth();
        			var windowWidth = document.documentElement.clientWidth;
        			var windowHeight = document.documentElement.clientHeight;
        			target = windowHeight/2-popupHeight/2 + $(window).scrollTop();
        			popup.css({ "left": windowWidth/2 - popupWidth/2 });
        			if ($.browser.msie) {
        				from = parseInt(popup.css('top'));
        				if (Math.abs(target-from) > 1)
        					{ popup.css({ "top": (from + (target - from) * 0.4) + "px" }); }
        				else
        					{ popup.css({ "top": target + "px" }); }
        			}
        			else {
        				popup.css({ "top": (windowHeight/2-popupHeight/2) + "px" });
        			}
        		}, 25);
    		}
		
    		
    		popup.css({ "z-index": 10003 }).fadeIn(this.options.popup_speed);
    	},
    	
    	_oncomplete: function () {
    		return true;
    	}
	
    });

	// Let's rock.
	$(function() { $.ubox._init(); });
	
})(jQuery);


// bgIframe plugin
(function($){
$.fn.bgIframe = $.fn.bgiframe = function(s) {
	// This is only for IE6
	if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
		s = $.extend({
			top     : 'auto', // auto == .currentStyle.borderTopWidth
			left    : 'auto', // auto == .currentStyle.borderLeftWidth
			width   : 'auto', // auto == offsetWidth
			height  : 'auto', // auto == offsetHeight
			opacity : true,
			src     : 'javascript:false;'
		}, s || {});
		var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
		    html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
		               'style="display:block;position:absolute;z-index:-1;'+
			               (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
					       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
					       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
					       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
					       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
					'"/>';
		return this.each(function() {
			if ( $('> iframe.bgiframe', this).length == 0 )
				this.insertBefore( document.createElement(html), this.firstChild );
		});
	}
	return this;
};

})(jQuery);