
var grayBoxHandler = new function()
{
    this.isIntialised = false;

    this.init = function()
    {
        if (!grayBoxHandler.isInitialised)
        {
            grayBoxHandler.isInitialised = true;
            $("div.grayBoxH").each(function(i)
            {
                grayBoxHandler.load(this);
            });    
        };
            
    };
    
    this.load = function(obj)
    {
//        if (!(obj.getAttribute("formatted") == "true"))
//        {
//            
//            var id = $(obj).parent().attr("id");
//            
//            if ((id != null) && (id.length > 0))
//            {
//                var s = "<span class=\"hide\"><input onclick=\"javascript:grayBoxHandler.click(this);\" type=\"checkbox\"/></span> \
//                         <span class=\"hide\">Hide</span>";
//                $(obj).html("<span>" + $(obj).html() + "</span>");
//                $(obj).append(s);
//                
//                var hide = $.cookie(window.location.pathname + "?pref_" + id);
//                if ((hide == null) || (hide.length == 0))
//                    $.get("/ws/profile/preferenceGet.aspx", {prefId:id, path:window.location.pathname}, function(data, status){ grayBoxHandler.initAction(data,status); });
//                else
//                    grayBoxHandler.showHide($(obj), hide);
//            }
//            else
//            {
//               $(obj).next().slideDown();
//            }
//            
//            $(obj).attr("formatted", "true");
//        }
    }

    this.initAction = function(data, status)
    {
    
        var id = $(data).find("preference").attr("prefId");
        var hide = $(data).find("preference").attr("hide");
        if ((id != null) && (id.length > 0))
            grayBoxHandler.update(id, hide);
    };

    

    this.click = function(obj)
    {
        var hide = obj.checked ? "true" : "false";
        var id = $(obj).parents("div.grayBoxH").parent().attr("id");
        if ((id != null) && (id.length > 0))
        {
            grayBoxHandler.update(id, hide);
            $.get("/ws/profile/preferenceUpdate.aspx", {prefId:id, path: window.location.pathname, hide:hide});
            
        }
    }
    
    this.update = function(id, hide)
    {
    
        $.cookie(window.location.pathname + "?pref_" + id, hide, { expires: 21, path: "/", secure: false });
        var obj = $("#"+id).find("div.grayBoxH");
        grayBoxHandler.showHide(obj, hide);
    }
    
    this.showHide = function(obj, hide)
    {
        obj.attr("status", hide);
        if (hide=="true")
        {
            obj.find("input").attr("checked", "checked");
            if (obj.next("div.grayBoxC").attr("visible") == "true")
            {
                obj.next("div.grayBoxC").attr("visible", "false");
                obj.next("div.grayBoxC").slideUp();
            }
        }
        else
        {
            obj.find("input").removeAttr("checked");
            obj.next("div.grayBoxC").attr("visible", "true");
            obj.next("div.grayBoxC").slideDown();
        }
    };

}

//$(document).ready(function() {grayBoxHandler.init();});
