/*
 * 	Snapple Module 1.0 - jQuery plugin
 *	written by Mike Fey (Hello Monday)	
 *	http://hellomonday.com
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 *  creates "swinging" effect for element
 */
 (function($) {

  var methods = {
    init: function(options) {

      // default configuration properties
      var defaults = {
        intensity: 1, //swing intensity
        delay: 0 //delay between swings
      }

      var mainDiv;
      var moveIntensity = 0;
      var moveInt;
      var opts = $.extend(defaults, options);
      var switchTimeout = 0;

      return this.each(function() {
        mainDiv = $(this);
        mainDiv.data('moveInterval', moveInt);
        setTimeout(function() {
          swing();
          swing2();
        },
        opts.delay);

      });
      
			//starts the swing timer
      function swing() {
        moveint = setInterval(swing2, 17000);
      }
      
			//swing animation part 2
      function swing2() {
        var mAmount = 4 + Number(opts.intensity);
        mainDiv.stop(true, true).animate({
          rotate: mAmount + 'deg'
        },
        300);
        moveint = setTimeout(swing3, 300)
      }
       
			//swing animation part 3
      function swing3() {
        var mAmount = -3 - Number(opts.intensity);
        mainDiv.stop(true, true).animate({
          rotate: mAmount + 'deg'
        },
        300);
        moveint = setTimeout(swing4, 300)
      }
      
			//swing animation part 4
      function swing4() {
        var mAmount = 2 + Number(opts.intensity);
        mainDiv.stop(true, true).animate({
          rotate: mAmount + 'deg'
        },
        400);
        moveint = setTimeout(swing5, 400)
      }
      
			//swing animation part 5
      function swing5() {
        mainDiv.stop(true, true).animate({
          rotate: '0deg'
        },
        500);
      }

    },
    
		//stops the swing
    stop: function() {
      return this.each(function() {
        mainDiv = $(this);
        var mt = mainDiv.data('moveInterval');
        if (mt) {
          clearInterval(mt);
        }
      });
    }
  };

  //calls the proper method based on the string passed
  $.fn.snappleSwing = function(method) {
    if (methods[method]) {
      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method) {
      return methods.init.apply(this, arguments);
    } else {
      $.error('Method ' + method + ' does not exist on snappleSwing');
    }
  };

})(jQuery);
