var LAST_SELECTED_INDEX = 0;
var IntervalMyCourses;
function PageLoad() {
    GetUserCourses();
    courseChanged(0, false);
    if (jQuery.browser.msie) {
        if (jQuery.browser.version < 8) {
            window.location = '/upgrade.aspx'
        }
    }
}
function GetUserCourses() {
    VideoService.GetUserCourses(onGetUserCoursesSuccess);
}
function onGetUserCoursesSuccess(result) {
    if (result != '')
        $('lblMyCourses').innerHTML = result;
    else if ($('pnlActivate') != null) {
        $('lblMyCourses').innerHTML = $('previewVideoList').innerHTML;
        $('pnlActivate').show();
    } else
        $('lblMyCourses').innerHTML = $('previewVideoList').innerHTML;
    if ($('pnlActivate') != null)
        $('activateBtn').innerHTML = 'Activate New Videos';
    if ($('lblMyCourses').innerHTML != '')
        clearInterval(IntervalMyCourses);
    else
        IntervalMyCourses = setInterval('GetUserCourses()', 6000);
}
function AddUserCourse(LicenseKey) {
    VideoService.AddUserCourse(LicenseKey, onAddUserCourseSuccess);
}
function onAddUserCourseSuccess(result) {
    if (result.length > 0)
        $('lblActivateError').innerHTML = result;
    else
        location.reload(true);
}
/*************************************************************
Methods for the Courses
*************************************************************/
function courseChanged(courseId, _startOver) {
    if (_startOver || parseInt($('cidDiv').innerHTML) != courseId || parseInt($('cidDiv').innerHTML) == 0) {
        CreatePlayerForCourse(courseId);
        DisplayVideoList(courseId);
        VideoService.GetFirstVideoId(courseId, onGetFirstVideoIdSuccess);
        VideoService.GetMoreProducts(courseId, onGetMoreProductsSuccess);
        VideoService.GetExamNameNumber(courseId, onGetExamNameNumberSuccess);
    }
    if (courseId != 0)
        videoList();
}
function onGetFirstVideoIdSuccess(result) {
    //refreshVideoRating(result);
}
function onGetMoreProductsSuccess(result) {
    $('pnlMore').innerHTML = result;
    if (result.toString().indexOf('carousel') != -1) {
        $('pnlMoreDetails').innerHTML = $('pnlMoreDetailsStockData').innerHTML;
        $('pnlMoreDetails').show();
        myProductTools();
    }
}
function onGetExamNameNumberSuccess(result) {
    $('ExamNameNumber').innerHTML = result;
}
function CreatePlayerForCourse(courseId) {
    setCurrentCourseId(courseId);
    //reset seleceted index back to zero
    LAST_SELECTED_INDEX = 0;
    //The second parameter is videoId which doesn't apply in this case, so it is passed as zero. 
    createPlayer('playlist.aspx%3Fcid=' + courseId + '%26index=0', 0, '', "true", "true");
}
function setCurrentCourseId(courseId) {
    var cidDiv = $('cidDiv');
    cidDiv.innerHTML = courseId;
}
function getCurrentCourseId() {
    var cidDiv = $('cidDiv');
    return +(cidDiv.innerHTML);
}
function onGetCourseIdSuccess(result) {
    // result contains a courseId
    setCurrentCourseId(result.courseId);
}
/*************************************************************
Methods for VideoList
*************************************************************/
function DisplayVideoList(CourseId) {
    $('lblVideoList').innerHTML = 'Loading...';
    VideoService.DisplayVideoList(CourseId, DisplayVideoListOnComplete);
}
function DisplayVideoListOnComplete(result) {
    $('lblVideoList').innerHTML = result;
    setVideoListSelectionIndex(0);
}
function onVideoClick(VideoId, CourseId, IndexId) {
    createPlayer('playlist.aspx%3Fcid=' + CourseId + '%26index=' + IndexId, VideoId, '', "true", "true");
    //refreshVideoRating(VideoId);
    setVideoListSelectionIndex(IndexId);
}
function GetMoreDetails(LibraryAccessUserItemId) {
    $('pnlMoreDetails').show();
    $('pnlMoreDetails').innerHTML = 'Loading...';
    VideoService.GetMoreDetails(LibraryAccessUserItemId, GetMoreDetailsOnComplete);
}
function GetMoreDetailsOnComplete(result) {
    $('pnlMoreDetails').innerHTML = result;
}
/*************************************************************
Methods for Rating
*************************************************************/
function refreshVideoRating(videoId) {
    // get video rating from service
    VideoService.GetVideoRating(videoId, onRatingSuccess);
    var ratingControl = $find("videoRatingControl");
    if (ratingControl != null)
        ratingControl._tag = videoId;
}
function onRatingSuccess(result) {
    var videoRating = result;
    // find the rating control
    var ratingControl = $find("videoRatingControl");
    // set the rating
    if (ratingControl != null)
        ratingControl.set_Rating(videoRating);
}
//Update the user's rating to the server
function updateVideoRating() {
    videoId = $find("videoRatingControl")._tag;
    newRating = $find("videoRatingControl")._currentRating;
    VideoService.UpdateUserVideoRating(videoId, newRating, onCallComplete, onCallError);
}
/*************************************************************
Methods for Selecting Current Playing Video in VideoList
*************************************************************/
function setVideoListSelectionIndex(selectedItemIndex) {
    resetVideoListSelectionIndex();
    if (selectedItemIndex == undefined)
        selectedItemIndex = 0;
    //alert(selectedItemIndex);
    LAST_SELECTED_INDEX = selectedItemIndex;
    $('VideoInList_Video_' + selectedItemIndex).className = 'selectedVideoInList';
}
function resetVideoListSelectionIndex() {
    var allLinks = $('lblVideoList').childNodes;
    for (i=0; i < allLinks.length; i++) {
        if (allLinks[i].tagName == "A") {
            var a = allLinks[i];
            a.className = 'nonSelectedVideoInList';
        }
    }
}
/*************************************************************
Utility Methods
*************************************************************/
function IsUserLoggedIn() {
    VideoService.IsUserLoggedIn(onIsUserLoggedInComplete);
}
function onIsUserLoggedInComplete(result) {
    if (!result && $('cidDiv').innerHTML != '0')
        window.location.href = window.location.href;
}
function onCallComplete(result, userContext, methodName) {
    //alert(result);
}
function onCallError(error, userContext, methodName) {
    /*if(error !== null)
        alert(error.get_message());*/
}
function escapeHTML (str) {
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
};
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
