/*
Copyright Justin Whitford 2006.
  http://www.whitford.id.au/
Perpetual, non-exclusive license to use this code is granted
on the condition that this notice is left in tact.
*/

var delim = '^';
var titleReplace = / ?[|-].*/;
var trailLength = 9;
var chunks;
var DAY = 24 * 60 * 60 * 1000;

var titleMaxlen = 24;
var placeHolder = '...';

function doCrumbs() {
    if (cookieTest('xxx')) {
        crumbList = new CrumbList();
        if (getCookie('trailLinks')) {
            var staleLinkCrumbs = getCookie('trailLinks').split(delim);
            var staleTextCrumbs = getCookie('trailText').split(delim);
            var nstale = staleLinkCrumbs.length;
            var startPos =
                (nstale < trailLength ||
                 document.location == staleLinkCrumbs[nstale - 1])
                 ? 0 : 1;
            for (i = startPos; i < nstale; i++) {
                crumbList.add(staleLinkCrumbs[i], staleTextCrumbs[i]);
            }
        }
        if (document.location != crumbList.links[crumbList.links.length-1]) {
            crumbList.add(document.location,document.title);
        }
        setCookie('trailLinks',crumbList.links.join(delim),1);
        setCookie('trailText',crumbList.text.join(delim),1);
        crumbList.output();
    }
}

function CrumbList() {
    this.links = new Array();
    this.text = new Array();
    this.lookup = {};
    this.add = crumbListAdd;
    this.output = crumbListShow;
}

function shortenTitle(title, placeholder) {
    if (!placeholder) {
        placeholder = '';
    }
    var tlen = title.length;
    if (tlen < titleMaxlen) {
        return title;
    }
    var newstr = title.replace(titleReplace, '');
    if (newstr != title) {
        return newstr + placeholder;
    }
    var plen = placeHolder.length;
    var over = tlen - titleMaxlen;
    var rlen = over + plen;
    return title.substr(0, tlen - rlen) + placeholder;
}

function shortenTitleMiddle(title) {
    var tlen = title.length;
    if (tlen < titleMaxlen) {
        return title;
    }
    var plen = placeHolder.length;
    var over = tlen - titleMaxlen;
    var rlen = over + plen;
    var startidx = parseInt(tlen / 2 - rlen / 2);
    var endidx = startidx + rlen;
    var shortened = title.substr(0, startidx) + placeHolder + title.substr(endidx);

    return shortened;
}

function crumbListAdd(href,text) {
    if (this.lookup[href]) {
        return;
    }
    this.lookup[href] = 1;
    this.links[this.links.length] = href;
    //this.text[this.text.length] = shortenTitle(text);
    this.text[this.text.length] = text;
}

function crumbListShow() {
    var nlinks = this.links.length;
    var text;
    for (var i = 0; i < nlinks; i++) {
        if (this.links[i] == window.location.protocol + "//" + window.location.hostname + "/") {
            text = "Home";
        }
        else {
            text = this.text[i];
        }
        if (i == nlinks - 1) {
            document.write(
                    ((i == 0) ? "" : " &nbsp &gt; &nbsp ")
                    + '<span title="' + this.text[i] + '">'
                    + shortenTitle(text) + "</span>"
                    );
        }
        else {
            document.write(
                    ((i == 0) ? "" : " &nbsp &gt; &nbsp ")
                    + '<a href="' + this.links[i] + '"'
                    + ' title="' + text + '">'
                    + shortenTitle(text) + "</a>"
                    );
        }
    }
}

function cookieTest(name) {
    try {
        setCookie(name,'true',1);
        chunks = document.cookie.split("; ");
        return (getCookie(name)=='true');
    }
    catch(e) {
        return false;
    }
}

function getCookie(name) {
    var returnVal = null;
    var nchunks = chunks.length;
    for (var i = 0; i < nchunks; i++) {
        var chunk = chunks[i].split("=");
        returnVal = (chunk[0] == name)
            ? unescape(chunk[1])
            : returnVal;
    }
    return returnVal;
}

function setCookie(name, value, days) {
    if (value != null && value != "" && days > 0) {
        var expiry=
            new Date(new Date().getTime() + days * DAY);
        document.cookie=
            name +"="+ escape(value) +"; expires="
            + expiry.toGMTString();
        chunks = document.cookie.split("; ");
    }
}


