/*
Requires:
	mootools 1.2 core
*/
var swSquares = new Class({

	initialize: function(params){
		this.items = params.items;
		// set all items to hidden
		this.items.setStyle('opacity', 0);
		this.items.setStyle('display', 'inline');
//		this.size = params.size || 240;
//		this.box = params.box.setStyle(this.modes[this.mode][1],(this.size*this.items.length)+'px');
		this.button_event = params.button_event || 'click';
		this.handle_event = params.handle_event || 'click';
		this.onWalk = params.onWalk || null;
		this.noRandomMove = params.noRandomMove || false;
		this.noFade = params.noFade || false;		
		this.currentIndex = null;
		this.previousIndex = null;
		this.nextIndex = null;
		this.interval = params.interval || 5000;
		this.autoPlay = params.autoPlay || false;
		this._play = null;
		this.handles = params.handles || null;
	
		this.s_width = params.s_width || 0;
		this.s_height = params.s_height || 0;
		this.s_rows = params.s_rows || 0;
		this.s_cols = params.s_cols || 0;
		this.prevx = 0;
		this.prevy = 0;

		if(this.handles){
			this.addHandleButtons(this.handles);
		}
		this.buttons = {
			previous: [],
			next: [],
			play: [],
			playback: [],
			stop: []
		};
		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}),{property:this.modes[this.mode][0]}));
		if (this.items.length > 0)
			this.walk((params.startItem||0),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]));
		}
	},

	addActionButtons: function(action,buttons){
		for(var i=0; i<buttons.length; i++){
			switch(action){
				case 'previous': buttons[i].addEvent(this.button_event,this.previous.bind(this,[true])); break;
				case 'next': buttons[i].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(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);
	},

	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){
			// cancel any existing transitions
			if (this.fx) {
				this.fx.cancel();
			}
			if (this.fx2) {
				this.fx2.cancel();
			}
			
if (!this.noFade) {
			// set all the other divs to 0 opacity (in case they were in the middle of a fade)
			for (var i=0; i<this.items.length; i++) {
				if (i != item && i != this.currentIndex)
					this.items[i].setStyle('opacity', 0);
			}
			prev_obj=null;
			cur_obj = this.items[item];
			if (this.currentIndex != null) {
				prev_obj = this.items[this.currentIndex];
				this.fx = new Fx.Morph(prev_obj, {duration:150,wait:false}).start({'opacity':0});
			}
}
else {
			prev_obj=null;
			cur_obj = this.items[item];
//	cur_obj.style.display = 'inline';
}
			if (!this.noRandomMove) {
				var ranx = Math.floor(Math.random() * this.s_cols);
				var rany = Math.floor(Math.random() * this.s_rows);
				while (ranx == this.prevx && rany == this.prevy) {
					ranx = Math.floor(Math.random() * this.s_cols);
					rany = Math.floor(Math.random() * this.s_rows);
				}
				this.prevx = ranx;
				this.prevy = rany;
				cur_obj.style.left = (ranx*this.s_width) + "px";
				cur_obj.style.top = (rany*this.s_height) + "px";
			}
			
if (!this.noFade) {
			this.fx2 = new Fx.Morph(cur_obj, {duration:150,wait:false}).start({'opacity':1});
			this.currentIndex=item;
}
else {
			this.fx2 = new Fx.Morph(cur_obj, {duration:0,wait:false}).start({'opacity':1});
			this.currentIndex=item;
}
			if(manual){
				this.stop();
			}
			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));
			}
		}
	}
	
});
