﻿var WebService=new Class({
	Implements:[Chain,Options,Events],
	request:null,
	initialize:function (options) {
		this.setOptions(options);
	},
	send:function (methodName,data,onSuccess,onFailure) {
		this.request=new Request.JSON({
			url:this.url+"/"+methodName,
			onSuccess:onSuccess,
			onFailure:function (ex) {
				var error=eval("("+ex.responseText+")").Message;
				if (onFailure) onFailure(error);
				else alert(error);
			}.bind(this),
			data:JSON.encode(data || {}),
			urlEncoded:false
		});
		this.request.headers
			.erase("Accept")
			.erase("X-Request")
			.extend({"Content-Type":"application/json; charset=utf-8"});

		WebService.fireEvent("onSending",[this.request,methodName]);
		this.fireEvent("onSending",[this.request,methodName]);
		this.request.send();
		this.fireEvent("onSent",[this.request,methodName]);
		WebService.fireEvent("onSent",[this.request,methodName]);

		return this;
	},
	abort:function () {
		if (this.request) this.request.cancel();

		return this;
	}
});
Events.makeObjectEventable(WebService);
/*
WebService.addEvents({
	onSending:function (request,methodName) {
		request.headers.extend({"I-Am-A":"Custom Header"});
		request.options.url="i-am-a-custom.url";
	},
	onSent:function (request,methodName) {
	}
});
*/
$DL(function () {
	WebService.AuthenticationServiceClass=new Class({
		Extends:WebService,
		url:Config.RootUrl+"Authentication_JSON_AppService.axd",
		login:function (userName,password,createPersistentCookie,onSuccess,onFailure) {
			return WebService.prototype.send.apply(
				WebService.AuthenticationService,
				[
					"Login",
					{userName:userName,password:password,createPersistentCookie:createPersistentCookie},
					onSuccess,
					onFailure
				]
			);
		},
		logout:function (redirectUrl,onSuccess,onFailure) {
			return WebService.prototype.send.apply(
				WebService.AuthenticationService,
				[
					"Logout",
					null,
					onSuccess ? onSuccess.bind(null,[redirectUrl]) : function (redirectUrl) {
						// if no redirectUrl supplied - location.href=self will reload the page (without submitting forms if were)
						location.href=redirectUrl || location.href;
					},
					onFailure
				]
			);
		}
	});
	WebService.AuthenticationService=new WebService.AuthenticationServiceClass();

	WebService.ProfileServiceClass=new Class({
		Extends:WebService,
		url:Config.RootUrl+"Profile_JSON_AppService.axd",
		properties:new Hash(),
		save:function (propertyNamesToSave,onSuccess,onFailure) {
			var propertiesWithValues={};
			propertyNamesToSave.each(function (name) {
				propertiesWithValues[name]=this.properties[name];
			},this);
			return WebService.prototype.send.apply(
				WebService.ProfileService,
				[
					"SetPropertiesForCurrentUser",
					{values:propertiesWithValues,authenticatedUserOnly:false},
					onSuccess,
					onFailure
				]
			);
		},
		load:function (propertyNamesToLoad,onSuccess,onFailure) {
			var methodName,args=new Hash();

			if (!propertyNamesToLoad) {
				methodName="GetAllPropertiesForCurrentUser";
			} else {
				methodName="GetPropertiesForCurrentUser";
				propertyNamesToLoad.removeDuplicates();
				args.extend({properties:propertyNamesToLoad});
			}
			args.extend({authenticatedUserOnly:false});

			return WebService.prototype.send.apply(
				WebService.ProfileService,
				[
					methodName,
					args,
					function (result) {
						Hash.each(result,function (value,key) {
							this.properties[key]=value;
						},this);
						
						if (onSuccess) onSuccess(result);
					}.bind(this),
					onFailure
				]
			);
		}
	});
	WebService.ProfileService=new WebService.ProfileServiceClass();
},2);