/*******************************************************************************
 * A set of helper classes/methods for dealing with game options and their UI's.
 ******************************************************************************/

/*
 * All options must support the getName() and getValue() methods. Most will also
 * support the getDocumentElement() method to return a UI.
 */
var Options = new function() {
	var useOptions = false;

	this.setUseOptions = function() {
		useOptions = true;
	}

	this.getUseOptions = function() {
		return useOptions;
	}

	this.HiddenField = function(name, value) {
		this.name = name;
		this.value = value;

		this.getName = function() {
			return name;
		}
		this.getValue = function() {
			return value;
		}
	}

	this.FixedField = function(name, title, value) {
		var name = name;
		var title = title;
		var value = value;

		this.getName = function() {
			return name;
		}
		this.getValue = function() {
			return value;
		}

		this.getDocumentElement = function() {
			var spanE = document.createElement('span');
			var titleE = document.createTextNode(title);
			spanE.appendChild(titleE);
			spanE.appendChild(document.createTextNode(value));
			return spanE;
		}
	}

	this.RadioButtons = function(name, defaultValue, title) {
		this.name = name;
		this.defaultValue = defaultValue;
		this.title = title;
		this.descriptions = new Array();
		this.values = new Array();
		var that = this;

		// Add's a radio button choice
		this.addButton = function(description, value) {
			this.descriptions.push(description);
			this.values.push(value);
		}

		this.getName = function() {
			return name;
		}
		this.getValues = function() {
			return values;
		}

		this.getDocumentElement = function() {
			var spanE = document.createElement('span');
			var titleE = document.createTextNode(this.title);
			spanE.appendChild(titleE);

			for ( var i = 0; i < this.values.length; i++) {
				var id = getGroupName() + i;
				var radioE = Utils.DOM.createRadioButton(id, getGroupName(),
						this.values[i]);

				spanE.appendChild(radioE);
				spanE
						.appendChild(document
								.createTextNode(this.descriptions[i]));

				if (this.values[i] == defaultValue) {
					radioE.checked = true;
				}
			}

			return spanE;
		}

		this.getValue = function() {
			for ( var i = 0; i < this.values.length; i++) {
				var radioE = document.getElementById(getGroupName() + i);

				if (radioE && radioE.checked == true) {
					return radioE.value;
				}
			}
			return defaultValue;
		}

		function getGroupName() {
			return that.name + 'RadioGroup';
		}
	}

	this.getAdvancedOptionsFromXML = function(optionsXML) {
		return this.getOptionsFromXML(optionsXML, true);
	}

	this.getOptionsFromXML = function(optionsXML, advanced) {
		if (optionsXML == null) {
			return null;
		}
		
		var options = new Array();
		var optionsE = optionsXML.getElementsByTagName('options')[0];

		var i;
		for (i = 0; i < optionsE.childNodes.length; i++) {
			var optionE = optionsE.childNodes[i];

			if (optionE.nodeType == 1) {
				if (this.isAdvanced(optionE) && advanced) {
					options.push(this.getOptionFromElement(optionE));
				}

				if (!this.isAdvanced(optionE) && !advanced) {
					options.push(this.getOptionFromElement(optionE));
				}
			}
		}

		return options;
	}

	this.isAdvanced = function(optionE) {
		return "admin" == optionE.getAttribute("visibility");
	}

	this.getOptionFromElement = function(optionE) {
		var name = optionE.nodeName;

		if ("fixed-option" == name) {
			return this.createFixedFieldFromXML(optionE);
		} else if ("radio-option" == name) {
			return this.createRadioButtonsFromXML(optionE);
		}

		alert("Unsupported option type: " + name);
		return null;
	}

	this.createFixedFieldFromXML = function(fieldE) {
		var name = fieldE.getAttribute("name");
		var text = fieldE.getAttribute("text");
		var value = fieldE.getAttribute("value");
		return new Options.FixedField(name, text, value);
	}

	this.createRadioButtonsFromXML = function(radioE) {
		var radioOption = null;
		var name = radioE.getAttribute("name");
		var text = radioE.getAttribute("text");

		var choicesE = radioE.getElementsByTagName('choice');
		for (i = 0; i < choicesE.length; i++) {
			var choiceE = choicesE[i];
			var item = choiceE.getAttribute('item');
			var value = choiceE.getAttribute('value');

			if (radioOption == null) {
				radioOption = new Options.RadioButtons(name, value, text);
			}

			radioOption.addButton(item, value);
		}

		if (radioOption == null) {
			alert("Found radio option with no choices!");
		}
		
		return radioOption;
	}
}