﻿var _IsSidebarExpanded = false;
var _IsLoaded = false;
var _ImageButton_ExpandCollapse = null;
var _Panel_Sidebar = null;
var _Panel_Feature = null;
var _ExpandedImagePath = '';
var _CollapsedImagePath = '';

Sys.Application.add_load(LoadProfileService);

function LoadProfileService()
{
    if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack())
        Sys.Services.ProfileService.load(null, LoadSidebarState, OnProfileServiceError);
}

function LoadSidebarState()
{
    _IsSidebarExpanded = Sys.Services.ProfileService.properties.IsSidebarExpanded;
    _IsLoaded = true; 
    
    if (_Panel_Sidebar && _Panel_Feature)
        ExpandCollapseSidebar();
}

function ExpandCollapseSidebar()
{
    if (_IsSidebarExpanded)
    {
        _Panel_Sidebar.style.overflow = "visible";
        _Panel_Sidebar.style.width = "233px";
        _Panel_Feature.style.position = "relative";
        _Panel_Feature.style.left = "249px";
        _Panel_Feature.style.width = "680px";
    }
    else
    {
        _Panel_Sidebar.style.overflow = "hidden";
        _Panel_Sidebar.style.width = "14px";
        _Panel_Feature.style.position = "relative";
        _Panel_Feature.style.left = "29px";
        _Panel_Feature.style.width = "900px";
    }

    _IsSidebarExpanded = !_IsSidebarExpanded;
    
    if (_IsLoaded)
    {
        Sys.Services.ProfileService.properties.IsSidebarExpanded = !_IsSidebarExpanded;
        Sys.Services.ProfileService.save(null, OnProfileServiceSuccess, OnProfileServiceError);
    }

    _ImageButton_ExpandCollapse.src = _IsSidebarExpanded ? _CollapsedImagePath : _ExpandedImagePath;
}

function OnProfileServiceSuccess(result)
{
    window.status = "Profile updated: Sidebar is " + (_IsSidebarExpanded ? "collapsed" : "expanded");
    
    if (null != $get("default"))
        ResizeReport();

    setTimeout(function() { window.status = ''; }, 5000);
}

function OnProfileServiceError(error)
{
    window.status = error.get_message();
}