/**
 * Goweb Slider
 * 
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 * 
 */

if(typeof(Prototype) == "undefined") {
    throw "Goweb.Scroller requires Prototype to be loaded."; }


if(typeof(Goweb) == "undefined") {
	var Goweb = Class.create();
}

Goweb.Slider = Class.create({
	initialize: function(name, options) {
		this.name = name;
		this.options = {
				currentItem: 0,
				canRender: 1,
				duration: 3,
				auto: 1,
				interval: 1,
				firstElement: 0,
				direction: 1,
				size: 564
		};
		Object.extend(this.options, options || {});
		this.options.currentItem = $$('#'+this.name+' .slide_content')[0].firstDescendant();
		this.options.currentItem.style.zIndex = '60';
		this.renderElement(this.options.currentItem);

	}, renderElement: function(elemento){
		if(this.options.canRender){
			if(this.options.firstElement){
				if(this.options.direction){
					xis = this.options.size;
				} else {
					xis = -1*this.options.size;
				}
			} else {
				xis = 0;
			}
			if (this.options.auto){
				this.options.canRender = 0;
			}
			
			elemento.style.zIndex = '15';
			this.options.nextElement = elemento;
			new Effect.Move(this.options.currentItem, {
				x: xis, 
				y: 0,
				mode: '',
				transition: Effect.Transitions.sinoidal,
				afterFinish: this.afterFinishFunction.bind(this)
			});
		}
	}, previousElement: function(){
		this.options.direction = 0;
		this.options.firstElement = 1;
		if (this.options.currentItem.previous()) {
			this.renderElement(this.options.currentItem.previous());
		} else {
			this.renderElement($$('#'+this.name+' .slide_content')[0].lastDescendant());
		}
	}, nextElement: function(){
		this.options.direction = 1;
		this.options.firstElement = 1;
		if (this.options.currentItem.next()) {
			this.renderElement(this.options.currentItem.next());
		} else {
			this.renderElement($$('#'+this.name+' .slide_content')[0].firstDescendant());
		}
	},
	afterFinishFunction: function(effect) {
		effect.element.setStyle({ top: '0px', left: '0px'});
		elemento = this.options.currentItem = this.options.nextElement;
		
		$$('#'+this.name+' .item').each(function(element) {
			if(elemento==element){
				elemento.setStyle('zIndex', '20');
			}else{
				element.setStyle('zIndex', '10');
			}
		});
		if (this.options.auto){
			clearInterval(this.options.interval);
			var self = this;
			this.options.interval = window.setTimeout(function(){ self.nextElement(); }, this.options.duration*1000);
			this.options.canRender = 1;
		}
	}
});
