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 Popup(url, width, height, options) {
    var LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
    var TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
    popupWin = window.open(url, "popupWin", "width=" + width + ",height=" + height + ",top=" +TopPosition+ ",left=" +LeftPosition + (options != "" ? ", " + options : ""));
    if (!popupWin.opener) popupWin.opener = window;
    popupWin.focus();
}
function InitCollapsibleText() {
    $("h2.collapsible").click(function() {
        $(this).next("p").toggle();
        if ($(this).hasClass("closed")) $(this).removeClass("closed").addClass("open");
        else $(this).removeClass("open").addClass("closed");
    });
    $("h2.collapsible").addClass("closed").css("cursor", "pointer");
    $("h2.collapsible ~ p").addClass("collapsible-content-indent").hide();
}
function InitHoverButtons() {
    $("img.j-hover").hover(
        function() {
            if ($(this).hasClass("png") && jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) return;
            $(this).attr('src', $(this).attr('src').replace('.', '-hover.'));
        },
        function() {
            if ($(this).hasClass("png") && jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) return;
            $(this).attr('src', $(this).attr('src').replace('-hover.', '.'));
        }   
    );
}
function InitNewWindows() {
    $("a.pdf,a.new-window").each(function() {
        $(this).click(function() {
            window.open(this.href);
            return false;
        });
    });
}
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);
                pageTracker._trackPageview(href);
            });
            $(this).rightClick(function() {
                var href = ConvertURL($(this).attr('href'), location.pathname);
                pageTracker._trackPageview(href);
            });
        };
    });   
}
function ConvertURL(relative, absolute) {
    var href = relative;
    var path = absolute;
    if (href.indexOf('../') == 0) {
        var path = location.pathname;
        var href = href.split("../");
        var count = href.length;
        href = relative;
        while (href.indexOf('../') != -1) href = href.replace("../", "");
        path = path.split("/");
        href = path.slice(0, count).join("/") + "/" + href;
    }
    return href;
}
$(document).ready(function() {
    InitAnalyticsFileTracking();
    InitCollapsibleText();
    InitNewWindows();
    InitHoverButtons();
});