/* paneScroller jQuery Plugin   by: Jared Havican   requires: scrollTo plugin, jQuery ui.core */
(function($){
	$.ui = $.ui || {}; // create the namespace
	$.widget("ui.paneScroller", {
		/********************************************
		 * num = How many items to scroll
		 * duration = duration of scrolling animation 
		 ********************************************/
		next: function(num, duration){
			num=(num==null)?this.getData('numToScroll'):num;
			this.gotoItem(this.getData('currentItem')+num, duration);
		},
		/********************************************
		 * num = How many items to scroll
		 * duration = duration of scrolling animation 
		 ********************************************/
		prev: function(num, duration){
			num=(num==null)?this.getData('numToScroll'):num;
			this.gotoItem(this.getData('currentItem')-num, duration);
		},
		/********************************************
		 * num = Index of item to scroll to
		 * duration = duration of scrolling animation 
		 ********************************************/
		gotoItem: function(num, duration){
			if (!this.getData('enabled')) return;
			if (num>this.getData('lastToScrollTo')){
				num = this.getData('lastToScrollTo');
			} else if (num<1) {
				num=1;
			}
			if (this.getData('currentItem')==num)
				return;
			var numToScroll = this.getData('numToScroll');
			var toScrollTo=this.element.find(this.getData('items')).eq(num-1);
			if (duration==null){
				var duration = this.getData('scrollDuration');
				duration = duration * Math.abs(this.getData('currentItem')-num)/this.getData('numToScroll');
			}
			this.element.scrollTo(toScrollTo, duration,{
				axis: this.getData('axis')
			});
			this.setData('currentItem', num);
			var change = this.getData('change');
			if ($.isFunction(change))change(this.getData('currentItem'), this.getData('lastToScrollTo'));
		},
		init: function(){
			var totalElements = this.element.find(this.getData('items')).length;
			var lastToScrollTo = totalElements - this.getData('perPane')+1;
			this.setData('lastToScrollTo', lastToScrollTo);
			this.setData('totalElements', totalElements);
			this.setData('enabled', totalElements>this.getData('perPane'));
			this.gotoItem(1,0);
		}
		
	});
	$.ui.paneScroller.defaults = {
		items: 'tr', // Selector for items to scroll to
		axis: 'y', // Axis to scroll on
		numToScroll: 4, // Default number of items to scroll
		perPane: 5, // Number of items that fit in a pane
		scrollDuration: 300 // Duration of scrolling animation
	};
})(jQuery);