/*

	BILLY The Carousel jquery plugin v 0.3
	By. Jason Howmans (jasonhowmans@me.com)
	
	-- Options
	
	- scrollSpeed : The time a single transition will take
	- slidePause : Amount of time between scrolls
	- indicators : The target <ul> for displaying the indicators (indicators arent required for basic functionality)
	- indicatorLinks : Clicking on indicators jumps to slide, true by default
	- activeClass : Class to attach to the active indicator. default is 'active'
	- scrollAmount : Amount to scroll by on each transition. Set to 'auto' by default
	- nextLink : The element which will scroll next on click
	- prevLink : The element which will scroll back on click
	- autoAnimate : Do you want the carousel to play without the user pressing the next/prev buttons
	- loop : Loops back to the beginning after final slide
	
*/

(function($){     
	$.fn.extend({   

		billy: function(options) { 
		
			// Defaults
			var defaults = {
				scrollSpeed		: 1,
				slidePause		: 3900,
				indicators		: $('ul#billy_indicators'),
				indicatorLinks	: true,
				activeClass		: 'active',
				scrollAmount	: 'auto',
				nextLink		: $('#billy_next'),
				prevLink		: $('#billy_prev'),
				autoAnimate		: true,
				loop			: true
			};
			
			// Set Options
			var options = $.extend(defaults, options);
			
			// Loop throuch Carousels
			return this.each(function() {
				
				// Set Options
				var option = options;
				// Set currently selected
				var object = $(this);
				// Sets up slide size
				var slides = object.find('li');
				if (option.scrollAmount	== 'auto') { 
					var slidewidth		= slides.width();
				}else{ 
					var slidewidth = option.scrollAmount; 
				}
				// Other vars
				var slidecount = Math.round((slides.width() * slides.length) / slidewidth);
				var currentslide = 0;
				
				// If there's slides, continue
				if (slides.length > 0) {
					
					// Loop / no. of slides
					for (var i = 0; i<slidecount; i++) {
						// -- Insert Indicators
						if (!option.indicatorLinks) {
							option.indicators.append('<li></li>');
						}else{
							option.indicators.append('<li><a href="#'+i+'"></a></li>');
						}
					};
					
	             
			
					
					// -- Jump Functions
					var jumptostart = function() {
					    currentslide = 0;
						object.animate({opacity: 0.0}, 300);    
					    object.animate({'marginLeft': "0"}, option.scrollSpeed);
					object.animate({opacity: 1.0}, 200); 
					    option.indicators.find('li').removeClass();
					    option.indicators.find('li:eq('+(currentslide)+')').addClass(option.activeClass);
					};	
					var jumptoend = function() {
					    currentslide = slidecount-1;
					    object.animate({'marginLeft': "-"+currentslide*slidewidth}, option.scrollSpeed);
					    option.indicators.find('li').removeClass();
					    option.indicators.find('li:eq('+(currentslide)+')').addClass(option.activeClass);
					};
					var jumpnext = function() {
						if (currentslide < (slidecount - 1)) {
							currentslide ++;  
							
							object.animate({opacity: 0.0}, 300); 
							object.animate({'marginLeft': "-"+(slidewidth*currentslide)}, option.scrollSpeed);
							object.animate({opacity: 1.0}, 200); 
							option.indicators.find('li').removeClass();
							option.indicators.find('li:eq('+(currentslide)+')').addClass(option.activeClass);
							if (option.autoAnimate) {
								clearInterval(period);
								period = window.setInterval(function() {
									if (currentslide >= (slidecount - 1)) {
										jumptostart();
									}else{
										jumpnext();
									}
								}, option.slidePause);
							}
						}else{
							if (option.loop)
								jumptostart();
						}
					};
					var jumpback = function() {
						if (currentslide > 0) {
							currentslide --;
							object.animate({'marginLeft': "-"+(slidewidth*currentslide)}, option.scrollSpeed);
							option.indicators.find('li').removeClass();
							option.indicators.find('li:eq('+(currentslide)+')').addClass(option.activeClass);
							if (option.autoAnimate) {
								clearInterval(period);
								period = window.setInterval(function() {
									if (currentslide >= (slidecount - 1)) {
										jumptostart();
									}else{
										jumpnext();
									}
								}, option.slidePause);
							}
						}else{
							if (option.loop)
								jumptoend();
						}
					};
 
					
			 
					if (option.autoAnimate) {
						// -- Periodical
						var period;
						// Run
						period = window.setInterval(function() {
				
							if (currentslide >= (slidecount - 1)) {
								jumptostart();
							}else{
								jumpnext();
							}
		
						}, option.slidePause);
					}
				}
				
			});
			
		}
		
	});
})(jQuery);
