function UserProfile() {	
	this.serviceUrl = "/x-user.ashx";
	this.standardViews = "view=agent|user";
	this.initialised = false;
	this.onReady = null;
	this.onUpdate = null;
	this.context = null;
	
	this.ts = function() {
		return Number(new Date());
	};
	
	this.init = function() {
		var obj = this;		
		$.ajax({
			url : obj.serviceUrl + "?" + this.standardViews,
			cache : false,
			dataType : "json",
			success : function(data) {
				obj.context = data;
				obj.initialised = true;
				if(typeof(obj.onReady)=="function"){
					obj.onReady();
				}
			},
			error : function() {
				obj.context = null;
				obj.initialised = false;
				if(typeof(obj.onReady)=="function"){
					obj.onReady();
				}
			}
		});
	};
	
	this.isInRole = function(role){
		if(this.initialised && this.context){
			var roles = this.context.agent.profile.roles;
			if(roles) {
				for(var i=0; i < roles.length; i++){
					if(roles[i] == role){
						return true;
					}
				}
			}
		}
		return false;
	};
	
	this.appendRolesToUrl = function(url) {	
		if(this.context != null && url != null) {
			var roles = this.context.agent.profile.roles;			
			if(roles && roles.length > 0){			
				if(url.indexOf("?") == -1){
					url += "?";
				}
				else {
					url += "&";
				}
				
				url += "target=";
				
				for(var i=0; i < roles.length; i++){
					if(i > 0) url += ",";
					url += roles[i];
				}
			}
		}
		
		return url;
	};
	
	this.updateSetting = function(key, value){
		this.updateSettings([{name:key, value:value}]);
	};
	
	this.updateSettings = function(values) {
		var obj = this;
			
		var dataItems = "";
		
		for(var i = 0; i < values.length; i++){
			 dataItems += values[i].name + "=" + values[i].value + ";";
		}
			
		$.post(
			obj.serviceUrl + "?" + this.standardViews, 			
			{ action : "set", data : dataItems},
			function(data) {
				obj.context = data;
				obj.initialised = true;
				if(typeof(obj.onUpdate) == "function"){
					obj.onUpdate();
				}
			},
			"json"
		);
	};
	
	this.getRedirect = function(key, condition) {
		return this.serviceUrl + "?redirect=" + key + "&condition=" + encodeURIComponent(condition);
	};
	
	this.profileStoreTime = 30;
	
	this.ajaxEnabled = function() {
		var ajax = stateManager.readCookie("ajax");
		if(ajax == null) {
			stateManager.createCookie("ajax", "true", this.profileStoreTime);
			return true;
		}
		else {
			if(ajax == "true") {
				return true;
			}
			else {
				return false;
			}
		}
	};
	
	this.onAjaxPreferenceChange = null;
	this.enableAjax = function(){		
		stateManager.createCookie("ajax", "true", this.profileStoreTime);
		if(typeof(this.onAjaxPreferenceChange) == "function") this.onAjaxPreferenceChange(true);
	};
	this.disableAjax = function(){		
		stateManager.createCookie("ajax", "false", this.profileStoreTime);
		if(typeof(this.onAjaxPreferenceChange) == "function") this.onAjaxPreferenceChange(true);
	};	
}
