/// <reference path="../jquery-1.4-vsdoc.js" />
if(jimungo===undefined)
	var jimungo = {};

jimungo.Login = function(urlToGet, urlToRedirect, communityLoginPage) {
	///<summary>The Login object.</summary>
	var _urlToGet = urlToGet;
	var _urlToRedirect = urlToRedirect;
	var _communityLoginPage = communityLoginPage;
	var _public = {
		authenticate: function(u, p, func) {

			if ($.browser.msie && (u.match(/\'/) || p.match(/\'/))) {
				if (confirm("Sorry we have an issue with logging your details into the site right now.\n\nPress OK to Log in from the Jimungo Community login page."))
					window.location.replace(_communityLoginPage + "?ReturnUrl=" + _urlToRedirect);
				return;
			}

			var user = u;
			var pass = p;

			///<summary>Calls the authentication handler</summary>
			var ops = ({
				dataType: "jsonp",
				contentType: "application/json; charset=utf-8",
				url: urlToGet,
				data: { username: user, password: pass },
				success: function(data, textStatus) {
					if (data.result === true) {
						if (_urlToRedirect !== undefined) {
							location.replace(_urlToRedirect);
						} else {
							$("#result").addClass('CommonMessageSuccess')
							.html("You have successfully signed in to your Jimungo account.")
							.show("fast");
						}
					} else {
						$("#result").addClass('CommonMessageError')
							.html("Oops, you have entered an incorrect Sign in Name or Password. Please try entering your details again.")
							.show("fast");
					}
					if (func !== undefined) {
						func();
					}
					this;
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$("#result").addClass('CommonMessageError').html("Oops! An error occured when attempting to sign you in. Please try again.").show("fast");
					this;
				}
			});
			$.ajax(ops);
		}
	};
	return _public
};