/*
 * ATcarousel.js - A jQuery Plugin
 * A jQuery plugin for creating carousels.
 *
 * By Andy Tennison (andrew@tennisons.com / http://www.tennisons.com)
 *
 * Version 1.0.1
 * September 16th, 2009
 *
 * Copyright (c) 2009 Andy Tennison
 * Dual licensed under the MIT and GPL licenses.
*/

(function($) {
	$.fn.ATcarousel = function(options) {
		var opts = $.extend({}, $.fn.ATcarousel.defaults, options);
				
		return this.each(function(ii){
						
			var $c = $(this);
			
			$c.append('<div class="cNav"><a class="prev" href="#">previous</a><ul class="navH"></ul><a class="next" href="#">next</a></div>');
			$c.append('<div class="timer"></div>')
			
			var curIndex = 0;
			var $nav = $(this).find('.cNav');
				var $navList = $nav.find('ul');
				var $navPrev = $nav.find('.prev');
				var $navNext = $nav.find('.next');
			var $timer = $(this).find('.timer')	
			var tWidth = $timer.width();
			
			var $title = $(this).find(opts.title);
			var $list = $(this).find(opts.list);
			var maxIndex = $list.children(opts.page).length;
			var $p = [];
			
			// buttons
			$navPrev.click(function(e){
				e.preventDefault();
				curIndex--;
				slide(curIndex);
				stopCount();
			});
			$navNext.click(function(e){
				e.preventDefault();
				curIndex++;
				slide(curIndex);
				stopCount();
			});
			initialDelay();

			
			return $list.children(opts.page).each(function(i){
				
				$p[i] = new pages();
				function pages(id, title, href){
					this.id = id,
					this.title = title,
					this.href = href
				};
				
				$p[i].id = $(this);
				$p[i].title = $(this).find('h3').text();
				$p[i].href = $(this).find('.more').attr('href');
				
				$nav.find('ul').append("<li><a title="+$p[i].title+" href='#'>"+$p[i].title+"</a></li>")
				var thisNav = $nav.find('li').eq(i);
				thisNav.find('a').click(function(e){
					e.preventDefault();
					curIndex = i;
					slide(curIndex);
					stopCount();
				});
				setCurrent(0);
				setStatus(0);
				
			});
			

			// animate to next page
			function slide(){
				if(curIndex <= 0){curIndex = 0}
				else if (curIndex >= maxIndex-1){curIndex = maxIndex-1}
				var left = -(opts.width*curIndex);
				$list.animate({'left':left},opts.animate);
				setCurrent(curIndex);
				updateTitle(curIndex);
				setStatus(curIndex);
				updateTitle(curIndex);
			}
			
			// set correct nav
			function setCurrent(curIndex){
				$navList.find('.on').removeClass('on');
				$navList.find('li').eq(curIndex).addClass('on')
			};
			
			function setStatus(curIndex){
				$navPrev.removeClass('off');
				$navNext.removeClass('off');
				if(curIndex >= maxIndex-1 ){$navNext.addClass('off')}
				else if(curIndex <= 0){$navPrev.addClass('off');}
			};
			
			// change title + link
			function updateTitle(curIndex){
				$title.find('h3 a').text($p[curIndex].title);
				$title.find('h3 a').attr('href', $p[curIndex].href);
			};
			
			var t, tt, delay;
			function initialDelay(){ // initial gallery delay
				delay = opts.delay;
				$timer.animate({'width':0},delay)
				t=setTimeout(timedCount,delay);
			}
			
			function timedCount(){ // delay between slides
				curIndex++;
				$timer.stop().css('width',0)//.animate({'width':tWidth},delay)//.css('width',300)
				
				if (curIndex >= maxIndex){ curIndex = 0; delay = opts.delay; } 
				else if(curIndex == maxIndex -1){	delay = opts.delay+4000; }
				else{ delay = opts.delay;	};
				
				slide(curIndex);
				$timer.animate({'width':300},opts.animate).animate({'width':0},delay-opts.animate);
				
				t=setTimeout(timedCount,delay);

			};
			
						
			function stopCount(){
				$timer.stop().animate({'width':0},200);//.width(0);
				clearTimeout(t);
			};
			
			
			
		}); // end each carousel
		
	}; // end plugin ATcarousel


// end of closure
})(jQuery);

$.fn.ATcarousel.defaults = {
	// set main carousel elements
	title: '.cTitle',	// title block
	list: '.cList',		// ul list
	page: '.cItem',		// carousel inner
	width: 980,
	animate: 1000,
	delay: 8000,
	
	
};
