/**
 * jQuery Default Animation Speed Plugin 1.0.1
 * ===========================================
 * 
 * Copyright, License, and Warranty
 * --------------------------------
 * This software is (c) 2007 Michael Melvin <mmelvin0@gmail.com>.
 * 
 * This software is released under the Apache License:
 * <http://www.apache.org/licenses/LICENSE-2.0>
 * 
 * This software is provided WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * 
 * Documentation
 * -------------
 * This is a very simple plugin that allows you to set the default animation
 * duration for all jQuery animations. As of this writing, jQuery 1.2.1 uses a
 * hard-coded value of 400 milliseconds, with no-built in way to set the default
 * (although it is easy enough to <i>get</i> the default.) I needed to be able to
 * set this default globally and on-the-fly for an internal company project, so I
 * decided to do it "the right way" and release it in case anyone else has the
 * same problem.
 * 
 * The plugin adds one function to the global jQuery object (it is not chainable.)
 * It accepts a single parameter, which is any valid animation speed setting.
 * 
 * Calling the function without any parameters or with invalid value will set the
 * animation speed back to the default. The function returns the default animation
 * speed as an integer.
 * 
 * You call the function like this:
 * 
 * $.setDefaultAnimationSpeed("slow");
 * 
 * That's it, enjoy!
 */
(function($) {
	
	/**
	 * Original jQuery.speed() function
	 */
	var original = $.speed;
	
	/**
	 * Default animation duration
	 */
	var duration = original().duration;
	
	/**
	 * Determine if a value is a valid animation speed
	 * 
	 * Valid values are any number and the strings "slow" or "fast".
	 * Additionally, any object with a "duration" property that matches the above criteria is also valid.
	 * 
	 * This function returns false if the value is not valid, otherwise returns the value.
	 * If you pass an object with a "duration" property, the value of that property is returned.
	 * 
	 * @param   {mixed}  speed     Value to check
	 * @return  {mixed}            
	 */
	function valid(speed) {
		if (speed) {
			if (speed.constructor == Number || (speed.constructor == String && (['slow', 'fast'].indexOf(speed) > -1))) {
				return speed;
			} else if (speed.constructor == Object && speed.duration) {
				return valid(speed.duration);
			}
		}
		return false;
	}
	
	$.extend({
		
		/**
		 * Set the default animation duration
		 * 
		 * @param  {mixed}  speed  Any value that jQuery would normally accept as an animation duration.
		 */
		setDefaultAnimationSpeed: function(speed) {
			if ((speed = valid(speed)) !== false) {
				duration = speed;
			} else {
				duration = original().speed;
			}
			return original().speed;
		},
		
		/**
		 * Replacement for (and wrapper around) the original jQuery.speed() function.
		 * 
		 * @param   {mixed}     speed
		 * @param   {string}    [easing]
		 * @param   {function}  [fn]
		 * @return  {object}
		 */
		speed: function(speed, easing, fn) {
			var flag = valid(speed) === false;
			var opt = original(speed, easing, fn);
			if (flag) {
				opt.duration = duration;
			}
			return opt;
		}
		
	});
	
})(jQuery);