(function($) { 

	// plugin definition 
	$.fn.truncate = function(options) { 

		// build main options before element iteration 
		var opts = $.extend({}, $.fn.truncate.defaults, options); 

		// iterate and reformat each matched element 
		return this.each(function() { 
			$this = $(this); 
			// build element specific options 
			var o = $.metadata ? $.extend({}, opts, $this.metadata()) : opts;

			//string truncation
            $this.each(function(){
                if($this.text().length >= o.stringLength){
                    var subText = $this.text().substring(0, o.stringLength + 1);
                    var count = $this.text().split('\('); // does a (carousel filter) result counter exist?            
                    if(count[1]){ // if result count exists, alter truncation
                        count = count[1];
                        subText = $this.text().substring(0, parseInt(o.stringLength - count.length, 10));
                        $this.text(subText + o.replaceWith + '( '+count);
                    }else{
                        $this.text(subText + o.replaceWith);
                    }
                }
            });
            ////
		}); 
	}; 

	// plugin defaults 
	$.fn.truncate.defaults = { 
		stringLength: 18,
		replaceWith: '...'
	}; 
})(jQuery); 

