/*
Requires:
	mootools 1.2 core
*/

var slideShow = new Class({

	initialize: function(params){
		this.items = document.getElementById('Imagebox').getElementsByClassName('image_container');
		this.mode = 'horizontal';
		this.modes = {horizontal:['left','width'], vertical:['top','height']};
		this.size = 900;
		this.box = $('Imagebox').setStyle(this.modes[this.mode][1],(this.size*this.items.length)+'px');
		this.button_event = 'click';
		this.handle_event = 'click';
		this.onWalk = null;
		this.currentIndex = null;
		this.previousIndex = null;
		this.nextIndex = null;
		this.interval = 5000;
		this.autoPlay = true;
		this._play = null;
		this.handles = null;
		if(this.handles){
			this.addHandleButtons(this.handles);
		}
		this.buttons = {
			previous: [],
			next: []
		};

		this.addActionButton('previous', $('prev'));
		this.addActionButton('next', $('next'));

this.fx = new Fx.Tween(this.box,$extend(({duration:900,transition: Fx.Transitions.Expo.easeInOut,wait:true}),{property:this.modes[this.mode][0]}));
		//this.walk(0,true,true); 
		//randomized start
		this.walk(parseInt(Math.random() * (this.items.length)),true,true);
	},

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

	addActionButton: function(action,button){
		//for(var i=0; i<buttons.length; i++){
			switch(action){
				case 'previous': button.addEvent(this.button_event,this.previous.bind(this,[true])); break;
				case 'next': button.addEvent(this.button_event,this.next.bind(this,[true])); break;
/*				case 'play': buttons[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'next',false])); break;
				case 'playback': buttons[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'previous',false])); break;
				case 'stop': buttons[i].addEvent(this.button_event,this.stop.bind(this)); break;*/
			//}
			this.buttons[action].push(button);
		}
	},

	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);
	},

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

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

	walk: function(item,manual,noFx){
		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);
			if(manual){
				this.stop();
			}
			if(noFx){
				this.fx.cancel().set((this.size*-this.currentIndex)+'px');
			}else{
				this.fx.start(this.size*-this.currentIndex);
			}
			if(manual && this.autoPlay){
				this.play(this.interval,'next',true);
			}
			if(this.onWalk){
				this.onWalk((this.items[this.currentIndex] || null), (this.handles && this.handles[this.currentIndex] ? this.handles[this.currentIndex] : null));
			}
		}
	}
	
});
