(function($) {
	$.fn.multipleElementsCycle = function(options){
		var defaults = {
			elementContainer: '#cycleElements',	// Selector for element (ul) container (selector)
			prevElement: '#cycleElementsLeft',	// Selector to scroll previous (selector)
			nextElement: '#cycleElementsRight', // Selector to scroll next (selector)
			speed: 500,							// Speed to scroll elements (int)
			containerWidth: false,				// Override default width (int with size)
			showCount: 4,						// Items to show from the list (int)
			overrideStart: false				// Override the start with a defined value (int)
		};
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {
			// GET ELEMENTS
			var totalElements = $(this).find("li");
			var maxIndex = totalElements.length - 1;
			
			// WORK OUT START INDEX
			// assumes that total elements is greater then the slice
			var lowerIndex = (options.overrideStart == false) ? Math.floor((maxIndex - options.showCount) / 2) : options.overrideStart;
			var elementWidth = $(this).find("li").outerWidth(true);
			var margin = ((lowerIndex) * elementWidth) * -1;
			var upperIndex = lowerIndex + options.showCount;
			var parent = $(this);
			var scrollIndex = 0;
			
			// HIDE ARROWS IF NONE
			if(upperIndex >= totalElements.length) {
				$(options.nextElement).hide();
			}
			if(lowerIndex <= 0) {
				$(options.prevElement).hide();
			}
			
			// SORT OUT STYLES
			$(this).find(options.elementContainer).css({
				'width': (options.containerWidth) ? options.containerWidth : elementWidth * options.showCount,
				'overflow': 'hidden'
			});
			$(this).find("ul").css({
				'width': (maxIndex + 1) * elementWidth,
				'padding': '0'
			});
			
			// INIT
			cycle("load");
	
			$("body").everyTime("7s", "rotate", function() {
				cycle("next", true);
			});
			// CLICK
			$(options.nextElement).click(function(){ 
				cycle("next");
				$("body").stopTime("rotate");
				return false; 
				
			});
			$(options.prevElement).click(function(){ 
				cycle("prev"); 
				$("body").stopTime("rotate");
				return false; 
			});	
			
			// CYCLE
			
			// @todo this needs to be refactored. It is less
			// then ideal!
			function cycle(dir, click){		
				switch(dir){
					case "next":
						if(upperIndex <= maxIndex) {
							$(options.prevElement).show();
							margin = margin - elementWidth;
							upperIndex = upperIndex + 1;
							lowerIndex = lowerIndex + 1;
							$("ul",parent).animate({
									marginLeft: margin
								},options.speed);
								
							if(upperIndex > maxIndex) {
								$(options.nextElement).hide();
							}
						}	
						if(click === true) {
							if(scrollIndex <= maxIndex) {
								$("#FeaturedContent li").addClass('hidden');
								$(".currentlyViewing").removeClass("currentlyViewing");
								$("ul li a", parent).eq(scrollIndex).addClass("currentlyViewing");
								$("#FeaturedContent li.banner"+$("ul li a", parent).eq(scrollIndex).attr("rel")).removeClass('hidden');	
								scrollIndex++;
							}
							else {
								// stop timer.
								$("body").stopTime("rotate");
								
								// go back to start
								$("ul",parent).animate({ marginLeft: 0 },options.speed);
								scrollIndex = 0;
								$("#FeaturedContent li").addClass('hidden');
								$(".currentlyViewing").removeClass("currentlyViewing");
								$("ul li a", parent).eq(scrollIndex).addClass("currentlyViewing");
								$("#FeaturedContent li.banner"+$("ul li a", parent).eq(scrollIndex).attr("rel")).removeClass('hidden');
							}
						}	
						break;	
					case "prev":
						if(lowerIndex >= 0) {
							$(options.nextElement).show();	
							upperIndex = upperIndex - 1;
							lowerIndex = lowerIndex - 1;
							margin = margin + elementWidth;
							$("ul",parent).animate({
								marginLeft: margin
							}, options.speed);
							if((lowerIndex-1) < 0) {
								$(options.prevElement).hide();
							}
						}
						break;
						
					case "load": {
						$("#FeaturedContent li").addClass('hidden');
						$("ul li a", parent).eq(scrollIndex).addClass("currentlyViewing");
						$("#FeaturedContent li.banner"+$("ul li a", parent).eq(scrollIndex).attr("rel")).removeClass('hidden');
						scrollIndex++;
						$("ul",parent).animate({
							marginLeft: margin
						}, options.speed);

						break;
					}
					default:
						break; 
				};													
			};
		}); 
	};
})(jQuery);



