/**
 *
 *
 * Depends:
 *	ui.core.js
 */

(function($) {
	var uiDialogDiv = 'ui-popup-div';
	var uiOverlay = 'ui-popup-overlay';

	$.widget('ui.popup', {
		_init: function()
		{
			var widget = this;
			var oOverlay = $('<div id="' + uiOverlay + '"></div>')
			.appendTo(document.body)
			.hide();

			var oDialog = $('<div id="' + uiDialogDiv + '" class="last"><a href="#" class="close" onclick="return false;"></a><div class="content"></div></div>')
			.appendTo(document.body)
			.hide();

			// get all links with class popup and add the popup functionality to it
			$('body a.popup').live('click', function(){
				var href = $(this).attr('href');
				if('#' != href)
				{
					// ? 'span-12' : '';
					$('#' + uiDialogDiv).removeClass('span-10');
					$('#' + uiDialogDiv).removeClass('span-12');
					if ($(this).hasClass('span12'))
					{
						$('#' + uiDialogDiv).addClass('span-12');
					}
					else
					{
						$('#' + uiDialogDiv).addClass('span-10');
					}
					widget._show(href);
				}
				return false;
			});

			$('a.close', this.element).live('click', function(){
				widget.close();
			});
		},

		_show: function(href)
		{
			var widget = this;
			var scrollTop = $(window).scrollTop();

			var screenDims = widget._getScreenDims();

			$('#' + uiOverlay).width(screenDims.width);
			$('#' + uiOverlay).height($(document).height());
			$('#' + uiOverlay).css({'z-index': 100});

			var offsetLeft = (screenDims.width - 619)/2;

			$('#' + uiDialogDiv).css({'top': scrollTop + 50 + 'px', 'left': offsetLeft + 'px', 'z-index': 101});

			$('.content', '#' + uiDialogDiv).load(href, function(){
				$('#' + uiOverlay).show();
				$('#' + uiDialogDiv).show();
			});
		},

		_getScreenDims: function()
		{
			var bIsIE = (-1 != navigator.userAgent.indexOf('MSIE')) ? true : false;

			if(!bIsIE)
			{
				var iHeight = (window.innerHeight >= document.body.offsetHeight) ? window.innerHeight : document.body.offsetHeight;
				var iWidth = (window.innerWidth >= document.body.offsetWidth) ? window.innerWidth : document.body.offsetWidth;
			}
			else
			{
				var iHeight = (document.documentElement.clientHeight >= document.body.offsetHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
				var iWidth = (document.documentElement.clientWidth >= document.body.offsetWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
			}

			return {width: iWidth, height: iHeight};
		},

		close: function()
		{
			$('#' + uiOverlay).hide();
			$('#' + uiDialogDiv).hide();
		}
	});
})(jQuery);
