$(function(){
});

jQuery.fn.truncate = function(criticalValue,rounding,showText,hideText) {
	  	return this.each(function(){
		  	showText = showText ? showText : "Show more";
		  	hideText = hideText ? hideText : "Hide";
		  	idVar = $(this).attr('id');
		  	$(this).after("<div id='"+idVar+"-truncated'></div> <div id='"+idVar+"-hidden' style='display:none'></div>");
		  	oldWordCount = 0;
		  	wordCount = 0;
		  	futureWordCount = 0;
		  	fullUp = false;
	    	$(this).children().each(function(){

			    	oldWordCount = wordCount;
		  			wordCount = wordCount + $(this).text().split(" ").length;
		  			futureWordCount = wordCount + $(this).next().text().split(" ").length;
		  			if($("#"+idVar+"-truncated p").size()==0){
			  			$(this).appendTo("#"+idVar+"-truncated");
		  			}			  			
		  			else if(wordCount<=criticalValue){
						$(this).appendTo("#"+idVar+"-truncated");	  
		  			}
		  			else if (rounding != "down" && fullUp == false && ((criticalValue-oldWordCount>wordCount-criticalValue)||rounding=="up" )){
			  			$(this).appendTo("#"+idVar+"-truncated");
			  			fullUp = true;
		  			}
		  			else{
			  			$(this).appendTo("#"+idVar+"-hidden");
		  			}		  			
	  	
	  		});
	  		if($("#"+idVar+"-hidden").children().size()>0){
			$("<a id='text_tool' href='#'>"+showText+"</a>").click(function(){
	  			if($(this).text()==showText){
	  				$("#"+idVar+"-hidden").show();
	  				$(this).text(hideText);
  				}else{
	  				$("#"+idVar+"-hidden").hide();
	  				$(this).text(showText);
  				}
	  			return false;
  			}).appendTo("#"+idVar+"-truncated");
			}
	    }).remove();
	  }
	
	
	jQuery.fn.tagName = function() {
   		return this.get(0).tagName.toLowerCase();
	}