/*
	Name: Rotary
	Purpose: fairly loose function for a few different types of interactivity; image fading/sliding with carousel type interactions (index / next/previous)
	Author: James Crockford
	Version: 0.4 beta
*/

/*
OPTIONS:
	auto: {interval:5000},
	changer: 'img',
	control: {
		ind: {
			element: $('#ind a'),
			binding: 'mouseover',
			onclass: 'current'
		},
		arrows: {
			nxt: $('#rightarrow'),
			prv: $('#leftarrow'),
			binding: 'click'
		}
	},
	onstage: {
		opacity:1
	},
	offstage: {
		opacity:0
	},
	ext: {
		queue:false,
		duration:500,
		easing:'swing'
	}
	
*/


//compiled
(function(h){h.fn.extend({rotary:function(i){i=h.extend({auto:{interval:3E3},changer:"img",control:{ind:false,arrows:false},onstage:{opacity:1},offstage:{opacity:0},ext:{queue:false,duration:500,easing:"swing"},hash:false},i);return this.each(function(){function f(k){e.children(a.changer).animate(a.offstage,{queue:a.ext.queue,easing:a.ext.easing,duration:a.ext.duration}).eq(k).animate(a.onstage,{queue:a.ext.queue,easing:a.ext.easing,duration:a.ext.duration});typeof a.control.ind.onclass=="string"&& a.control.ind.element.removeClass(a.control.ind.onclass).eq(k).addClass(a.control.ind.onclass)}var a=i,e=h(this),g,b=false,c={auto:typeof a.auto,control:typeof a.control,changer:typeof a.changer,ind:typeof a.control.ind,arrows:typeof a.control.arrows,onclass:typeof a.control.ind.onclass};if(e.children(a.changer).length)var j=e.children(a.changer).length-1;if(a.hash&&location.hash){first=location.hash.substr(1);if(first>e.children(a.changer).length)first=e.children(a.changer).length}else first=1;var d= first-1;if(c.changer=="string"){e.children(a.changer).css(a.offstage).eq(first-1).css(a.onstage);c.onclass=="string"&&a.control.ind.element.removeClass(a.control.ind.onclass).eq(0).addClass(a.control.ind.onclass)}if(c.auto=="object"&&c.changer=="string"){d=first;g=setInterval(function(){f(d);if(d==j)d=0;else d++},a.auto.interval)}if(c.control=="object"&&c.changer=="string"){c.ind=="object"&&a.control.ind.element.bind(a.control.ind.binding,function(){c.auto=="object"&&clearInterval(g);ti=a.control.ind.element.index(this); if(c.arrows=="object")b=ti;f(ti);return false});if(c.arrows=="object"){a.control.arrows.nxt.bind(a.control.arrows.binding,function(){if(c.auto=="object"&&b===false){clearInterval(g);b=d}else{if(b===false)b=a.hash?d:0;if(b==j)b=0;else b++}f(b);return false});a.control.arrows.prv.bind(a.control.arrows.binding,function(){if(c.auto=="object"&&b===false){clearInterval(g);b=d}else{if(b===false)b=a.hash?d:0;if(b==0)b=j;else b--}f(b);return false})}}})}})})(jQuery);
(function($){

 	$.fn.extend({ 
 		rotary: function(options) {
	
			var defaults = {
				auto: {interval:3000},
				changer: 'img',
				control: {
					ind: false,
					arrows: false
				},
				onstage: {
					opacity:1
				},
				offstage: {
					opacity:0
				},
				ext: {
					queue:false,
					duration:500,
					easing:'swing'
				},
				hash: false
			};
			

				
			var options =  $.extend(defaults, options);

    		return this.each(function() {
				var opt = options;
				var obj = $(this);
				
				//used for interval
				var anim;
				
				//used as index for next/prev
				var ac=false;
				
				//more efficient than calling typeof() multiple times?
				var type = {
					auto: typeof(opt.auto),
					control: typeof(opt.control),
					changer: typeof(opt.changer),
					ind: typeof(opt.control.ind),
					arrows: typeof(opt.control.arrows),
					onclass: typeof(opt.control.ind.onclass)
				};
				
				
				//changes main slide
				function animateObj(ind){
					obj.children(opt.changer)
					.animate(opt.offstage,{
						queue:opt.ext.queue, 
						easing: opt.ext.easing, 
						duration:opt.ext.duration
					}) 
					.eq(ind)
					.animate(opt.onstage,{
						queue:opt.ext.queue, 
						easing: opt.ext.easing,
						duration:opt.ext.duration
					});
					
					if(typeof(opt.control.ind.onclass)=='string'){
						opt.control.ind.element
						.removeClass(opt.control.ind.onclass)
						.eq(ind)
						.addClass(opt.control.ind.onclass);
					}
				}
				
				//get length of slides
				if(obj.children(opt.changer).length) var oclength=obj.children(opt.changer).length-1;
				
				//determines hash value / first slide
				if(opt.hash && location.hash){
					first=location.hash.substr(1);
					if(first>obj.children(opt.changer).length){
						first=obj.children(opt.changer).length;
					}

				} else {
					first=1;
				} 

				var c=first-1;
				
				//set slides/index to first
				if(type.changer=="string") {
					
					obj.children(opt.changer).css(opt.offstage).eq(first-1).css(opt.onstage);
					if(type.onclass=="string") {
						opt.control.ind.element
						.removeClass(opt.control.ind.onclass)
						.eq(0)
						.addClass(opt.control.ind.onclass);
					}
				}
				
				/*
					INCREMENT
				*/
				if(type.auto=="object" && type.changer=="string"){
					var c=first;
				
					anim=setInterval(function(){
						animateObj(c);
						if(c==oclength) c=0; else c++;						
					},opt.auto.interval);
					
				}
				
				
				if(type.control=="object" && type.changer=="string") {
					/*
						INDEX CONTROLS
					*/
					if(type.ind=="object") {
						opt.control.ind.element.bind(opt.control.ind.binding, function(){
							if(type.auto=="object") clearInterval(anim);
							
							ti=opt.control.ind.element.index(this);
							
							if(type.arrows=="object") ac=ti;
							
							animateObj(ti);
							return false;						
						});
					}
					
					/*
						ARROW CONTROLS
					*/


					if(type.arrows=="object") {
						/*
							NEXT
						*/
						opt.control.arrows.nxt.bind(opt.control.arrows.binding, function(){
							
							if(type.auto=="object" && ac===false) {
								clearInterval(anim); ac=c;
							} else {
								if(ac===false){
									if(opt.hash){
										ac=c;
									} else {
										ac=0;
									}
								}
								if(ac==oclength) ac=0; else ac++;
							}
							animateObj(ac);	
							return false;						
						});
						/*
							PREVIOUS
						*/
						opt.control.arrows.prv.bind(opt.control.arrows.binding, function(){
							if(type.auto=="object" && ac===false) {
								clearInterval(anim); ac=c;
							} else {
								if(ac===false){
									if(opt.hash){
										ac=c;
									} else {
										ac=0;
									}
								}
								if(ac==0) ac=oclength; else ac--;
							}
							animateObj(ac);
							return false;						
						});
						
					}
					
				}
				
				

			
    		});
    	}
	});
	
})(jQuery);

