/* A class that implements some kind of tab switch facility.
 */

/* Constructor
 * bool selectedIndex: index of the active tab (default 0)
 * bool repeat: repeat at beginning/end when end/beginning has been reached (default true)
 */
function TabSwitch(selectedIndex, repeat) {
	this.selectedIndex = selectedIndex == null ? 0 : selectedIndex;
	this.repeat = repeat == null ? true : repeat;

	/* Events */
	this.onSelected = null;
	this.onUnSelected = null;
	this.onFirstSelected = null;
	this.onLastSelected = null;

	/* PRIVATE */
	this.items = [];
}
function TabSwitch_getLength(index) {
	return this.items.length;
}
function TabSwitch_getItem(index) {
	return this.items[index];
}
function TabSwitch_getSelectedItem() {
	return this.items[this.selectedIndex];
}
function TabSwitch_addItem(tabSwitchItem) {
	this.items[this.items.length] = tabSwitchItem;
}
function TabSwitch_select(index) {
	if (index < this.getLength()) {
		if (this.onUnSelected) this.onUnSelected(this);
		this.getSelectedItem().doOnUnSelected();
		this.selectedIndex = index;
		this.getSelectedItem().doOnSelected();
		if (this.onSelected) this.onSelected(this);
		if (index == 0 && this.onFirstSelected) this.onFirstSelected(this);
		if (index == this.getLength() && this.onLastSelected) this.onLastSelected(this);
	}
}
function TabSwitch_selectNext() {
	if (this.selectedIndex == this.getLength() - 2) {
		this.selectLast();
	}
	else if (this.selectedIndex >= this.getLength() - 1) {
		if (this.repeat) this.selectFirst();
	}
	else {
		this.select(this.selectedIndex + 1);
	}
}
function TabSwitch_selectPrevious() {
	if (this.selectedIndex == 1) {
		this.selectFirst();
	}
	else if (this.selectedIndex == 0) {
		if (this.repeat) this.selectLast();
	}
	else {
		this.select(this.selectedIndex - 1);
	}
}
function TabSwitch_selectFirst() {
	if (this.getLength() > 0) {
		this.select(0);
	}
}
function TabSwitch_selectLast() {
	if (this.getLength() > 0) {
		this.select(this.getLength() - 1);
	}
}
function TabSwitch_selectRandom() {
	if (this.getLength() > 1) {
		var randomIndex = this.selectedIndex;
		while (randomIndex == this.selectedIndex) {
			randomIndex = Math.round((this.getLength() - 1) * Math.random());
		}
		this.select(randomIndex);
	}
	else this.selectFirst;
}
/* Enables automatic switching.
 * int duration: interval between switch in milliseconds
 * int autoMode: one of the AUTOMODE constants (default AUTOMODE_NEXT)
 */
function TabSwitch_setAutomatic(duration, autoMode) {
	if (autoMode == null) autoMode = TabSwitch.AUTOMODE_NEXT;
	if (this.autoSwitcher) clearTimeout(this.autoSwitcher);
	if (duration > 0) {
		switch (autoMode) {
			// checking that autoSwitcher has already been set prevents switching directly after call
			case TabSwitch.AUTOMODE_NEXT:
				if (this.autoSwitcher) this.selectNext()
				break;
			case TabSwitch.AUTOMODE_PREVIOUS:
				if (this.autoSwitcher) this.selectPrevious()
				break;
			case TabSwitch.AUTOMODE_RANDOM:
				if (this.autoSwitcher) this.selectRandom()
				break;
		}
		this.autoSwitcher = setTimeout(TabSwitch.bindFunction(this.setAutomatic, this, duration, autoMode), duration);
	}
	else this.autoSwitcher = null;
}
function TabSwitch_argsToArray(args) {
  var arr = new Array();
  for (var i = 0; i < args.length; i++) arr[arr.length] = args[i];
  return arr;
}
function TabSwitch_bindFunction() {
  var args = TabSwitch.argsToArray(arguments), __method = args.shift(), object = args.shift();
  return function() {
    return __method.apply(object, args.concat(TabSwitch.argsToArray(arguments)));
  }
}
TabSwitch.AUTOMODE_NEXT = 0;
TabSwitch.AUTOMODE_PREVIOUS = 1;
TabSwitch.AUTOMODE_RANDOM = 2;
TabSwitch.argsToArray = TabSwitch_argsToArray;
TabSwitch.bindFunction = TabSwitch_bindFunction;
TabSwitch.prototype.addItem = TabSwitch_addItem;
TabSwitch.prototype.getItem = TabSwitch_getItem;
TabSwitch.prototype.getLength = TabSwitch_getLength;
TabSwitch.prototype.getSelectedItem = TabSwitch_getSelectedItem;
TabSwitch.prototype.select = TabSwitch_select;
TabSwitch.prototype.selectFirst = TabSwitch_selectFirst;
TabSwitch.prototype.selectLast = TabSwitch_selectLast;
TabSwitch.prototype.selectNext = TabSwitch_selectNext;
TabSwitch.prototype.selectPrevious = TabSwitch_selectPrevious;
TabSwitch.prototype.selectRandom = TabSwitch_selectRandom;
TabSwitch.prototype.setAutomatic = TabSwitch_setAutomatic;

/* A class that implements a tab within a TabSwitch.
 */
function TabSwitchItem(onSelected, onUnSelected) {
	this.onSelected = onSelected;
	this.onUnSelected = onUnSelected;
}
function TabSwitchItem_doOnSelected() {
	if (this.onSelected) this.onSelected(this);
}
function TabSwitchItem_doOnUnSelected() {
	if (this.onUnSelected) this.onUnSelected(this);
}
TabSwitchItem.prototype.doOnSelected = TabSwitchItem_doOnSelected;
TabSwitchItem.prototype.doOnUnSelected = TabSwitchItem_doOnUnSelected;
