﻿/// <reference path="jquery-vsdoc.js" />
var ajaxLoad;
var loading = false;
var styCookie;
var title;

jQuery(document).ready(function() {
    setLinks();
    addStyleToggle();

    var styCookie = readCookie("style");
    title = styCookie ? styCookie : getPreferredStyleSheet();
    setActiveStyleSheet(title);
    jQuery('#stylepicker option[value="' + title + '"]', this).attr("selected", "selected");
    jQuery('body').removeClass('cargowire edmerritt').addClass(title);
    if ((!$.browser.msie)&&(title!='edmerritt')){
        Cufon.replace('#content h1, #footer h1');
        Cufon.now();
        }
    addWallpaperToggle();
});
jQuery(window).unload(function() {
    var title = getActiveStyleSheet();
    createCookie("style", title, 365);
});

function setLinks() {
    jQuery('a[rel=external], a[rel=me external]').attr('target', '_blank');
    setExpandableTables();
    setPaginationLinks();
    setPortfolio();
    HighlightCodeExamples();
    jQuery('.imagelink').addClass('thickbox');
}
function setExpandableTables() {
    jQuery('.fowa tbody').hide();

    jQuery('.fowa thead').click(function() {
        if (jQuery(this).siblings("tbody").is(':hidden')) {
            jQuery(this).addClass('expanded');
            jQuery(this).siblings("tbody").show('slow');
        } else {
            jQuery(this).removeClass('expanded');
            jQuery(this).siblings("tbody").slideUp();
        }
    });
    jQuery('.fowa tfoot').click(function() {
        if (jQuery(this).siblings("tbody").is(':hidden')) {
            jQuery(this).siblings("tbody").show('slow');
        } else {
            jQuery(this).siblings("tbody").slideUp();
            location.href = '#' + jQuery(this).parent().attr('id');
        }
    });
}
function setPaginationLinks() {
    jQuery('.previousLink').click(function() {
        jQuery('#linkNav').prepend(getAjaxImage());
        jQuery('#ajaxLoad').show();
        var query = jQuery(this).attr('href');
        if (query.substring(0, 7) == "http://") { query = query.substring(query.indexOf('/', 8)); }
        jQuery(this).parents('div:eq(0)').load(query.substr(0, query.lastIndexOf('.') > 0 ? query.lastIndexOf('.') : query.length) + '.ajax', '', setLinks);
        if (jQuery(window).scrollTop() < jQuery('#content').offset().top)
            jQuery('html,body').animate({ scrollTop: 0 }, 500);
        jQuery('#ajaxLoad').hide();
        return false;
    });
    jQuery('.nextLink').click(function() {
        jQuery('#linkNav').prepend(getAjaxImage());
        jQuery('#ajaxLoad').show();
        var query = jQuery(this).attr('href');
        if (query.substring(0, 7) == "http://") { query = query.substring(query.indexOf('/', 8)); }
        jQuery(this).parents('div:eq(0)').load(query.substr(0, query.lastIndexOf('.') > 0 ? query.lastIndexOf('.') : query.length) + '.ajax', '', setLinks);
        if(jQuery(window).scrollTop() > jQuery('#content').offset().top)
            jQuery('html,body').animate({ scrollTop: 0 }, 500);
        
        jQuery('#ajaxLoad').hide();
        return false;
    });
}
function getAjaxImage() {
    if (!ajaxLoad) {
        ajaxLoad = jQuery('#ajaxLoad');
        if ((ajaxLoad) && (ajaxLoad.length != 0))
            return ajaxLoad;
        else {
            ajaxLoad = jQuery("<img id='ajaxLoad' src='/content/images/ajaxLoad.gif' alt='' />")
            return ajaxLoad;
        }
    } else {
        return ajaxLoad;
    }
}
function setPortfolio() {
    jQuery('a[rel=portfolio]').each(function(intIndex){
        jQuery(this).addClass("thickbox").attr("href", jQuery(this).attr("href").replace(".ajax", "") + ".ajax" + "?height=550&width=778");
    });
};
function addStyleToggle() {
    var chooserHTML = '<select id="stylepicker"><option value="cargowire"' + ((title == "cargowire") ? " selected=\"selected\"" : "") + '>Craig</option><option value="edmerritt"' + ((title == "edmerritt") ? " selected=\"selected\"" : "") + '>Ed</option></select>';
    jQuery("#sitenoteslink").after(jQuery(chooserHTML));
    jQuery('#stylepicker').change(function() {
        jQuery('body').removeClass('cargowire edmerritt').addClass(title);
        setActiveStyleSheet(jQuery(this).val());
        window.location.href = window.location.href; // Hack for sifr
    });
}
function addWallpaperToggle() {
    var wpo = readCookie('wpoff')
    if (wpo) {
        jQuery("body").addClass("nowallpaper");
    }
    if (jQuery("body").hasClass("cargowire")) {
        jQuery("#pNav").append(jQuery("<dt><a href='#' id='wpToggle'>Wallpaper?</a></dt><dd>Lose the paper :-(</dd>"));
        jQuery("#wpToggle").click(function() {
            var wp = readCookie('wpoff')
            if (wp) {
                jQuery("body").toggleClass("nowallpaper");
                eraseCookie('wpoff');
            } else {
                jQuery("body").toggleClass("nowallpaper");
                createCookie('wpoff', '1', 1);
            }
            return false;
        });
    }
}
function HighlightCodeExamples() {
    dp.SyntaxHighlighter.ClipboardSwf = '/Content/multimedia/clipboard.swf';
    dp.SyntaxHighlighter.HighlightAll('code');
}

/* Extensions */
/* Cookie Scripts from http://www.quirksmode.org/js/cookies.html */
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
function eraseCookie(name) {
    createCookie(name, "", -1);
}

/* Stylesheet scripts from http: //www.alistapart.com/stories/alternate/ */
function setActiveStyleSheet(title) {
    var i, a, main;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if (a.getAttribute("title") == title) a.disabled = false;
        }
    }
}
function getActiveStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
    }
    return null;
}
function getPreferredStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) return a.getAttribute("title");
    }
    return null;
}