if (jQuery) (function($) {
	function fisherYates ( myArray ) {
		var i = myArray.length;
		if ( i == 0 ) return false;
		while ( --i ) {
			var j = Math.floor( Math.random() * ( i + 1 ) );
			var tempi = myArray[i];
			myArray[i] = myArray[j];
			myArray[j] = tempi;
		}
	}
	function installScrollable(o) {
		var $this = jQuery(this);
		var lastParent, ptr = this;
		while (ptr != null) {
			lastParent = ptr;
			ptr = ptr.parentNode;
		}
		var timerId, index = 0, hovering = false;
		var o = jQuery.extend(
			{
				speed:		600,
				itemSelector:	'.item',
				flashSelector:	'.FlashMovie',
				interval:	2000,
				startAt:	0,
				hovering:	true,
				animating:	true,
				nextAction:	'',
				prevAction:	'',
				randomize:	false,

				onBeforeItemOff:	function($container, $items, index) {
				},
				onAfterItemOff:		function($container, $items, index) {
				},
				onBeforeItemOn:		function($container, $items, index) {
				},
				onAfterItemOn:		function($container, $items, index) {
				}
			},
			o,
			$this.metadata()
		);
		var animating = o.animating, ignoreEvents = false;
		var $items = $this.find(o.itemSelector);
		if ($items.length == 0) return;
		var items = [];
		for (var i = 0; i < $items.length; i++) {
			items[items.length] = $items[i];
		}
		if (o.randomize) {
			fisherYates(items);
		}
		$items = jQuery(items);
		function installTimer() {
			if (!timerId && !hovering && animating) timerId = setTimeout(function() {
				timerId = null;
				setNext();
			}, o.interval);
		}
		function removeTimer() {
			if (!timerId) return;
			clearTimeout(timerId);
			timerId = null;
		}
		function change(changer) {
			var newParent;
			ptr = $this[0];
			while (ptr != null) {
				newParent = ptr;
				ptr = ptr.parentNode;
			}
			if (lastParent != null && newParent !== lastParent) return;
			lastParent = newParent;
			o.onBeforeItemOff($this, $items, index);
			ignoreEvents = true;
			$items.filter('.current').fadeTo(o.speed, 0, function() {
				if (o.flashSelector) jQuery(this).find(o.flashSelector).flashstatus(false);
				$items.filter('.current').hide();
				o.onAfterItemOff($this, $items, index);
				changer();
				$items.removeClass('current');
				//$current.unbind('mouseenter', over).unbind('mouseleave', out);;
				var $current = jQuery($items[index]);
				o.onBeforeItemOn($this, $items, index);
				$current.show();
				$current.fadeTo(o.speed, 1, function() {
					if (o.flashSelector) $current.find(o.flashSelector).flashstatus(true);
					o.onAfterItemOn($this, $items, index);
					$current.addClass('current').css('opacity', 1);
					//$current.bind('mouseenter', over).bind('mouseleave', out);
					ignoreEvents = false;
					installTimer();
				});
			});
		}
		function setNext() {
			change(function() {
				index++;
				if (index >= $items.length) index = 0;
			});
		}
		function setPrev() {
			change(function() {
				index--;
				if (index < 0) index = $items.length - 1;
			});
		}
		if (o.hovering) {
			function over() {
				$this.addClass('hover');
				removeTimer();
				hovering = true;
			}
			function out() {
				hovering = false;
				$this.removeClass('hover');
				installTimer();
			}
			$this.hover(over, out);
		}
		installTimer();
		$this.bind('animation-stop', function() {
			animating = false;
			removeTimer();
		});
		$this.bind('animation-start', function() {
			animating = true;
			installTimer();
		});
		$this.bind('jump-next', function() {
			if (ignoreEvents) return;
			animating = false;
			removeTimer();
			setNext();
		});
		$this.bind('jump-prev', function() {
			if (ignoreEvents) return;
			animating = false;
			removeTimer();
			setPrev();
		});
		if (o.nextAction && o.nextAction.length > 0) {
			$this.find(o.nextAction).click(function() {
				$this.trigger('jump-next');
			});
		}
		if (o.prevAction && o.nextAction.length > 0) {
			$this.find(o.prevAction).click(function() {
				$this.trigger('jump-prev');
			});
		}
		index = o.startAt;
		if (index < 0) index += $items.length;
		o.onBeforeItemOff($this, $items, index);
		$items.removeClass('current').css('opacity', index).hide();
		if (o.flashSelector) $items.find(o.flashSelector).flashstatus(false);
		o.onAfterItemOff($this, $items, index);
		var $first = jQuery($items[index]);
		o.onBeforeItemOn($this, $items, index);
		$first.addClass('current').css('opacity', 1).show();
		if (o.flashSelector) $first.find(o.flashSelector).flashstatus(true);
		//$first.bind('mouseenter', over).bind('mouseleave', out);
		o.onAfterItemOn($this, $items, index);
	}
	jQuery.fn.wsscrollable = function(o) {
		return this.each(function() {
			installScrollable.call(this, o);
		});
	};
})(jQuery);

