var Tube = new Class({

	initialize: function(params) {
		this.box = params.box;
		this.items = params.items;
		this.size = params.size || 576;
		this.currentIndex = null;
		this.previousIndex = null;
		this.nextIndex = null;
		this._play = null;
		this.interval = params.interval || 8000;
		this.handles = params.handles || null;
		this.running = false;
		if(this.handles){
			this.addHandleButtons(this.handles);
		}
		this.buttons = {
				previous: [],
				next: [],
				play: [],
				stop: [],
				toggle: []
		};	
		if(params.addButtons){
			for(var action in params.addButtons){
				this.addActionButtons(action, $type(params.addButtons[action])=='array' ? params.addButtons[action] : [params.addButtons[action]]);
			}
		}	
		this.fx = new Fx.Tween(this.box,$extend((params.fxOptions||{duration:500,wait:false,transition:Fx.Transitions.Quad.easeInOut}),{property:"left"}));
		this.walk(0,true);
	},

	addHandleButtons: function(handles){
		for(var i=0;i<handles.length;i++){
			handles[i].addEvent('click',this.walk.bind(this,[i,true]));
		}
	},

	addActionButtons: function(action,buttons){
		for(var i=0; i<buttons.length; i++){
			switch(action){
				case 'previous': buttons[i].addEvent('click',this.previous.bind(this,[true])); break;
				case 'next': buttons[i].addEvent('click',this.next.bind(this,[true])); break;
				case 'play': buttons[i].addEvent('click',this.play.bind(this,[this.interval,false])); break;
				case 'stop': buttons[i].addEvent('click',this.stop.bind(this)); break;
				case 'toggle': buttons[i].addEvent('click',this.toggle.bind(this)); break;
			}
			this.buttons[action].push(buttons[i]);
		}
	},
	
	previous: function(manual){
		this.walk((this.currentIndex>0 ? this.currentIndex-1 : this.items.length-1),manual);
	},

	next: function(manual){
		this.walk((this.currentIndex<this.items.length-1 ? this.currentIndex+1 : 0),manual);
	},

	stop: function(){
		$clear(this._play);
		this.running = false;
	},

	toggle: function(){
		if (this.running) {
			$clear(this._play);
			this.running = false;
		} else {
			this.running = true;
			this.walk((this.currentIndex<this.items.length-1 ? this.currentIndex+1 : 0),true);
		}
	},

	play: function(interval,wait){
		this.stop();
		if(!wait){
			this.next(false);
		}
		this.running = true;
		this._play = this.next.periodical(interval,this,[false]);
	},

	walk: function(item, manual){
		if(item!=this.currentIndex){
			this.currentIndex=item;
			this.previousIndex = this.currentIndex + (this.currentIndex>0 ? -1 : this.items.length-1);
			this.nextIndex = this.currentIndex + (this.currentIndex<this.items.length-1 ? 1 : 1-this.items.length);
			
			// Highlight the current index
			for(var i=0;i<this.handles.length;i++){
				var node = this.handles[i];
				if (i == this.currentIndex) {
					node.store('active', true);
					node.setAttribute('src',node.retrieve('origSrc').replace('/tin', '/tac')); 
				} else {
					node.store('active', false);
					node.setAttribute('src',node.retrieve('origSrc')); 
				}
			}
			
			if (manual) {
				this.stop();
			}
			this.fx.start(this.size*-this.currentIndex);
			if (manual) {
				this.play(this.interval,true);
			}
		}
	}
});

var tube;

window.addEvent('domready',function(){
    var preLoadArray = new Array(); 
    var preLoadNum = 0; 

	$$('#tube img').each(function(el){ 
        // Preloader 
        preLoadArray[preLoadNum] = new Image(); 
        preLoadArray[preLoadNum].src = el.src.replace('/tin', '/tac'); 
        preLoadNum++;
        el.store('origSrc', el.src);
        el.addEvent('mouseover',function(){ 
            this.setAttribute('src',el.retrieve('origSrc').replace('/tin', '/tac')); 
        }); 
        el.addEvent('mouseout',function(){ 
        	if (!el.retrieve('active')) {
        		this.setAttribute('src',el.retrieve('origSrc')); 
        	}
        }); 
    });  

	tube = new Tube({
		box: $('tubeitems'),
		items: [0,1,2,3],
		size: 576,
		handles: $$('#tubenumbers img'),
		addButtons: {
			previous: $('tubeprev'),
			next: $('tubenext')
		}
	});

});
