// ------ dvarotator -----
/*
Copyright (C) 2010 dvagroup.co.uk 
Requires: jQuery

Launch with:
	$(document).ready(function(){
		$().dvarotator();  
	});

Options are available:
	$(document).ready(function(){
		$().dvarotator( {  
	   dvaRotatorSlideHold: 7000,  
	   dvaRotatorOutSpeed: 300,  
	   dvaRotatorInSpeed: 2000
	   dvaRotatorType: 'fade'
		 dvaRotatorJQEase: 'easeOutSine'
		});  
	});

Option details:
		dvaRotatorSlideHold = 7000; // in ms
		dvaRotatorOutSpeed = 300; // in ms
		dvaRotatorInSpeed = 2000; // in ms
		// dvaRotatorPause = 0; // delay between fadeout and fadein *not currently used
		// dvaRotatorFinishFades = TRUE; // force fadeout to finish before fadein *not currently used
	  dvaRotatorTypeIn or Out: 'fade' 'overlay-' 'slide-' or 'none' to skip transition

		Now uses: jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
		dvaRotatorJQEase: 'easeInBounce',

To Do: 
		"Reveal" effect, animate OUT to reveal static IN underneath (reverse of "overlay")
		upgrade button change effect, need to define button css in settings? can we animate between 2 css classes?		
		Next/Previous navigation as well as buttons
		
*/

