(function($) {

	var __currentSlide = 1, __slidesCount = 0, __slidesOffset = 0,
	
	//Private functions

	delta = function(val1, val2) {
		return Math.max(val1, val2) - Math.min(val1, val2);	
	};

	//Public functions
	$.fn.contentSlider = function(options) {
		// build main options before element iteration
		var opts = $.extend({}, $.fn.contentSlider.defaults, options);

		return this.each(function() {
			$this = $(this);
			// build element specific options
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

			$this.css({
				float: 'left',
				width: o.width,
				height: o.height,
				background: '#fff'
			});
			
			//Setup the slides
			$slides = $this.find('.slide');
			__slidesCount = $slides.length;

			$slides.each(function() {
				$(this).css({
					float: 'left',
					width: o.width,
					height: o.height,
					color: '#fff'
				});
			});

			//Setup the slides viewport
			$this.empty();
			$slidesViewport = $('<div id="slides-viewport" />');
			$slidesViewport.css({
					float: 'left',
					width: o.width,
					height: o.height,
					color: '#fff',
					overflow: 'hidden'
			})
			.appendTo($this);
			
			//Setup the slides container wich will be animated
			$slidesContainer = $('<div id="slides-container" />');
			$slidesContainer
			.append($slides)
			.css({
				float: 'left',
				width: o.width * __slidesCount,
				height: o.height			})
			.appendTo($slidesViewport);
			
			if(o.arrows) {
				$leftArrow = $('<div id="slider-arrow-left"></div>');
				$leftArrow.bind('click', slideLeft)
				.prependTo($this);

				
				$rightArrow = $('<div id="slider-arrow-right"></div>');
				$rightArrow.bind('click', slideRight)
				.appendTo($this);
				
				$this.css({
					width: $this.width() + ($rightArrow.width() + $leftArrow.width())
				});
			}
			

			
			if(o.bullets && __slidesCount > 1) {
				$bulletsDiv = $('<div id="slider-bullets" />');
				if(o.arrows && __slidesCount > 1) {
					$bulletsDiv.css('margin-right', $rightArrow.width());
				}
				$bulletsUl = $('<ul></ul>');
				for(var x = 1; x <= __slidesCount; x++) {
					$('<li>'+x+'</li>')
					.bind('click', {x: x},function(evt){
						gotoSlide(evt.data.x);
					})
					.appendTo($bulletsUl);
				}
				$bulletsDiv.append($bulletsUl);
				$this.append($bulletsDiv);
				setActiveBullet();
			}
			
			if(o.autoSlide) {
				timerId = setInterval(slideRight, o.autoSlideDelay*1000);	
			}
			
			if(o.infinite && __currentSlide == 1) {
				$slides = $('.slide');
				$($slides[$slides.length-1]).insertBefore($($slides[0]));
				
				margin = o.width * -1;
				$slidesContainer.css('margin-left', margin);
				
				__currentSlide = 2;
			}
			
			function slideLeft() {
				gotoSlide('prev');	
			}
			
			function slideRight() {
				gotoSlide('next');	
			}
			
			function setActiveBullet() {
				$bullets = $('#slider-bullets ul li');
				$bullets.removeClass();
				$.each($bullets, function(index, bullet) {
					if(index == __currentSlide - 1) {
						$(bullet).addClass('active');	
					}
				});	
			}
			
			function gotoSlide(slide) {
				
				if(o.autoSlide) {
					clearInterval(timerId);
					timerId = setInterval(slideRight, o.autoSlideDelay*1000);
				}
				
				var margin = 0;
				var marginMultiplier;
				var animationMultiplier;

				if(!o.infinite) {
					if(typeof(slide) == 'number') {
						marginMultiplier = slide - 1;
						animationMultiplier = delta(slide, __currentSlide);
					} else if(typeof(slide) == 'string') {
						if(slide == 'next') {
							if(__currentSlide + 1 > __slidesCount) {
								marginMultiplier = 0;
								animationMultiplier = __slidesCount-1;
							} else {
								marginMultiplier = __currentSlide;
								animationMultiplier = 1;
							}
						} else {
							if(__currentSlide -1 < 1) {
								marginMultiplier = __slidesCount - 1;
								animationMultiplier = __slidesCount-1;
							} else {
								marginMultiplier = __currentSlide - 2;
								animationMultiplier = 1;
							}
						}
					}
					
					if(marginMultiplier > 0) {
						margin = marginMultiplier * -o.width;
						__currentSlide = marginMultiplier + 1;
					} else {
						margin = 0;
						__currentSlide = 1;
					}
					
					$slidesContainer.stop().animate({'margin-left': margin}, o.animationSpeed * animationMultiplier);
					
					if(o.bullets && __slidesCount > 1) {
						setActiveBullet();	
					}

				} else {
				
					if(__currentSlide == __slidesCount) {					
						$slides = $('.slide');
						$($slides[0]).insertAfter($($slides[$slides.length-1]));
						
						margin = ((__slidesCount-1) * -o.width) + o.width;
						$slidesContainer.css('margin-left', margin);
						
						__currentSlide = __slidesCount - 1;
					} else if(__currentSlide == 1) {
						$slides = $('.slide');
						$($slides[$slides.length-1]).insertBefore($($slides[0]));
	
						margin = o.width * -1;
						$slidesContainer.css('margin-left', margin);
	
						__currentSlide = 2;
					}
				
				}

			}

		});
	};
	
	// plugin defaults
	$.fn.contentSlider.defaults = {
		width					:	900,
		height					:	380,
		autoSlide				:	true,
		autoSlideDelay			:	15,
		animationSpeed			:	450,
		arrows					:	true,
		bullets					:	true,
		infinite				:	false
	};

// end of closure
})(jQuery);
