// Create a self-executing function closure so that we map the
// jQuery library to the $ variable without any global conflicts.
/***************************************************************
 *	 			$.from() Utility Function
 ***************************************************************/
(function($){
    $.fn.from = function(ancestors){
        ancestors = jQuery(ancestors);
        return (this.filter(function(){
            var myAncestors = $(this).parents();
            for (var i = 0; i < myAncestors.size(); i++) {
                if (ancestors.index(myAncestors[i]) >= 0) {
                    return (true);
                }
            }
            return (false);
        }));
    }
})(jQuery);
/* ====== DO NOT EDIT ABOVE THIS LINE ====== */

$(document).ready(function(){
    /***************************************************************
     *	 			Code that handles the correct
     *				styling/display of the leftnav
     ***************************************************************/
    /* Check if url contains 'index.html'. If not then add it to the url. */
    //First check if it's got some other name than index.html...
    var curr_url = "";
    var fname = ($.url.attr("file") == null) ? "" : $.url.attr("file");
    switch (fname) {
        case "":
            curr_url = $.url.attr("path") + "index.html";
            break;
        case "index.html":
            curr_url = $.url.attr("path");
            break;
        default:
            curr_url = $.url.attr("path").substr(0, ($.url.attr("path").length - $.url.attr("file").length)) + "index.html";
            break;
    }
    var curr_url_el;
    var inLeftNav = 0;
    
    //If URL in leftnav?
    if ($("a[href$='" + curr_url + "']").from("#leftnav").length > 0) {
        curr_url_el = $("a[href$='" + curr_url + "']").from("#leftnav");
        
        curr_url_el.parent("li").addClass("active");
        if (curr_url_el.from("#leftnav ul li ul")) {
            curr_url_el.parent("li").parent().parent().addClass("activechild");
            curr_url_el.parent("ul").show();
        }
        curr_url_el.parent().children("ul:first").show();
        inLeftNav = 1;
    }
    else {
        //strip off last subfolder from URL and test against leftnav urls.
        
        curr_url = curr_url.replace(/\/index.html/i, "");
        var fsIndex = curr_url.lastIndexOf("/");
        curr_url = curr_url.substring(0, fsIndex) + "/index.html";
        
        curr_url_el = $("a[href$='" + curr_url + "']").from("#leftnav");
        inLeftNav = 0;
    }
    
    /***************************************************************
     * Title Caps
     *
     * Ported to JavaScript By John Resig - http://ejohn.org/ - 21 May 2008
     * Original by John Gruber - http://daringfireball.net/ - 10 May 2008
     * License: http://www.opensource.org/licenses/mit-license.php
     ***************************************************************/
    (function(){
        var small = "(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)";
        var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
        
        this.titleCaps = function(title){
            var parts = [], split = /[:.;?!] |(?: |^)["Ò]/g, index = 0;
            
            while (true) {
                var m = split.exec(title);
                
                parts.push(title.substring(index, m ? m.index : title.length).replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all){
                    return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all);
                }).replace(RegExp("\\b" + small + "\\b", "ig"), lower).replace(RegExp("^" + punct + small + "\\b", "ig"), function(all, punct, word){
                    return punct + upper(word);
                }).replace(RegExp("\\b" + small + punct + "$", "ig"), upper));
                
                index = split.lastIndex;
                
                if (m) 
                    parts.push(m[0]);
                else 
                    break;
            }
            
            return parts.join("").replace(/ V(s?)\. /ig, " v$1. ").replace(/(['Õ])S\b/ig, "$1s").replace(/\b(AT&T|Q&A)\b/ig, function(all){
                return all.toUpperCase();
            });
        };
        
        function lower(word){
            return word.toLowerCase();
        }
        
        function upper(word){
            return word.substr(0, 1).toUpperCase() + word.substr(1);
        }
    })();
    
    /***************************************************************
     * 			Function generating the breadcrumb
     * 			Only works for pages that exist in the leftnav.
     ***************************************************************/
    function genBc(){
        var bc_str = "<div id=\"bc\"><span id=\"bctop\"><a href=\"/index.html\">US Home</a></span> &gt;";
        
        if (inLeftNav == 0) {
            if (curr_url_el.from("#leftnav ul ul").length > 0) {
                //2nd level link
                bc_str = bc_str + "&nbsp;<span id=\"bcmain\"><a href=\"" + curr_url_el.closest("#leftnav").children("h3").children("a").attr("href") + "\">" + $.trim(curr_url_el.closest("#leftnav").children("h3").text()) + "</a></span>&nbsp;&gt;";
                bc_str = bc_str + "&nbsp;<span><a href=\"" + curr_url_el.closest("ul").prev("a").attr("href") + "\">" + $.trim(stripCompTxt(curr_url_el.closest("ul").prev("a").text())) + "</a></span>&nbsp;&gt;";
                bc_str = bc_str + "&nbsp;<span><a href=\"" + curr_url_el.attr("href") + "\">" + $.trim(stripCompTxt(curr_url_el.text())) + "</a></span>&nbsp;&gt;";
                bc_str = bc_str + "&nbsp;<span>" + titleCaps($.trim($("h1").text())) + "</span>";
            }
            else 
                if (curr_url_el.from("#leftnav ul").length > 0) {
                    //1st level link
                    bc_str = bc_str + "&nbsp;<span id=\"bcmain\"><a href=\"" + curr_url_el.closest("#leftnav").children("h3").children("a").attr("href") + "\">" + $.trim(curr_url_el.closest("#leftnav").children("h3").text()) + "</a></span>&nbsp;&gt;";
                    bc_str = bc_str + "&nbsp;<span><a href=\"" + curr_url_el.attr("href") + "\">" + $.trim(stripCompTxt(curr_url_el.text())) + "</a></span>&nbsp;&gt;";
                    bc_str = bc_str + "&nbsp;<span>" + titleCaps($.trim($("h1").text())) + "</span>";
                }
                else 
                    if (curr_url_el.parent().is("h3")) {
                        bc_str = bc_str + "&nbsp;<span><a href=\"" + curr_url_el.attr("href") + "\">" + $.trim(stripCompTxt(curr_url_el.text())) + "</a></span>&nbsp;&gt;";
                        bc_str = bc_str + "&nbsp;<span>" + titleCaps($.trim($("h1").text())) + "</span>";
                    }
        }
        else {
            if (curr_url_el.from("#leftnav ul ul").length > 0) {
                //2nd level link
                bc_str = bc_str + "&nbsp;<span id=\"bcmain\"><a href=\"" + curr_url_el.closest("#leftnav").children("h3").children("a").attr("href") + "\">" + $.trim(curr_url_el.closest("#leftnav").children("h3").text()) + "</a></span>&nbsp;&gt;";
                bc_str = bc_str + "&nbsp;<span><a href=\"" + curr_url_el.closest("ul").prev("a").attr("href") + "\">" + $.trim(stripCompTxt(curr_url_el.closest("ul").prev("a").text())) + "</a></span>&nbsp;&gt;";
                bc_str = bc_str + "&nbsp;<span>" + $.trim(stripCompTxt(curr_url_el.text())) + "</span>";
            }
            else 
                if (curr_url_el.from("#leftnav ul").length > 0) {
                    //1st level link
                    bc_str = bc_str + "&nbsp;<span id=\"bcmain\"><a href=\"" + curr_url_el.closest("#leftnav").children("h3").children("a").attr("href") + "\">" + $.trim(curr_url_el.closest("#leftnav").children("h3").text()) + "</a></span>&nbsp;&gt;";
                    bc_str = bc_str + "&nbsp;<span>" + $.trim(stripCompTxt(curr_url_el.text())) + "</span>";
                }
                else 
                    if (curr_url_el.parent().is("h3")) {
                        bc_str = bc_str + "&nbsp;<span>" + $.trim(stripCompTxt(curr_url_el.text())) + "</span>";
                    }
        }
        $("#bc").replaceWith(bc_str);
    }
    function noClicksHere(){
        var myLocation = "" + window.location;
        $("div#leftnav ul a, div#leftnav h3 a").each(function(i){
            if (this.href == myLocation || this.href + "index.html" == myLocation) {
                $(this).css("cursor", "text");
                $(this).css("textDecoration", "none");
                if ($(this).parent("h3")) {
                    $(this).css("color", "#CE1339");
                }
                $(this).click(function(){
                    return false;
                });
            }
        });
    }
    /***************************************************************
     * Function stripping the [Compare All] tags from url text. This is specifically
     * for the bcGen function
     ***************************************************************/
    function stripCompTxt(str){
        if (str.indexOf("[Compare") > 0) {
            return str.split("[Compare")[0];
        }
        else {
            return str;
        }
    }
    
    //Generate breadcrumbs
    if (curr_url != "/index.html") {
        genBc();
    }
    else 
        if (($.url.attr("path").indexOf("/legal-notices/") > -1) || ($.url.attr("path").indexOf("/privacy/") > -1) || ($.url.attr("path").indexOf("/repository/") > -1) || ($.url.attr("path").indexOf("/cps/") > -1)) {
            //Manually generate breadcrumb
            var bc_str = "<div id=\"bc\"><span id=\"bctop\"><a href=\"/index.html\">US Home</a></span> &gt;";
            bc_str = bc_str + "&nbsp;<span>" + $.trim($("h1").text()) + "</span>";
            $("#bc").replaceWith(bc_str);
        }
    noClicksHere();
});

