/*!
* Carousel Setup, adding previous/next buttons and extra required containers
* @uses jCarouselLite
* @see http://www.gmarwaha.com/jquery/jcarousellite/
* @author: Trevor Morris (trovster)
* @published: 18/02/2010
*/
if(typeof jQuery != "undefined") {	
	(function($){
		$.fn.extend({
			carousel: function(options) {
				var settings = $.extend({}, $.fn.carousel.defaults, options);
				
				return this.each(function() {
					var $$, o, $items, $wrap,
						total, selector;
					
					$$			= $(this).addClass('carousel');
					o			= $.metadata ? $.extend({}, settings, $$.metadata()) : settings;
					$items 		= $('li', $$);
					o.total		= $items.length;
					selector	= '#' + $(this).closest('div[id]').attr('id') + ' ';
			
					if($items.length > 0 && $.fn.jCarouselLite) {						
						if(typeof o.arrows !== 'undefined' && o.arrows === true) {
							o.btnPrev		= selector + 'a.progress.previous';
							o.btnNext		= selector + 'a.progress.next';
						}
						if(typeof o.pagination !== 'undefined' && o.pagination === true) {
							o.btnGo	= [];
							for(i = 1; i<=o.total; i++) {
								o.btnGo[i-1] = selector + '.p' + i;
							}
						}
						
						o.beforeStart	= $.fn.carousel.beforeStart;
						o.afterEnd		= $.fn.carousel.afterEnd;

						if(o.auto !== null) {
							o.timeoutValue = o.auto;
							o.auto = null;
						}
						
						if(o.total > 1 && o.total >= o.visible) {
							$wrap = $$.wrap(o.wrap).parent().addClass('has-carousel');
							$wrap.prevNext(o.total, o).pagination(o.total, o).jCarouselLite(o);
							//o.timeout = setTimeout(function(){ slideStart(o) }, o.timeoutValue);
						}
					}
				});
			}
		});
				
		// defaults
		// Similar to http://www.gmarwaha.com/jquery/jcarousellite/
		$.fn.carousel.defaults = {
			wrap:			'<div />',
			mouseWheel: 	false,
			auto: 			null,
			speed: 			250,
			easing: 		null,
			vertical: 		false,
			circular: 		false,
			pagination: 	false,
			arrows:			true,
			visible: 		4,
			start: 			0,
			scroll: 		4,
			text:	{
				previous:	'Previous',
				next:		'Next'
			}
		};
		
		$.fn.carousel.beforeStart	= function(a){
			var $$, $first, $last, $lis, index,
				$carousel, $div, $links;
			
			$$ 			= $(a);
			$first		= $$.filter('.f');
			$last		= $$.filter('.l');
			
			$carousel	= $$.closest('ul.carousel');
			$lis		= $carousel.children('li');
			$div 		= $carousel.closest('div.has-carousel');
			$links	 	= $div.next('ul.scroller-arrows').find('li.previous, li.next');
			index		= $lis.index($$);
			
			// blur the prev/next button arrows and pagination
			$links.blur();
			
			// fixes the first item
			if(index > $lis.length) {
				index = index - $lis.length;
			}
			
			if($first.length === 1 && $last.length === 1) {
				$links.addClass('disabled');
			}
			else if($first.length === 1) {
				// first image
				$links.filter('.next').addClass('disabled');
			}
			else if($last.length === 1) {
				// last image
				$links.filter('.previous').addClass('disabled');
			}
		};
		
		$.fn.carousel.afterEnd		= function(a){
			var $$, $first, $last, $lis, index,
				$carousel, $div, $links;
			
			$$ 			= $(a);
			$first		= $$.filter('.f');
			$last		= $$.filter('.l');
			
			$carousel	= $$.closest('ul.carousel');
			$lis		= $carousel.children('li');
			$div 		= $carousel.closest('div.has-carousel');
			$links	 	= $div.next('ul.scroller-arrows').find('li.previous, li.next');
			index		= $lis.index($$);
			
			// blur the prev/next button arrows and pagination
			$links.blur();
			
			// fixes the first item
			if(index > $lis.length) {
				index = index - $lis.length;
			}
			
			if($first.length === 1 && $last.length === 1) {
				$links.addClass('disabled');
			}
			else if($first.length === 1) {
				// first image
				$links.filter('.previous').addClass('disabled').siblings().removeClass('disabled');
			}
			else if($last.length === 1) {
				// last image
				$links.filter('.next').addClass('disabled').siblings().removeClass('disabled');
			}
			else {
				$links.removeClass('disabled');
			}
		};
		
		$.fn.prevNext = function(total, o) {
			return this.each(function() {
				var $ul, $prevLi, $nextLi, $prevA, $nextA;
				
				if(typeof o.arrows !== 'undefined' && o.arrows === true) {
					$ul			= $('<ul />').addClass('scroller-arrows');
					$prevLi		= $('<li />').addClass('previous');
					$nextLi		= $('<li />').addClass('next');
					$prevA 		= $('<a />').addClass('previous').addClass('progress').attr('href', '#previous').text(o.text.previous);
					$nextA		= $('<a />').addClass('next').addClass('progress').attr('href', '#next').text(o.text.next);
					
					$ul.append($prevLi.append($prevA));
					$ul.append($nextLi.append($nextA));
					
					if(o.start === 0 && o.circular === false) {
						$prevLi.addClass('disabled');
					}
					if(total - o.start <= o.visible && o.circular === false) {
						$nextLi.addClass('disabled');
					}
					
					$(this).addClass('has-scroller-arrows').after($ul);
					
					$(o.btnNext).add(o.btnPrev).bind('click', function(event){
						slideStop(o);
					});
				}
			});
		};
		
		$.fn.pagination = function(total, o) {
			return this.each(function() {
				var $ul,
					lis, ii;
				
				if(typeof o.pagination !== 'undefined' && o.pagination === true) {
					$ul	= $('<ul />').addClass('scroller-pagination');
					lis = [];
					ii	= 0;
					
					for(i = 1; i <= total; i++) {
						lis[ii++] = '<li>';
						lis[ii++] = '<a href="#' + i + '" class="p' + i + '">';
						lis[ii++] = i;
						lis[ii++] = '</a>';
						lis[ii++] = '</li>';
					}
				
					$(this).addClass('has-scroller-pagination').after($ul.append(lis.join('')));
				}
			});
		};

		slideStart = function(o){
			$(o.btnNext).trigger('click');
			o.timeout = setTimeout(function(){
				slideStart(o);
			}, o.timeoutValue);
		};

		slideStop = function(o){
			clearTimeout(o.timeout);
		};

	})(jQuery);
}
