/*\
|*|  GroupButton
|*|
|*|  Copyright (c) 1999 Dolphin Inc.  All rights reserved.
|*|  Dave Sugar 12/9/99
|*|
|*|  Use:
|*|    This file defines two JavaScript classes; GroupButton and ButtonGroup.
|*|    The names have been created to be close to each other to help confuse
|*|    the developer.  The GroupButton is a sub-class of the PushButton class
|*|    (required, see push_button.js).  The ButtonGroup is used to define a
|*|    group of GroupButtons.  When a ButtonGroup is define only one of the
|*|    buttons in the group can be enabled at once.  The last button clicked
|*|    will automatically stay selected.  These classes are designed to be 
|*|    used when you have a menu system where one item needs to be selected
|*|    at all times.
|*|
|*|    The ButtonGroup class will take care of setting the selection of the 
|*|    correct button.  If you want certain events to happen when each button
|*|    is pressed you will need to the 'onClick' function when creating the
|*|    GroupButton.
|*|
|*|    See PushButton documentation for all the optional parameters available
|*|    for GroupButton
|*|
|*|  Base Class:
|*|    PushButton (push_button.js)
|*|
|*|  Example 1:
|*|    var btn1 = new GroupButton (document, 'images/blah1.gif');
|*|    var btn2 = new GroupButton (document, 'images/blah2.gif');
|*|    var btn3 = new GroupButton (document, 'images/blah3.gif');
|*|    
|*|    var btnController = new ButtonGroup (btn1, btn2, btn3);
|*|
\*/

/***********************************
	Class: RollGroupButton

	Desc: Subclass of GroupButton. It has a rollover state when selected
	It will look for a graphic with an _rs in the filename.
	ex: blah1_rs.gif
	
************************************/

function GroupButton (i_Doc, i_szBasePath)
{

	// show the rollover if the button is enabled AND the image exists
	GroupButton.prototype.over = function (i_bMouseOver)
	{
		var a_szFuncName = ["onmouseout", "onmouseover"];
		if (this.m_bEnable && !this.m_bPress && this.m_bInside != i_bMouseOver)
		{
			this.m_bInside = i_bMouseOver;
			this.updateImage ();
			this.execParam (a_szFuncName[i_bMouseOver ? 1 : 0]);
		}
	};

	// called when the button is pressed or released
	GroupButton.prototype.press = function (i_bPressed)
	{
		var a_szFuncName = ["onmouseup", "onmousedown"];
		if (this.m_bEnable && this.m_bPress != i_bPressed)
		{
			this.m_bPress = i_bPressed;
			this.m_bInside = i_bPressed;
			this.updateImage ();
			if (i_bPressed && this.m_pController != null)
				this.m_pController.clicked (this);
			this.execParam (a_szFuncName[i_bPressed ? 1 : 0]);
		}
	};

	// called when the button is clicked
	GroupButton.prototype.click = function ()
	{
		if (this.m_bEnable)
		{
			this.m_bPress = true;
			this.updateImage ();
			if (this.m_pController != null)
				this.m_pController.clicked (this);
			this.execParam ("onclick");
		}

		var szHref = this.getParam ("href");
		return (this.m_bEnable && szHref != "");
	};

	GroupButton.prototype.setController = function (i_pController)
	{
		this.m_pController = i_pController;
	};

	
	// current state
	this.m_bEnable = true;
	this.m_bPress = false;
	this.m_bInside = false;


		// any sub-classes need to do this stuff to see anything!
	// call if there is an image path given!
	if (i_szBasePath != "")
	{

  	this.m_aImages = new Array (4);
  	this.m_aParams = new Object ();
  
  	this.m_document = i_Doc;
  	this.m_szBasePath = i_szBasePath;
  	this.m_pController = null;
  	
  	this.parseParams (arguments, 2);
  	this.buildAnchor ();
	}
};

GroupButton.prototype = new PushButton ("", "");


