
window.addEvent('domready', POPUP_init);

function POPUP_init(){
	$$("a.popupbox").each(function(el){el.onclick=POPUP_bind});
}

function POPUP_bind(event) {
	var event = new Event(event);
	// stop default behaviour
	event.preventDefault();
	// remove click border
	this.blur();
	// get caption: either title or name attribute
	var caption = this.title || this.name || "";
	// get rel attribute for image groups
	var group = this.rel || false;
	// display the box for the elements href
	POPUP_open(caption, this.href, group);
	return true;
}

// called when the user clicks on a smoothbox link
function POPUP_open(caption, url, rel) {
	var defaultWidth= 500;
	var defaultHeight= 640;
	var scrollbars= 'YES';
	var directories= 'NO';
	var w_location= 'NO';
	var p_width= defaultWidth;
	var p_height= defaultHeight;
	var x= 0;
	var y= 0;

	try{	
		var queryString = url.match(/\?(.+)/)[1];
		var params = parseQuery(queryString);
		if ( (params['width']*1)>0)
			p_width= (params['width']*1);
		if ( (params['height']*1)>0)
			p_height=(params['height']*1);
	}	catch (e)	{
	}
	
	if (screen) {
		y = (screen.availHeight - p_height)/2;
		x = (screen.availWidth - p_width)/2;
	}
	
	window.open(url, caption.replace(/ /g,'_'), 'width='+p_width+',height=' + p_height+',scrollbars='+ scrollbars +',directories=' + directories + ',location=' + w_location +',screenX='+x+',screenY='+y+',top='+y+',left='+x);
}

function parseQuery ( query ) {
	// return empty object
	if( !query )
		return {};
	var params = {};
	
	// parse query
	var pairs = query.split(/[;&]/);
	for ( var i = 0; i < pairs.length; i++ ) {
		var pair = pairs[i].split('=');
		if ( !pair || pair.length != 2 )
			continue;
		// unescape both key and value, replace "+" with spaces in value
		params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
   }
   return params;
}

