/*
 * jQuery Slider - all-round fat-free slider
 *
 * 2010 Jakub Ferenc (jakubferenc.info)
 */

(function($) {

	$.fn.slider = function(options) {
		
		var options = $.extend({}, $.fn.slider.defaults, options);

		return this.each(function() {

			var $this = $(this);

			var temp = [];

			/*
				initialization
			*/
			var $container = $this.find(".ui-slider-container");
			var $list = $this.find(options.listName);

			temp["itemLen"] = parseInt($list.children().length);
			temp["itemW"] = parseFloat($list.children().filter(":first").width());

			var $btnBack = $this.find(options.btnBackSelector);
			var $btnNext = $this.find(options.btnNextSelector);
			
			var $actualIndex = 0;
			var $itemLen = temp["itemLen"];
			
			/*
				if the are more than one item in the step
				insert div.ui-slider-list-item as a container in each group of items divided by options.itemsInStep
				recount the length of items & the width of item
			*/
			if (options.itemsInStep != 1) {
				
				temp["set"] = $list.children();
				temp["itemLen"] = temp["set"].length;
				
				for (var i=0; i < temp["itemLen"]; i+=options.itemsInStep) {
				
					temp["set"].slice(i, i+options.itemsInStep).wrapAll('<div class="ui-slider-list-item"/>');
					
				}
				
				temp["itemLen"] = parseInt($list.children().length);
				temp["itemW"] = parseFloat($list.children().filter(":first").width());
			}
			
			
			if (options.hasNav == true) {
				
				var $nav = $this.find(options.navName);
				
				$nav.find("a").click(function(){
					
					if (!$list.is(":animated")) {
					
						$nav.find("." + options.navActiveClass).attr("class", "");
					
						$(this).addClass(options.navActiveClass);
						
						var index = parseInt($(this).attr("href").replace("#","")) - 1;

						goToIndex(index);
						
					}

					
					return false;
				
				});
			
			}
						
			
			
			/*
				set the width of main container
				set the width of list
				set the width of items
			*/
			$container.width(temp["itemW"]);
			$list.width(temp["itemLen"] * temp["itemW"]);
			$list.children().width(temp["itemW"]);			
			
			
			/*
				clean the temp
			*/
			delete i;
			
			
			/*
				button next handler
			*/			
			$btnNext.click(function() {

				
				if(isNext()) {
				
					next();
					
				} else {
				
					goToFirst();
				
				}
				
				return false;
			
			});
			
			
			/*
				button back handler
			*/				
			$btnBack.click(function() {
			
				if(isPrevious()) {
					
					back();
				} else {
				
					goToLast();
				
				}	
				
				return false;
	
			});
			
			

			
			/*
				private function
			*/			
			function next() {
				
				$list.not(":animated").animate({
				
					marginLeft: parseInt($list.css("margin-left")) - temp["itemW"]
					
				}, options.speed);
				
				$actualIndex++;
			
			};

			/*
				private function
			*/				
			function back() {
			
				$list.not(":animated").animate({
				
					marginLeft: parseInt($list.css("margin-left")) + temp["itemW"]
					
				}, options.speed);
				
				$actualIndex--;
				
			}
			
			
			function goToIndex(index) {
			
				if (index > $actualIndex && (index - $actualIndex) == 1) {
					
					next();
				
				} else if (index < $actualIndex && ($actualIndex - index) == 1) {
					
					back();
				
				} else if (index > $actualIndex) {
				
					temp["newMargin"] = (index - $actualIndex) * temp["itemW"];
					
					$list.not(":animated").animate({
					
						marginLeft: parseInt($list.css("margin-left")) - temp["newMargin"]
						
					}, options.speedFast);					
					
					$actualIndex = index;
					
				} else if (index < $actualIndex) {
				
					temp["newMargin"] = ($actualIndex - index) * temp["itemW"];
					
					$list.not(":animated").animate({
					
						marginLeft: parseInt($list.css("margin-left")) + temp["newMargin"]
						
					}, options.speedFast);					
					
					$actualIndex = index;
					
				}
				
				
			};
			
			/*
				private function
			*/				
			function goToFirst() {
			
				$list.not(":animated").animate({
				
					marginLeft: 0
					
				}, options.speedFast);			
			
				$actualIndex = 0;
			
			}
			
			/*
				private function
			*/		
			function goToLast() {
			
				$list.not(":animated").animate({
				
					marginLeft: parseInt($list.width() - temp["itemW"]) * -1
					
				}, options.speedFast);	

				$actualIndex = $itemLen - 1;
			
			}	
			
			/*
				private function
			*/	
			function isNext() {
			
				return (-1 * (parseInt($list.css("margin-left")) - temp["itemW"]) < $list.width()) ? true : false;
			
			}
			
			/*
				private function
			*/				
			function isPrevious() {
			
				return (parseInt($list.css("margin-left") + temp["itemW"]) < 0) ? true : false;
			
			}			

			
		});
		
	  
	}

	/*
		defaults
	*/		
	$.fn.slider.defaults = {
	
		btnBackSelector: ".ui-slider-back",
		btnNextSelector: ".ui-slider-next",
		listName: "ul",
		navName: ".ui-slider-nav",
		navActiveClass: "active",
		itemsInStep: 1,
		behaviour: {
			afterLastOne: "rewind"
		},
		speed: 1000,
		speedFast: 800
	};
	
	
})(jQuery);

