﻿/// <reference path="jquery-1.4.1.js" />
/// <reference path="MicrosoftAjax.js" />
/// <reference path="MicrosoftMvcAjax.js" />
/// <reference path="MicrosoftMvcValidation.js" />
/// <reference path="base.js" />
/// <reference path="messaging.js" />

/// <reference path="utilities.js" />

var __userController = oop.instance({});

__userController.prototype =
	{
		users: new Array()
		, addUser: function (user)
		{
			this.users.push(user);
		}
		, username: ''
		, deleteFinal: function ()
		{
			var url = '/Admin/DeleteUser';
			var username = this.username;
			$.ajax({
				type: 'POST',
				url: url,
				data: String.format('username={0}', username),
				success: function (data)
				{
					var key = String.format('#user_{0}', username.replace(/\./g, '\\.'));
					$(key).remove();
					$('.userrow').removeClass('row_0');
					$('.userrow').removeClass('row_1');
					$('.userrow:odd').addClass('row_0');
					$('.userrow:even').addClass('row_1');
					var msg = String.format('User {0} deleted!', username);
					Msg.alert('#userlist', Msg.MSGTYPES.INFO, msg, msg);
				},
				dataType: 'json'
			});
		}
		, deleteUser: function (username)
		{
			this.username = username;
			$('#mb_confirmdelete').dialog(
				{ buttons: [
					{ text: 'Yes', click: function () { crow.userController.deleteFinal(); $(this).dialog('close'); } },
					{ text: 'No', click: function () { $(this).dialog('close'); } }]
				});
		}
		, prepareList: function ()
		{
			$('#mb_confirmdelete').dialog('close');
			$('.rolecb').click(function (e)
			{
				var username = $(this).val();
				var role = $(this).attr('role');
				var checked = $(this).attr('checked');
				var url = '/Admin/ApplyRole';
				$.ajax({
					type: 'POST',
					url: url,
					data: String.format('username={0}&role={1}&hasRole={2}', username, role, checked),
					success: function (data)
					{
						var msg = String.format('{0} {2} {1}!', role, username, checked ? 'Applied to ' : 'Removed from ');
						Msg.alert('#userlist', Msg.MSGTYPES.INFO, msg, msg);
					},
					dataType: 'json'
				});

			});
		}
	}

	crow.userController = new __userController();
	$(document).ready(function ()
	{
		crow.userController.prepareList();
	});
