/**
 * Bookmarks a page.
 * @param title The title of the page.
 * @param url The URL of the page.
 */
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)");
}

/**
 * Sets the target and title attributes for anchor and area elements.
 */
function anchorRels() {
    $("a[rel='group']").each(function(i){
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the page will open in a new window";
    });
    $("a[rel='corporate']").each(function(i){
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the page will open in a new window";
    });
    $("a[rel='customer']").each(function(i){
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the page will open in a new window";
    });
    $("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);};
    });
    $("a[rel='jpg']").each(function(i){
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the image will open in a new window";
        this.onclick = function() {trackPageview(this.href);};
    });
    $("a[rel='png']").each(function(i){
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the image will open in a new window";
        this.onclick = function() {trackPageview(this.href);};
    });
    $("a[rel='gif']").each(function(i){
        if (!this.target) {
            this.target = "_blank";
        }
        this.title = "the image 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);};
    });
}

/**
 * Initialises href and target attributes for social bookmark links.
 */
function socialBookmarks() {
    var url   = encodeURIComponent(location.href);
    var title = encodeURIComponent(document.title);
    var delicious = document.getElementById("delicious");
    if (delicious) {
        delicious.href = delicious.href + "?v=5&noui&jump=close&url=" + url + "&title=" + title;
        delicious.target = "delicious";
    }
    var digg = document.getElementById("digg");
    if (digg) {
        digg.href = digg.href + "?url=" + url + "&title=" + title + "&media=news&topic=business_finance";
        digg.target = "digg";
    }
    var reddit = document.getElementById("reddit");
    if (reddit) {
        reddit.href = reddit.href + "?url=" + url + "&title=" + title;
        reddit.target = "reddit";
    }
    var stumbleupon = document.getElementById("stumbleupon");
    if (stumbleupon) {
        stumbleupon.href = stumbleupon.href + "?url=" + url;
        stumbleupon.target = "stumbleupon";
    }
}

/**
 * Executes anchorRels() and socialBookmarks().
 */
$(document).ready(function () {
    anchorRels();
    socialBookmarks();
});
