(function($) {
		
	$.fn.highSlider = function () {
		
		 return this.each(function () {
			 
			var self = $(this);
			
			//vars
			var itemWidth = parseInt(self.find(".pictures ul li").css("width").replace("px", "")) + parseInt(self.find(".pictures ul li").css("margin-right").replace("px", "")), 
				numItems = self.find(".pictures ul li").length, 
				listWidth = numItems*itemWidth, 
				lastItemScroll = (numItems*itemWidth)-itemWidth, 
				index = 1, 
				time = 6000, 
				visibleItems = 1, 
				limit = (numItems-(Math.floor(numItems/visibleItems)-visibleItems)),
				timeout, 
				subtitle;
			
			function animateList(element, pixels, direction) {
				element.find(".pictures ul").animate({"left" : ((direction) ? direction + "=" : "") + pixels + "px"});
			}
			
			var cycle = function() {
				self.find(".right").trigger("click");
			}
			
			function handleTimeout() {
				clearTimeout(timeout);
				timeout = setTimeout(cycle, time);
			}
			
			function manageThumbClass(element) {
				element.parent().find("li").removeClass("current");
				element.addClass("current");
			}
			
			function manageSubtitles(index){
				self.find(".subtitles ul li").removeClass("active");
				self.find(".subtitles ul li:eq("+ index +")").addClass("active");
			}

			//initialize
			self.find(".pictures ul").css({"width" : listWidth});
			timeout = setTimeout(cycle, time);
			
			//thumbs click
			self.find(".thumbs ul li").click(function(){
				
				var element = $(this);
				index = element.index()+1;
				manageThumbClass(element);
				
				self.find(".pictures ul").animate({"left" : "-" + (itemWidth*$(this).index()) + "px"}, function(){
					//updateAmount(index, numItems);
					handleTimeout();
				});
			});
					
			self.find(".btn").click(function(){
				var option = $(this).attr("class");
				
				if (option == "btn right") {
					/* && index < limit*/
					if (index < numItems) {
						animateList(self, itemWidth, "-");
						manageSubtitles(index);
						index++;
					} else {
						animateList(self, 0, "");
						manageSubtitles(index-numItems);
						index = 1;
					}
						
				} else if (option == "btn left") {
					if (index > 1) {
						animateList(self, itemWidth, "+");
						manageSubtitles(index-2);
						index--;
					} else {
						animateList(self, lastItemScroll, "-");
						manageSubtitles(index+numItems-2);
						index = numItems;
					}
									
				}
				
				handleTimeout();
				
			});
		});	
		
	};
	
})(jQuery);
