/**
 * Adds a bookmark.
 * @param title The title of the bookmark.
 * @param url The URL of the bookmark.
 */
function bookmark(title, url) {
    try {

        // Gecko or compatible
        if (window.sidebar) {
            window.sidebar.addPanel(title, url, "");
            return;
        }

        // Opera or compatible
        if (window.opera && window.print) {
            var anchor = document.createElement('a');
            anchor.setAttribute('href', url);
            anchor.setAttribute('title', title);
            anchor.setAttribute('rel', 'sidebar');
            anchor.click();
            return;
        }

        // Internet Explorer 4 or compatible
        if (document.all && (parseInt(navigator.appVersion, 10) >= 4)) {
            window.external.AddFavorite(url, title);
            return;
        }

    } catch (e) {
        // fall through
    }

    alert("To bookmark the current page press Ctrl-D" +
            "\n(Internet Explorer, Firefox, Safari and Chrome)" +
            "\nor Ctrl-T (Opera)");
}

/**
 * Opens the URL selected in the HTMLSelectElement with id="select_a_site"
 */
function selectASite() {
    var element, newURL;
    element = document.getElementById("select_a_site");
    newURL = element.options[element.selectedIndex].value;
    if (newURL) {
        window.open(newURL, "_new");
    }
}

/**
 * Sets the target and title attributes for anchor and area elements.
 */
function anchorRels() {
    $("a[rel='external']").each(function (i) {
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the page will open in a new window";
        this.onclick = function () {
            trackPageview(this.href);
        };
    });
    $("a[rel='pdf']").each(function (i) {
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the PDF document will open in a new window";
        this.onclick = function () {
            trackPageview(this.href);
        };
    });
    $("a[rel='doc']").each(function (i) {
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the Word document will open in a new window";
        this.onclick = function () {
            trackPageview(this.href);
        };
    });
    $("a[rel='xls']").each(function (i) {
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the Excel spreadsheet will open in a new window";
        this.onclick = function () {
            trackPageview(this.href);
        };
    });
    $("a[rel='ppt']").each(function (i) {
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the Power Point presentation will open in a new window";
        this.onclick = function () {
            trackPageview(this.href);
        };
    });
    $("a[rel='wav']").each(function (i) {
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the audio clip will open in a new window";
        this.onclick = function () {
            trackPageview(this.href);
        };
    });
    $("a[rel='mp3']").each(function (i) {
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the MP3 audio clip will open in a new window";
        this.onclick = function () {
            trackPageview(this.href);
        };
    });
    $("area[class='rel_external']").each(function (i) {
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the page will open in a new window";
        this.onclick = function () {
            trackPageview(this.href);
        };
    });
}

/**
 * Initializes links that open in a new window.
 */
$(document).ready(function () {
    anchorRels();
});
