﻿function GetQueryStringVariable(key) {
    var query = window.location.search;
    var re = new RegExp("[?|&]" + key + "=(.*?)&");
    var matches = re.exec(query + "&");
    if (!matches || matches.length < 2) {
        return "";
    }
    return decodeURIComponent(matches[1].replace("+", " "));
}


$(document).ready(function() {

    //Global variables
    var _chapterNumber = parseInt(GetQueryStringVariable("chapter"));
    var _pageNumber = parseInt(GetQueryStringVariable("page"));

    if (isNaN(_chapterNumber)) {
        _chapterNumber = 1;

    }

    if (isNaN(_pageNumber)) {
        _pageNumber = 1;
    }

    var _hlNext = $(".navigationBar .hlNext");
    var _hlPrevious = $(".navigationBar .hlPrevious");
    var _hlExamCenter = $(".navigationBar .hlExamCenter");

    var _imgNext = $(".navigationBar .hlNext img");
    var _imgPrevious = $(".navigationBar .hlPrevious img");
    var _imgExamCenter = $(".navigationBar .hlExamCenter img");

    var _lblTimeMessage = $(".navigationBar .lblTimeMessage");
    var _lblTime = $(".navigationBar .lblTime");

    var _endOfTime = -1;

    //Functions
    function InitNextButton() {

        _hlNext.attr("href", _nextPageUrl);
        _imgNext.attr("src", "/global/controls/courseManual/images/next_section.gif");

    }

    function InitPreviousButton() {

        if (_previousPageUrl != "") {
            _hlPrevious.attr("href", _previousPageUrl);
            _imgPrevious.attr("src", "/global/controls/courseManual/images/prev_section.gif");
        }
    }

    function InitQuizButton() {

        _hlExamCenter.attr("href", _examCenterUrl);
        _imgExamCenter.attr("src", "/global/controls/courseManual/images/take_quiz.gif");

    }

    var remainingSeconds = GetTimeRemaining();


    function SectionComplete() {

        if ($.ajax({
            url: "/global/controls/courseManual/services/NavigationBar.ashx",
            data: ({ customerId: _customerId, manualId: _manualId, chapterNumber: _chapterNumber, pageNumber: _pageNumber, type: "SectionComplete" }),
            async: false,
            cache: false
        }).responseText == "True") {
            _isChapterComplete = true;
            InitQuizButton();
        }
    }


    function InitTimeRemaining() {

        if (_isAuthenticated) {
            //Display minutes and seconds
            var minutesToDisplay = "0";
            var secondsToDisplay = "00";

            if (remainingSeconds > _endOfTime) {
                remainingSeconds -= 1;
                //Calculate minutes and seconds
                if (remainingSeconds >= 60) {
                    minutesToDisplay = parseInt(remainingSeconds / 60);
                    secondsToDisplay = remainingSeconds % 60;
                }

                else if (remainingSeconds < 60) {
                    minutesToDisplay = "0";
                    secondsToDisplay = remainingSeconds;
                }

                if (parseInt(secondsToDisplay) < 0) {
                    secondsToDisplay = "00";
                } else if (parseInt(secondsToDisplay) < 10) {
                    secondsToDisplay = "0" + secondsToDisplay;
                }

                SetTimeRemaining(minutesToDisplay, secondsToDisplay);


            } //closes if (remainingSeconds > 0)

            if (remainingSeconds > _endOfTime) {
                setTimeout(InitTimeRemaining, 1000);
            }

            else {
                if (remainingSeconds <= 0) {
                    SectionComplete();
                    InitNextButton();
                }

                SetTimeRemaining(minutesToDisplay, secondsToDisplay);

                if (_isChapterComplete) {
                    InitQuizButton();

                }

            } //closes else remainingSeconds <= 0
        } //finish authenticated

        //notAuthenticated
        else {

            InitNextButton();
            InitQuizButton();

        }
    }


    function SetTimeRemaining(minutesToDisplay, secondsToDisplay) {

        if (_isRequiredStudy) {

            _lblTimeMessage.text("TIME REMAINING: ");
            _lblTime.text(minutesToDisplay + ":" + secondsToDisplay);
        }
        else if (_showRecommendedTime) {

            _lblTimeMessage.text("RECOMMENDED TIME REMAINING: ");
            _lblTime.text(minutesToDisplay + ":" + secondsToDisplay);
        }


    }
    //Variables from server

    var _isRequiredStudy = GetIsRequiredStudy();
    var _isAuthenticated = GetIsAuthenticated();
    var _nextPageUrl = GetNextPageUrl();
    var _previousPageUrl = GetPreviousPageUrl();
    var _showRecommendedTime = GetShowRecommendedTime();
    var _isChapterComplete = GetIsChapterComplete();
    var _examCenterUrl = GetExamCenterUrl();
    var _customerId = GetCustomerId();
    var _manualId = GetManualId();
    var _pageNumber = GetPageNumber();
    var _chapterNumber = GetChapterNumber();


    if (_isRequiredStudy == false) {

        InitNextButton();
        InitQuizButton();

    }

    InitTimeRemaining();
    InitPreviousButton();

    PunchIn();

});

if (GetIsChrome()) {
    window.onbeforeunload = PunchOut;
} else {
    window.onunload = PunchOut;
}

function PunchIn() {
    $.ajax({
        url: "/global/controls/courseManual/services/NavigationBar.ashx",
        data: ({ customerId: GetCustomerId(), manualId: GetManualId(), chapterNumber: GetChapterNumber(), pageNumber: GetPageNumber(), type: "PunchIn" }),
        async: true,
        cache: false
    });
}

function PunchOut() {
    $.ajax({
        url: "/global/controls/courseManual/services/NavigationBar.ashx",
        data: ({ customerId: GetCustomerId(), manualId: GetManualId(), chapterNumber: GetChapterNumber(), pageNumber: GetPageNumber(), type: "PunchOut" }),
        async: false,
        cache: false
    });
}


//function PunchOut() {
//*note: this ajax call doesn't fire properly in Chrome
//    $.ajax({
//        url: "/global/controls/courseManual/services/NavigationBar.ashx",
//        data: ({ customerId: GetCustomerId(), manualId: GetManualId(), chapterNumber: GetChapterNumber(), pageNumber: GetPageNumber(), type: "PunchOut" }),
//        async: true,
//        cache: false
//    });

//    xmlHttp = GetXmlHttpObject();

//    if (xmlHttp == null) {
//        alert("Browser does not support HTTP Request");
//        return false;
//    }
//    var data = "customerId=" + GetCustomerId() + "&manualId=" + GetManualId() + "&chapterNumber=" + GetChapterNumber() + "&pageNumber=" + GetPageNumber() + "&type=PunchOut";
//        
//    xmlHttp.open("GET", "/global/controls/courseManual/services/NavigationBar.ashx?" + data, true);

//    xmlHttp.send();
//}

//function GetXmlHttpObject() {
//    var objXMLHttp = null;

//    if (window.XMLHttpRequest) {
//        objXMLHttp = new XMLHttpRequest();
//    }
//    else if (window.ActiveXObject) {
//        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
//    }

//    return objXMLHttp;
//}