// Willow.Popup.js
//
// Popup functionality for Willow sites
//
// http://yensdesign.com/2008/09/how-to-create-a-stunning-and-smooth-popup-using-jquery/

var wpopup = new WillowJSPopup();

$(document).keypress(function(e) {  
	if(e.keyCode==27) {  
		wpopup.closeCurrent();
	}  
});

/* Willow /**/
function WillowJSPopup() 
{
	var _current = null;
	var _currentBG = null;
	var __curIEFrame = null;
	
	var _speed = 200;

	this.getObject = function(obj)
	{
		if (obj != null)
		{
			// get object if only name is provided
			if (!isObject(obj))
			{
				obj = $('#' + obj);
			} else if (!(obj instanceof jQuery)) {
				obj = $(obj);
			}
			return obj;
		} 
		return null;
	}

	this.value = function(obj, val)
	{
		obj = this.getObject(obj);
		if (obj != null)
		{
			obj.html(val);
		}
	}

	/*
		var option = { 
				"size": { "width": "100%", "height": "100%" },
				"value": "some html" 
			};

	/**/

	this.show = function(obj, options)
	{
		this.closeCurrent();
		obj = this.getObject(obj);
	
		if (obj != null)
		{
			this._current = obj;
			
			// get id of bg
			var BGid = this._current.attr('id') + 'bg';
			var cbg = $('#' + BGid);
		
			if (options != null)
			{
				// option.value
				// option.size.width
				// option.size.heigth
				if (options.value != null)
					this.value(obj, options.value);
					
				if (options.size != null)
				{
					if (options.size.width != null)
						this._current.width(options.size.width);
					if (options.size.height != null)
						this._current.height(options.size.height);
				}
			}

			if ($.browser.msie)
			{
				var ieFrameId = this._current.attr('id') + 'IEFrame';
				var ieFrame = $('#' + ieFrameId);

				if (ieFrame.size() == 0);
				{
					$("body").prepend("<iframe class='willowPopupIEFrame' style='display:none;' id='" + ieFrameId + "'></iframe>");
				}

				// get frame and set width and height
				this.__curIEFrame = $('#' + ieFrameId);
				this.__curIEFrame.width( this._current.width() );
				this.__curIEFrame.height( this._current.height() );
			} 	
									
			this._currentBG = cbg;
			this.center();
			
			// show
			cbg.click(function() { wpopup.closeCurrent() });
			cbg.fadeIn(this._speed);
			this._current.fadeIn(this._speed);
			if (this.__curIEFrame != null)
				this.__curIEFrame.fadeIn(this._speed);
		} else {
			alert('no object provided for popup.show');
		}
	}

	this.closeCurrent = function() 
	{
		if (this._current != null)
		{
			this._current.fadeOut(this._speed);
			this._currentBG.fadeOut(this._speed);
			if (this.__curIEFrame != null)
				this.__curIEFrame.fadeOut(this._speed);
			
			this._current = null;
			this._currentBG = null;
			this.__curIEFrame = null;
		}
	}
	
	this.center = function() 
	{
		if (this._current != null)
		{
			//request data for centering
			this._current.center();
			
			if (this.__curIEFrame != null)
				this.__curIEFrame.center();
			
			//only need force for IE6
			if ($.browser.msie && $.browser.version == 6.0) {  
				this._currentBG.css({
					"width": $(document).width(),
					"height": $(document).height()
				});
			}
		}
	}
}

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}
