/**
 * QuantizedIndicator
 *
 * A class for managing an indicator with distinct states.
 *
 * Dependencies:
 */

function QuantizedIndicator()
{
	var self = this;
	var itsElements = new Properties();
	var itsCurrentLabel = null;


	this.addElement = function(inElement)
	{
		itsElements.setProperty(inElement.label,inElement);
		return (self);
	};


	this.addElements = function(inLabels,inOnSuffix,inOnVisibleStyle,inOnInvisibleStyle,inOffSuffix,inOffVisibleStyle,inOffInvisibleStyle)
	{
		var i;

		if (inOffVisibleStyle === undefined)
			inOffVisibleStyle = inOnVisibleStyle;
		if (inOffInvisibleStyle === undefined)
			inOffInvisibleStyle = inOnInvisibleStyle;

		for (i = 0; i < inLabels.length; i++)
		{
			self.addElement({
				label: inLabels[i],
				onElement: document.getElementById(inLabels[i] + inOnSuffix),
				onVisibleStyle: inOnVisibleStyle,
				onInvisibleStyle: inOnInvisibleStyle,
				offElement: document.getElementById(inLabels[i] + inOffSuffix),
				offVisibleStyle: inOffVisibleStyle,
				offInvisibleStyle: inOffInvisibleStyle
			});
		}

		return (self);
	};


	this.select = function(inLabel)
	{
		var theElement;

		if (itsCurrentLabel != null)
			self.off(itsCurrentLabel);

		self.on(inLabel)
		itsCurrentLabel = inLabel;
	};


	this.on = function(inLabel)
	{
		var theElement;

		theElement = itsElements.getProperty(inLabel);
		if (theElement != null)
		{
			swapClassNames(theElement.offElement,theElement.offVisibleStyle,theElement.offInvisibleStyle);
			swapClassNames(theElement.onElement,theElement.onInvisibleStyle,theElement.onVisibleStyle);
		}

		return (self);
	};


	this.off = function(inLabel)
	{
		var theElement;

		theElement = itsElements.getProperty(inLabel);
		if (theElement != null)
		{
			swapClassNames(theElement.onElement,theElement.onVisibleStyle,theElement.onInvisibleStyle);
			swapClassNames(theElement.offElement,theElement.offInvisibleStyle,theElement.offVisibleStyle);
		}

		return (self);
	};


	function swapClassNames(inElement,inOldClassName,inNewClassName)
	{
		if (inElement != null)
			// Use delimiters to ensure that no substring replacements occur.
			inElement.className = (" " + inElement.className + " ").replace(" " + inOldClassName + " "," " + inNewClassName + " ");
	}
}
