/**
 * Utility class holds convenience methods for use across all CG shared
 * libraries
 * 
 * @requires jQuery
 */

require('jQuery', 'CG.Util');

(function($) {
	/**
	 * jQuery plugin to get data from HTML5 data attributes
	 * 
	 * @param String key The name of the data key.  The attribute should 
	 * 						be defined in the attribute using this notation:
	 * 						data-someattribute="somevalue"
	 * @return Object Returns undefined (if the attribute doesn't exist)
	 */
	$.fn.getData = function(key) {
		var att = this.attr('data-' + key), data;
		
		if(att) {
			try {
				data = eval('(' + att + ')');
			} catch (e) {
				data = att;
			};
		}
		
		return data;
	};
	
	/**
	 * jQuery plugin to truncate content for the all matched elements
	 * 
	 * @param Int length The number of characters before content is truncated
	 * @param String elips The string to indicate content has been truncated
	 * @return jQuery The original jQuery object
	 */
	$.fn.truncate = function(length, elips) {
		var length = length || 20,
			elips = elips || '...';

		return this.each(function() {
			var obj = $(this),
				content = obj.text();
			
			if(content.length > length) {
				obj.text(content.substr(0, length) + elips);
			}
		});
	};
	
})(jQuery);