// Datagraf jQuery excerpt plugin
// Author: Lars Danielsen
//
// Required: jQuery
//
//
// Synopsis:
//
// jQuery(elem).excerpt(3);
//
// Result is that the text in the handled elements are cut short so that only
// 3 lines are visible, and an … are added if any words were cut away
// autoPosition:    Attemt to position the hint correctly, basic
//
// no html will survive the process, so use only on plain text unless you can
// live with that


jQuery.fn.excerpt = function(init) {
    var defaults = {
        lines: 3,
        utils: {
            start: 0,
            rightSize: 0,
            tooFew: 0,
            tooMany: 0,
            trialIndex: 0,
            lastTrialIndex: 0
        }
    };
    
    return this.each(function() {
        var o = jQuery(this);
        if (typeof init != 'object') {
            init = {lines: init};
        }
        var options = jQuery.extend(defaults, init);
        doit(o, options);
    });
    
    function doit (o, options) {
        options.utils.arrText = o.text().split(' ');
        options.utils.tooMany = options.utils.arrText.length + 1;
        GetSizes(o, options);
        if (o.height() > options.utils.rightSize) {
            var finished = false;
            var escape = 100;
            while (!finished) {
				//alert('tooFew: ' + options.utils.tooFew +
				//	  ' - tooMany: ' + options.utils.tooMany +
				//	  ' - trialIndex: ' + options.utils.trialIndex +
				//	  ' - rightSize: ' + options.utils.rightSize +
				//	  ' - o.height(): ' + o.height());
                shortenText(o, options);
                finished = isFinished(o, options);
                if (escape <=0) {
                    finished = true;
                    //alert('escaped');
                }
                escape--;
            }
            fillElement (o, options, options.utils.tooFew);
        }
    }
	
    function isFinished (o, options) {
        return noProgress (o, options) || isRightSize(o, options) && endOfLineFound(o, options);
    }
	
    function noProgress (o, options) {
        return options.utils.trialIndex == options.utils.lastTrialIndex;
    }
	
    function isRightSize (o, options) {
        return Math.round(o.height()/options.utils.rightSize) == 1;
    }
    
    function endOfLineFound (o, options) {
        return options.utils.tooMany - options.utils.tooFew == 1;
    }
    
    function GetSizes (o, options) {
        var initialHeight = o.height();
        var testchar = '&nbsp;';
        var teststring = testchar;
        o.html(teststring);
        var oneLineHeight = o.height();
        var initialLineCount = initialHeight / oneLineHeight;
        for (var i = 1; i < options.lines; i++) {
            teststring += '<br />' + testchar;
        }
        o.html(teststring);
        options.utils.rightSize = o.height();
        options.utils.trialIndex = Math.floor(options.utils.arrText.length * options.lines / initialLineCount);
        o.html(options.utils.arrText.join(' '));
    }
    
    function fillElement (o, options, index) {
        o.text(options.utils.arrText.slice(options.utils.start, index).join(' ') + ' ' + String.fromCharCode(8230));
    }
    
    function shortenText(o, options) {
        fillElement (o, options, options.utils.trialIndex);
        if (o.height() > options.utils.rightSize) {
            options.utils.tooMany = options.utils.trialIndex;
        } else {
            options.utils.tooFew = options.utils.trialIndex;
        }
		options.utils.lastTrialIndex = options.utils.trialIndex;
        options.utils.trialIndex = Math.ceil((options.utils.tooMany + options.utils.tooFew) / 2);
        //options.utils.fraction *= options.utils.factor;
    }
}

