/*
 * jQuery FlexSlider v1.4
 * http://flex.madebymufffin.com
 *
 * Copyright 2011, Tyler Smith
 * Free to use under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 *
 */


(function ($) {
  $.fn.extend({
    flexslider: function(options) {
      //Plugin options and their default values
      var defaults = {
        slideshowSpeed: 4000,           //Set the speed of the slideshow cycling, in milliseconds
        animationDuration: 500,         //Set the speed of animations, in milliseconds
			}
			
			//Get slider, slides, and other useful variables
			var options =  $.extend(defaults, options),
			    slider = this,
			    slides = $('.slides li', slider),
			    length = slides.length;
			    ANIMATING = false,
          currentSlide = 0;
      
      slides.hide().eq(currentSlide).fadeIn(options.animationDuration);
      
      control = $('#slides-controls span');
      control.live('click', function(){
				if($(this)[0] == control[0] && currentSlide != 0)
					flexAnimate(0);
				if($(this)[1] == control[1] && currentSlide != 1)
					flexAnimate(1);
      });
      	
    	///////////////////////////////////////////////////////////////////
    	// FLEXSLIDER: ANIMATION TYPE
    	function flexAnimate(target) {
        if (!ANIMATING) {
          ANIMATING = true;
       	  slider.css({"minHeight": slides.eq(currentSlide).height()});
     	    slides.eq(currentSlide).fadeOut(options.animationDuration, function() {
            slides.eq(target).fadeIn(options.animationDuration, function() {
              ANIMATING = false;
              currentSlide = target;
            });
            slider.css({"minHeight": "inherit"});
          });
      	}
  	  }
    	///////////////////////////////////////////////////////////////////
    	      
    	//////////////////////////////////////////////////////////////////
      //FLEXSLIDER: ANIMATION SLIDESHOW
        
        function animateSlides() {
          if (ANIMATING) {
            return;
          } else {
        	  var target = (currentSlide == length - 1) ? 0 : currentSlide + 1;

        	  if (options.controlNav) {
          	  controlNav.removeClass('active');
          	  controlNav.eq(target).addClass('active');
      	    }
      	  
        	  flexAnimate(target);
          }
        }

        var animatedSlides;
        //pauseOnHover
        slider.hover(function() {
          clearInterval(animatedSlides);
        }, function() {
          animatedSlides = setInterval(animateSlides, options.slideshowSpeed);
        });
        
        //Initialize animation
         animatedSlides = setInterval(animateSlides, options.slideshowSpeed);
    	//////////////////////////////////////////////////////////////////

	  }
  });
  
})(jQuery);
