(function($) {
	$.tiny = $.tiny || {};
	$.tiny.popup = {
		options : {
			model : 'popup',
			closeFn : function() {
			},
			loadFn : function() {
			}
		}
	};
	$.fn.tinypopup = function(options) {
		var options = $.extend( {}, $.tiny.popup.options, options);
		new Popup($(this), options)
		return this;
	};
	function Popup(root, options) {
		root.appendTo('body');
		var oPopup = $('.popup', root);
		var oPopupMain = $(root.children()[root.children().size() - 1]);

		if (oPopup.size() == 0) {
			oPopup = $("<div class='popup'></div>");
			oPopupMain = $("<div class='bgPopup'></div>");

			if (root.html() == null || jQuery.trim(root.html()) == '') {
				oPopup.appendTo(root);
				oPopupMain.appendTo(root);
				$.get(root.attr('url'), function(data) {
					oPopup.html(data);
					root.attr('url', null);
					doPopup(root, options, oPopup, oPopupMain);
				});
				return false;
			} else {
				if (root.children().size() == 0) {
					oPopup.html(root.html());
					root.empty();
				} else {
					while (root.children().size() > 0) {
						$(root.children()[0]).appendTo(oPopup);
					}
				}
				oPopup.appendTo(root);
				oPopupMain.appendTo(root);
				return doPopup(root, options, oPopup, oPopupMain);
			}
		} else {
			return doPopup(root, options, oPopup, oPopupMain);
		}
	}
	;

	function doPopup(root, options, oPopup, oPopupMain) {
		var oClose = $('.close', root);
		var oWrapper = root;
		var popupStatus = 0;
		var oSelf = this;

		function initialize() {
			setEvent();
			update();
		}
		;

		function setEvent() {
			oClose.click(function() {
				disablePopup();
			});

			if (options.model == 'popup') {
				oPopupMain.click(function() {
					disablePopup();
				});
			}

		}
		;

		function update() {
			oPopupMain.css( {
				"opacity" : "0.6",
				"filter" : "alpha(opacity=60)",
				"display" : "none",
				"position" : "fixed",
				"height" : "100%",
				"width" : "100%",
				"top" : "0px",
				"left" : "0px",
				"background" : "#000000",
				"z-index" : "34"
			});

			oPopup.css( {
				"display" : "none",
				"position" : "absolute",
				"z-index" : "35"
			});
			loadPopup();
			options.loadFn();
			return false;
		}
		;

		function loadPopup() {
			if (popupStatus == 0) {
				$(document).bind('keypress', keypress);
				oWrapper.css('display', 'block');
				oPopupMain.fadeIn("slow");
				oPopup.fadeIn("slow");
				popupStatus = 1;
			}
			centerPopup();
			return false;
		}
		;

		function keypress(e) {
			if (e.keyCode == 27 && popupStatus == 1) {
				disablePopup();
			}
		}
		;

		// disabling popup with jQuery magic!
		function disablePopup() {
			if (popupStatus == 1) {
				popupStatus = 0;
				$(document).unbind('keypress', keypress);
				oPopupMain.hide();
				oPopup.hide();
				options.closeFn();
			}
		}
		;

		// centering popup
		function centerPopup() {
			var height = ($(window).height() - oPopup.outerHeight()) / 2
					+ $(window).scrollTop();
			oPopup.css("top", (height < 0 ? $(window).scrollTop() : height) + "px");
			oPopup.css("left", (($(window).width() - oPopup.outerWidth()) / 2)
					+ $(window).scrollLeft() + "px");

		}
		return initialize();
	}
})(jQuery);
