﻿var _PageSize = 20;
var _CurrentPage = 1;
var _DivMore;
var _Loading;
var _DataAvailable;
var _DivContainer;

  var app = Sys.Application;
app.add_load(applicationLoadHandler);

function applicationLoadHandler(sender, args)
 {
    if(_CurrentPage == 1)
    {
        _DivMore = $get("divMore");
        _DivContainer = $get("divFaqs");
        _DataAvailable = true;
        _Loading = false;
        _DivMore.style.display = "none";
        _DivContainer.innerHTML = "";
        window.onscroll = function(){LoadNextPage();};

        GetFaqs();
    }
     
}
  
function LoadNextPage()
{
    var clientHeight = getClientHeight();
    var clientWidth = getClientWidth();
    var scrollTop = getScrollTop();
    var scrollHeight = getScrollHeight();
    var offset = 100;
    
    if((scrollHeight - (clientHeight + scrollTop)) <= offset)
    {
        if(_Loading == false && _DataAvailable == true)
        {
            GetFaqs();
        }
    }
    
    if(scrollHeight == (clientHeight + scrollTop) && _DataAvailable == false)
    {
        _DivMore.style.display = "none";
    }
    else if(_DataAvailable == true)
    {
        _DivMore.style.display = "";
        _DivMore.style.top = (clientHeight + scrollTop) - 20 + "px";
        _DivMore.style.left = clientWidth - 55 + "px";
    }
}

                  
function GetFaqs()
{  
    var SchoolId = parseInt($get("ctl00_cphContent_hidSchooId").value);
    var curDate = new Date();
    
    _Loading = true;

    displayProgress(true);
    GDB.Services.GDBEvents.GetFaqBySchoolIdWithPaging(SchoolId, _PageSize, _CurrentPage, curDate.toString(), onSuccess, onFailure, curDate);
    
}


function onSuccess(result, context)
{
    var Faqs = result;
    var clientWidth = getClientWidth();
    var clientHeight = getClientHeight();
    var scrollTop = getScrollTop();
    _DataAvailable = false;
   
    if(Faqs != null)
    {
        if(Faqs.length > 0)
        {
            _CurrentPage = _CurrentPage + result.length;
            _DataAvailable = (Faqs.length == _PageSize)? true: false;
            renderFaqs(Faqs);
        }
        else
	    {
	        DisplayNoRecords();
	    }
    }
    else
    {
        DisplayNoRecords();
    }
    
    displayProgress(false);
    
    if(_DataAvailable == true)
    {
        _DivMore.style.display = "";
        _DivMore.style.top = (clientHeight + scrollTop) - 20 + "px";
        _DivMore.style.left = clientWidth - 70 + "px";
    }
    else
    {
        _DivMore.style.display = "none";
    }

    _Loading = false;
}

function onFailure(result, context)
{
    displayProgress(false);
     _Loading = false;
    _DivMore.style.display = "none";
}


function renderFaqs(Faqs)
{
    var objFaq;
    var intTotalCount = Faqs.length;
    var divQuestion;
    var divAnswer;
    var divPostedBy;
    var divAnswerContainer;
    var divSpacer;

    for(intIndex = 0; intIndex <= intTotalCount - 1; intIndex++)
    {
        objFaq = Faqs[intIndex];
        var Question =objFaq.Question;
        var Answer = objFaq.Answer;
        var Email = objFaq.Email;
        var PostedBy = objFaq.FullName;
        var PostedOn = getFormmatedDate(objFaq.AnsweredOn) ;
        var CreatedDate = objFaq.CreatedDate;
        var IsAnswered =  objFaq.IsAnswered;

        
        divAnswerContainer = document.createElement('DIV');
        divQuestion = document.createElement('DIV');
        divAnswer = document.createElement('DIV');
        divPostedBy = document.createElement('DIV');
        divSpacer = document.createElement('DIV');
        divAnswerContainer.id = "divA_" + objFaq.FAQId;
        divQuestion.id = "divQ_" + objFaq.FAQId;

        var divQuestionText = document.createElement('DIV');
        var divQuestionimg = document.createElement('DIV');

        divQuestionimg.className="floatleft width16";
        var imgQuestion = document.createElement('IMG');
        imgQuestion.src ="../images/icon_play.gif";
        imgQuestion.width ="16";
        imgQuestion.height ="16";
        imgQuestion.className = "floatleft";
        //divQuestionimg.appendChild(imgQuestion);
        divQuestionText.innerHTML =Question ;
        divQuestionText.className="questionfaq";

        divQuestion.appendChild(divQuestionimg);
        divQuestion.appendChild(divQuestionText);


        var spanAnswerText = document.createElement('SPAN');

        spanAnswerText.innerHTML =Answer ;
        divAnswer.appendChild(spanAnswerText);

        var spanPostedBy = document.createElement('SPAN');
        var spanPostedByText = document.createElement('SPAN');
        var spanPostedOn = document.createElement('SPAN');
        var spanPostedOnText = document.createElement('SPAN');

        spanPostedBy.innerHTML = "Posted By :&nbsp;&nbsp;";
        spanPostedBy.className ="paddingleft15px";
        spanPostedBy.Width = "100px";
        spanPostedByText.innerHTML =PostedBy + "&nbsp;&nbsp;&nbsp;&nbsp" ;

        spanPostedOn.innerHTML = "Posted On :&nbsp;&nbsp;";
        spanPostedOn.className ="paddingleft15px";
        spanPostedOn.Width = "100px";
        spanPostedOnText.innerHTML =PostedOn ;


        divPostedBy.appendChild(spanPostedBy);
        divPostedBy.appendChild(spanPostedByText);
        divPostedBy.appendChild(spanPostedOn);
        divPostedBy.appendChild(spanPostedOnText);


        divQuestion.className="question";
        divAnswer.className="answer";
        divPostedBy.className="postedby";
        divAnswerContainer.className="answercontainer";

        divAnswerContainer.appendChild(divPostedBy);
        divAnswerContainer.appendChild(divAnswer);
        
        _DivContainer.appendChild(divQuestion);
        _DivContainer.appendChild(divAnswerContainer);
         

        divSpacer.className = "bodyspacer";
        divSpacer.innerHTML = "&nbsp;";
        _DivContainer.appendChild(divSpacer);
        
        $("#" + "divA_" + objFaq.FAQId).hide();
        
        $("#" + "divQ_" + objFaq.FAQId).click(function()
          {
            $(this).toggleClass("questionsel").next(".answercontainer").slideToggle(600);
          });
    }
    
    /*$(".question").click(function()
      {
        $(this).next(".answercontainer").slideToggle(600);
      });*/
}  


function DisplayNoRecords()
{
    var divMsg = document.createElement("DIV");
    var h3Msg = document.createElement("H3");
    
    divMsg.className = "divnorecords";
    h3Msg.innerHTML = "No Questions available";
    
    divMsg.appendChild(h3Msg);
    
    _DivContainer.innerHTML = "";
    _DivContainer.appendChild(divMsg);
}