(function($){  
	$.fn.dvarotator = function(options) {
		
	  var defaults = {  
			dvaRotatorSlideHold: 3000,  
			dvaRotatorOutSpeed: 500,  
			dvaRotatorInSpeed: 500,
			dvaRotatorType: "fade",
			dvaRotatorJQEase: "easeOutSine"
	  };  
	  var options = $.extend({}, defaults, options);  
	
		// required variables
		var dvaRotatorCount 				= $(".dvaRotatorSelector a").size(); // count number of images in this instance
		var dvaRotatorDisplayX 			= $(".dvaRotatorDisplay").width(); // auto detect display width from CSS
		var dvaRotatorDisplayY 			= $(".dvaRotatorDisplay").height(); // auto detect display height from CSS
		var dvaRotatorCurrentId 		= Math.floor(Math.random()*dvaRotatorCount)+1; // random start
		var dvaRotatorOldID 				= dvaRotatorCurrentId;
		var dvaRotatorRunning 			= true; // set initial value for running the rotator (pause in cycle)
		var dvaRotatorSubType 			= options.dvaRotatorType.substring(options.dvaRotatorType.indexOf('-')); // to pull specific directions


		// --- transition function ---
		function doTransitions(outID,inID) {
			
			// Setup start and end values depending on animation direction
			// Notes: 
			//    r=right, l=left, t=top, b=bottom
			//    dvaRotatorOutCSS   = start CSS state of OUT object * REUSE *
			//    dvaRotatorInCSS    = start CSS state of IN object
			//    dvaRotatorAnimCSS  = CSS properties and values to be animated

		 	if (dvaRotatorSubType.indexOf('-rl')>-1) {
	     	// var dvaRotatorOutCSS 	= { 'z-index': '1' + outID, 'display' : 'none', 'left' : '0px' };
	     	var dvaRotatorInCSS		= { 'z-index' : '5000', 'display' : 'block', 'left' : dvaRotatorDisplayX + 'px' };
				var dvaRotatorAnimCSS	= {	'left': '-='+dvaRotatorDisplayX };
				if (options.dvaRotatorType.indexOf('alt-')>-1) { dvaRotatorSubType = '-lr'; } // switch dvaRotatorSubType for alternating anims
	
			} else if (dvaRotatorSubType.indexOf('-lr')>-1) {
	     	//var dvaRotatorOutCSS 	= { 'z-index': '1' + outID };
	     	var dvaRotatorInCSS		= { 'z-index' : '5000', 'display' : 'block', 'left' : '-' + dvaRotatorDisplayX + 'px' };
				var dvaRotatorAnimCSS	= {	'left': '+='+dvaRotatorDisplayX };
				if (options.dvaRotatorType.indexOf('alt-')>-1) { dvaRotatorSubType = '-rl'; } // switch dvaRotatorSubType for alternating anims

			} else if (dvaRotatorSubType.indexOf('-tb')>-1) {
				// var dvaRotatorOutCSS 	= { 'z-index': '1' + outID, 'display' : 'none', 'top' : '0px' };
	     	var dvaRotatorInCSS		= { 'z-index' : '5000', 'display' : 'block', 'top' : '-'+dvaRotatorDisplayY+'px' };
				var dvaRotatorAnimCSS	= {	'top': '+='+dvaRotatorDisplayY };
				if (options.dvaRotatorType.indexOf('alt-')>-1) { dvaRotatorSubType = '-bt'; } // switch dvaRotatorSubType for alternating anims
				
			} else { // -bt
				// var dvaRotatorOutCSS 	= { 'z-index': '1' + outID, 'display' : 'none', 'top' : '0px' };
	     	var dvaRotatorInCSS		= { 'z-index' : '5000', 'display' : 'block', 'top' : dvaRotatorDisplayY+'px' };
				var dvaRotatorAnimCSS	= {	'top': '-='+dvaRotatorDisplayY };
				if (options.dvaRotatorType.indexOf('alt-')>-1) { dvaRotatorSubType = '-tb'; } // switch dvaRotatorSubType for alternating anims
				
			} // end start and end values


		 	// select transition type
		 	// SLIDE IN/OUT = "slide-rl", "slide-lr", "slide-tb", "slide-bt"
		 	// or ALTERNATE = "slidealt-rl", "slidealt-lr", "slidealt-tb", "slidealt-bt"
		 	if (options.dvaRotatorType.indexOf('slide')>-1) {

   	   	// out...
     		$(".dvaRotatorDisplay #image"+outID).animate( dvaRotatorAnimCSS,
     			{ 
     				duration: options.dvaRotatorOutSpeed,
     				easing: options.dvaRotatorJQEase, 
     				complete: function(){}
	     		}); // end move in			

   	   	// in...
				$(".dvaRotatorDisplay #image"+inID).css( dvaRotatorInCSS ); // set start position of new image
     		$(".dvaRotatorDisplay #image"+inID).animate( dvaRotatorAnimCSS,
     			{ 
     				duration: options.dvaRotatorInSpeed,
     				easing: options.dvaRotatorJQEase, 
     				complete: function(){}
	     		}); // end move in
	     		

			// OVERLAY (animate IN over a static) = "overlay-rl", "overlay-lr", "overlay-tb", "overlay-bt"
		 	// or ALTERNATE = "overlayalt-rl", "overlayalt-lr", "overlayalt-tb", "overlayalt-bt"
		 	} else if (options.dvaRotatorType.indexOf('overlay')>-1) {

	     	// simple actions to control static
	     	$(".dvaRotatorDisplay #image"+outID).css({'z-index': '1' + outID}); // push to lower index	
				$(".dvaRotatorDisplay #image"+dvaRotatorOldID).css({'display' : 'none', 'z-index' : '0'}); // hide unseen image blocks in case of conflict in overlay priorities
				dvaRotatorOldID = outID; // reset for next round

   	   	// in...
				$(".dvaRotatorDisplay #image"+inID).css( dvaRotatorInCSS ); // set start position of new image
     		$(".dvaRotatorDisplay #image"+inID).animate( dvaRotatorAnimCSS,
     			{ 
     				duration: options.dvaRotatorInSpeed,
     				easing: options.dvaRotatorJQEase, 
     				complete: function(){}
	     		}); // end bounce in


			// REVEAL (animate OUT over a static) = "reveal-rl", "reveal-lr", "reveal-tb", "reveal-bt"
		 	// or ALTERNATE = "revealalt-rl", "revealalt-lr", "revealalt-tb", "revealalt-bt"
		 	} else if (options.dvaRotatorType.indexOf('reveal')>-1) {

   	   	// in...
	     	// simple actions to control static
	     	$(".dvaRotatorDisplay #image"+inID).css({'display' : 'block', 'z-index': '0'}); // push to lower index	
				$(".dvaRotatorDisplay #image"+dvaRotatorOldID).css({'display' : 'none', 'z-index' : '0'}); // ensure last block is hidden in case of conflict in overlay priorities
				dvaRotatorOldID = inID; // reset for next round

				$(".dvaRotatorDisplay #image"+outID).css({'z-index' : '5000'}); // set start position of new image
     		$(".dvaRotatorDisplay #image"+outID).animate( dvaRotatorAnimCSS,
     			{ 
     				duration: options.dvaRotatorInSpeed,
     				easing: options.dvaRotatorJQEase, 
     				complete: function(){}
	     		}); // end bounce in


			// FADE - simple cross fade, other transitions possible?
			} else if (options.dvaRotatorType.indexOf('fade')>-1) {	
	     	// out...
	     	$(".dvaRotatorDisplay #image"+outID).animate({'opacity': 'hide'}, { 
     				duration: options.dvaRotatorInSpeed,
     				easing: options.dvaRotatorJQEase, 
     				complete: function(){}
     		}); // fade out last image (any visible image)

	      // in...
	     	$(".dvaRotatorDisplay #image"+inID).animate({'opacity': 'show'}, { 
     				duration: options.dvaRotatorInSpeed,
     				easing: options.dvaRotatorJQEase, 
     				complete: function(){}
     		}); // fade out last image (any visible image)


			} else {	
				// SNAP ON/OFF
	     	// out...
	     	$(".dvaRotatorDisplay #image"+outID).fadeOut(1); // fade out last image (any visible image)
				/* $(".dvaRotatorDisplay .productRotator .headline").fadeOut(options.dvaRotatorOutSpeed); */ // FOR THE LABEL/CAPTION    
	      // in...
	      $(".dvaRotatorDisplay #image"+inID).fadeIn(1); // fade in image (this id)
				/* $(".dvaRotatorDisplay .productRotator .headline").fadeIn(options.dvaRotatorInSpeed); */ // FOR THE LABEL/CAPTION 
				   
			} // end type


			// --- BUTTONS ---
			// finish with button change
			$(".dvaRotatorSelector #button"+outID).removeClass("currentId"); // remove button ON state (any id)     
			$(".dvaRotatorSelector #button"+inID).addClass("currentId"); // remove button ON state (any id)						

		} // end transition function



		// --- auto change & time delay ---
		function doAutoRotate() {						
			if (dvaRotatorRunning == true) {
				var dvaRotatorNewID = dvaRotatorCurrentId; dvaRotatorNewID++; // get next auto id
				if (dvaRotatorNewID > dvaRotatorCount) { dvaRotatorNewID = 1; } // reset loop when at end
				// only do transition if we have a different id
				if (dvaRotatorCurrentId != dvaRotatorNewID) {
			     	doTransitions(dvaRotatorCurrentId,dvaRotatorNewID); // trigger transitions
						dvaRotatorCurrentId = dvaRotatorNewID; // reset current ids     	
				} // end if
			} else {			
				// we just skip the increment and transition because of button click
				dvaRotatorRunning = true;
			} // end if rotate=true
			// only if count is 2 or more
			if (dvaRotatorCount > 1) {
				setTimeout (function() { doAutoRotate()} , options.dvaRotatorSlideHold, "JavaScript"); // timer and launch self again
			} // end if count
		} // end doAutoRotate


		// --- button actions ---
		$(".dvaRotatorSelector a").click(function(event){
     	event.preventDefault();
			dvaRotatorNewID = this.id.replace(/[^0-9]/g, ''); // strip label and leave unique id number
			// skip if same item
			if (dvaRotatorCurrentId != dvaRotatorNewID) {
	     	doTransitions(dvaRotatorCurrentId,dvaRotatorNewID); // trigger transitions
				dvaRotatorCurrentId = dvaRotatorNewID; // reset rotatorCurrentId
				dvaRotatorRunning = false;
			}	// end if
		}); // end button actions


		// functions defined, now start the rotator (providing there are 2 or more items)
		if (dvaRotatorCount > 1) {
			doAutoRotate(); 
		} // end if
		
 };
})(jQuery);
