// ----------------------------------------------------------------------
// Global Utility Functions
// ----------------------------------------------------------------------

function isset(x){
	return typeof x != "undefined" && x != null && x != false && x != "";
}

// ----------------------------------------------------------------------
// WindowHelper Object
// ----------------------------------------------------------------------

WindowHelper = new Object();

WindowHelper.getScreenWidth = function(){
	return window.screen ? window.screen.availWidth : 795;
}
WindowHelper.getScreenHeight = function(){
	return window.screen ? window.screen.availHeight : 500;
}
WindowHelper.getScreenPosition = function(size,pos){
	var sWidth = this.getScreenWidth();
	var sHeight = this.getScreenHeight();
	switch(pos){
		case "left": pos = 0; break;
		case "center": pos = Math.round((sWidth-size)/2); break;
		case "right": pos = sWidth-size; break;
		case "top": pos = 0; break;
		case "middle": pos = Math.round((sHeight-size)/2); break;
		case "bottom": pos = sHeight-size; break;
		default: pos = null;
	}
	return pos;
}
WindowHelper.getScreenWidthPercentage = function(width){
	return Math.round(this.getScreenWidth()/(100/parseInt(width)));
}
WindowHelper.getScreenHeightPercentage = function(height){
	return Math.round(this.getScreenHeight()/(100/parseInt(height)));
}

// ----------------------------------------------------------------------
// WindowLauncher Class
// ----------------------------------------------------------------------

WindowLauncher = function(){
	this.features = {};
	this.setChromeFeatures(false);
	this.setScrollFeatures(false);
	return this;
}
WindowLauncher.prototype.setFeature = function(name,value){
	if(value == true) value = "yes";
	if(value == false) value = "no";
	this.features[name] = value;
}
WindowLauncher.prototype.setChromeFeatures = function(show){
	this.setFeature("toolbar", show);
	this.setFeature("menubar", show);
	this.setFeature("location", show);
	this.setFeature("status", show);
}
WindowLauncher.prototype.setScrollFeatures = function(show){
	this.setFeature("scrollbars", show);
	this.setFeature("resizable" , show);
}
WindowLauncher.prototype.getFeatures = function(){
	var features = "";
	for(var i in this.features) features += ","+i+"="+this.features[i];
	return features.substr(1);
}
WindowLauncher.prototype.open = function(url,name,width,height,x,y){
	if(isset(width)) this.setFeature("width", width);
	if(isset(height)) this.setFeature("height", height);
	if(isset(x)){this.setFeature("screenX", x); this.setFeature("left", x);}
	if(isset(y)){this.setFeature("screenY", y); this.setFeature("top", y);}
	this.reference = window.open(url,name,this.getFeatures());
	if(this.reference != null && !this.reference.closed){
		this.reference.focus();
		if(x && y) this.reference.moveTo(x,y);// Could be a problem in IE6
	}
}

// ----------------------------------------------------------------------
// Preset windows

WindowLauncher.prototype.openChrome = function(url,name,width,height,x,y){
	this.setChromeFeatures(true);
	this.open(url,name,width,height,x,y);
}
WindowLauncher.prototype.openScroll = function(url,name,width,height,x,y){
	this.setScrollFeatures(true);
	this.open(url,name,width,height,x,y);
}
WindowLauncher.prototype.openChromeScroll = function(url,name,width,height,x,y){
	this.setChromeFeatures(true);
	this.setScrollFeatures(true);
	this.open(url,name,width,height,x,y);
}
WindowLauncher.prototype.openCenter = function(url,name,width,height){
	var x = WindowHelper.getScreenPosition(width,"center");
	var y = WindowHelper.getScreenPosition(height,"middle");
	this.open(url,name,width,height,x,y);
}
WindowLauncher.prototype.openCenterChrome = function(url,name,width,height){
	this.setChromeFeatures(true);
	this.openCenter(url,name,width,height);
}
WindowLauncher.prototype.openCenterScroll = function(url,name,width,height){
	this.setScrollFeatures(true);
	this.openCenter(url,name,width,height);
}
WindowLauncher.prototype.openCenterChromeScroll = function(url,name,width,height){
	this.setScrollFeatures(true);
	this.setChromeFeatures(true);
	this.openCenter(url,name,width,height);
}
WindowLauncher.prototype.openKiosk = function(url,name){
	this.setFeature("fullscreen", true);
	var width = WindowHelper.getScreenWidth();
	var height = WindowHelper.getScreenHeight();
	this.open(url,name,width,height,0,0);
}
WindowLauncher.prototype.openBlank = function(url){
	this.setScrollFeatures(true);
	this.setChromeFeatures(true);
	var name = new Date();
	name = name.getTime();
	name = name.toString();
	this.open(url,name,null,null,null,null);
}
