/**
 * @author jdreissig
 */

if (typeof(mobile) == "undefined") var mobile = {};

// Utility methods for popup links & menus.
mobile.PopupHelper = {
	
	/**
	 * Clicking on popup links in menu should open new window.
	 */
	initPopupMenus: function() {
		mobile.observe('a.popup', 'click', (function(event, link) {		
			var width = 1024;
			var height = 768;			
			var dimensionClassParameter = new RegExp(/^.*\s(\d{2,4}x\d{2,4}){1}.*$/).exec(link.getAttribute("class"));
			if(dimensionClassParameter) {
				var dimensions = dimensionClassParameter[dimensionClassParameter.length-1].split("x");
				if(dimensionClassParameter.length==2) {
					width = dimensions[0];
					height = dimensions[1];
				}
			}
			this.openPopUp(link.getAttribute("href").strip(), width , height, 50, 50, "no", "no", "no", "no", "yes", "yes");
			return false;
		}).bind(this), {allowNested: true});
		
		mobile.observe('a.close-popup', 'click', function() {
			window.close();
			return false;
		});
	},
	
	openPopUp: function(linkURL, width, height, left, top, toolbar, directories, status, menubar, scrollbars, resizable) {
		// From mobile.js... TODO: refactor?
		var win = window.open(linkURL,'popup','width='+width+',height='+height+',left='+left+',top='+top+',toolbar='+toolbar+',location='+location+',directories='+directories+',status='+status+',menubar='+menubar+',scrollbars='+scrollbars+',resizable='+resizable+'');
		if (win)
			win.focus();
	},
	
	//close popup and display content in the opener window
	leavePopUpFocusOpener: function(url){
		window.opener.location.href = url;
		window.opener.focus();
	}
};

document.observe('mobile:ready', function() {
	// make menu popups pop up...
	mobile.PopupHelper.initPopupMenus();
});
