
if (typeof Effect.ScrollHorizontal == 'undefined') {
	throw("gallery.js requires including script.aculo.us' elementscroller.js library!");
}

var myUtils	= {

	swapClass:	function(element, class1, class2) {
		element.removeClassName(class1);
		element.addClassName(class2);
	}

}

Element.addMethods(myUtils);

var slideShow = Class.create();
slideShow.prototype = {

	oldItem			: null,
	options 		: null,
	listItem		: [],
	listToggle		: [],
	currentIndex	: 0,
	auto			: null ,

	initialize: function(container, options) {

		if (!$(container)) {
			throw(container+" doesn't exist!");
			return false;
		}

		this.options = Object.extend({
			container		: false,
			delay			: 0,
			duration		: 0.20,
			toggle			: false,
			toggleOff		: false,
			previous		: false,
			next			: false,
			item			: 'slideshow',
			onEvent 		: 'click',
			move			: 'horizontal',
			defaultView		: 0,
			auto			: false,
			delayAuto		: 3,
			direction		: 1,
			onStop			: 'mouseover',
			onRestart		: 'mouseout',
			callBack		: null

		}, options || {});

		if(this.options.container == false) {
			this.options.container	= container;
		}

		this.listItem	= $$('#' + this.options.container + ' .' + this.options.item);

		if(this.options.toggle) {
			this.listToggle	= $$('#' + container + ' .' + this.options.toggle);

			if(this.listToggle.length != this.listItem.length) {
				throw Error('Number of toggle/item does not match! toggle : ' + this.listToggle.length + ' - item : ' + this.listItem.length);
				return false;
			}

			for(var i = 0; i < this.listToggle.length; i++) {
				Event.observe(this.listToggle[i], this.options.onEvent, this.moveTo.bind(this, i));
				this.listToggle[i].style.cursor = 'pointer';
				this.listToggle[i].swapClass(this.options.toggle, this.options.toggleOff);

				if (this.options.onEvent == 'click') {
				  this.listToggle[i].onclick	= function() {return false;};
				}
			}

		} else if(this.options.previous && this.options.next) {

			previous	= $$('#' + container + ' .' + this.options.previous)[0];
			next		= $$('#' + container + ' .' + this.options.next)[0];

			Event.observe(previous, this.options.onEvent, this.moveTo.bind(this, -1));
			Event.observe(next, this.options.onEvent, this.moveTo.bind(this, 1));

			if (this.options.onEvent == 'click') {
				  previous.onclick	= function() {return false;};
				  next.onclick 		= function() {return false;};
			}

		} else {
		}

		if(this.options.auto == true) {
			Event.observe(container, this.options.onStop, this.stop.bind(this));
			Event.observe(container, this.options.onRestart, this.start.bind(this));
		}

		this.moveTo(this.options.defaultView);
		this.currentIndex	= this.options.defaultView;

		if(this.options.auto == true) {
			this.auto	= new PeriodicalExecuter(this.autoMove.bind(this), this.options.delayAuto);
		}

	},

	start: function(index) {
		if(this.options.auto == true) {
			this.auto	= new PeriodicalExecuter(this.autoMove.bind(this), this.options.delayAuto);
		}
	},

	stop: function(index) {
		if(this.options.auto == true) {
			this.auto.stop();
		}
	},

	autoMove: function(index) {
		index	= this.currentIndex;

		if(index == 0) {
			this.options.direction	= 1;
		} else if(index == this.listItem.length-1) {
			this.options.direction	= -1;
		}

		index	= this.currentIndex + this.options.direction;

		this.moveTo(index);

	},

	moveTo: function(step) {

		index	= step;
/*
		if(this.options.auto == true) {
			index	= this.currentIndex + this.options.direction;

			if(index == 0) {
				this.options.direction	= 1;
			} else if(index == this.listItem.length-1) {
				this.options.direction	= -1;
			}
		} else
*/

		if(this.options.previous && this.options.next) {

			index	= this.currentIndex + step;

			if(index < 0 || index == this.listItem.length-1) {
				index	=  index - step;
			}

			this.options.direction	= step;

		}
//		alert(this.listItem.length);
		if(this.listItem[index].positionedOffset().left < this.listItem[this.currentIndex].positionedOffset().left) {
//					alert('aaa');

//				transition: Effect.Transitions.sinoidal,
			new Effect.ScrollHorizontal(this.options.container, {
				queue: 'end', duration: this.options.duration, from: this.listItem[this.currentIndex].positionedOffset().left, to: this.listItem[index].positionedOffset().left
			});

		} else {
//			alert(this.currentIndex + ' - ' + index);
//					alert('bbb');
			new Effect.ScrollHorizontal(this.options.container, {
				queue: 'end', duration: this.options.duration, from: this.listItem[this.currentIndex].positionedOffset().left, to: this.listItem[index].positionedOffset().left});
		}

		if(this.options.toggle) {
			this.listToggle[this.currentIndex].swapClass(this.options.toggle, this.options.toggleOff);
			this.listToggle[index].swapClass(this.options.toggleOff, this.options.toggle);
		}

		if(this.options.auto == false) {
			if(this.currentIndex < index) {
				this.options.direction	= 1;
			} else {
				this.options.direction	= -1;
			}
		}

		if(this.currentIndex != index) {
			this.oldItem	= this.currentIndex;
		}

		this.currentIndex	= index;

		if (this.options.callBack){
			this.options.callBack(this);
		}
	}

}

