/*

		stickyButtonGroup.js
		
		Mootools-based class to provide "sticky" button functionality for a group of buttons
		
		Written by:		Ken Kolodziej
		Created on:		April 20, 2009
		
		USAGE:			Instantiate a new sticky button class, passing in element and image arrays
*/

var StickyButtons = new Class({
	options: {
		els: null,
		upURLs: null,
		downURLs: null,
		actions: null,
		startIndex: 0
	},
	initialize: function(options){
		this.setOptions(options);
		/*
		if(this.options.els.length != this.options.upURLs.length
		   && this.options.els.length != this.options.downURLs.length)
			throw('Array lengths do not match!');
		*/
		// Set up states
		this.states = new Array();
		this.images = new Array();
		
		for(var i = 0; i < this.options.els.length; i++) {
			if(this.options.els[i]) {
				this.options.els[i].addEvent('click', function(e) {
					var e = new Event(e);
					
					var theTarget = $(e.target);
					if(theTarget.tagName != 'div')
						theTarget = theTarget.getParent('div');
					
					var id = theTarget.id;
					
					this.toggleAll();
					this.executeAction(id);
				}.bind(this));
				
				this.states.push(0);
				
				this.images.push(new Element('img', {
					src: this.options.upURLs[i],
					border: 0
				}).inject(this.options.els[i]));
				
				if(i == this.options.startIndex)
					this.toggle(i);
			}
		}
	},
	toggle: function(index) {
		if(this.states[index] == 0) {
			if(this.options.downURLs[index])
				this.images[index].src = this.options.downURLs[index];
			this.states[index] = 1;
		}
		else {
			if(this.options.upURLs[index])
				this.images[index].src = this.options.upURLs[index];
			this.states[index] = 0;
		}
	},
	toggleAll: function() {
		for(var i = 0; i < this.options.els.length; i++)
			this.toggle(i);
	},
	executeAction: function(id) {
		for(var i = 0; i < this.options.els.length; i++) {
			if(this.options.els[i].id == id) {
				if(this.options.actions[i])
					this.options.actions[i]();
				return;
			}
		}
	}
});
StickyButtons.implement(new Options, new Events);