function ButtonGroup ()
{
	ButtonGroup.prototype.clicked = function (i_pBtn)
	{
		for (var i = 0; i < this.m_btns.length; i++)
		{
			if (this.m_btns[i] != i_pBtn)
				this.m_btns[i].press (false);
		}
	};

	ButtonGroup.prototype.select = function (i_nSelect)
	{
		if (this.m_nSelection == i_nSelect)
			return;
			
		if (this.m_nSelection >= 0)
			this.m_btns[this.m_nSelection].press (false);

		this.m_nSelection = i_nSelect;
		if (i_nSelect >= 0 && i_nSelect < this.m_btns.length)
		{
			this.m_btns[i_nSelect].press(true);
		}
	};

	ButtonGroup.prototype.getSelection = function ()
	{
		return (this.m_nSelection);
	};

	this.m_nSelection = -1;

	// store pointers to all the buttons begin controlled
	var nBtns = arguments.length;
	this.m_btns = new Array (nBtns);
	for (var i = 0; i < nBtns; i++)
	{
		this.m_btns[i] = arguments[i];
		this.m_btns[i].setController (this);
	}
};

function RollGroupButton(i_Doc, i_szBasePath)
{
	// show the rollover if the button is enabled AND the image exists
	RollGroupButton.prototype.over = function (i_bMouseOver)
	{
		var a_szFuncName = ["onmouseout", "onmouseover"];
		if (this.m_bEnable && this.m_bInside != i_bMouseOver)
		{
			this.m_bInside = i_bMouseOver;
			var bOldPress = this.m_bPress;
			
			if(this.m_bPress)
			{
				//if pressed then set new rollover image
				this.set_img_over(this.m_szBasePath.replace(/(\.gif|\.jpg|\.png)/, ("_" + this.m_sRolloverExt + "$1")));  
			}
			else
				this.set_img_over(this.m_szBasePath.replace(/(\.gif|\.jpg|\.png)/, ("_" + this.m_aXtn[this.IMG_OVER] + "$1")));
			
			//temporarily set press to false so that rollover happens
			if(i_bMouseOver)
			{
				//alert("set press to false");
				this.m_bPress = false;
			}
			this.updateImage ();
			this.m_bPress = bOldPress;
			this.execParam (a_szFuncName[i_bMouseOver ? 1 : 0]);
		}
	};

	// called when the button is pressed or released
	RollGroupButton.prototype.press = function (i_bPressed)
	{
		var a_szFuncName = ["onmouseup", "onmousedown"];
		if (this.m_bEnable && this.m_bPress != i_bPressed)
		{
		
			if(i_bPressed)
			{
				//if pressed then set new rollover image
				this.set_img_over(this.m_szBasePath.replace(/(\.gif|\.jpg|\.png)/, ("_" + this.m_sRolloverExt + "$1")));  
			}
			else
				this.set_img_over(this.m_szBasePath.replace(/(\.gif|\.jpg|\.png)/, ("_" + this.m_aXtn[this.IMG_OVER] + "$1")));
			
			
			this.m_bPress = i_bPressed;
			//in order for the rollover to work the this can't be set
			//this.m_bInside = i_bPressed;
			this.updateImage ();
			if (i_bPressed && this.m_pController != null)
				this.m_pController.clicked (this);
			this.execParam (a_szFuncName[i_bPressed ? 1 : 0]);
		}
	};

	this.m_sRolloverExt = "rs";
	
		// current state
	this.m_bEnable = true;
	this.m_bPress = false;
	this.m_bInside = false;

	// any sub-classes need to do this stuff to see anything!
	// call if there is an image path given!
	if (i_szBasePath != "")
	{
  	this.m_aImages = new Array (4);
  	this.m_aParams = new Object ();
  
  	this.m_document = i_Doc;
  	this.m_szBasePath = i_szBasePath;
  	this.m_pController = null;
  	
  	this.parseParams (arguments, 2);
  	this.buildAnchor ();
	}
};

RollGroupButton.prototype = new GroupButton ("", "");
