/**
 * Handle tasks related to making popup windows.
 *
 *	@param	isA		The name of one of the predefined "boilerplates"
 *					used to determine the features of the popups.
 *	@param	featureString	A string of "name=value,..."s as is used 
 *					in Window's open() method. Values here will
 *					take precedence over those in the boilerplate.
 */
function PopupWindow(isA, featureString) {
	if(arguments[0]){
		// first arg could be a name for a boilerplate feature set
		var match = arguments[0].toLowerCase().match(/^[a-z]+$/)
		if (match) {
			this.isA = match[0]
			featureString = arguments[1]
		}
		// or, if there is no boilerplate given the first arg might be a feature string
		else {
			this.isA = null
			featureString = arguments[0]
		}
	}
	// the features of the windows this object creates are stored in this property
	this.features = new Object()
	
	this.setType(isA) 
		
	// parse the featureString
	this.features = this.parseFeatureString(featureString, this.features)
}	

/**
 * Stringify the features object for this PopupWindow, in a form
 * that can be used to construct new window.
 *
 *	@return	a String made from the features property
 */
PopupWindow.prototype.getFeatureString = function() {
	var pairs = new Array()
	for (i in this.features) {
		pairs[pairs.length] = i+"="+this.features[i]
	}
	return pairs.join(",")
}

PopupWindow.prototype.setType = function(isA){
	this.isA = isA
	
	// populate with some default feature values
	this.features.status = "yes"
	this.features.toolbar = "no"
	this.features.menubar = "no"
	this.features.location = "no"
	this.features.directories = "no"
	this.features.resizable = "yes"
	this.features.scrollbars = "yes"
	

	
	// overwrite default features with values from the boilerplate parameter
	switch(this.isA) {
		case "poll":
			this.features.height = "510"
			this.features.width = "560"
			this.adjustForVScrollBar()
		break
		case "video":
			this.features.height = "372"
			this.features.width = "570"
		break
		case "realplayer"://added for backward compatability(deprecated). Use "video" type instead 
			this.features.height = "372"
			this.features.width = "570"
		break
		case "realtest":
			this.features.height = "310"
			this.features.width = "125"
			this.features.scrollbars = "no"
		break
		case "audio":
			this.features.height = "372"
			this.features.width = "570"
		break
		case "quiz":
			this.features.height = "366"
			this.features.width = "465"
		break
		case "photoessay":
			this.features.height = "400"
			this.features.width = "500"
		break
		case "ecard":
			this.features.height = "350"
			this.features.width = "550"
			this.features.scrollbars = "auto"
			this.features.resizable = "yes"
		break
		case "email":
			this.features.height = "600"
			this.features.width = "500"
			this.features.scrollbars = "no"
		break
		case "email_no_ad":
			this.features.height = "502"
			this.features.width = "500"
			this.features.scrollbars = "no"
		break
		case "charticle":
			this.features.height = "585"
			this.features.width = "500"
			this.adjustForVScrollBar()
		break
		case "timeline":
			this.features.height = "585"
			this.features.width = "410"
		break
		case "speaktruth":
			this.features.height = "495"
			this.features.width = "625"
		break
		case "infobox":
			this.features.height = "585"
			this.features.width = "410"
		break
		case "newsletter":
			this.features.height = "400"
			this.features.width = "220"
			this.features.scrollbars = "no"
		break
		case "rules":
			this.features.height = "470"
			this.features.width = "465"
		break
		case "full":
			this.features.status = "yes"
			this.features.toolbar = "yes"
			this.features.menubar = "yes"
			this.features.location = "yes"
			this.features.directories = "yes"
		break
		case "tour":
			this.features.height = "550"
			this.features.width = "640"
			this.features.resizable = "no"
		break
		case "quizPoll":
			this.features.height = "400"
			this.features.width = "500"
			this.features.status = "yes"
			this.features.toolbar = "no"
			this.features.menubar = "no"
			this.features.location = "no"
			this.features.directories = "no"
			this.features.resizable = "yes"
			this.features.scrollbars = "auto"
			this.adjustForNoScrollBar()
		break
		case "quizPoll2":
			this.features.height = "520"
			this.features.width = "569"
			this.features.status = "yes"
			this.features.toolbar = "no"
			this.features.menubar = "no"
			this.features.location = "no"
			this.features.directories = "no"
			this.features.resizable = "yes"
			this.features.scrollbars = "auto"
			this.adjustForNoScrollBar()
		break
		case "quizPoll3":
			this.features.height = "415"
			this.features.width = "500"
			this.features.status = "yes"
			this.features.toolbar = "no"
			this.features.menubar = "no"
			this.features.location = "no"
			this.features.directories = "no"
			this.features.resizable = "yes"
			this.features.scrollbars = "auto"
			this.adjustForNoScrollBar()
		break
		case "browser_upgrade":
			this.features.height = "316"
			this.features.width = "380"
			this.features.scrollbars = "no"
			this.adjustForNoScrollBar()
		break
		case "contests":
			this.features.height = "510"
			this.features.width = "600"
			this.features.status = "no"
			this.features.toolbar = "no"
			this.features.menubar = "no"
			this.features.location = "no"
			this.features.directories = "no"
			this.features.resizable = "yes"
		break
	}
}

