/**
 * @author pop webdev [cn]
 * @version: 0.8.
 * @classDescription: Content Tab Switcher
 * @dependencies: Prototype v1.6.1.
 */
// start TabSwitcher
var TabSwitcher = Class.create ({
	initialize: function(container,triggers,targets,options)
	{
        this.container = container;											// $ container for the entire widget
        this.triggers = triggers;											// $$ all the triggers
        this.targets = targets;												// $$ all the targets
		this.length = this.triggers.size();									// length of triggers
		this.container.writeAttribute('role', 'tablist');					// add aria role 'tablist' to container
		this.container.writeAttribute('aria-multiselectable', 'false');		// add aria-multiselectable attribute to container
		this.triggers.invoke('writeAttribute', 'role', 'tab');				// add aria role 'tab' to triggers
		this.triggers.invoke('writeAttribute', 'aria-selected', 'false');	// add aria-selected attribute to triggers
		this.targets.invoke('writeAttribute', 'role', 'tabpanel');			// add aria role 'tabpanel' to targets
		this.targets.invoke('writeAttribute', 'aria-expanded', 'false');	// add aria-expanded attribute to targets
		this.targets.invoke('writeAttribute', 'aria-hidden', 'true');		// add aria-hidden attribute to targets
		this.options = Object.extend({
			initialIndex: 0,												// index of initial item
			currentClassName: 'current',								// css class for current trigger
			disabledLinkClassName: 'disabled',								// css class for disabled prev/next links
			appearEffect: false,											// boolean, default(false) = 'show', override(true) = 'appear'
			speed: 0.5,														// animation speed, appearEffect must be true
			lnkPrev: false,													// $ element ID or false
			lnkNext: false,													// $ element ID or false
			autoRotate: false,												// boolean for auto rotation
			equalHeight: false												// boolean, todo?: accept user inputed value
		}, options || {});
		if (this.options.initialIndex >= this.length) {this.options.initialIndex = 0;}		// reset initial index if too high
		this.urlHash = window.location.hash.replace('#','') || false;						// get url hash
		// check url hash to override this.options.initialIndex
		if (this.urlHash) {
			for (var i=0; i<this.length; i++) {
				if (this.targets[i].id == this.urlHash) {
					this.options.initialIndex = i;
				}
			}
		}
		this.currentIndex = this.options.initialIndex;

		if (this.length == 1) {
			if (this.options.lnkPrev) {this.options.lnkPrev.addClassName(this.options.disabledLinkClassName);}		// disable prev link if only one item
			if (this.options.lnkNext) {this.options.lnkNext.addClassName(this.options.disabledLinkClassName);}		// disable next link if only one item
		}

		if (this.options.lnkPrev) {
			this.options.lnkPrev.observe('click', this.__ClickPrev.bindAsEventListener(this));
		}
		if (this.options.lnkNext) {
			this.options.lnkNext.observe('click', this.__ClickNext.bindAsEventListener(this));
		}

		if (this.options.equalHeight) {
			this.initHeight = 0;															// start with 0 height baseline
			this.topPad = parseInt(this.targets[0].getStyle('padding-top'));				// get first elements padding-top in pixels as a number
			this.botPad = parseInt(this.targets[0].getStyle('padding-bottom'));				// get first elements padding-bottom in pixels as a number
			this.setEqualHeight();
		}

		this.ShowHide(this.currentIndex);

		
		if (this.options.autoRotate) {
			if (this.urlHash) {return;} // don't rotate if url hash
			this.interval = setInterval(function(){
				this.rotateItems();
			}.bind(this), 3000); // show different item every 3 seconds
		}

		var boundLinkClick = this.__Click.bindAsEventListener(this);
		this.triggers.invoke('observe', 'click', boundLinkClick);

	},
	setEqualHeight: function()
	{
		for (var i=0; i<this.length; i++) {
			if (this.targets[i].getHeight() > this.initHeight) {
				this.initHeight = this.targets[i].getHeight();
			}
		}
		//this.initHeight = this.initHeight; // duh
		this.adjHeight = this.initHeight - (this.topPad + this.botPad); // remove vertical padding from height calculation
		this.targets.invoke('setStyle', {height: this.adjHeight + 'px'});
		this.targets[0].up().setStyle({height: this.initHeight + 'px'});
	},
	rotateItems: function()
	{
		this.currentIndex++;
		if (this.currentIndex == this.length) {this.currentIndex = 0;}
		this.ShowHide(this.currentIndex);
	},
	__ClickPrev: function(e)
	{
		e.stop();
		var el = e.findElement('a');
		if (!el.hasClassName(this.options.disabledLinkClassName)) {
			clearInterval(this.interval);
			if (this.currentIndex == 0) {this.currentIndex = this.length}
			this.currentIndex--;
			this.ShowHide(this.currentIndex);
			this.container.fire('tabswitcher:triggered', {index: this.currentIndex});
		}
	},
	__ClickNext: function(e)
	{
		e.stop();
		var el = e.findElement('a');
		if (!el.hasClassName(this.options.disabledLinkClassName)) {
			clearInterval(this.interval);
			this.currentIndex++;
			if (this.currentIndex == this.length) {this.currentIndex = 0}
			this.ShowHide(this.currentIndex);
			this.container.fire('tabswitcher:triggered', {index: this.currentIndex});
		}
	},
	__Click: function(e)
	{
		e.stop();
		var el = e.findElement('a');
		var index = this.triggers.indexOf(el);
		clearInterval(this.interval); // clear timer once link is clicked
		this.currentIndex = index;
		this.ShowHide(this.currentIndex);
		this.container.fire('tabswitcher:triggered', {index: this.currentIndex});
	},
	ShowHide: function(index)
	{
		for (var i=0; i<this.length; i++) {
			if (i == index) {
				this.triggers[i].up().addClassName(this.options.currentClassName);
				this.triggers[i].writeAttribute('aria-selected', 'true');
				if (this.options.appearEffect) {
					this.targets[i].appear({ duration: this.options.speed });
				} else {
					this.targets[i].show();
				}
				this.targets[i].writeAttribute('aria-expanded', 'true');
				this.targets[i].writeAttribute('aria-hidden', 'false');
			} else {
				this.triggers[i].up().removeClassName(this.options.currentClassName);
				this.triggers[i].writeAttribute('aria-selected', 'false');
				this.targets[i].hide();
				this.targets[i].writeAttribute('aria-expanded', 'false');
				this.targets[i].writeAttribute('aria-hidden', 'true');
			}
		}
		this.UpdateLinks(index);
	},
	UpdateLinks: function(index)
	{
		/* set 'disabled' class on prev/next links */
		if (this.options.lnkPrev) {
			if (index == 0) {
				this.options.lnkPrev.addClassName(this.options.disabledLinkClassName);
			} else {
				this.options.lnkPrev.removeClassName(this.options.disabledLinkClassName);
			}
		}
		if (this.options.lnkNext) {
			if (index == this.length - 1) {
				this.options.lnkNext.addClassName(this.options.disabledLinkClassName);
			} else {
				this.options.lnkNext.removeClassName(this.options.disabledLinkClassName);
			}
		}
	}
});
// end TabSwitcher

