/*
*
* TooLong
* jQuery plugin to shorten elements text
* http://code.google.com/p/toolong/
* Version 1.0
*
* By Guillaume Gaubert
* http://widgetulous.com/
*
* Date: November, 23 of 2010 11:58AM
*
* Basic use :
* $('#element').TooLong({ 'len' : 14 });
*
*/

(function( $ ){

  $.fn.TooLong = function(options) {  

    var settings = {
      'len' : 20,
      'cutter' : '...',
	  'count' : false,
	  'position' : 'right'
    };

    return this.each(function() {        
      // If options exist, lets merge them
      // with our default settings
      if (options) { 
        $.extend(settings, options);
      }

	// TooLong plugin code here
	var stringLen = $(this).text().length;
	var cutLen = 0;
	
	
	// Check if we use 'ccount' parameter
	if(settings.count)
	{
		// We use it, set the cutter length
		var cutLen = settings.cutter.length;
	}
// alert( stringLen +" > "+ settings.len);

//	if(stringLen > settings.len)
//	{
		var final = $(this).text();
		
		if(settings.position == "right")
		{		
			final = final.substr(0, (stringLen-(stringLen-settings.len))-cutLen) + settings.cutter;
		}
		if(settings.position == "left")
		{
			final = settings.cutter + final.substr((stringLen-settings.len)+cutLen, (stringLen-(stringLen-settings.len)));
		}
		
		if(settings.position == "middle")
		{
			// Middle of the shortened string length
			var middle = Math.floor(settings.len/2);
			// Middle of the cutter string length
			var cmiddle = Math.floor(settings.cutter.length/2);
			// The beginning position of cutter in the middle of the string
			var pos = middle-cmiddle;
			
			var begin = final.substr(0, pos);
			/*// We remove an empty space which can be maleciously hidden here
			if(begin.substr(-1) == " ")
			{
				begin = begin.substr(0, begin.length-1);
			}*/
			
			var finalstring = final.substr(-middle);
			
			final = begin + settings.cutter + finalstring;
			
		}
		
		// Replace
//		alert(final);
				originalText = $(this).text();
				 var t = $(this.cloneNode(true))
				 	.hide()
                    .css({
                        'position': 'absolute',
                        'overflow': 'visible',
                        'max-width': 'none',
                        'max-height': 'none',
                    });
//				alert(t.text());
//				alert("av with"+$(this).width());
				t.css("height", "auto").width($(this).width());
				$(this).after(t);
				
                //alert(t.text());
				var fullHeight = t.height();
				
//				alert("available hight"+$(this).height());
//				alert("available hight test"+ t.height());
				
		 		var avail = $(this).height();
                var test = t.height();
                var foundMin = false, foundMax = false;
                if (test > avail) {
                    //Binary search style trimming of the temp element to find its optimal size
					
                    var min = 0;
                    var max = originalText.length;
                    while (min <= max) {
                        var trimLocation = (min + max) / 2;
                        var text = originalText.substr(0, trimLocation);
                        t.html(text + settings.cutter);
//  						alert(text);
                        test = t.height();
                        if (test > avail) {
                            if (foundMax)
                                foundMin = true;
  
                            max = trimLocation - 1;
                            if (min > max) {
                                //If we would be ending decrement the min and regenerate the text so we don't end with a
                                //slightly larger text than there is space for
                                trimLocation = (max + max - 2) / 2;
                                text = originalText.substr(0, trimLocation);
                                t.html(text +settings.cutter );
                                break;
                            }
                        }
                        else if (test < avail) {
                            min = trimLocation + 1;
                        }
                        else {
                            if (foundMin && foundMax && ((max - min) / max < .2))
                                break;
                            foundMax = true;
                            min = trimLocation + 1;
                        }
                    }
                }
				
//				alert("asdfad"+t.text());
		$(this).html(t.html());

//	}
	
    });

  };
})(jQuery);





