﻿
var scrollHeight = 122;
var scrollSpeed = 200;

function nextQuestionsPage(list, excludedQuestions) {
    var listId = list.attr("id");
    var currentPage = questionListData[listId].currentPage;
    var totalPagesCount = questionListData[listId].totalPagesCount;
    var loadedPagesCount = questionListData[listId].loadedPagesCount;
    var typeOfQuestions = questionListData[listId].typeOfQuestions;

    if (currentPage != totalPagesCount) {//dont move if we are on the last page
        if (currentPage == loadedPagesCount) {

            var excludedQuestionString;
            if (excludedQuestions) {
                var excludedQuestionList = new Array();
                excludedQuestions.each(function () {
                    excludedQuestionList.push($(this).attr('href'));
                });
                excludedQuestionString = excludedQuestionList.join(',');
            }

            //if the next page is not yet loaded, let's load it!
            GetQuestionsContent(parseInt(currentPage) + 1, list, totalPagesCount, excludedQuestionString); //the animation will be performed after the loading is done
            loadedPagesCount++;
        }
        else {
            //move to next page
            list.animate({ "marginTop": "-=" + scrollHeight + "px" }, scrollSpeed, function () { nextQuestionsPageAdjustButtons(typeOfQuestions, currentPage, totalPagesCount); });
         }
        currentPage++;
    }

    questionListData[listId].currentPage = currentPage;
    questionListData[listId].totalPagesCount = totalPagesCount;
    questionListData[listId].loadedPagesCount = loadedPagesCount;    
}

function nextQuestionsPageAdjustButtons(typeOfQuestions, currentPage, totalPagesCount) {
    $("#" + typeOfQuestions + "PreviousPageLink img").attr("src", "/_layouts/fs/images/TripleBox/tillbaka3.jpg");
    $("#" + typeOfQuestions + "NextPageLink").css("cursor", "pointer");
    $("#" + typeOfQuestions + "PreviousPageLink").css("cursor", "pointer");

    if (currentPage == totalPagesCount) {
        $("#" + typeOfQuestions + "NextPageLink img").attr("src", "/_layouts/FS/Images/TripleBox/visafler2.jpg");
        $("#" + typeOfQuestions + "NextPageLink").css("cursor", "default");
    }
}

function previousQuestionsPage(list) {
    var listId = list.attr("id");
    var currentPage = questionListData[listId].currentPage;
    var totalPagesCount = questionListData[listId].totalPagesCount;
    var loadedPagesCount = questionListData[listId].loadedPagesCount;
    var typeOfQuestions = questionListData[listId].typeOfQuestions;

    if (currentPage > 1) {
        list.animate({ "marginTop": "+=" + scrollHeight + "px" }, scrollSpeed, function () { previousQuestionsPageAdjustButtons(typeOfQuestions, currentPage); });
        currentPage--;
    }

    questionListData[listId].currentPage = currentPage;
    questionListData[listId].totalPagesCount = totalPagesCount;
    questionListData[listId].loadedPagesCount = loadedPagesCount; 
}

function previousQuestionsPageAdjustButtons(typeOfQuestions, currentPage) {
    $("#" + typeOfQuestions + "NextPageLink img").attr("src", "/_layouts/FS/Images/TripleBox/visafler3.jpg");
    $("#" + typeOfQuestions + "NextPageLink").css("cursor", "pointer");
    $("#" + typeOfQuestions + "PreviousPageLink").css("cursor", "pointer");

    if (currentPage == 1) {
        $("#" + typeOfQuestions + "PreviousPageLink img").attr("src", "/_layouts/fs/images/TripleBox/tillbaka.jpg?v=2");
        $("#" + typeOfQuestions + "PreviousPageLink").css("cursor", "default");
    }
}

function GetQuestionsContent(currentPage, list, totalPagesCount, excludedQuestions) {
    var listId = list.attr("id");
    var typeOfQuestions = questionListData[listId].typeOfQuestions;
    $.get('../_layouts/fs/rpcservices/QuestionsListing.ashx?asd=' + Math.floor(Math.random() * 10000),
        { page: currentPage, typeOfQuestions: typeOfQuestions, excludedQuestions: excludedQuestions },
        function (data) {
            list.append(data);
            list.animate({ "marginTop": "-=" + scrollHeight + "px" }, scrollSpeed, function () { nextQuestionsPageAdjustButtons(typeOfQuestions, currentPage, totalPagesCount); });
        }
    );
}



