// POPUP //
(function($){
	////////////////// image preloading ///////////////////////
	if ( typeof $.preloadImage === "undefined" ) {
		$.preloadImage = function(image, callback) {
			var self = this;
			this.file = image;
			this.callback = callback;
			this.loaded = false;
			this.aborted = false;
			this.error = false;
			this.width = 0;
			this.height = 0;
			this.src = '';
			this.image;
			
			this.cancelEvent = function() {
				if (this.image && this.image.onload) {
					this.image.onload = null;
				}
				if (this.image && this.image.onabort) {
					this.image.onabort = null;
				}
				if (this.image && this.image.onerror) {
					this.image.onerror = null;
				}
				
				this.callback = null;
				this.image = null;
				this.dummy.remove();
			};
			
			this.imageLoaded = function() {
				var self = this;
				
				this.width = this.image.width;
				this.height = this.image.height;
				
				//create dummy <img> element to cache rendered image (not only loaded)
				this.dummy = $("<img src='' style='position:absolute;left:-10000px;top:0;visibility:hidden;' />").appendTo("body");
				
				$(this.dummy).bind("load", function() {
					if ( typeof self.callback !== "undefined" && typeof self.callback == "function") {
						self.callback.apply(self, [self, "loaded"]);
					}
					
					self.cancelEvent();
					self = null;
				});
				
				this.dummy.get(0).src = this.src;
			}
			
			//this.image.src = this.file;
			this.image = new Image();
			this.image.src = this.file;
			this.src = this.image.src;
			
			this.autoCheckIntervalId = 0;
			this.autoCheck = function() {
				if (self.loaded || self.aborted || self.error) {
					window.clearInterval(self.autoCheckIntervalId);
					return;
				}
				
				if (self.image && self.image.complete) {
					window.clearInterval(self.autoCheckIntervalId);
					self.loaded = true;
					
					self.imageLoaded();
					return;
				}
				
				if (self.image && (self.image.width || self.image.height)) {
					window.clearInterval(self.autoCheckIntervalId);
					self.loaded = true;
					
					self.imageLoaded();
					return;
				}
			};
			
			this.autoCheckIntervalId = window.setInterval( self.autoCheck, 500 );
		};
	}
	
	
	function num(el, props) {
		var r = 0;
		$.each(props.split(/\s+/) || [], function(i,v){
			r += parseInt($(el).css(v)) || 0;
		});
		return r;
	}
	
	function isImage(url) {
		return url.match(/\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(\.*))?$/i);
	}
	
	function isURL(url) {
		return /^https?:\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url);
	}
	
	function parse_url (str, component) {
		// http://phpjs.org
		// *     example 1: parse_url('http://username:password@hostname/path?arg=value#anchor');
		// *     returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'}
		
		// modified: reduced function, parser mode is always 'php'
		
		var key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', 
							'relative', 'path', 'directory', 'file', 'query', 'fragment'],
			parser = /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/;

		var m = parser.exec(str),
			uri = {},
			i = 14;
		while (i--) {
			if (m[i]) {
			  uri[key[i]] = m[i];  
			}
		}

		if (component) {
			return uri[component.replace('PHP_URL_', '').toLowerCase()];
		}
		
		return uri;
	}
	
	$.fn.popup = function(options){
		var isMethodCall = (typeof options == "string"),
			args = Array.prototype.slice.call(arguments, 1);
			
		return this.each(function() {
			var instance = $(this).data("popup");
			if (isMethodCall && instance && $.isFunction(instance[options])) {
				instance[options].apply(instance, args);
			} else if (!instance) {
				$(this).data("popup", new $.popup(this, options));
			}
		});
	};
	
	var popup = $.popup = function(element, options) {
		this.element = $(element);
		this.options = $.extend(true, {},$.popup.defaults, options);
		this.init();
	};
	
	$.extend($.popup.prototype, {
		init: function() {
			var opt = this.options;
			$(this.element).click(function(){
				
				$.popup.initialize();
				$.popup.show(this.href, opt, this);
				
				return false;
			});

		}
	});
	
	
	$.extend($.popup, {
		defaults: {
			opacity: 0.8,
			modal:false
		},
		debug: false,
		stack: [],
		guid: 1,
		minWidth: 200,
		minHeight: 200,
		current: "",
		modal:false,
		
		setOptions: function(opt) {
			$.popup.defaults = $.extend({}, $.popup.defaults, opt);
		},
		
		initialize: function() {
			$("body").append(
				'<div id="POPUP_overlay"></div>' +
				'<div id="POPUP">' +
					'<div id="POPUP_borders">' +
						'<div class="POPUP_borders_tt"><div class="POPUP_borders_tl"></div><div class="POPUP_borders_t1"></div><div class="POPUP_borders_t"></div><div class="POPUP_borders_t2"></div><div class="POPUP_borders_tr"></div></div>' +
						'<div class="POPUP_borders_ll">' +
							'<div class="POPUP_borders_l1"></div><div class="POPUP_borders_l"></div><div class="POPUP_borders_l2"></div>' +
						'</div>' +
						'<div id="POPUP_wrapper"><div id="POPUP_content"></div></div>' +
						'<div class="POPUP_borders_rr">' +
							'<div class="POPUP_borders_r1"></div><div class="POPUP_borders_r"></div><div class="POPUP_borders_r2"></div>' +
						'</div>' +
						'<div class="POPUP_borders_bb"><div class="POPUP_borders_bl"></div><div class="POPUP_borders_b1"></div><div class="POPUP_borders_b"></div><div class="POPUP_borders_b2"></div><div class="POPUP_borders_br"></div></div>' +
						'<div href="#" title="Close" class="POPUP_close"><a href="#" title="Close">Close</a></div>' + 
					'</div>' +
				'</div>');
			
			$("#POPUP_overlay").hide();
			$("#POPUP").hide();
			
			$("#POPUP").bind("mousedown", function(event){
				if ( $(event.target).is("#POPUP") && !popup.modal ) {
					popup.closePopup();
				}
			});

			
			$("#POPUP .POPUP_close").bind("click", function(event){
				popup.closePopup();
				return false;
			});
			
			$.popup.initialize = $.noop;
		},
		
		back: function() {
			var id = popup.stack.pop();
			if ( id ) {
				popup.show(id);
			}
			
			//pop once again because popup.show will add current popup to stack again
			popup.stack.pop();
		},
		
		closePopup: function() {
			//if (popup.current != "") {
				popup.hideCurrent();
				
				$("#POPUP").hide();
				$("#POPUP_overlay").hide();
				
				popup.loadingImage = "";
			//}
		},
		
		hideCurrent: function(fade) {
			var parent, current,
				f = typeof fade === "undefined" ? false : fade;
			
			function saveContent() {
				if (popup.saveParent) {
					parent = $("#" + popup.saveParent);
					if (parent.length > 0) {
						parent.append(current);
					} else {
						current.appendTo("body");
					}
				} else {
					current.appendTo("body");
				}
				
				current = null;
			}
			
			if ( typeof popup.ajax_request !== "undefined" && popup.ajax_request !== null ) {
				try {
					popup.ajax_request.abort();
				} catch(e) { }
				
				popup.ajax_request = null;
				popup.ajax_cancelled = true;
				popup.ajax_url = "";
				popup.ajax_opt = null;
			}
			
			if (popup.current == "") {
				return;
			}
			
			current = $("#" + popup.current);
			
			if ( typeof popup.is_ajax_loaded !== "undefined" && popup.is_ajax_loaded ) {
				if (current.length > 0) {
					current.remove();
				}
			} else {
				if (current.length > 0) {
					
					if ( f ) {
						$.FadeHide(current, 2000, "swing", function(){
							saveContent();
						});
						
					} else {
						saveContent();
					}
					/*
					if (popup.saveParent) {
						parent = $("#" + popup.saveParent);
						if (parent.length > 0) {
							parent.append(current);
						} else {
							current.appendTo("body");
						}
					} else {
						current.appendTo("body");
					}
					*/
				}
			}
			
			$("#" + popup.current).trigger("popupHide");


			popup.is_ajax_loaded = false;
			parent = null;
			popup.current = "";
		},
		
		show: function(element, options, caller) {
			var 
				minWidth = (typeof options.minWidth !== "undefined") ? options.minWidth : popup.minWidth,
				minHeight = (typeof options.minHeight !== "undefined") ? options.minHeight : popup.minHeight,
				oldWidth = minWidth, oldHeight = minHeight,
				opt = $.extend({}, popup.defaults, options),
				myGroup, myIdx,
				toShow, ajax = false, iframe = false, image = false, t, y, tmp;
			
			if (!element) {
				return;
			}
			
			popup.modal = options.modal;
			
			if ( typeof opt.hideCloseButton !== "undefined" && opt.hideCloseButton ) {
				$(".POPUP_close", "#POPUP").hide();
			} else {
				$(".POPUP_close", "#POPUP").show();
			}
			
			//element id
			if (typeof element === "string") {
				if ( element.indexOf(document.location.href) == 0 ) {
					tmp = document.location.href.replace(/#.*$/, '');
					element = element.replace(tmp + '#', '');
				}
				
				if ( $("#" + element).length > 0 ) {
					element = element.replace(document.location.href, element);
					toShow = element;
					
				} else if ( isImage(element) ) {
					image = true;
					toShow = element;
					//not supported for now
					
				} else if ( isURL(element) ) {
					//
					
					urlInfo = parse_url(element);
					
					if (urlInfo.host !== window.location.hostname) {
						iframe = true;
					}
					
					ajax = true;
					toShow = element;
				} else {
					return;
				}
			} 
			//DOM Element
			else if (element.nodeType) {
				if ( element.id === "" ) {
					toShow = element.id = "POPUP_" + popup.guid++;
				} else {
					toShow = element.id;
				}
				
			//$()
			} else if (element.jquery) {
				if (element.length < 1) {
					return;
				}
				
				if ( element[0].id === "" ) {
					toShow = element[0].id = "POPUP_" + popup.guid++;
				} else {
					toShow = element[0].id;
				}
				
			} else {
				//unknown
				return;
			}
			
			
			//
			$.popup.initGroup(toShow, opt, caller);
			
			if (popup.current) {
				//store old popup (note that 'popup.current' is ID string)
				popup.stack.push(popup.current);
				
				$("#POPUP_content").stop();
				$("#POPUP_wrapper").stop();
				$("#POPUP_borders").stop();
				
				if ( image && popup.isGroup ) {
					popup.hideCurrent(true);
				} else {
					popup.hideCurrent();
				}
				
				//popup.cy = $("#POPUP_wrapper").width();
				//oldHeight = $("#POPUP_wrapper").height();
				
				oldWidth = (typeof popup.curWidth === "undefined") ? minWidth : popup.curWidth;
				oldHeight = (typeof popup.curHeight === "undefined") ? minHeight : popup.curHeight;
				
				t = $("#POPUP_borders .POPUP_borders_ll .POPUP_borders_l1").height() * 2;
				

			} else {
				//start new
				popup.stack = [];
				
				oldWidth = popup.curWidth = (typeof popup.curWidth === "undefined") ? minWidth : popup.curWidth;
				oldHeight = popup.curHeight = (typeof popup.curHeight === "undefined") ? minHeight : popup.curHeight;
				
				$("#POPUP").css({
					display:"block",
					visibility:"hidden",
					width:"100%",
					height: $(document).height()
				});
				
				t = $("#POPUP_borders .POPUP_borders_ll .POPUP_borders_l1").height() * 2;
				

				$("#POPUP_borders").css({
					top: $(window).scrollTop() + (($(window).height() - Math.max(popup.curHeight, popup.minHeight) - t) / 2),
					width: popup.curWidth,
					height: popup.curHeight
				});
				
				$("#POPUP_overlay").css({opacity: opt.opacity || 0.8}).show();
				$("#POPUP_content").css({
					width: popup.curWidth,
					height: popup.curHeight
				});
				
				$("#POPUP_wrapper").css({
					width: popup.curWidth,
					height: popup.curHeight
				});
				
				$("#POPUP").css("visibility", "visible");
			}
			
			y = Math.floor( $(window).scrollTop() +  ($(window).height() - Math.max(oldHeight, popup.minHeight) - t) / 2 );
			
			if ( y < 0 ) {
				y = 0;
			}
			
			$("#POPUP_borders").css({
				width: oldWidth,
				height: oldHeight,
				top: y
			});
			
			$("#POPUP_borders .POPUP_borders_t").css("width", oldWidth - t );
			$("#POPUP_borders .POPUP_borders_b").css("width", oldWidth - t );
			$("#POPUP_borders .POPUP_borders_l").css("height", oldHeight - t );
			$("#POPUP_borders .POPUP_borders_r").css("height", oldHeight - t );
			
			$("#POPUP_content").addClass("POPUP_loading");
			
			opt.isIframe = false;
			opt.isImage = false;
			
			if ( iframe ) {


				popup.loadIFrame( toShow, opt, caller );
			} else if ( ajax ) {
				popup.loadContent( toShow, opt, caller );
			} else if ( image ) {
				opt.isImage = true;
				popup.loadImage( toShow, opt, caller );
			} else {
				popup._show( toShow, opt, caller );
			}
		},
		
		getCachedImage: function(src) {
			if ( typeof popup.image_caches === "undefined" ) {
				popup.image_caches = {};
			}
			
			if ( typeof popup.image_caches[src] !== "undefined" ) {
				if ( $("#" + popup.image_caches[src]).length ) {
					return popup.image_caches[src];
				} else{
					return "";
				}
			}
			
			return "";
		},
		
		cacheImage: function(obj) {
			var src = obj.file,
				id, el, container;
				
			if ( typeof popup.image_caches === "undefined" ) {
				popup.image_caches = {};
			}
			
			if ( typeof popup.image_caches[src] !== "undefined" ) {
				if ( $("#" + popup.image_caches[src]).length ) {
					return popup.image_caches[src];
				}
			}
			
			container = $("#POPUP_images_container");
			if ( container.length < 1 ) {
				container = $('<div id="POPUP_images_container" style="display:none;"></div>').appendTo("body");
			}
			
			id = "POPUP_" + popup.guid++;
			el = $("<div id='" + id + "' class='POPUP-image' style='position:absolute;left:0;top:0;'></div>").append(obj.dummy.clone(false).removeAttr("style")).appendTo(container);
			
			popup.image_caches[src] = id;
			
			return id;
		},
		
		loadImage: function(image, opt, caller) {
			var cache = popup.getCachedImage(image);
			
			if ( cache ) {
				popup.loadingImage = "";
				popup._show( cache, opt, caller );
				
			} else {
				popup.loadingImage = image;
				
				new $.preloadImage(image, function(obj, status) {
					if ( status == "loaded" ) {
						var cache = popup.cacheImage(obj);
						
						if ( popup.loadingImage === obj.file ) {
							popup.hideCurrent();
							popup._show(cache, opt, caller);
							
							popup.loadingImage = "";
						}
						
						
					}
				});
				

			}
		},
		
		loadIFrame: function(url, opt, caller) {
			var iframename = "iframe_" + popup.guid++;
			var html = '<iframe src="'+ url +'" width="' + opt.width + '" height="' + opt.height +'" name="'+ iframename +'" frameborder="0" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" noresize></iframe>';
			
			

			popup.ajax_opt = $.extend({}, opt);
			popup.ajax_request_complete(html, true, caller);
		},
		
		loadContent: function(url, opt, caller) {
			popup.ajax_cancelled = false;
			popup.ajax_url = url;
			popup.ajax_opt = $.extend({}, opt);
			
			try {
				popup.ajax_request = $.ajax({
					url: url,
					type: "get",
					dataType: "html",
					success: function(html) {
						popup.ajax_request_complete(html || "", false, caller);
					},
					error: function(jqXHR, textStatus, errorThrown) {
						popup.ajax_url = "";
						popup.ajax_opt = null;
						popup.ajax_request = null;
						popup.is_ajax_loaded = false;
						
						popup.ajax_request_complete("", false, caller);
					}
				});
			} catch (e) {
				popup.ajax_url = "";
				popup.ajax_opt = null;
				popup.ajax_request = null;
				popup.is_ajax_loaded = false;
				popup.ajax_request_complete("", false, caller);
				//error
			}
		},
		
		ajax_request_complete: function(html, isIFrame, caller) {
			var id, el, parent, opt;
			
			if ( typeof isIFrame === "undefined" || isIFrame !== true ) {
				if ( typeof popup.ajax_request === "undefined" || popup.ajax_cancelled || popup.ajax_request == null ) {
					return;
				}
				
				if ( html == "" ) {
					return;
				}
			}
			
			id = "POPUP_" + popup.guid++;





			el = $("<div id='" + id + "'></div>").html(html);
			


			if ( typeof popup.ajax_opt.parent !== "undefined" ) {
				if ( $.isFunction(popup.ajax_opt.parent) ) {
					parent = popup.ajax_opt.parent.call();
					
				} else if ( $(popup.ajax_opt.parent).length ) {
					parent = $(popup.ajax_opt.parent);
					
				} else {
					parent = $("body");
				}
			} else {
				parent = $("body");
			}
			
			
			el.appendTo(parent);
			
			opt = popup.ajax_opt;
			
			popup.ajax_url = "";
			popup.ajax_opt = null;
			popup.ajax_request = null;
			popup.is_ajax_loaded = true;
			
			popup._show(id, opt, caller);
		},
		
		_show: function(id, opt, caller) {
			var parent, current, l, t, y, oldWidth=popup.curWidth, oldHeight=popup.curHeight, opt, scrollTop = $(window).scrollTop(), canGoBack = false;

			
			popup.current = id;
			current = $("#" + popup.current);


			parent = current.parent();
			
			if ( parent[0].id === "" ) {
				popup.saveParent = parent[0].id = "POPUP_" + popup.guid++;
			} else {
				popup.saveParent = parent[0].id;
			}
			
			$("<div id='POPUP_tmp' style='position:absolute;visibility:hidden;'></div>").append(current).appendTo("#POPUP_content");
			
			if ( typeof opt.beforeShow !== "undefined" && $.isFunction(opt.beforeShow) ) {
				opt.beforeShow.call(current);
			}
			
			if ( popup.stack.length ) {
				canGoBack = true;
				$(".popupBack, .btnPopupBack", "#POPUP_content").show();
			} else {
				$(".popupBack, .btnPopupBack", "#POPUP_content").hide();
			}
			




			
			if ($("#POPUP_tmp").children().children().children("iframe").length) {


				$("#POPUP_tmp").children().children().children("iframe").each(function(){
					







					popup.curWidth = $(this).outerWidth();
					popup.curHeight = $(this).outerHeight();
					
					$(this)
						.siblings().each(function(){
							popup.curHeight += $(this).outerHeight(true);
						})
						.end()
						.parent().css({
							width: popup.curWidth,
							height: popup.curHeight
						});
					
					popup.curWidth += num( this.parentNode, "paddingLeft paddingRight borderLeft borderRight" );
					popup.curHeight += num( this.parentNode, "paddingTop paddingBottom borderTop borderBottom" );
					
				});



			} else {
				$("#POPUP_tmp").children().each(function(){
					popup.curWidth = $(this).outerWidth();
					popup.curHeight = $(this).outerHeight();
				});
			}
			
			if ( popup.isGroup && opt.isImage ) {
				current.hide();
			}
			
			$("#POPUP_content").removeClass("POPUP_loading");
			$("#POPUP_content").append(current);
			$("#POPUP_tmp").remove();
			
			if ( popup.isGroup && opt.isImage ) {
				current.css({
					left:"50%",
					marginLeft: -popup.curWidth/2
				});
				$.FadeShow(current, 2000, "swing");
			}
			

			l = Math.floor((popup.curWidth - oldWidth) / 2);
			t = Math.floor((popup.curHeight - oldHeight) / 2);
			
			$("#POPUP_content").css({
				width: popup.curWidth,
				height: popup.curHeight,
				left: -l,
				top: -t
			});
			
			$("#POPUP_wrapper").css({
				width: oldWidth,
				height: oldHeight
			});
			
			t = $("#POPUP_borders .POPUP_borders_ll .POPUP_borders_l1").height() * 2;
			

			y = Math.floor( scrollTop +  ($(window).height() - Math.max(oldHeight, popup.minHeight) - t) / 2 );
			
			if ( y < 0 ) {
				y = 0;
			}
			
			$("#POPUP_borders").css({
				width: oldWidth,
				height: oldHeight,
				top: y
			});
			
			$("#POPUP_borders .POPUP_borders_t").css("width", oldWidth - t );
			$("#POPUP_borders .POPUP_borders_b").css("width", oldWidth - t );
			$("#POPUP_borders .POPUP_borders_l").css("height", oldHeight - t );
			$("#POPUP_borders .POPUP_borders_r").css("height", oldHeight - t );
			
			y = Math.floor( scrollTop + ($(window).height() - Math.max(popup.curHeight, popup.minHeight) - t) / 2 );
			
			if ( y < 0 ) {
				y = 0;
			}
			
			$("#POPUP").css({
				display:"block",
				visibility:"visible"
			});
			
			$(".btnPopupBack", current).unbind();
			
			if (canGoBack) {
				$(".btnPopupBack", current).bind("click", function(){
					popup.back();
					return false;
				});
			}
			
			
			
			$("#POPUP_borders .POPUP_borders_t").stop().animate({width:popup.curWidth -t}, {queue:false, duration:500, easing: "easeInOutExpo"});
			$("#POPUP_borders .POPUP_borders_b").stop().animate({width:popup.curWidth -t}, {queue:false, duration:500, easing: "easeInOutExpo"});
			$("#POPUP_borders .POPUP_borders_l").stop().animate({height:popup.curHeight - t}, {queue:false, duration:500, easing: "easeInOutExpo"});
			$("#POPUP_borders .POPUP_borders_r").stop().animate({height:popup.curHeight - t}, {queue:false, duration:500, easing: "easeInOutExpo"});
			
			$("#POPUP_content").stop().animate({left:0, top:0}, {queue:false, duration:500, easing: "easeInOutExpo"});
			$("#POPUP_wrapper").stop().animate({width:popup.curWidth, height:popup.curHeight}, {queue:false, duration:500, easing: "easeInOutExpo"});
			$("#POPUP_borders").stop().animate({top:y, width:popup.curWidth, height:popup.curHeight}, {queue:false, duration:500, easing: "easeInOutExpo"});
			
			$("#" + popup.current).trigger("popupShow");

			
			current = parent = null;
		},
		
		createGroupNav: function() {
			return $('<div id="POPUP_group-nav"><a href="#" title="" class="prev">&laquo; Back</a><a href="#" title="" class="next">Next &raquo;</a></div>').appendTo("#POPUP_borders");
		},
		
		reinitGroupNav: function(opt, caller) {
			$("a.next, a.prev", "#POPUP_group-nav").unbind().bind("click", function() {
				var el = $(this), myGroup, myIdx, idx;
				
				myGroup = popup.getGroup(opt.groupSelector);
				myIdx = myGroup.index(caller);
				
				if ( el.hasClass("next") ) {
					idx = myIdx + 1;
					if ( idx > myGroup.length -1 ) {
						idx = 0;
					}
				} 
				else {
					idx = myIdx - 1;
					if ( idx < 0 ) {
						idx = myGroup.length - 1;
					}
				}
				
				//console.debug(myGroup[idx].href, opt, myGroup[idx]);
				
				popup.show(myGroup[idx].href, opt, myGroup[idx]);
				
				return false;
			});
		},
		
		initGroup: function(id, opt, caller) {
			var myGroup, myIdx, groupNav;
			
			groupNav = $("#POPUP_group-nav");
			
			if ( typeof opt.groupSelector !== "undefined" ) {
				myGroup = popup.getGroup(opt.groupSelector);
				myIdx = myGroup.index(caller);
				
				//console.debug(myGroup, myIdx);
				
				if ( myGroup.length && myIdx >= 0 ) {
					if ( groupNav.length < 1 ) {
						groupNav = popup.createGroupNav();
					}
					
					popup.reinitGroupNav(opt, caller);
				}
				
				popup.isGroup = true;
				
			} else {
				if ( groupNav.length ) {
					groupNav.hide();
				}
				
				popup.isGroup = false;
			}
			
		},
		
		getGroup: function(selector) {
			var links;
			
			if ( typeof $.popup.cached_groups === "undefined" ) {
				$.popup.cached_groups = {};
			}
			
			if ( typeof $.popup.cached_groups[selector] !== "undefined" ) {
				return $.popup.cached_groups[selector];
			}
			
			links = $(selector);
			
			$.popup.cached_groups[selector] = links;
			
			return links;
		},
		
		resize: function() {
			var y, t, scrollTop = $(window).scrollTop();

			
			if (popup.current == "") {
				return;
			}
			
			$("#POPUP_content").stop();
			$("#POPUP_wrapper").stop();
			$("#POPUP_borders").stop();
			
			$("#POPUP_content").css({
				width:"auto",
				height:"auto"
			});
			
			popup.curWidth = $("#POPUP_content").width();
			popup.curHeight = $("#POPUP_content").height();
			
			t = $("#POPUP_borders .POPUP_borders_ll .POPUP_borders_l1").height() * 2;
			
			y = Math.floor( scrollTop + ($(window).height() - Math.max(popup.curHeight, popup.minHeight) - t) / 2 );
			if ( y < 0 ) {
				y = 0;
			}
			
			$("#POPUP_content").css({
				width: popup.curWidth,
				height: popup.curHeight
			});
			
			$("#POPUP_borders .POPUP_borders_t").stop().animate({width:popup.curWidth -t}, {queue:false, duration:500, easing: "easeInOutExpo"});
			$("#POPUP_borders .POPUP_borders_b").stop().animate({width:popup.curWidth -t}, {queue:false, duration:500, easing: "easeInOutExpo"});
			$("#POPUP_borders .POPUP_borders_l").stop().animate({height:popup.curHeight - t}, {queue:false, duration:500, easing: "easeInOutExpo"});
			$("#POPUP_borders .POPUP_borders_r").stop().animate({height:popup.curHeight - t}, {queue:false, duration:500, easing: "easeInOutExpo"});
			
			$("#POPUP_content").stop().animate({left:0, top:0}, {queue:false, duration:500, easing: "easeInOutExpo"});
			$("#POPUP_wrapper").stop().animate({width:popup.curWidth, height:popup.curHeight}, {queue:false, duration:500, easing: "easeInOutExpo"});
			$("#POPUP_borders").stop().animate({top:y, width:popup.curWidth, height:popup.curHeight}, {queue:false, duration:500, easing: "easeInOutExpo"});
			
		}
	});
	
	//EXPOSE
	window.showPopup = function(element, opt) {
		$.popup.initialize();
		opt = $.extend({}, $.popup.defaults, opt);
		$.popup.show(element, opt);
	}
	
	window.closePopup = popup.closePopup;
	window.resizePopup = popup.resize;
})(jQuery);
