/*
	keepRhythm is a jQuery plugin by Jeffrey D. King with KINGdesk Web Design
	feedback may be sent to jk@kingdesk.com
*/

// "(function()" reduces risk of namespace collisions
// the "($)" allows internal code like $(this) without interfering with the user's declaration a jQuery alias
(function($) {
	
	// main plugin definition
	$.fn.keepRhythm = function(options) {

		// use default options if parameter not passed
		// "{}" as first param causes $.fn.pluginName.defaults to remain unmodified
		options = $.extend({}, $.fn.keepRhythm.defaults, options);			

		// if lineHeight parameter is not passed, assume that the standard line-height is that of the body
		// this can not be set at the time of declaring $.fn.keepRhythm.defaults because that is pre page load
		options.lineHeight = ( options.lineHeight.count ) ? options.lineHeight : parseFloat($("body").css('line-height'));

		// for each passed element, keep the rhythm
		return this.each(function() {
			$this = $(this); // allows reference to this single element
			
			var increase = false; // if we can't subtract, this is our failsafe
			var propertyHeight = parseFloat( $this.css( options.property ) );
			var rhythmCorrection;
			var newPropertyHeight;
			if( options.increase !== true ) {
				// try to keep rhythm by subtraction...
				rhythmCorrection = $this.height() % options.lineHeight;
				if( propertyHeight <= rhythmCorrection ) {
					// there is not enough room to subtract without going negative, so we'll add 
					increase = true;
				} else {
					// there is enough overhead, so subtract
					newPropertyHeight = propertyHeight - rhythmCorrection;
				}
			}
			if( ( options.increase === true ) || ( increase === true ) ) {
				// keep rhythm by addition...
				rhythmCorrection = options.lineHeight - $this.height() % options.lineHeight;
				newPropertyHeight = propertyHeight + rhythmCorrection;
			}
			
			// I'd like to just pass property to the .css hash, but it is not processed as you would expect...
			if( options.property == "height" ) {
				// height is different than the rest, in that it is typically a calculated value
				$this.height( newPropertyHeight );
			} else if( ( options.property == "marginTop" ) || ( options.property == "margin-top" ) ) {
				$this.css( { "margin-top" : newPropertyHeight + "px" } );
			} else if( ( options.property == "marginBottom" ) || ( options.property == "margin-bottom" ) ) {
				$this.css( { "margin-bottom" : newPropertyHeight + "px" } );
			} else if( ( options.property == "borderTopWidth" ) || ( options.property == "border-top-width" ) ) {
				$this.css( { "border-top-width" : newPropertyHeight + "px" } );
			} else if( ( options.property == "borderBottomWidth" ) || ( options.property == "border-bottom-width" ) ) {
				$this.css( { "border-bottom-width" : newPropertyHeight + "px" } );
			} else if( ( options.property == "paddingTop" ) || ( options.property == "padding-top" ) ) {
				$this.css( { "padding-top" : newPropertyHeight + "px" } );
			} else {
				$this.css( { "padding-bottom" : newPropertyHeight + "px" } );
			}
		});
		
	};

	// plugin defaults - added as a property on our plugin function
	$.fn.keepRhythm.defaults = {
		'lineHeight' : false, //defaults to body's line-height
		'property': 'padding-bottom',
		'increase' : true,
	};		

})(jQuery);
