go.users = new (function() {

this.get = (
	function(id, emp)
	{
		if (objects[id]) {
			return objects[id];
		}
		if (!profiles[id]) {
			if (emp) {
				return new cUsers(id, ["deleted", "", "", 0, 0, 0]);
			}
			return null;
		}
		return (objects[id] = new cUsers(id, profiles[id]));
	}
);

this.set = (
	function(id, params)
	{
		return (objects[id] = new cUsers(id, params));
	}
);

this.load = (
	function(users, onload)
	{
		if (typeof(users) != "object") {
			users = [users];
		}
		var U = [];
		var len = users.length;
		for (var i = 0; i < len; i++) {
			var id = users[i];
			if (!profiles[id]) {
				U.push(id);
			}
		}
		if (U.length == 0) {
			if (typeof(onload) == "function") {
				setTimeout(onload, 0);
			} else {
				setTimeout((function() {go.methodExec(onload);}), 0);
			}
			return true;
		}
		var ajax  = go.ajax.getAction("users");
		ajax.get  = "ids=" + U.join(",");
		ajax._onl = onload;
		ajax.handler = onloadusers;
		ajax.send();
		return true;
	}
);

var pUsers = {
	"getFIO": (
		function(c)
		{
			return this.name + (this.nick ? " " + this.nick : "") + " " + this.surname;
		}
	),
	"getName": (
		function(c, pog, pod)
		{
			return formName(this.name, c, this.sex, pog, pod);
		}
	),
	"getSurname": (
		function(c, pog, pod)
		{
			return formSurname(this.surname, c, this.sex, pog, pod);
		}
	),	

	"srcAvatar": (
		function(format)
		{
			if (!this.photo) {
				if (format == "mini") {return "/i/noavatar_mini.jpg";}
				if (format == "maxi") {return "/i/noavatar_maxi.jpg";}
				return "/i/noavatar_midi.jpg";
			}
			if (format == "mini") {return "/i/avatars/min/" + this.id + ".jpg?" + this.photo;}
			if (format == "maxi") {return "/i/avatars/" + this.id + ".jpg?" + this.photo;}
			return "/i/avatars/p/" + this.id + ".jpg?" + this.photo;
		}
	),
	"imgAvatar": (
		function(format)
		{
			var img = document.createElement("img");
			img.setAttribute("src", this.srcAvatar(format));
			img.setAttribute("alt", "");
			return img;
		}
	),
	"hrefProfile": (
		function()
		{
			return "/?user=" + this.id;
		}
	),	
	"strSex": (
		function(male, female) 
		{
			return this.sex ? male : female;
		}
	)
};

/*** PRIVATE: ***/

var profiles = {};
var objects  = {};

this.setProfiles = (
	function(p)
	{
		profiles = p;
		return true;
	}
);

function cUsers(id, profile)
{
	this.id      = id;
	this.name    = profile[0];
	this.surname = profile[1];
	this.nick    = profile[2];
	this.photo   = profile[3];
	this.online  = profile[4];
	this.sex     = profile[5];
}

cUsers.prototype = pUsers;

var _this = this;

go.addListenerDEl("user", (
	function(types, el, res) 
	{
		var user = _this.get(types[1], true);
		var name = user.name + " " + user.surname;
		var a = document.createElement("A");
		a.appendChild(document.createTextNode(name));
		a.setAttribute("href", "/?user=" + types[1]);
		return a;		
	}
));

go.addListenerDEl("sex", (
	function(types, el, res) 
	{
		var user = _this.get(types[1], true);
		return document.createTextNode(user.strSex(types[2], types[3]));
	}
));

function onloadusers()
{
	if (!this._action.ok) {
		return false;
	}
	var u = this.JSON();
	for (var k in u) {
		profiles[k] = u;
	}
	if (this._onl) {
		go.methodExex(this._onl);
	}
	return true;
}

var formName = go.declension.formName;
var formSurname = go.declension.formSurname;

})();