/*****************************************************************************
 *
 *  TITLE:    AbleEngine - Rotator
 *  MODIFIED: 2011.10.14
 *  AUTHOR:   Graham Wheeler
 *  REQUIRES: AE Core 2.1.1
 *
 ****************************************************************************/

AE.Plugins.Add({
	
	name: "Rotator",
	attachToEvents: "PAGE_LOAD,DOM_UPDATED",
	init: function(scope) {
	
		AE.select(".rotator", scope).each( function() {
			
			var container = AE.select(this);
			var items = container.find(container.attr("itemselector") || "> li");
			var currentItem = 0;
			var delay = Number(container.attr("data-rotationdelay") || 5000);
			var autoCycle = Boolean(container.attr('data-autocycle') || true);
			var transitionSpeed = Math.floor(Number(container.attr("data-transitionspeed") || 1000) / 2);
			var order = container.attr("data-rotationorder") || "sequential";
			var transitionType = container.attr("data-transitiontype") || "fadeout-fadein";

			container.startTimer = function() {
			  if (!autoCycle) {
			    return;
			  }
				if (AE.debug) { AE.console.print("Rotator.startTimer()"); }
				container.cycleTimer = setTimeout( function() {
				  container.trigger((order == 'random')?'ROTATOR_CYCLE_RANDOM':'ROTATOR_CYCLE_NEXT'); 
        }, AE.select(items[currentItem]).attr("data-rotationdelay") || delay );
			};
			
			container.bind('ROTATOR_AUTOCYCLE_OFF', function(){
			  autoCycle = false;
			  clearTimeout(container.cycleTimer);
			});
			
      container.bind('ROTATOR_AUTOCYCLE_ON', function(){
        autoCycle = true;
        container.startTimer();
      });
			
			container.bind('ROTATOR_CYCLE_PREV', function(e){
			  var oldItem = currentItem;
			  currentItem = (currentItem > 0) ? currentItem-1 : items.length -1;
			  container.rotateTo(oldItem, currentItem);		  
			});
      
      container.bind('ROTATOR_CYCLE_NEXT', function(e){
        var oldItem = currentItem;
        currentItem = (currentItem < items.length - 1) ? currentItem+1 : 0;
        container.rotateTo(oldItem, currentItem);        
      });
      
      container.bind('ROTATOR_CYCLE_TO', function(e, newIndex){
        var oldItem = currentItem;
        currentItem = newIndex;
        container.rotateTo(oldItem, currentItem);
      });
      
      container.bind('ROTATOR_CYCLE_RANDOM', function(e){
        var oldItem = currentItem;
        var counter = 0;
        while (currentItem == oldItem && counter < 1000) {
          currentItem = Math.floor( Math.random() * items.length );
          counter++;
        }
        container.rotateTo(oldItem, currentItem);
      });
			
			container.rotateTo = function(oldIndex, newIndex) {
				if (AE.debug) { AE.console.print("Rotator.rotateTo("+oldIndex+", "+newIndex+")"); }
        container.trigger('ROTATOR_TRANSITION_START', [oldIndex, newIndex]);
				switch ( transitionType ) {					
					case "fadeout-fadein": {
						if( AE.debug ) { AE.console.print("Rotator => transitionType = fadeout-fadein"); }
						AE.select(items[oldIndex]).removeClass("active").fadeOut(transitionSpeed, function() {
							AE.select(items[newIndex]).addClass("active").fadeIn(transitionSpeed, function() {
								container.startTimer();
								container.trigger('ROTATOR_TRANSITION_END');
							});
						});
						break;
					}
					case "crossfade": {
						if( AE.debug ) { AE.console.print("Rotator => transitionType = crossfade"); }
						AE.select(items[oldIndex]).removeClass("active").fadeOut(transitionSpeed);
						AE.select(items[newIndex]).addClass("active").fadeIn(transitionSpeed, function() {
							container.startTimer();
							container.trigger('ROTATOR_TRANSITION_END', [oldIndex, newIndex]);
						});
						break;
					}
					case "instant": {
						if( AE.debug ) { AE.console.print("Rotator => transitionType = instant"); }
						AE.select(items[oldIndex]).removeClass("active").hide();
						AE.select(items[newIndex]).addClass("active").show();
						container.startTimer();
						container.trigger('ROTATOR_TRANSITION_END', [oldIndex, newIndex]);
						break;
					}
					default: break;
				}
			};

			if ( items.length > 1 ) {
				items.hide().eq(currentItem).show();
				container.startTimer();
			} else {
				items.eq(0).show();
			}
			
		});
	}

});
