//(function($) {
	var num = function(el, prop) {
		return parseInt(jQuery.css(el.jquery?el[0]:el,prop))||0;
	};

	jQuery.fn.jPopup = function(fct)
	{
		var container = (window.parent === window) ? this[0] : window.__jPopupContainer;

		if (fct && ((fct.constructor === String && '/' === fct.substr(0, 1)) || (fct.constructor === Object)))
		{
			[].push.call(arguments, fct);
			fct = 'create';
		}

		if (!container)
		{
			var $container = jQuery('<div />');
			var popup = (window.parent || window)
				.jQuery.fn.jPopup.apply($container, arguments);
			popup[0].homeWindow = window;
			return popup;
		}

		if (!('create' in container))
		{
			jQuery.extend(container, jQuery.fn.jPopup.fn);
		}

		if (!('homeWindow' in container))
		{
			container.homeWindow = window;
		}
		
		var r = container[fct].apply(container, [].slice.call(arguments, 1));
		return r === undefined ? this : r;
	}

	jQuery.fn.jPopup.fn =
	{
		settings: {
			url: null
			, id: null
			, onLoad: null
			, onClose: null
			, x: 'center'
			, y: 'center'
			, width: 'auto'
			, height: 'auto'
			, anchor: window
			, title: 'Popup Window'
			, modal: true
			, busyMsg: 'Please wait ...'
			, watchResize: false
			, useJcakeUrl: true
			, closeButtonUrl: '/img/close.png'
			, showBoxHead: true
			, backgroundColor: '#fff'
			, border: '1px solid black'
		}
		, create: function(settings) {
			if (settings && settings.constructor === String) {
				settings = {url: settings};
			} else if (!settings) {
				settings = {};
			}

			settings = jQuery.extend(jQuery.fn.jPopup.fn.settings, settings);

			if (settings.useJcakeUrl) {
				var url = (settings.url) ? jCake.url(settings.url) : '';
			} else {
				var url = (settings.url) ? settings.url : '';
			}

			var $iframe = jQuery('<iframe src="'+url+'" />').css({
				border: 'none'
				, width: '100%'
				, height: '100%'
				, 'overflow-y': 'auto'
				, 'overflow-x': 'auto'
			});

			var $backdrop = jQuery('#jPopupBackdrop');
			
			if (!$backdrop.length && true == settings.modal) {
				$backdrop = jQuery('<div />').css({
					position: 'absolute'
					, 'z-index': 9998
					, width: '100%'
					, height: Math.max(jQuery('body').outerHeight(), 800)
					, top: 0
					, left: 0
					, opacity: 0.55
					, 'background-color': '#000'
				})
				.attr('id', 'jPopupBackdrop')
				.appendTo('body');
			}
			
			var $container = jQuery('<div />').css({
				position: 'absolute'
				, top: '150px'
				, left: '150px'
				, width: '350px'
				, height: '350px'
				, border: settings.border
				, 'z-index': 9999
				, 'background-color': settings.backgroundColor
			}).attr({
				id: settings.id
				, 'class': 'box j-popup'
			})
			.append('<div class="boxhead"><img class="close" src="'+jCake.url(settings.closeButtonUrl)+'"/>'+this.settings.title+'</div>')
			.append($iframe[0])
			.appendTo('body');
				
			var container = $container[0], iframe = $iframe[0];

			jQuery.extend(container, jQuery.fn.jPopup.fn);
			jQuery.extend(container, {
				settings: settings
				, $iframe: $iframe
				, $backdrop: $backdrop
			});

			container.busy(true);

			jQuery.fn.jPopup.fn.frameReady(iframe, function() {
				iframe.contentWindow.__jPopupContainer = container;
				container.document = iframe.contentDocument;

				iframe.contentWindow.resizeClosure = function(){container.onResize(true)};
				jQuery(iframe.contentWindow).unbind('resize').bind('resize', iframe.contentWindow.resizeClosure);
				jQuery(window).unbind('resize').bind('resize', iframe.contentWindow.resizeClosure);

				if (settings.watchResize == true) {
					iframe.resizeTimer = setInterval(function() {
						if (!iframe || !iframe.contentWindow) {
							clearInterval(iframe.resizeTimer);
							return;
						}

						iframe.contentWindow.resizeClosure();
					}, 100);
				}

				if (url) {
					container.busy(false);
					container.onResize();
				}

				if (typeof container.settings.onLoad == 'function') {
					container.settings.onLoad.call(container);
				}
			});

			jQuery('img.close', container).bind('click', function(){container.close();});

			container.onResize();
			container.updatePosition();

			return $container;
		}
		, onResize: function(secondary) {
			this.updateSize(secondary);
			this.updatePosition();
		}
		, close: function()
		{
			var close = true;
			if (typeof this.settings.onClose == 'function')
			{
				close = this.settings.onClose.call(this) !== false;
			}

			if (true == close)
			{
				jQuery(this).add(this.$backdrop[0]).remove();
				return true;
			}
			return false;
		}
		, $: function(selector, context)
		{
			return jQuery(selector, context || this.$iframe[0].contentDocument);
		}
		, busy: function(message)
		{
			if (message === undefined)
			{
				return jQuery('div.popup-busy', this).length > 0;
			}
			else if (message === false)
			{
				this.$busy.remove();
				this.$iframe.css({opacity: 1, width: '100%', height: '100%'});
				jQuery('div.boxhead', this).show();
				this.settings.height = this.$busy.oldHeight;

				return;
			}
			else if (message === true)
			{
				message = this.settings.busyMsg;
			}

			this.$iframe.css({opacity: '0.01', width: '0px', height: '0px'});
			jQuery('div.boxhead', this).hide();
			this.$busy = jQuery('<div class="popup-busy"><p><img src="'+jCake.baseUrl+'img/activity-small.gif'+'" />'+message+'</p></div>')
				.appendTo(this);
			this.$busy.oldHeight = this.settings.height;
			this.settings.height = '67px';
		}
		, updateSize: function(secondary) {
			if (!secondary) {
				jQuery(this).css({width: (this.settings.width === 'auto') ? '600px' : this.settings.width, height: '10px'});
			}

			var rootElement = (this.busy() ? this.$busy[0] : jQuery('html', this.document).get(0));

			if (this.busy()) {
				jQuery(this).css({ width: '180px', height: '90px'});
			}

			var paddings = {top: 10, bottom: 10, left: 5, right: 5};
			var width = (this.settings.width == 'auto' || this.busy() ? parseInt(rootElement[(this.busy() ? 'offset' : 'scroll') + 'Width']) : parseInt(this.settings.width));
			var height = (this.settings.height == 'auto' ? parseInt(rootElement[(this.busy() ? 'offset' : 'scroll') + 'Height']) : parseInt(this.settings.height));

			width += (paddings.left + paddings.right);
			height += (paddings.top + paddings.bottom);

			var dimensions = {
				width: width + 'px'
				, height: height + 'px'
			};

			var previous = this.previousDimensions || {width: 0, height: 0};
			if (parseInt(previous.width) == parseInt(dimensions.width) && parseInt(previous.height) == (parseInt(dimensions.height)-paddings.bottom)) {
				return;
			}

			jQuery(this).css(dimensions);
			this.previousDimensions = dimensions;
		}
		, updatePosition: function()
		{
			var position =
			{
				left: (this.settings.x == 'center')
					? (parseInt(jQuery(this.settings.anchor).width()) / 2 - parseInt(jQuery(this).width()) / 2)+'px'
					: this.settings.x
				, top: (this.settings.y == 'center')
					? ((parseInt(jQuery(this.settings.anchor).height()) / 2 - parseInt(this.offsetHeight) / 2)+jQuery(window).scrollTop())+'px'
					: this.settings.y
			};

			jQuery(this).css(position);
		}
		, frameReady: function(frame, callback)
		{
			if (frame.ready)
			{
				callback();
			}

			var frameLoaded = function()
			{
				frame.contentDocument.removeEventListener( "DOMContentLoaded", arguments.callee, false);
				jQuery(frame.contentWindow).unbind('load');
				jQuery(frame).unbind('load');
				frame.ready = true;
				callback();
			}

			var aboutBlank = function()
			{
				if (!frame.contentDocument)
				{
					clearInterval(arguments.callee.timer);
					return false;
				}

				if (frame.contentDocument.location.href == 'about:blank')
				{
					return;
				}

				clearInterval(arguments.callee.timer);
				frame.contentDocument.addEventListener( "DOMContentLoaded", frameLoaded, false);
				jQuery(frame.contentWindow).bind('load', frameLoaded);
			}
			jQuery(frame).bind('load', frameLoaded);
			aboutBlank.timer = setInterval(aboutBlank, 1);

		}
		, _frameReady: function()
		{

		}
		, frameLoaded: function()
		{

		}
	}
//}(jQuery));

