﻿function SetStudentName(nameText) {
    document.cookie = "StudentName=" + escape(nameText) + ";path=/;";
}

function InitializeUi() {

	$('#Previous-button').click(function () {
		location.href = $('ul#Navigation-container li a.selected').parent().prev().find('a').attr('href');
	});

	$('#Next-button').click(function () {
		location.href = $('ul#Navigation-container li a.selected').parent().next().find('a').attr('href');
	});

	$('.watermarked').watermark('watermark');

	var pageName = location.href.substring(location.href.lastIndexOf('/') + 1);
	var navItem = $('ul#Navigation-container li a[href="' + pageName + '"]');

	if (navItem.length > 0) {
		navItem.addClass('selected');

		var navScrollHeight = $("ul#Navigation-container").attr("scrollHeight");
		var navHeight = $('ul#Navigation-container').height();
		var navItemTop = navItem.position().top;
		if (navScrollHeight > navHeight && navItemTop > navHeight/2) {
			$("ul#Navigation-container").animate({ scrollTop: navItemTop - navHeight/2 }, 3000);
		}


		if (navItem.parent().prev().length > 0) {
			$('#Previous-button').removeAttr('disabled');
		}
		else {
			$('#Previous-button').attr('disabled', 'disabled');
		}

		if (navItem.parent().next().length > 0) {
			$('#Next-button').removeAttr('disabled');
		}
		else {
			$('#Next-button').attr('disabled', 'disabled');
		}
	}

	if (isAuthenticated) {
		var viewedUsageUri = dataServicesRoot + "CingletreeLearning.svc/IncrementViewedUsage?ContentObjectId=" + navItem.attr('id');
		OData.read(
            viewedUsageUri,
            function (data) {
            	// do nothing?
            },
            function (err) {
            	// do nothing?
            }
        );
	}
}
