$(document).ready(function() {
    $("ul.topic-list>li").hover(
        function() {
            $(this).addClass("hover");
        },
        function() {
            $(this).removeClass("hover");
        }
    ).click(function() {
        $(this).children("ul.sub-topics").mouseleave(function() {
            var subTopic = this;
            if ($(this).attr("t")) window.clearTimeout($(this).attr("t"));
            $(subTopic).attr("t", window.setTimeout(function() { $(subTopic).hide(1, function() { $(this).removeAttr("t") }); }, 500));
        }).mouseenter(function() {
            if ($(this).attr("t")) {
                var t = $(this).attr("t");
                window.clearTimeout(t);
            }
        }).show().find("a").hover(
            function() {
                $(this).parent("li").addClass("hover");
            },
            function() {
                $(this).parent("li").removeClass("hover");
            }
        ).click(function() { location.href = $(this).attr("href"); });
        if ($(this).children("a:first-child").attr("href").replace("#", "") != "") return true;
        return false;
    });
    $(".pager li[class!=nav]").hover(
        function() {
            if (!$(this).children("a").hasClass("current")) $(this).addClass("hover");
        },
        function() {
            $(this).removeClass("hover");
        }
    );
    $(".pager li[class!=nav] a.current").click(function() { return false; });
    $(".subscribe-links img[alt*=RSS],.subscribe-links img[alt*=iTunes]").hover(
        function() {
            $(this).css("opacity", 0.8);
        },
        function() {
            $(this).css("opacity", 1);
        }
    );
    $("#cb-downloadable-only input").attr("checked", location.href.indexOf("downloadable=1") != -1).click(function() {
        var sep = "&";
        var loc = location.href;
        var key = "downloadable=1";
        var checked = $(this).attr("checked");

        if (checked) {
            if (loc.split("?").length == 1) sep = "?";
            loc += sep + key;
        } else {
            if (loc.indexOf(key) != -1) loc = loc.replace(key, '').replace("&&", "&").replace("?&", "?");
        }
        loc += (loc.indexOf("refresh=1") == -1 ? "&refresh=1" : "");
        location.href = NormalizeQueryString(loc);
    });
    $("#rb-worship-only input").attr("checked", location.href.indexOf("worship=1") != -1).click(function() {
        var sep = "&";
        var loc = location.href;
        var key = "worship=1";

        if (loc.split("?").length == 1) sep = "?";
        loc += sep + key + (loc.indexOf("refresh=1") == -1 ? "&refresh=1" : "");
        location.href = NormalizeQueryString(loc);
    });
    $("#rb-all-events input").attr("checked", location.href.indexOf("worship=1") == -1).click(function() {
        var sep = "&";
        var loc = location.href;
        var key = "worship=1";

        if (loc.indexOf(key) != -1) loc = loc.replace(key, '').replace("&&", "&").replace("?&", "?");
        loc += (loc.indexOf("refresh=1") == -1 ? "&refresh=1" : "");
        location.href = NormalizeQueryString(loc);
    });
    $("a.new-window").click(function() {
        window.open($(this).attr("href"));
        return false;
    });
});
function NormalizeQueryString(loc) {
    var url = "";
    var qs;
    loc = loc.split("?");
    if (loc.length > 1) {
        url = loc[0];
        qs = loc[1];
    }
    qs = qs.replace("&&", "&");
    
    if (qs.charAt(0) == "&") qs = qs.substr(1);
    
    return url + (qs ? "?" + qs : "");
}