
/* ================================ */
/*	Plugin:		WiselySlider 0.2    */
/* 	Date: 		2011-02-23		    */
/*	Company: 	Wisely AB		    */
/*	Author: 	Chris Lind		    */
/* ================================ */
/* 	   No license or copyright.     */
/* 	  Screw around all you want!    */
/* ================================ */

(function($){
 
    $.fn.extend({
         
        wiselySlider: function(settings) {
			var defaults = {
				active: 0,
				width: this.width(),
				speed: 800, //Animation speed
				auto_play: false,
				auto_play_interval: 4000, //Slideshow time interval
				slideshow_wait_interval: 7000, //Time for slideshow to start up again
				use_arrows: false
		    } 
		    
		    settings = $.extend(defaults, settings);
		    
		    var active = settings.active;
		    var total_length = $(this).children('#puffar').children().length;
		
					
			$(this).children('#puffar').css({'width': ($(this).children('#puffar').children().length * settings.width) + 'px' , 'position':'absolute'});
		
			$(this).append('<ul id="toggles"></ul>');
			
			if(settings.use_arrows) 
			 	$(this).children('#toggles').append('<li id="prev"><a href="#">' + i +'</a></li>');
			
			for(var i = 0; i < $(this).children('#puffar').children().length; i++) {
			 	$(this).children('#toggles').append('<li id="toggle' + i + '"><a href="#">' + i +'</a></li>');
			 	if(i == 0) {
			 		$(this).children('#toggles li:last-child a').addClass('active');
			 	} else if(i == $(this).children('#puffar').children().length - 1) {
			 		$(this).children('#toggles li:last-child').css({'padding-right': '0'});
			 	}
			}
			
			if(settings.use_arrows) 
			 	$(this).children('#toggles').append('<li id="next"><a href="#">' + i +'</a></li>');
			 	
			$('#toggles li').click(function() {
				
				clearInterval(slideshow_interval);
				clearInterval(wait_interval);
				wait_interval = setInterval(startSlideShow, settings.slideshow_wait_interval);
				
				if(this.id != 'next' && this.id != 'prev'){
					var li_id = this.id.split('toggle')[1];
					
					if(active != li_id){
						showImage(li_id);
					}
				} else {
					if(this.id == 'next')
						nextslide()
					else
						prevslide()
				}
			});
			
			// Set active item
			var start_num = active + 1;
			
			if(settings.use_arrows)
				start_num++;
			
			var start_left = -(active * settings.width);
		
			$('#toggles li:nth-child('+ start_num +') a').addClass('active');
			
			$('#puffar').css({ left: start_left + "px" }, settings.speed); 
		
			var left_offset = ($(this).children('#toggles').children().outerWidth()- $(this).children('#toggles').children().width())/2; //Because of the last items padding, to center #toggles
			$(this).children('#toggles').css({'width': (($(this).children('#toggles').children().length * $(this).children('#toggles').children().outerWidth())) + 'px', 'left': ($(this).outerWidth()/2 - $('#toggles').outerWidth()/2) + left_offset });
		
			function showImage(rawID){
				
				active = parseInt(rawID);
				
				var _id = parseInt(rawID);
				
				var amountLeft = 0;
				
				if(_id != 0)
					amountLeft = -(_id * settings.width);
				
				$('#toggles li a').removeClass('active');
				
				var child_num = _id + 1;
				
				if(settings.use_arrows) //If we have a previous arrow in front of everything
					child_num++;
				
				$('#toggles li:nth-child('+ child_num +') a').addClass('active');
				
				$('#puffar').stop().animate({ left: amountLeft + "px" }, settings.speed); 	
			}
			
			wait_interval = null;
			slideshow_interval = null;
			
			if (settings.auto_play == true)
				startSlideShow();
				
				
			function startSlideShow(){
				
				clearInterval(wait_interval);
				clearInterval(slideshow_interval);
				
				slideshow_interval = setInterval(nextslide, settings.auto_play_interval);
			}
			
			function nextslide(){
				if(active == total_length - 1)
					active = 0;
				else
					active++;
				
				showImage(active);
				//clearInterval(slideshow_interval);
					
			}
			
			function prevslide(){
				if(active == 0)
					active = total_length - 1;
				else
					active--;
				
				showImage(active);
				//clearInterval(slideshow_interval);
					
			}
		
		}
    });
})( jQuery );
