(function($) {
	$.fn.extend({
		popup: function(opts){
			opts = $.extend({},$.fn.popup.defaults,opts);

			function readExtraConfig(rel){
				var ret = $.extend({},opts), p = /popup\(([a-z0-9_\$\.]+)\)/i, ex;
				if(rel.indexOf('(') != -1 && (ex = p.exec(rel)) && ex && ex[1] && $.fn.popup.configs[ex[1]]){
					ret = $.extend({},opts, $.fn.popup.configs[ex[1]]);
				}
				return ret;
			}

			function popup(){
				var elm = $(this),
				s = elm.attr("class"),
				eOpt = readExtraConfig(elm.attr('rel')),
				windowName = eOpt.windowName || Date.parse(new Date());

				s = s.split(' ');
				$.each(s,function(i){
					reg = /^pu-([a-zA-Z]+)_([a-zA-Z0-9]+)$/;
					var ar = reg.exec(this);
					if(ar)
					{
						eOpt[ar[1]] = ar[2];
					}
				});
				if (eOpt.center)
				{
					switch (eOpt.center) {
						case 't':
							eOpt.top = 0;
							eOpt.left = (screen.availWidth - eOpt.width) / 2;
							break;

						case 'r':
							// r
							eOpt.top = (screen.availHeight - eOpt.height) / 2;
							eOpt.left = screen.availWidth - eOpt.width;
							break;

						case 'b':
							eOpt.top = screen.availHeight - eOpt.height;
							eOpt.left = (screen.availWidth - eOpt.width) / 2;
							break;

						case 'l':
							eOpt.top = (screen.availHeight - eOpt.height) / 2;
							eOpt.left = 0;
							break;

						// c
						default:
							eOpt.top = (screen.availHeight - eOpt.height) / 2;
							eOpt.left = (screen.availWidth - eOpt.width) / 2;
					}
				}
				// get destination
				var link = elm.attr('href');

				// set parameters for the popup window
				var params = ''
								+'width='+eOpt.width
								+',height='+eOpt.height
								+',toolbar='+eOpt.toolbar
								+',resizable='+eOpt.resizable
								+',scrollbars='+eOpt.scrollbars
								+',status='+eOpt.status
								+',location='+eOpt.location
								+',menubar='+eOpt.menubar;

				// set top-parameter if it is defined

				if (eOpt.top !== null)
				{
					params += ',top='+eOpt.top;
				}

				// set left-parameter if it is defined
				if (eOpt.left !== null)
				{
					params += ',left='+eOpt.left;
				}

				var f = (eOpt.openIt)? window.open(link,windowName,params) : window;
				f.focus();
				eOpt.callback.apply(f, [elm, eOpt]);
				return false;

			}
			return this.each(function(){
				var elm = $(this);
				var eOpt = readExtraConfig(elm.attr('rel'));
				elm.bind('click.popup',popup).addClass(eOpt.popupClass);
				if(eOpt.title){
					var title = elm.attr('title');
					title = (title)? title+', '+eOpt.title:eOpt.title;
					elm.attr({'title': title});
				}
			});
		}
	});

	// set defaults
	$.fn.popup.defaults = {
		width: 600,
		height: 600,
		toolbar: 'yes',
		resizable: 'yes',
		scrollbars: 'yes',
		status: 'yes',
		location: 'yes',
		menubar: 'yes',
		center: null,
		top: null,
		left: null,
		title: 'Ziel öffnet sich in einem neuen Fenster',
		windowName: '',
		popupClass: 'popup',
		callback: function(){},
		openIt: true
	};
	$.fn.popup.configs = function(c){
		$.extend($.fn.popup.configs,c);
	};
})(jQuery);


