/***************************************************************
 * jQuery.afCarousel 1.7.1    6/7/2010
 * Author: John Norton - jnorton@amplifystudios.com
 * This version works best with jquery 1.4+
 ***************************************************************/
if(typeof jQuery != 'function'){ alert('You need to include jQuery 1.4.x first.'); } 
(function($) {
	var afCarousel = function($c,opts){
		var opt = {
			animation:      'fade', //fade, slide, array()
			inTime:         3000,
			outTime:        1000,
			waitTime:       5000,
			auto:           false,
			stopOnChange:   true,
			json:           false, // {"item": [{"html": "HTML_HERE", "animation": "ANIMATION_HERE"}]}
			href:			false,
			containerClass: 'afCarousel-case',
			showNav:        true,
			navClass:       'afCarousel-nav',
			navItem:        '<a href="#{#}" class="afCarousel-navItem">{#}</a>', //use {#} to display the nav number
			showArrows:     true,
			arrowNext:      '<a href="#" class="afCarousel-arrowNext afCarousel-arrow">&gt;</a>',
			arrowPrev:      '<a href="#" class="afCarousel-arrowNext afCarousel-arrow">&gt;</a>'
		};
		var ia = new Array();
		var gi = new Array();
		var l = 0;
		var ai = null;
		$.extend(opt, opts ? opts : {});
		
		var init = function(){
			$c.css('position', 'relative').wrap('<div class="'+opt.containerClass+'" />');
			if(opt.showArrows)
				$c.after(function(){return opt.arrowNext+opt.arrowPrev});
			if(opt.href){
				create(); //make ajax call k?
			}else
				create();
			$c.data('afCarousel.active', function(){ai});
		};
		var create = function(){
			var da = 'fade'; //default animation
			var $n = opt.showNav ? $('<div class="'+opt.navClass+'" />') : false;
			//if(opt.json){
			//	for(var i=0; i < opt.json; i++){
			//		addItem(opt.json.item[i].html, opt.json.item[i].animation ? opt.json.item[i].animation : typeof opt.animation == 'string' ? opt.animation : opt.animation.length >= i ? opt.animation[i] : da, true, $n);
			//	}
			//}else{
				$.each($(' > *', $c),function(i){
					$o = $(this);
					var anm = opt.animation[i];
					/*var anm = da;
					if(typeof opt.animation == "string")
						anm = opt.animation;
					if(!opt.animation.constructor.toString().indexOf("Array") == -1){
						alert('hi');
						if(opt.animation.length >= i)
							anm = opt.animation[i];
					}*/
					addItem($o, anm, false, $n);
				});
			//}
			$c.parent().append($n);
			start();
		};
		var addItem = function($o, a, c, $n){
			if(c)
				$o = $($o).appendTo($c);
			$o.css({display:'none', position:'absolute', top:0, left:0, width:$c.width()});
			$s = $(opt.navItem.replace('{#}', ia.length));
			$s.click(function(){return jumpLoc();});
			$n.append($s);
			ia.push(a);
			gi.push($o);
		};
		var start = function(){
			ai = gi[l];
			ai.show();
			if(opt.auto)
				setTimeout(function(){next();}, opt.waitTime);
		};
		var next = function(){
			ai.stop().fadeOut(opt.outTime); //this should be its own setting...
			l = (l+1) == gi.length ? 0 : l+1;
			ai = gi[l];
			if(typeof ia[l] == 'string'){
				switch(ia){
					case 'slide':
						ai.SlideDown(opt.inTime);
						break;
					case 'fade':
					default:
						ai.fadeIn(opt.inTime);
						break;
				}
				if(opt.auto)
					setTimeout(function(){next();}, opt.inTime+opt.waitTime);
			}else{
				t = ia[l]();
				if(opt.auto)
					setTimeout(function(){next();}, opt.waitTime+t);
			}
		};
		var prev = function(){
			ai.stop().fadeOut(opt.outTime); //this should be its own setting...
			l = l-1 < 0 ? l-- : gi.length-1;
			ai = gi[l];
			if(typeof ia[l] == 'string'){
				switch(ia){
					case 'slide':
						ai.SlideDown(opt.inTime);
						break;
					case 'fade':
					default:
						ai.fadeIn(opt.inTime);
						break;
				}
				if(opt.auto)
					setTimeout(function(){next();}, opt.inTime+opt.waitTime);
			}
			else{
				if(opt.auto)
				setTimeout(function(){next();}, opt.inTime+ia[l]());
			}
		};
		var jumpLoc = function(){
			$o = $(this);
			ol = $o.attr('href');
			ol = ol.substr(1);
			jump(ol);
		}
		var jump = function(i){
			if(opt.stopOnChange)
				opt.auto = false;
			ai.stop().fadeOut(opt.outTime); //this should be its own setting...
			l = i;
			ai = gi[l];
			return false;
			if(typeof ia[l] == 'string'){
				switch(ia){
					case 'slide':
						ai.SlideDown(opt.inTime);
						break;
					case 'fade':
					default:
						ai.fadeIn(opt.inTime);
						break;
				}
				if(opt.auto)
					setTimeout('next()', opt.inTime+opt.waitTime);
			}
			else{
				if(opt.auto)
				setTimeout('next()', opt.inTime+ia[l]());
			}
		}
		
		init();
	};
	
	$.fn.afCarousel = function(opt){
		return this.each(function(){
			(new afCarousel($(this),opt));
		});
	};
	$.fn.afCarousel.active = function() {
		return this.each(function() {
			$(this).data('afCarousel.active');
		});
	};
	$.fn.afCarousel.version = "0.0.1";
})(jQuery);
