Array.prototype.contains = function(val) {
    for (var x=0;x<this.length;x++) {
      if (this[x] == val) return true;
    }
    return false;
}
String.prototype.trim = function() {
    var text = this;
    text = text.split(" ");
    text = text.join("");
    return text
}


function InitAnalyticsFileTracking() {
    var regex = /\.pdf$/i;
    
    $("#main a").each(function(idx) {
        var href = $(this).attr('href');

        if (regex.test(href) && (href.indexOf('http://' + location.hostname) == 0 || href.indexOf('http://') == -1)) {
            $(this).click(function() {
                var href = ConvertURL($(this).attr('href'), location.pathname);
                //alert('Track PDF -- ' + href);
                pageTracker._trackPageview(href);
            });
            $(this).rightClick(function() {
                var href = ConvertURL($(this).attr('href'), location.pathname);
                //alert('Track PDF -- ' + href);
                pageTracker._trackPageview(href);
            });
        };
    });   
}
function ConvertURL(rel_href, cur_pathname) {
    var href = rel_href;
    var pathbase = cur_pathname;
    var backupcount = 0;
    if (href.indexOf('/') == 0) return href                          //already completely qualified href
    if (href.indexOf('../') == 0) {                                     //need to back up one or more parent levels
        var nodes= href.split("../");
        backupcount = nodes.length-1;
        href  = rel_href;
        while (href.indexOf('../') != -1) href = href.replace("../", "");
    }
    //the href is relative to the cur_pathname; need to complete the qualification
    var nodes = pathbase.split("/");
    var nodecount = nodes.length;
    if ( $("base").length == 0 ) {     //No base tag exists, so cur_pathname includes the pagename as the last node
        nodecount=nodecount-1; 
    }  //No base tag exists, so we're executing from a page (as opposed to a default
                                                                             
    href = nodes.slice(0, nodecount-backupcount).join("/")  + "/" + href;
    return href;
}
$(document).ready(function() {
    InitAnalyticsFileTracking();
    $("a.pdf,a.new-window").each(function() {
        $(this).click(function() {
            window.open(this.href);
            return false;
        });
    });

    $("input#search").keyup(function() {
        var val = $(this).val().trim().toLowerCase();
        if (!val || val == "search") {
            $("form#search-form a").addClass("disabled");
        } else {
            $("form#search-form a").removeClass("disabled");
        }
    }).focus(function() {
        if ($(this).val().trim().toLowerCase() == "search") $(this).val("");
    }).blur(function() {
        if ($(this).val().trim() == "") $(this).val("SEARCH");
    });
    $("form#search a.submit, form#search-form a").click(function() {
        if (!$(this).hasClass("disabled")) $(this).parents("form").submit();
        return false;
    });
    $("form#search-form a").hover(
        function() {
            if (!$(this).hasClass("disabled")) $(this).addClass("hover");
        },
        function() {
            $(this).removeClass("hover");
        }
    );
    $("form#search-form a").focus(function() {
        if (!$(this).hasClass("disabled")) $(this).addClass("hover");
    });
    $("form#search-form a").blur(function() {
        $(this).removeClass("hover");
    });
});
