var sWidth = screen.availWidth;
var sHeight = screen.availHeight;
var popupWindow;

window.onload = init;

function init() {
	var items = document.getElementsByClass("popup");
	var n = items.length;
	while ( n-- ) {
		var link = items[ n ];
		link.onclick = function(){
			var disabled = this.parentNode.className.indexOf('disabled') != -1;
			if ( disabled || this.href == document.location + '#' ) {
				return preventDefaultAction();
			} else {
				return popup( this.href );
			}
		}
	}
}



function preventDefaultAction() {
	if (typeof event != "undefined") event.returnValue=false;
	return false;
}

function openPopup(theUrl, id, features) {
	if (!features)        features = {};
	if (!features.width)  features.width = sWidth;
	if (!features.height) features.height = sHeight;
	if (!features.left)   features.left = (sWidth - features.width)/2; 
	if (!features.top)    features.top = (sHeight - features.height)/2; 
	// Defaults to no features except titlebar, hotkeys and status
	var theFeatures = "";
	// Now we convert object values to feature string. Features default to true unless 
	for (var feature in features) {
		var val = features[feature];
		switch (feature) {
			case "width":
				theFeatures += 'width='+val+',innerWidth='+val+',';
				break;
			case "height":
				theFeatures += 'height='+val+',innerHeight='+val+',';
				break;
			case "left":
				theFeatures += 'left='+val+',screenX='+val+',';
				break;
			case "top":
				theFeatures += 'top='+val+',screenY='+val+',';
				break;
			default:
			val = (val==0 || val=='no' || val==false) ? 0 : 1;
			theFeatures += feature+'='+val+',';
		}
	}
	// open the popup and return it
	var win = window.open(theUrl, id, theFeatures);
	return win;
}

function popup(url) {
	var features = { resizable: true };

	if ( sWidth < 1024 || sHeight < 730 ) {
		features.scrollbars = true;
	} else {
		features.width = 1000;
		features.height = 680;
	}
	popupWindow = openPopup(url, 'LINZTool', features);
	return preventDefaultAction();
}

function closePopup() {
	popupWindow.close();
}

function quit() {
//	document.getElementById('wrapper').innerHTML = 'BYE BYE!';
	self.close();
	window.opener.closePopup();
}
