/*
 * 	RevSlider - jQuery plugin
 *  Author: Ryan LaBarre
 *  
 *	Based on "Easy Slider" by Alen Grakalic	
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

(function($) {

	$.fn.revSlider = function(options) {
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'',
			nextId: 		'nextBtn',	
			nextText: 		'',
			orientation:	'', //  'vertical' is optional;
			speed: 			750,
			showArrows: true
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $("li", obj).length;
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			
			var autoAnimation = "on"; // set to "off" to disable auto-animation
			var autoDirection = "next"; // default direction to begin auto animating
			var autoTime = 8200; // time between auto-scrolls, in ms
			var storedTimeout = null; // only allow one stored jump timeout at a time, and always reference it here
			
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);			
			if(!vertical) $("li", obj).css('float','left');
			if (options.showArrows) {
			  $(obj).after('<span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText
			        +'</a></span> <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>');
  			$("a","#"+options.prevId).hide();
  			$("a","#"+options.nextId).hide();
  			$("a","#"+options.nextId).click(function(){
  				suspendAutoAnimation();
  				animate("next");
  				if (t>=ts) $(this).fadeOut();
  				$("a","#"+options.prevId).fadeIn();
  			});
  			$("a","#"+options.prevId).click(function(){
  				suspendAutoAnimation();
  				animate("prev");
  				if (t<=0) $(this).fadeOut();
  				$("a","#"+options.nextId).fadeIn();
  			});
			}
			$(".promoBullet").click(function(){
				suspendAutoAnimation();
				animate("to", $(this).attr('value'));
				if ($(this).attr('value')<=0) $("a","#"+options.prevId).fadeOut();
				else $("a","#"+options.prevId).fadeIn();
				
				if ($(this).attr('value')<ts) $("a","#"+options.nextId).fadeIn();
				else $("a","#"+options.nextId).fadeOut();
			});
			function setActiveBullet(t) {
				$('.promoBullet').removeClass('selected');
				$('.promoBullet').filter(function() { return ($(this).attr('value') == t); }).addClass('selected');
			}
			
			function autoAnimate () {
				if (autoAnimation != "on") return true;
				
				if (t >= ts) { autoDirection = "prev"; }
				else if (t <= 0) { autoDirection = "next"; }

				animate(autoDirection);
				
				if (options.showArrows) {
  				if (t<=0) $("a","#"+options.prevId).fadeOut();
  				else $("a","#"+options.prevId).fadeIn();
  				
  				if (t<ts) $("a","#"+options.nextId).fadeIn();
  				else $("a","#"+options.nextId).fadeOut();
				}
				
				clearTimeout(storedTimeout);
				storedTimeout = setTimeout(autoAnimate, autoTime);
			}
			
			function resumeAutoAnimation () {
				autoAnimation = "on";
				autoAnimate();
			}
			
			function suspendAutoAnimation () {
				autoAnimation = "off";
				
				// wait a bit then resume autoAnimation
				clearTimeout(storedTimeout);
				storedTimeout = setTimeout(resumeAutoAnimation, 12000);
			}
			
			storedTimeout = setTimeout(autoAnimate, autoTime-500);
			
			function animate(dir, direct) {
				if (dir != "to") {
					if (dir == "next") {
						t = (t>=ts) ? ts : t+1;
					} else {
						t = (t<=0) ? 0 : t-1;
					};
				}
				else {
					directInt = parseInt(direct);
					if (! directInt || directInt <= 0) t = 0;
					else if (directInt >= ts) t = ts;
					else t = directInt;
				}
				setActiveBullet(t);
				if (!vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			if(s>1 && options.showArrows) $("a","#"+options.nextId).fadeIn();	
		});
	};
})(jQuery);
