/**
 * @author dave
 */
(function($) {
	$.fn.bluntSwitcher = function (options) { 
		var settings = { 
			  'transport'	: null
		}
		
		if (options) $.extend(settings, options);
		
		var $T = $(this) 
			,$C = settings.transport
			,$items = $T.children()
			,idx = 0
			,last
			,loopr
			,prev = $C[0]
			,play = $C[1]
			,next = $C[2]
			
			
			 
		$.log (' items : '+$items.length+' transport : '+$C.length);
		
		$items.each(function(){
			$(this).css('display','none');
		});
		
		$C.each(function(i,el) { 
			$.log(' i : '+i+' el '+$(el).attr('class') );
			
		 })
		
		//set up the events
		enableTransport();
		
		$($items[0]).show();
		
		// KICK IT OFF
		startLoop(); 
		
		function showNext(items, n,l) { 
			$(items[l]).hide();	
			$(items[n]).show();
		}
		
		function startLoop() { 
			loopr = setInterval(function() { 
				(idx < $items.length -1) ? idx++ : idx =0;
				(idx == 0 )? last = $items.length -1 : last = idx -1; 	
				showNext($items, idx, last);  
				
			}, 12000); 
		}
		
		function stopLoop() { 
			if(loopr)
				clearInterval(loopr);
		}
		
		function enableTransport() { 
			$(prev).click(function(e){
				var n; 
				$.log('prev clicked');
				stopLoop(); 
				$($items[idx]).hide();
				(idx == 0) ? n = $items.length -1 : n = idx - 1; 
				$($items[n]).show();
				idx = n;
				$(play).removeClass('On');
			});
			
			$(next).click(function(e){
				var n; 
				stopLoop(); 
				$($items[idx]).hide();
				(idx < $items.length -1) ? n =  idx+ 1 : n = 0 ; 
				$($items[n]).show();
				idx = n;
				$(play).removeClass('On');
			});
			
			$(play).click(function(e){ 
				if($(play).hasClass('On')) { 
					stopLoop(); 
					$(play).removeClass('On');
				} else { 
					(idx < $items.length -1) ? idx++ : idx =0;
					(idx == 0 )? last = $items.length -1 : last = idx -1; 
					showNext($items, idx, last); 
					
					$(play).addClass('On'); 
					startLoop();
				}
			});
		}
		
	
	}

})(jQuery);
