﻿Type.registerNamespace("Sui");

// ----------------------------------------------------------------------------- Sui.SuiService class
Sui.SuiService = function(webServiceUrl) 
{
	this._webServiceUrl = webServiceUrl;
	this.GetAssignmentsSucceededCallback = null;
	this.GetTrainingPlanSucceededCallback = null;
	this.GetCatalogFolderSucceededCallback = null;
	this.GetLessonSucceededCallback = null;
	this.GetMyTutoringLessonsSucceededCallback = null;
	this.GetMyLessonsSucceededCallback = null;
	this.CheckSessionSucceededCallback = null;
	this.PostOrGetMessagesSucceededCallback = null;
	this.GetAllNotificationsInfoCallback = null;
	this.GetLoginChallengeSucceededCallback = null;
	this.LogoutSucceededCallback = null;
	Sui.SuiService_Instance = this;
}

// -------------------------------------------------------------------------- _SuiService_SucceededCallback
// Event handler for successful return from all Ajax methods.
function _SuiService_SucceededCallback(response, userContext, methodName)
{
	if ((response != null) && response.IsSuccess)
	{
		switch (methodName)
		{
			case "GetCatalogFolder":
				if (userContext.GetCatalogFolderSucceededCallback != null)
				{	userContext.GetCatalogFolderSucceededCallback(response.LmsResponse.Folder); }
				break;
			case "GetTrainingPlan":
				if (userContext.GetTrainingPlanSucceededCallback != null)
				{	userContext.GetTrainingPlanSucceededCallback(response.LmsResponse.TrainingPlan); }
				break;
			case "GetLesson":
				if (userContext.GetLessonSucceededCallback != null)
				{	userContext.GetLessonSucceededCallback(response.LmsResponse.Lesson); }
				break;
			case "GetMyTutoringLessons":
			case "GetMyLessons":
				if (userContext.GetMyTutoringLessonsSucceededCallback != null)
				{	userContext.GetMyTutoringLessonsSucceededCallback(response.LmsResponse.MyLessons); }
				break;
			case "PostOrGetMessages":
				if (userContext.PostOrGetMessagesSucceededCallback != null)
				{ userContext.PostOrGetMessagesSucceededCallback(response.LmsResponse); }
				break;
			case "GetAllNotificationsInfo":
				if (userContext.GetAllNotificationsInfoCallback != null)
				{ userContext.GetAllNotificationsInfoCallback(response.LmsResponse); }
				break;
			default:
				if (eval("userContext."+methodName+"SucceededCallback != null"))
				{	eval("userContext."+methodName+"SucceededCallback(response.LmsResponse)"); }
				break;
		}
		// Update OnlineUserCount in user panel after ALL requests.
		if ((methodName != "CheckSession") && (userContext.CheckSessionSucceededCallback != null))
		{
			userContext.CheckSessionSucceededCallback(response.LmsResponse);
		}
		// Update UiStatus
		Sui.Page.UiStatus = response.UiStatus;
	}
	else if (response.LmsResponse.LastError == Utility.LastError.AccessDenied)
	{
		// 25.06.2008 Ps: Show catalog root instead of error page to prevent endless loop login->catalog->error page->login.
		window.location.href = "Catalog.aspx?parentId=0";
	}
	else if (response.LmsResponse.LastError != Utility.LastError.SessionLoggedOut)
	{
		window.location.href = "Error.aspx?err=6&msg=6&p1=" + methodName + '&p2=' + response.LmsResponse.LastError + ': ' +  response.LmsResponse.LastErrorMessage + '&p3=' + response.LmsResponse.LastErrorDiagnostic;
	}
	else
	{
		// If the session has timed out, redirect to login page.
		window.location.href = "Login.aspx";
	}
}

// -------------------------------------------------------------------------- _SuiService_FailedCallback
// Event handler for failure return from all Ajax methods.
function _SuiService_FailedCallback(response, userContext, methodName)
{
	window.location.href = "Error.aspx?err=1&msg=1&p1=" + this._context;
}

Sui.SuiService.prototype =
{
	// -------------------------------------------------------------------------- GetAssignments
	GetAssignments: function(forceReload, callback) {
		this.GetAssignmentsSucceededCallback = callback;
		SuiService.GetAssignments(forceReload, Sui.Page.UiStatus, _SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	},
	// -------------------------------------------------------------------------- GetTrainingPlan
	GetTrainingPlan: function(tplanId, forceReload, callback) {
		this.GetTrainingPlanSucceededCallback = callback;
		SuiService.GetTrainingPlan(tplanId, forceReload, Sui.Page.UiStatus, _SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	},
	// -------------------------------------------------------------------------- GetCatalogFolder
	GetCatalogFolder: function(folderId, forceReload, callback) {
		this.GetCatalogFolderSucceededCallback = callback;
		SuiService.GetCatalogFolder(folderId, forceReload, Sui.Page.UiStatus, _SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	},
	// -------------------------------------------------------------------------- GetLesson
	GetLesson: function(lessonId, startedFrom, tplanId, scheduleId, assignmentId, callback) {
		this.GetLessonSucceededCallback = callback;
		SuiService.GetLesson(lessonId, startedFrom, tplanId, scheduleId, assignmentId, Sui.Page.UiStatus, _SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	},
	// -------------------------------------------------------------------------- GetMyTutoringLessons
	GetMyTutoringLessons: function(itemType, lessonSid, title, subjectId, maxCount, callback) {
		this.GetMyTutoringLessonsSucceededCallback = callback;
		SuiService.GetMyTutoringLessons(itemType, lessonSid, title, subjectId, maxCount, Sui.Page.UiStatus, _SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	},
	// -------------------------------------------------------------------------- GetMyLessons
	GetMyLessons: function(itemType, lessonSid, title, subjectId, lessonStatus, usedAfter, maxCount, callback) {
		this.GetMyTutoringLessonsSucceededCallback = callback;
		SuiService.GetMyLessons(itemType, lessonSid, title, subjectId, lessonStatus, usedAfter, maxCount, Sui.Page.UiStatus, _SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	},
	// -------------------------------------------------------------------------- PostOrGetMessages
	PostOrGetMessages: function(roomName, message, leave, callback) {
		this.PostOrGetMessagesSucceededCallback = callback;
		SuiService.PostOrGetMessages(roomName, message, leave, Sui.Page.UiStatus, _SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	},
	// -------------------------------------------------------------------------- CheckSession
	CheckSession: function(callback) {
		this.CheckSessionSucceededCallback = callback;
		SuiService.CheckSession(Sui.Page.UiStatus, _SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	},
	// -------------------------------------------------------------------------- GetAllNotificationsInfo
	GetAllNotificationsInfo: function(callback) {
		this.GetAllNotificationsInfoCallback = callback;
		SuiService.GetAllNotificationsInfo(Sui.Page.UiStatus, false, _SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	},
	// -------------------------------------------------------------------------- GetLoginChallenge
	GetLoginChallenge: function(callback) {
		this.GetLoginChallengeSucceededCallback = callback;
		SuiService.GetLoginChallenge(_SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	},
	// -------------------------------------------------------------------------- Logout
	Logout: function(callback)
	{
		this.GetLogoutSucceededCallback = callback;
		SuiService.Logout(_SuiService_SucceededCallback, _SuiService_FailedCallback, this);
	}
};

// Instance of SuiService (there must be only one!).
Sui.SuiService_Instance = null;

Sui.SuiService.registerClass("Sui.SuiService", null, Sys.IDisposable);
// -----------------------------------------------------------------------------

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

