/**
 * 
 * 
 * ---------------------------------------------------------------------------
 * 
 * Copyright (C) 2007 Omnium Research Group
 * 
 * ---------------------------------------------------------------------------
 * 
 * LICENSE:
 * 
 * This file is part of Omnium(R) Software.
 * 
 * Omnium(R) Software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Omnium(R) Software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Omnium(R) Software; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * ---------------------------------------------------------------------------
 * 
 * @author    Sam Bauers <sam@omnium.net.au>
 * @copyright 2007 Omnium Research Group
 * @license   http://www.gnu.org/licenses/gpl.txt GNU GPL v2
 * @link      http://open.omnium.net.au Omnium Open
 **/



// Nifty rollover class to toggle elements and their toggle links
omRollover = Class.create();
omRollover.prototype = {
	initialize: function(rolloverID, targetID, text, icon, containerID)
	{
		this.rolloverObject = $(rolloverID);
		this.targetID = targetID;
		this.containerObject = $(containerID);
		
		this.text = text;
		this.icon = icon;
		
		if (this.text) {
			if (this.text[0].substring(0, 1) != ".") {
				this.text[0] = '.' + this.text[0];
			}
			this.text[0] = this.rolloverObject.down(this.text[0]);
		}
		if (this.icon) {
			if (this.icon[0].substring(0, 1) != ".") {
				this.icon[0] = '.' + this.icon[0];
			}
			this.icon[0] = this.rolloverObject.down(this.icon[0]);
			if (!this.icon[3]) {
				this.setMouseover();
				this.setMouseout();
			}
		}
		
		this.state = 'off';
		
		this.setClick();
	},
	
	setMouseover: function()
	{
		Event.observe(this.rolloverObject, 'mouseover', this.swapImage.bind(this, 'mouseover'));
	},
	
	setMouseout: function()
	{
		Event.observe(this.rolloverObject, 'mouseout', this.swapImage.bind(this, 'mouseout'));
	},
	
	setClick: function()
	{
		Event.observe(this.rolloverObject, 'click', this.swap.bind(this));
	},
	
	swapImage: function()
	{
		if ($A(arguments)[0] == 'mouseover') {
			this.icon[0].src = this.icon[2];
		} else {
			this.icon[0].src = this.icon[1];
		}
	},
	
	swap: function()
	{
		if (this.icon) {
			icon2 = this.icon[2];
			icon1 = this.icon[1];
			this.icon[2] = icon1;
			this.icon[1] = icon2;
			this.icon[0].src = this.icon[2];
		}
		if (this.text) {
			if (this.state == 'off') {
				this.state = 'on';
				this.text[0].update(this.text[2]);
			} else {
				this.state = 'off';
				this.text[0].update(this.text[1]);
			}
		}
		if (this.containerObject) {
			if (this.containerObject.hasClassName('on')) {
				this.containerObject.removeClassName('on');
			} else {
				this.containerObject.addClassName('on');
			}
		}
		if (this.targetID) {
			Element.toggle(this.targetID);
		}
	}
}