// jQuery Capitalize Plugin v.0.01a
// (c) W.R. de Vos
// License: MIT
// 
// Feel free to re-use this code, but leave the above copyright notice.
//

(function($){
	$.fn.capitalize = function(options) {
		var defaults = {
			// class name of the capital div
			cssClass: 'capital',
			// callback function
			callback: function(){}
		};
		var options = $.extend(defaults, options);

		return this.each(function() {
			if (this.nodeName == 'DIV') {
				var paragraph = $('p', this)[0];
				var container = this;
			} else {
				if (this.nodeName == 'P') {
					var paragraph = this;
					if (this.parentNode.nodeName != 'DIV') {
						$(this).wrap('<div class="capitalized" />');
						var container = this.parentNode;
					}
				} else {
					return false;
				}
			}

			var text = jQuery.trim($(paragraph).text());

			var letter = text.substr(0,1);

			var text = text.substr(1,text.length);

			function capitalize() {
				$(paragraph).text(text);
				$(container).prepend('<div class="'+ options.cssClass + '">' + letter + '</div>');

			}
			capitalize();
			options.callback();
		});
	}
})(jQuery);
