/*
 *	markup example for $("#loopedCarousel").loopedCarousel();
 *
 *	<div id="loopedCarousel">	
 *		<div class="container">
 *			<div class="slides">
 *				<div>1</div>
 *				<div>2</div>
 *				<div>3</div>
 *				<div>4</div>
 *				<div>5</div>
 *				<div>6</div>
 *				<div>7</div>
 *				<div>8</div>
 *				<div>9</div>
 *				<div>10</div>
 *				<div>11</div>
 *				<div>12</div>
 *				<div>13</div>
 *				<div>14</div>
 *				<div>15</div>
 *			</div>
 *		</div>
 *		<a href="#" class="previous">previous</a>
 *		<a href="#" class="next">next</a>	
 *	</div>
 *
 */

var currentLeftElement = 0;

(function($) {
	$.fn.loopedCarousel = function(options) {
		
	var defaults = {			
		container: '.container',
		slides: '.slides',
		slidespeed: 300, // Speed of slide animation
		items: 5, // Items show
		padding: 10 // Padding between items
	};
		
	this.each(function() {	
		var obj = $(this);
		var o = $.extend(defaults, options);
		var itemsPerPage = o.items; //i
		var m = 0;
		var t = 1;
		var numberOfElements = $(o.slides,obj).children().size(); //s
		var width = $(o.slides,obj).children().outerWidth()+o.padding; //w
		var c = Math.ceil($(o.slides,obj).children().size()/itemsPerPage);
		var pd = o.padding/2;
		var p = 0;
		var u = false;
		var n = 0;
		var pt = 0;
		var os = itemsPerPage*c-numberOfElements;
		var params = {}; 
		var startElement = $.cookie("fitnessplus_slider_pos", { path: '/' });

		//check if startElement is in range of numberOfElements
		if(startElement > (numberOfElements - 1)){
			startElement = numberOfElements - 1;
		}

		$(o.container,obj).css({width:(width*itemsPerPage)});
		$(o.slides,obj).css({width:(numberOfElements*width)});
		
		//set global css values
		$(o.slides,obj).children().each(function(){
			$(this).css({position:'absolute',left:p+pd,display:'block'});
			p=p+width;
		});

		//hide arrows when no sliding possible
		if(numberOfElements <= itemsPerPage){
			$('.next',obj).css('display', 'none');
			$('.previous',obj).css('display', 'none');
		}

		//shift to the middle if numberOfElements is less the number of items displayed per page
		if(numberOfElements <= itemsPerPage){
			$.cookie("fitnessplus_slider_pos", 0, { path: '/' });
			$(o.slides,obj).css('margin-left', (itemsPerPage - numberOfElements) * width / 2 + 'px');
		}

		//set position for all elements
		$(o.slides,obj).children().each(function(){
			position = pt - startElement;
			if(position < 0){
				position += numberOfElements;
			}
			$("#" + this.id).css('left', 5+width*position);
			pt++;
		});
		
		$('.next',obj).click(function(){
			if(u===false) {
				animate('next');
			}
			return false;
		});
		
		$('.previous',obj).click(function(){
			if(u===false) {	
				animate('prev');
			} return false;
		});
		
		function current(t) {
			if(t===c+1){t=1;}
			if(t===0){t=c;}
			$(o.pagination+' li a',obj).parent().siblings().removeClass('active');
			$(o.pagination+' li a[rel="' + (t) + '"]',obj).parent().addClass('active');
		}
		
		function animate(dir){	
			u = true;	
			switch(dir){
				case 'prev':
					//shift all elements to previous
					pt = 0;
					$(o.slides,obj).children().each(function(){
						$(o.slides,obj).children(':eq('+pt+')').css({left :  function(index, value) {
							if(parseFloat(value) > itemsPerPage * width){
								return parseFloat(value) - (numberOfElements) * width;
							}else{
								return parseFloat(value);
							}
						}});
						$(o.slides,obj).children(':eq('+pt+')').animate({'left' : "+="+width+"px"},o.slidespeed,function(){u = false;});
						pt = pt+1;
					});
					//update startElement and safe value to cookie
					startElement--;
					if(startElement < 0){
						startElement = numberOfElements - 1;
					}
					$.cookie("fitnessplus_slider_pos", startElement, { path: '/' });
					break; 
				case 'next':
					//shift all elements to next
					pt = 0;
					$(o.slides,obj).children().each(function(){	
						$(o.slides,obj).children(':eq('+pt+')').css({left :  function(index, value) {
							if(parseFloat(value) < 0){
								return parseFloat(value) + (numberOfElements) * width;
							}else{
								return parseFloat(value);
							}
						}});
						$(o.slides,obj).children(':eq('+pt+')').animate({'left' : "-="+width+"px"},o.slidespeed,function(){u = false;});
						pt = pt+1;
					});
					//update startElement and safe value to cookie
					startElement++;
					if(startElement >= numberOfElements){
						startElement = 0;
					}
					$.cookie("fitnessplus_slider_pos", startElement, { path: '/' });
					break; 
				default:
					break;
				}					
			}
		});
	};
})(jQuery);
