
// STILL BUGGY WHEN SHOWING MORE THAN 2 ITEMS - PUT ON HOLD SINCE CURRENT REQUIREMENT IS TO SHOW 2 ONLY <MB.>
(function($) {
$.fn.carousel = function (options) {

 	var defaults = {
 		name: 'Carousel',		// name for wrapper
 		slideDelay:   5000,		// time between slides
 		slideAuto:    true,		// automatically rotate
 		controlCount: true,		// show number controls
 		controlArrow: false,	// show arrow controls
 		visible:	  0			// how many can you see
 	};
 	
 	var options = $.extend(defaults, options); 
 
    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  	
  	$(this).addClass('carousel '+options.name);
  	
  	$(this).find('ul').wrap('<div class="wrapper"/>');
  	
  	$('.wrapper ul').addClass('main');
  	$('.wrapper li').addClass('feature');
  	$('.wrapper div').addClass('panel-overlay');
  	
  	if(options.addCount == true){$('.count li:first');}  
  	
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider  = $wrapper.find('> ul'),
            $items   = $slider.find('> li'),
            $single  = $items.filter(':first'),
            
            singleWidth  = $single.outerWidth(), 
            slides       = $slider.find('li:last').prevAll().length;
            visible      = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage  = 1,
            currentSlide = 1,
            animate      = true,
            pages        = Math.ceil($items.length / visible);            

 		if(options.visible!=0){visible=options.visible;}
 		
 		if(options.slideAuto == true) { window['timer-' + options.name] = setInterval( gotoPageAuto, options.slideDelay ); }
 	
 		function gotoPageAuto() { 
 			currentSlide = $wrapper.find('li.shown').prevAll().length;
 			gotoPage(currentSlide + 1); 
 		}
 	
 		$wrapper.mouseenter(function(){
 			clearInterval(window['timer-' + options.name]);
 		}).mouseleave(function(){
 			if (options.slideAuto == true){ window['timer-' + options.name] = setInterval( gotoPageAuto, options.slideDelay ); }
 		});
 	
        function gotoPage(slide) {
        
        	if (slide>slides){ slide = 0; } else if(slide<0){slide=slides;}
        	
        	var currentSlide = $wrapper.find('li.shown').prevAll().length,
            	betweenSlide = slide - currentSlide,
            	noSlide      = slides - visible + 1,
            	left         = singleWidth * betweenSlide;
            
            if (currentSlide == slides && betweenSlide == -1){ animate = false; } else { animate = true; }
            	
            $wrapper.find('li.shown').removeClass('shown');
            $wrapper.find('li').eq(slide).addClass('shown');	
            
            if(animate==false){
            } else if (slide==slides){
            	left = left-singleWidth;
            	$wrapper.filter(':not(:animated)').animate({
            		scrollLeft : '+=' + left
            	}, 500, function () {
            	});
            } else if(currentSlide==noSlide && betweenSlide>0) {
            	
            
            } else if(currentSlide>slide) {
            	$wrapper.filter(':not(:animated)').animate({
            		scrollLeft : '+=' + left
            	}, 500, function () {
            	});
            
            } else if(slide<slides){
            	$wrapper.filter(':not(:animated)').animate({
            		scrollLeft : '+=' + left
            	}, 500, function () {
            	});
            }
          	
          	// UPDATE SHOWN NUMBERS ON SLIDES
          	if(options.controlCount == true) {
	          	$wrapper.parent().find('ul.count li.shown').removeClass('shown');
    	      	countVisible = 0;
    	      	countShow    = slide;
            	while(countVisible < visible) {
	        	    $wrapper.find('ul li').each(function() {
		    	        $wrapper.parent().find('ul.count li').eq(countShow).addClass('shown');
		    	        if (slide == slides){ $wrapper.parent().find('ul.count li').eq(slide).prev().addClass('shown'); }
	        	    });
	        	    countVisible++;
	        	    countShow++;
    			}
    		}
    		        
            clearInterval(window['timer-' + options.name]);
            if(options.slideAuto == true) { window['timer-' + options.name] = setInterval( gotoPageAuto, options.slideDelay ); }
            
            return false;
        }
        
        
        $wrapper.find('li:first').addClass('shown');
        
        // ADD IN NUMBERS COUNT AND CONTROLS FOR THEM
        if(options.controlCount == true) {
        	$wrapper.after('<ul class="count"></ul>');
        
        	$itemCounter  = 0;
       		$numberCount  = 0;
       		$countVisible = 0;
       	
       		$wrapper.find('ul li').each(function() {
	 		  	$itemCounter++;
    		    $('ul.count').append('<li><a href="javascript:void(0);">'+$itemCounter+'</a></li>');
        	});
        	while($countVisible < visible) {
        		$wrapper.find('ul li').each(function() { $wrapper.parent().find('ul.count li').eq($countVisible).addClass('shown'); });
        	    $countVisible++;
        	}
        	$('ul.count').find('li a').bind('click', function(){
        		clearInterval(window['timer-' + options.name]);
        		clicked = $(this).parent().prevAll().length;
        		return gotoPage(clicked); 
        	});
        }
        
        // ADD IN ARROW CONTROLS
        if(options.controlArrow == true) {
	        $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
        
        	$('a.back', this).click(function () {
        		currentSlide = $wrapper.find('li.shown').prevAll().length;
        		clearInterval(window['timer-' + options.name]);
            	return gotoPage(currentSlide - 1);                
        	});
        
        	$('a.forward', this).click(function () {
        		currentSlide = $wrapper.find('li.shown').prevAll().length;
	    	    clearInterval(window['timer-' + options.name]);
        	    return gotoPage(currentSlide + 1);
        	});
        }
        
        $(this).bind('goto', function (event, slide) {
            gotoPage(slide);
        });
    });  
};
})(jQuery);
 
 
