/*
	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.6 beta
*/

/*
OPTIONS:
	auto: {interval:5000},
	changer: 'img',
	subchanger: {
		container: $('#chng div'),
		element: 'img',
		interval: 5000
		onstage: {
			opacity:1
		},
		offstage: {
			opacity:0
		},
		ext: {
			queue:false,
			duration:500,
			easing:'swing'
		}
	},
	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'
	},
	hash: false
	
*/


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

(function($){

 	$.fn.extend({ 
 		rotary: function(options) {
	
			var defaults = {
				auto: {interval:3000},
				changer: 'img',
				subchanger: false,
				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;
				var subanim;
				var c;
				var subc;
				
				//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 sub slides
				function animateSubObj(ind, subind){
					opt.subchanger.container.eq(ind)
					.children(opt.subchanger.element)
					.animate(opt.subchanger.offstage,{
						queue:opt.subchanger.ext.queue,
						easing: opt.subchanger.ext.easing,
						duration:opt.subchanger.ext.duration
					})
					.eq(subind)
					.animate(opt.subchanger.onstage,{
						queue:opt.subchanger.ext.queue,
						easing: opt.subchanger.ext.easing,
						duration:opt.subchanger.ext.duration
					});
				}
				
				//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
					});
					
					
					//subchanger
					
					if (typeof(opt.subchanger)=='object') {
						//setup
						opt.subchanger.container
						.eq(ind)
						.children(opt.subchanger.element)
						.css(opt.subchanger.offstage)
						.eq(0)
						.css(opt.subchanger.onstage);
						
						
						clearInterval(subanim);
						subc=0;
						var sublen=opt.subchanger.container.eq(ind).children(opt.subchanger.element).length-1;
						
						subanim=setInterval(function(){
							if(subc>=sublen)subc=0; else subc++;
							animateSubObj(ind,subc);
						},opt.subchanger.interval);
					};
					
					//--
					
					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;
				} 

				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);
					
				}
				
				/*
					CONTROLS
				*/
				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);

