﻿
//rev = areaid
//rel =  Closed = 0, Open = 1


$(document).ready(function() {

    //////    setUIPrefs();

    $(".collapsebutton").click(function() {

        //Find .collapsablearea in same parent as sender (anchor)
        var sender = $(this);
        //var h2 = $(this).parent().parent.children("h2");
        var collapseableAreas = $(this).parent().parent().children(".collapsablearea");

        collapseableAreas.slideToggle("fast", function() {

            if ($(sender).attr("rel") == "1") {
                $(sender).text("Expand");
                $(sender).attr("rel", "0")
                // AJAX call to update user prefs
                SetUIAreaPreference($(sender).attr("rev"), 0);

            } else {

                $(sender).text("Collapse");
                $(sender).attr("rel", "1")
                // AJAX call to update user prefs
                SetUIAreaPreference($(sender).attr("rev"), 1);
            }
        });

        return false;
    });

});

//Sets the status of the collapsable areas based on the user preference.
function setUIPrefs() {

    //Try to find corresponding link (may not be on this page)
    $(".collapsebutton").each(function() {

        var btn = $(this);

        $(this).parent().parent().children(".collapsablearea").each(function() {

            if ($(this).css("display") == "none") {
                $(btn).text("Expand");
                $(btn).attr("rel", "0");
            } else {
                $(btn).text("Collapse");
                $(btn).attr("rel", "1");
            }

        });
    });

}