jQuery.noConflict();

var sidebar =
{
    init: function()
    {
        jQuery("#sidebar div[@id$=Content]").each(function(i){
            jQuery(this).addClass("sidebarContent");

            var id = this.id.replace(/Content/g, "");
            sidebar.createClickEvent(id);
            sidebar.initFromCookies(id);
        });

        this.initRightCol();
        slideMiddleCol();

        jQuery("#postContent").slideToggle("fast");
    },

    initMiddleCol: function()
    {
        slideMiddleCol();
    },

    initRightCol: function()
    {
        jQuery("#respond").click(function() {
            sidebar.handleLeaveCommentClick();
         });
    },

    initFromCookies: function(id)
    {
        var st = jQuery.cookie(id + 'ContentState');

        if( st == "block" )
            this.handleClick(id);
    },

    createClickEvent: function(id)
    {
        jQuery("#" + id + "Link").click(function() {
            sidebar.handleClick(id);
         });
    },

    handleClick: function(id)
    {
        id += "Content";
        var st = getToggledState(jQuery("#" + id).css("display"));
        jQuery("#" + id).slideToggle("fast", function(){
           sidebar.focusSearchForm();
         });

        jQuery.cookie(id + "State", st, {expires: 30, path: '/'});


    },

    handleLeaveCommentClick: function()
    {
        jQuery("#respondForm").slideToggle("fast");
    },

    focusSearchForm: function()
    {
        if( jQuery("#searchContent").css("display") == "block" )
        {
            jQuery("#s").focus();
        }
    }
};

function slideMiddleCol()
{
    jQuery("#middleCol").slideToggle("slow", function(){
       postSlideMiddleCol()
     });
}

function postSlideMiddleCol()
{
    var st = jQuery.cookie("theComments");
    if( st == "block" )
        slideRightCol();

    if( window.location.href.search(/#comment/) != -1 )
    {
        if( st != "block" )
        {
            slideRightCol();
            setTimeout('gotoComment()', 500);
        }
        else
            gotoComment();
    }
}

function slideRightCol()
{
    var st = getToggledState(jQuery("#theComments").css("display"));
    jQuery("#theComments").slideToggle("slow");
    jQuery.cookie("theComments", st, {expires: 30, path: '/'});

    if( jQuery("h6.postComments[a:contains('Add Comment')]").length > 0 )
        sidebar.handleLeaveCommentClick();
}

function gotoComment()
{
    window.location = window.location.href;
}

function getToggledState(state)
{
    if( state == "block" )
        return "none";
    else
        return "block";
}

jQuery(document).ready(function()
{
    sidebar.init();
});