//  Popup 0.8 - 13-06-2008

var Popup = Class.create();
Popup.prototype = {

	win: null,
	options: null,
	current_options: null,
	tabWinOptions: null,

	initialize: function (options) {
		this.options = {
			'url': null,
			'title': null,
			'position': 'auto',
			'scrollbars': 'yes',
			'toolbar': 'no',
			'location': 'no',
			'status': 'no',
			'resizable': 'no',
			'width': 300,
			'height': 300,
			'top': 0,
			'left': 0
		};
		this.tabWinOptions = ['scrollbars', 'toolbar', 'location', 'status', 'resizable', 'width', 'height', 'top', 'left'];
		this.setOptions(options);
	},

	setOptions: function (options) {
		Object.extend(this.options, options || {});
	},

	autoPosition: function () {
		this.current_options.left = Math.round((screen.width-Number(this.current_options.width))/2);
		this.current_options.top = Math.round((screen.height-Number(this.current_options.height))/2);
	},

	openPopup: function (options) {
		this.current_options = Object.clone(this.options);
		Object.extend(this.current_options, options || {});
		if( this.current_options.position == 'auto' ) this.autoPosition();
		var window_options = [];
		for(var u=0; u<this.tabWinOptions.length; u++){ eval("window_options.push(this.tabWinOptions[u]+'='+this.current_options."+this.tabWinOptions[u]+");"); }
		this.win = window.open(this.current_options.url, this.current_options.title, window_options.join(','));
		this.win.focus();
	},

	closePopup: function () {
		this.win.close();
		this.win = null;
	},

	__void: function(){}

};

Event.observe(window, 'load', function(){
	var winObj = new Popup({
		'title':'win',
		'width':600,
		'height':600
	});
	$$('a.popup-jswindow').each(function(elem){
		elem.onclick = function(){
			var opt = {'url':this.href};
			var classArr = elem.classNames().toArray();
			if(classArr.length>1){
				var classDimJSWindow = classArr.grep(/^popup-jswindow-/).last();
				/([0-9]*)x([0-9]*)$/i.exec(classDimJSWindow);
				if(RegExp.$1){ Object.extend(opt, {'width':Number(RegExp.$1)}); }
				if(RegExp.$2){ Object.extend(opt, {'height':Number(RegExp.$2)}); }
			}
			winObj.openPopup(opt);
			return false;
		}
	});
});