/**
 * Parse this objects feature string (of name/value pairs), return a hash
 * object with the names as keys. For example:
 * <code>
 *     var popup = new PopupWindow("width=100,height=200")
 *     var hash = popup.getFeatures()
 *     var popupWidth = hash["width"] // 100 in this case
 * </code>
 *
 * @param	featureString As described in the constructer docs.
 *					The default value is the featureString property
 *					of this object.
 * @param	hash	An optional hash object that when given will 
 *					have the parsed entries added to it. This
 *					hash will be altered and returned.
 * @return	An object (to be treated as a hash) whose keys correspond
 *			to the names in the featureString, and whose values are
 *			likewise parsed from the featureString.
 */
PopupWindow.prototype.parseFeatureString = function(featureString, hash) {
	if(arguments[0] == null) featureString = this.getFeatureString()
	if(arguments[1] == null) hash = new Object()
	
	// parse the featureString
	var pairs = featureString.split(',')
	
	// overwrite features with those specified in the feature string parameter
	for (i in pairs) {
		pair = pairs[i].split('=')
		// strip leading and trailing spaces
		pair[0] = pair[0].replace(/(^\s+|\s+$)/g,"")
		pair[1] = pair[1].replace(/(^\s+|\s+$)/g,"")
		hash[pair[0]] = pair[1]
	}
	
	return hash
}

/**
 * Cause a new window to be created using this PopupWindow's 
 * features. A reference to the new window becomes a property
 * of this object.
 *
 *	@param	url		The URL that is used as the location of the new 
 *					window.
 *	@param	name	The window's name, can be used to refer to the  
 *					window. The default value is "popup".
 *	@param	isA 	The type of window to open   				
 */
PopupWindow.prototype.open = function(url, name, isA) {
	var wasA = null
	if(isA != null) {
		// remember what type this object was, we'll restore it later
		wasA = this.isA
		this.setType(isA) // temporarily
	}
	if (name == null || name == "") name = "popup"
	this.win = eval("open(\""+url+"\",\""+name+"\",\""+this.getFeatureString()+"\")")
	this.win.focus()
	if(typeof this.win.opener != "undefined" && this.win.opener != null) this.win.opener.name = "oxymain"
	
	if(wasA != null) {
		//return to your orignal type!
		this.setType(wasA)
	}
}

/**
 * In cases where the usable inner-dimensions of the window are critical
 * and there is a danger that crossbrowser differences in treatment of
 * scrollbars will create discrepencies in this, you can use this method 
 * to make adjustment for all this. This will only be necessary if you
 * are certain the popped window will have scrollbars added to it.
 *
 * @dependencies Client.js
 */
PopupWindow.prototype.adjustForVScrollBar = function() {
	if (window.Client == null) alert("PopupWindow.adjustForVScrollBar(): missing required js script \"Client.js\"")
	
	if(Client.PLATFORM == 'Win'){
		if (Client.BROWSER == 'IE') {
			this.features.width = parseInt(this.features.width)+16
		}
		if (Client.BROWSER == 'NS') {
			this.features.width = parseInt(this.features.width)+14
		}	
	}
	else if(Client.PLATFORM == 'Mac') {
		if (Client.BROWSER == 'NS') {
			this.features.width = parseInt(this.features.width)+15
		}
	}
}

PopupWindow.prototype.adjustForNoScrollBar = function() {
	if (window.Client == null) alert("PopupWindow.adjustForVScrollBar(): missing required js script \"Client.js\"")
		if(Client.PLATFORM == 'Mac') {
			if (Client.BROWSER == 'IE') {
				this.features.width = parseInt(this.features.width)-16
				this.features.height = parseInt(this.features.height)-16
			}
		}
}		