/**
 * Description goes here.
 * @class   RotatingTout
 * @author  Neal Mckinney
 * @date    28.08.2011
 * @version 0.1
**/

function RotatingTout(target) {
	this.target = target;
	this.index = 0;
	this.initObjects();
	//this.initEvents();
}

RotatingTout.prototype.toString = function() {
	return "RotatingTout";
}

// INITIALIZATION =======================================================================================================

RotatingTout.prototype.initObjects = function() {
	this.touts = this.target.getElementsBySelector("div.content");
	var len = this.touts.length;
	for (var i=1; i < len; i++) {
		this.touts[i].hide();
	};
	
	if (len > 1) {
		
		this.nextArrow = new Element("div");
		this.nextArrow.addClassName("arrow");
		this.nextArrow.addClassName("next");
		this.target.insert(this.nextArrow);
		
		this.prevArrow = new Element("div");
		this.prevArrow.addClassName("arrow");
		this.prevArrow.addClassName("prev");
		this.target.insert(this.prevArrow);
		
		//this.nextArrow = this.target.getElementsBySelector("div#toutNext")[0];
		//this.prevArrow = this.target.getElementsBySelector("div#toutPrev")[0];
		this.initEvents();
		this.invalidateArrows();
	}
}

RotatingTout.prototype.initEvents = function() {
	this.nextArrow.observe("click", this.onNextClick.bind(this));
	this.prevArrow.observe("click", this.onPrevClick.bind(this));
}

// ACTIONS ==============================================================================================================

RotatingTout.prototype.showIndex = function(dir) {
	this.touts[this.index].fade({duration:.25});
	this.index += dir;
	this.touts[this.index].appear({duration:.5, delay:.25});
	this.invalidateArrows();
};

RotatingTout.prototype.invalidateArrows = function() {
	
	if (this.index == 0) {
		this.disableElement(this.prevArrow);
	} else {
		this.enableElement(this.prevArrow);
	}
	
	if (this.index == this.touts.length-1) {
		this.disableElement(this.nextArrow);
	} else {
		this.enableElement(this.nextArrow);
	}
};

RotatingTout.prototype.disableElement = function(element) {
	if (!element.hasClassName("disabled")) element.addClassName("disabled");
};

RotatingTout.prototype.enableElement = function(element) {
	if (element.hasClassName("disabled")) element.removeClassName("disabled");
};



// EVENTS ===============================================================================================================

RotatingTout.prototype.onNextClick = function(e) {
	if (this.index < this.touts.length-1) this.showIndex(1);
};

RotatingTout.prototype.onPrevClick = function(e) {
	if (this.index > 0) this.showIndex(-1);
};



