﻿function shortenBreadcrumb(options) {
    /*  
    Shortens the breadcrumb to a specified width by removing the text from one list item
    at a time and replacing it with "..." - accepts an options object as an optional
    parameter with two options:
    shortenBreadcrumb({
    breadcrumb : $('div#breadcrumb > ul'),
    maxWidth : 725
    });
            
    The first is the selector for the breadcrumb ul, the second is the maximum width you
    want it to be.  If not set, the max width is the width of the container minus the width
    of the paragraph next to it ("You are here:").
    -JM
    */

    var breadcrumb = $('div#breadcrumb > ul');
    var maxWidth = parseInt($('div#breadcrumb').innerWidth()) - parseInt($('div#breadcrumb p').outerWidth(true)) + "px";

    if (options != undefined) {
        if (options.breadcrumb != null) { breadcrumb = options.breadcrumb; }
        if (options.maxWidth != null) { maxWidth = options.maxWidth; }
    }

    var levelCount = breadcrumb.find('li').size();
    var shortEnough = false;
    var totalWidth;
    while (shortEnough == false) {
        totalWidth = 0;
        breadcrumb.children('li').each(function() {
            totalWidth += $(this).outerWidth(true);
        });
        if (totalWidth > maxWidth) {
            var li = breadcrumb.children('li').not('.short').eq(1);
            li.addClass('short');
            li.children('a').attr('title', li.children('a').html());
            li.children('a').html('...');
        }
        else {
            shortEnough = true;
        }
    }
}
