﻿function CachelessUrl(url) {
    return url + (url.indexOf("?") == -1 ? "?" : "&") + "_r=" + new Date().getTime();
}

function UpdateFooterTag() {
    $("span#footerTag").fadeOut(1000, function() {
        $.get(CachelessUrl("randomQuote.ashx"), function(data) {
            $("span#footerTag").text(data);
            $("span#footerTag").fadeIn();
        });
    });
}

var badurl = "";
function ChangeSection(newurl) {
    if ($.cookie('currentSection') == newurl) return; // going to same section

    if (newurl.toString().substr(0, 3) != "404") $.cookie('currentSection', newurl);
    $("div#permalink").hide();
    $("div#main").slideUp("normal", function() {
        $("div#main").text("Error loading asyncronous content. Wouldn't you just know it.");
        $("div#main").load(CachelessUrl("resource/" + newurl), null, function() {
            if ($("div#main").text() == "err") {
                badurl = newurl;
                ChangeSection("404.htm");
            }
            $("div#main").slideDown();
            $("a#permalink").attr("href", newurl);
            $("div#permalink").show();
            Sparkle();
        });
    });
}

// after loading a new page, run this function to set up all of the clicks and changes and pulses
function Sparkle() {
    // wrap all instances of "the danger board" in <span class="tdb"></span> so we can make them pulse
    var divText = $("div#main").html();
    divText = divText.replace(/the danger board\'s/ig, "<span class='tdb'>the Danger Board's</span>");
    divText = divText.replace(/the danger board/ig, "<span class='tdb'>the Danger Board</span>");
    //divText = divText.replace(/bareback on beagles/ig, "<span class='bob'>Bareback on Beagles</span>");
    $("div#main").html(divText);
    $("div#main span.tdb").pulse({ textColors: ['#660000', '#000000', '#000000'], speed: 2000 });

    // auto-link "bareback on beagles"
    $("span.bob").click(function() { ChangeSection("merch_bareback.htm"); });

    // our own internal anchor tag system
    $("span[url]").click(function() { ChangeSection($(this).attr("url")); });

    // set-up submenu (or disable if none)
    $("div#submenu").text("");
    $("div#main span.submenuItem").appendTo("div#submenu");
    if ($("div#submenu").text() == "") $("div#submenu").hide();
    else $("div#submenu").show();

    // obfuscating email addresses
    $("span.email").each(function() {
    	var e = $(this).attr("username") + "@" + $(this).attr("domain");
    	var text = $(this).text();
    	if (text == "") text = e;
    	$(this).html("<a href='mailto:" + e + "'>" + text + "</a>");
    });

    // prepare video popups
    $("span[videoid]").click(function() {
        var videoid = $(this).attr("videoid");
        $("div#" + videoid).show();
        $("div.videooverlay").show();
    });
    $("div.videooverlay").click(function() {
        $("div.videooverlay").hide();
        $("div.videopopup").hide();
    });

    // "read more"
    $("span.readmore").click(function() {
        $(this).parent().hide();
        $(this).parent().next().show();
    });

    $("p#readoldnews").click(function() {
        $(this).hide();
        $(this).next().show();
    });

    $("span#altPayment").click(function() {
        $("div#altPaymentDiv").show();
    });
}
