/************************************************************************

History GIS MapServer

Ben Loh
Inquirium
ben@inquirium.net

Created 9/20/05


jsToggleButton.js

 ************************************************************************/



		
/*
 *	TOGGLE BUTTON FUNCTIONS
 */


//	General Functions
function ButtonToggle() {
	var self = this;			//	Needed for methods to be able to call each other
	this.myEl		= null;
	this.onMsg		= 'On';
	this.offMsg		= 'Off';
	this.state		= 'off'; 	// on | off | mouseover | pressed
	this.currentMsg	= this.offMsg;
	
	//	SETTERS
	this.SetElementById = function(id) {
		this.myEl = document.GetElementById( id );
		self.UpdateBtnMessage();
	}
	this.SetElement = function(el) {
		this.myEl = el;
		self.UpdateBtnMessage();
	}
	this.SetOnMsg = function(text) {
		this.onMsg = text;
	}
	this.SetOffMsg = function(text) {
		this.offMsg = text;
	}
	
	//	GETTERS
	this.IsOn = function() {
		if (this.state == 'on') {
			return true;
		} else {
			return false;
		}
	}
	
	//	MANAGEMENT METHODS
	this.ChangeBtnState = function(newState) {
		//	Generally, call this function to do all the changes for you
		self.SetState(newState);
		self.UpdateBtnMessage();
	}
	this.UpdateBtnMessage = function() {
		//	Sets the button text to the currentMsg
		if (this.myEl) {
			this.myEl.innerHTML = this.currentMsg;
		} else {
			alert( 'ButtonToggle.ShowBtnMessage: myEl is not defined while trying to set message ' + this.currentMsg );
		}
	}
	this.ToggleState = function() {
		//	Toggles the state between on and off
		if (this.state=='off') {
			self.SetState( 'on' );
		} else {
			self.SetState( 'off' );
		}
	}
	this.SetState = function(newState) {
		//	Sets the 'this.state' variable and updates the this.currentMsg var
		switch (newState) {
			case 'on':
				this.state = 'on';
				this.currentMsg = this.onMsg;
				break;
			case 'off':
				this.state = 'off';
				this.currentMsg = this.offMsg;				
				break;
		}
	}

	//	EVENTS
	this.DoAction = function() {
		//overridden by instance
	}
	
	this.Listener = function(e) {
		self.ToggleState();
		self.UpdateBtnMessage();
		self.DoAction();
	}

} // ButtonToggle()

