/*
 * Simple Image Slider
 * Read more at: http://stuff.nekhbet.ro/2009/06/23/simple-image-gallery-navigation-slider-plugin-in-jquery.html
 * Version: 1.0.1
 * Copyright (c) 2009 Trimbitas Sorin-Iulian
 * Free of use (personal and commercial) as long as you keep this header in the file
 * Requires: jQuery v1.3+
*/
;(function($) {

	var totalCount = 0,selector,options,firstPos = 0,isRunning = false, step = 0, speed = "300";

	$.fn.simple_slider = function(settings) {
		settings = $.extend({}, $.fn.simple_slider.defaults, settings);
		selector = this.selector;
		options = settings;
		//get the number of images
		totalCount = $(selector + " img").size();
		//init
		_init();

		function _init(){
			//hide them all (with the exception of the first X images)
			$(selector + " img").each(function(i){
				if (i >= options.display){
					this.style.display = "none";
				}
			});
			//put actions (onclick) on the buttons for navigation
			//left
			$("#" + options.leftID).click(function (){
				if (isRunning == false){
					_goLeft();
				}
			});
			$("#" + options.leftID).hover(function (){
				$(this).addClass("simple_slider_hover");
				}, function (){
				$(this).removeClass("simple_slider_hover");
			});
			//right
			$("#" + options.rightID).click(function (){ 
				if (isRunning == false){
					_goRight();
				}
			});
			$("#" + options.rightID).hover(function (){
				$(this).addClass("simple_slider_hover");
				}, function (){
				$(this).removeClass("simple_slider_hover");
			});
			$("#" + options.leftID).addClass("simple_slider_disabled");
			_checkNavigation();
		}
		
		function _goLeft(){
			isRunning = true;
			var imgs = $(selector + " img");
			
			if (firstPos > 0){
				// 最多还可以移动几步
				var tmp = firstPos;
				// 计算本次移动几步
				step = (tmp > options.move) ? options.move : tmp;
				
				var tag = firstPos + options.display ;
				imgs.slice(tag - step, tag).fadeOut(speed, function(){
					firstPos --;
					imgs.eq(firstPos).fadeIn(speed, function(){
						isRunning = false;
						_checkNavigation();
					});
				});
				
			} else {
				isRunning = false;
			}
		}
		
		function _goRight(){
			isRunning = true;
			var imgs = $(selector + " img");
			
			if ((firstPos + options.display) < totalCount){
				// 最多还可以移动几步
				var tmp = totalCount - firstPos - options.display;
				// 计算本次移动几步
				step = (tmp > options.move) ? options.move : tmp;
				
				var tag = firstPos;
				imgs.slice(tag, tag + step).fadeOut(speed, function(){
					firstPos ++;
					imgs.eq(firstPos + options.display - 1).fadeIn(speed, function(){
						isRunning = false;
						_checkNavigation();
					});
				});
				
			} else {
				isRunning = false;
			}
		}
		
		function _checkNavigation(){
			//left
			if (firstPos == 0){
				$("#" + options.leftID).addClass("simple_slider_disabled");
			} else {
				$("#" + options.leftID).removeClass("simple_slider_disabled");
			}
			//right
			if ( (firstPos + options.display) >= totalCount){
				$("#" + options.rightID).addClass("simple_slider_disabled");
			} else {
				$("#" + options.rightID).removeClass("simple_slider_disabled");
			}
		}
		
	}

	$.fn.simple_slider.defaults = {
		display				:	2,
		leftID				:	null,
		rightID				:	null,
		move                :   3
	};

})(jQuery);
