/*
 * jquery.bmhSlider.js
 *
 */


(function($) {
	$.fn.bmhSlider = function(options) {


		var _interval 	= null;
		var _height 	= 0;
		var _max_height = 0;
		var _curr_index = 0;


		var cfg = {
			frequency: 25,
			direction: 'vertical',
			control: null
		};


		options = $.extend({},cfg, options);


		var parent = this;


		var _getMaxHeight = function(){
			return $(parent).height();
		};

		var _getSlideHeight = function(){
			return $('#'+$(parent).attr('id')+' > *').height();
		};


		var _setSlideShowButton = function(index) {
			if(options.control) {
				var that = $('#'+options.control+' li:nth-child('+index+')');
				$('#'+options.control+' li').css('background-position','left top');
				that.css('background-position','left -28px');		
			}
		};

		var _scrollSlide = function(){

			var curr_pos = Math.abs(parseInt($(parent).css('top').replace('px',''),10));

			var new_pos = curr_pos + _height;
			var new_index = (new_pos / _height)+1;

			if(new_pos >= _max_height) {
				$(parent).animate({'top':0},1500);
				_setSlideShowButton(1);
			} else {
				$(parent).animate({'top':'-'+new_pos+'px'});
				_setSlideShowButton(new_index);
			}
		};


		var _scrollSlideTo = function(pos){
			window.clearInterval(_interval);
			var new_pos = (pos * _height) -_height;
			if(new_pos < _max_height) {
				$(parent).animate({'top':'-'+new_pos+'px'});
			} 
			_interval = _setInterval();
		};

		var _setInterval = function(){
			return window.setInterval(function() { _scrollSlide(); }, options.frequency * 1000);
		};


		

		

		
		return this.each(function(){

			_height 	= _getSlideHeight();
			_max_height = _getMaxHeight();

			
			if(options.frequency > 0) {
				_interval = _setInterval();
			}

			if(options.control) {
				$('#'+options.control+' li').click(function(){
					$('#'+options.control+' li').css('background-position','left top');
					$(this).css('background-position','left -28px');
					_scrollSlideTo($(this).text());					
				});
			}

		});
	};
})(jQuery);
