﻿//A json object that manages all actions relating to user documents
var accountMgr =
{
    foldersDivId: 'foldersDiv',
    queriesResultDiv: 'divQueries',
    $folderLinks: [],
    currentFolderId: 0,

    favoritesResultDiv: 'divFavorites', //the div where the user favorites list should be displayed
    favoritesCurrentPage: 1,
    favoritesSort: 'Entity.CreateDate',
    favoritesSortDirection: 'desc',
    favoritesPageSize: 15,

    commentsResultDiv: 'divComments', //the div where the user comments list should be displayed
    commentsCurrentPage: 1,
    commentsSort: 'Entity.CreateDate',
    commentsSortDirection: 'desc',
    commentsPageSize: 15,
    searchTypeHidden: Object,

    searchTypes:
    {
        Comments: 'Comments',
        Favorites: 'Favorites'
    },

    searchType:
    {
        Get: function()
        {
            return accountMgr.searchTypeHidden.value;
        },
        Set: function(value)
        {
            accountMgr.searchTypeHidden.value = value;
        }
    },

    //sets the result divs
    Init: function()
    {
        folderMgr.Init(false);
        accountMgr.LoadFolder(0);
    },

    //checks that data has been returned (result== true)
    //if it had been returned, puts the returned html (info) into the target's innerHtml
    //otherwise, puts a message  the target's innerHtml
    AppendResult: function(args)
    {
        if (args != null)
        {
            var target = document.getElementById(args.Target);

            if (args)
                target.innerHTML = args.Result;
            else
                target.innerHTML = '<div style="height: 150px">לא נמצאו מסמכים מתאימים</div>';
        }
    },

    //----------------------------------------------------------------------------------------------------------------------
    //      FOLDERS
    //----------------------------------------------------------------------------------------------------------------------

    LoadFolder: function(folderId)
    {
        accountMgr.currentFolderId = folderId;

        accountMgr.GetFavorites(folderId);
        accountMgr.GetComments(folderId);
        folderMgr.LoadFolderQueries('divQueries', folderId, false)
    },

    //----------------------------------------------------------------------------------------------------------------------
    //      COMMENTS
    //----------------------------------------------------------------------------------------------------------------------

    //calls the webservice to get the comments list into the comments results div
    GetComments: function(folderId)
    {
        if (!folderId)
            folderId = accountMgr.currentFolderId;

        ajaxHelper.ShowProgress('GetComments', accountMgr.commentsResultDiv);

        SearchEngineService.GetComments
        (
            folderId,
            accountMgr.commentsResultDiv,
            accountMgr.commentsPageSize,
            accountMgr.commentsCurrentPage,
            accountMgr.commentsSort,
            accountMgr.commentsSortDirection,
            accountMgr.GetComments_Complete
        );
    },

    //the "complete" event handler for the webmethod called in GetComments 
    //(appends the returned html to the comments result div
    GetComments_Complete: function(args)
    {
        ajaxHelper.HideProgress('GetComments');

        accountMgr.AppendResult(args);
    },

    DeleteComment: function(folderId, commentId)
    {
        if (confirm('האם אתה בטוח שברצונך למחוק הערה זו?'))
            SearchEngineService.DeleteComment(folderId, commentId, accountMgr.DeleteComment_Complete);
    },

    DeleteComment_Complete: function(args)
    {
        if (args != null)
        {
            if (args)
                accountMgr.GetComments();
            else
                alert("לא ניתן למחוק הערות שנכתבו על ידי משתמש אחר!");
        }
        else
        {
            alert("!ארעה תקלה בעת מחיקת ההערה, אנא נסה שנית");
        }
    },

    //----------------------------------------------------------------------------------------------------------------------
    //      FAVORITES
    //----------------------------------------------------------------------------------------------------------------------

    //calls the webservice to get the favorites list into the favorites results div
    GetFavorites: function(folderId)
    {
        if (!folderId)
            folderId = accountMgr.currentFolderId;

        ajaxHelper.ShowProgress('GetFavorites', accountMgr.favoritesResultDiv);

        SearchEngineService.GetFavorites
        (
            folderId,
            accountMgr.favoritesResultDiv,
            accountMgr.favoritesPageSize,
            accountMgr.favoritesCurrentPage,
            accountMgr.favoritesSort,
            accountMgr.favoritesSortDirection,
            accountMgr.GetFavorites_Complete
        );
    },

    //the "complete" event handler for the webmethod called in GetFavorites 
    //(appends the returned html to the favorites result div
    GetFavorites_Complete: function(args)
    {
        ajaxHelper.HideProgress('GetFavorites');

        accountMgr.AppendResult(args);
    },

    DeleteFavorite: function(folderId, favoriteId)
    {
        if (confirm('האם אתה בטוח שברצונך למחוק מועדף זה?'))
            SearchEngineService.DeleteFavorite(folderId, favoriteId, accountMgr.DeleteFavorite_Complete);
    },

    DeleteFavorite_Complete: function(args)
    {
        if (args != null)
        {
            if (args)
                accountMgr.GetFavorites();
            else
                alert("לא ניתן למחוק מועדפים שהוספו על ידי משתמש אחר!");
        }
        else
        {
            alert("!ארעה תקלה בעת מחיקת המועדף, אנא נסה שנית");
        }
    },

    //----------------------------------------------------------------------------------------------------------------------
    //      PRESENTATION
    //----------------------------------------------------------------------------------------------------------------------

    ToggleFavoritesSortDirection: function()
    {
        accountMgr.favoritesSortDirection = accountMgr.favoritesSortDirection == 'desc' ? 'asc' : 'desc';
    },

    ToggleCommentsSortDirection: function()
    {
        accountMgr.commentsSortDirection = accountMgr.commentsSortDirection == 'desc' ? 'asc' : 'desc';
    },

    GoToPage: function(page, listType)
    {
        if (listType in accountMgr.searchTypes)
        {
            accountMgr.searchType = listType;

            if (accountMgr.searchType == 'Favorites')
            {
                accountMgr.favoritesCurrentPage = page;
            }
            else if (accountMgr.searchType == 'Comments')
            {
                accountMgr.commentsCurrentPage = page;
            }

            accountMgr.GetListByType();
        }
    },

    GoToPageSizeSelect: function(select, listType)
    {
        if (listType in accountMgr.searchTypes)
        {
            accountMgr.searchType = listType;

            if (accountMgr.searchType == 'Favorites')
            {
                accountMgr.favoritesPageSize = select.options[select.selectedIndex].value;
            }
            else if (accountMgr.searchType == 'Comments')
            {
                accountMgr.commentsPageSize = select.options[select.selectedIndex].value;
            }

            accountMgr.GetListByType();
        }
    },

    GoToSort: function(SortExpresion, listType)
    {
        if (listType in accountMgr.searchTypes)
        {
            accountMgr.searchType = listType;

            if (accountMgr.searchType == 'Favorites')
            {
                accountMgr.favoritesSort = SortExpresion;
                accountMgr.ToggleFavoritesSortDirection();

            }
            else if (accountMgr.searchType == 'Comments')
            {
                accountMgr.commentsSort = SortExpresion;
                accountMgr.ToggleCommentsSortDirection();
            }

            accountMgr.GetListByType();
        }
    },

    GetListByType: function()
    {
        if (accountMgr.searchType in accountMgr.searchTypes)
        {
            if (accountMgr.searchType == 'Favorites')
            {
                accountMgr.GetFavorites();
            }
            else if (accountMgr.searchType == 'Comments')
            {
                accountMgr.GetComments();
            }
        }
    },
    //Decide if to show or hide the body comment
    ShowOrHideFullComment: function(chosen_div_id, currrent_image)
    {

        var chosen_div = $j("#" + chosen_div_id);
        if (chosen_div.css("visibility") == "visible")
        {
            chosen_div.css("visibility", "hidden");
            currrent_image.src = "Images/Plus.gif";
        }
        else if (chosen_div.css("visibility") == "hidden")
        {
            chosen_div.css("visibility", "visible");
            currrent_image.src = "Images/Minus.gif";
        }
       
        return false;
    }

};


//    OnDeleteCommentTimeOut: function(args)
//    {
//        alert("!ארעה תקלה בעת מחיקת הערה למסמך, אנא נסה שנית" + "\n" + args);
//    },

//    OnDeleteCommentError: function(args)
//    {
//        alert("!ארעה תקלה בעת מחיקת הערה למסמך, אנא נסה שנית" + "\n" + args);
//    },


//    OnDeleteFavoriteTimeOut: function(args)
//    {
//        alert("!ארעה תקלה בעת מחיקת מועדף, אנא נסה שנית" + "\n" + args);
//    },

//    OnDeleteFavoriteError: function(args)
//    {
//        alert("!ארעה תקלה בעת מחיקת מועדף, אנא נסה שנית" + "\n" + args);
//    }
