jQuery.fn.popupwin = function(options) {
	return this.each(function(){
		var url,name;
		if (typeof options ==  'object'){
			url = (options.url) ? options.url : $(this).attr('href');
			name = (options.name) ? options.name : 'mywin';
			
			var defaults = {
				location : 0,
				directories : 0,
				status : 0,
				menubar : 0,
				toolbar : 0,
				resizable : 0,
				scrollbars : 0,
				pos : 'center'
			}
			
			options = $.extend(defaults, options);
			
			var tmpOptions = [];
			for(n in options){
				if (n != 'url' && n != 'name' && n != 'pos'){
					tmpOptions.push(n + '=' + options[n]);
				}
			}
			
			if (options.pos && options.width && options.height){
				var leftPos,topPos;
				if (options.pos == "random") {
					leftPos=(screen.width) ? Math.floor(Math.random() * (screen.width - options.width)) : 100;
					topPos = (screen.height) ? Math.floor(Math.random()*((screen.height - options.height)-75)) : 100;
				} else if(options.pos == "center"){
					leftPos=(screen.width) ? (screen.width - options.width)/2:100;
					topPos=(screen.height)?(screen.height - options.height)/2:100;
				} else if ((options.pos != "center" && options.pos != "random") || options.pos == null){
					leftPos=0;
					topPos=20;
				}
				tmpOptions.push('top=' + topPos);
				tmpOptions.push('left=' + leftPos);
			}
			options = tmpOptions.join(',');
		} else {
			url = $(this).attr('href');
			name = 'mywin';
		}
		$(this).bind('click', {u:url,n:name,o:options}, function(e){
			var win = window.open(e.data['u'], e.data['n'], e.data['o']);
			if (win) win.focus();
			return false;
		});
		return this;
		
	});
};