top.cnt_requests = 0;
top.cnt_chats = 0;
top.cnt_ops = 0;

function chat_user(uid,level,status,name){
	this.uid = uid;
	this.level = level;
	this.status = status;
	this.name = name;
	this.request = null;
	this.box = null;
	this.node = null;
	this.loc = 0;
	this.new_messages = 0;

	this.cur_pos = -1;
	
	this.update_status = function(){
		if (!this.node)return;

		switch (this.cur_pos)
		{
		case 0:
			top.cnt_requests--;
			break;
		case 1:
			top.cnt_chats--;
			break;
		case 2:
			top.cnt_ops--;
			break;
		}

		var vis = false;
		if (this.status==1){
			if (this.level==1){
				vis = true;
			}
			else {
				if (this.request || (this.box&&this.box.sid!=0))
					vis = true;
			}
		}
		
		if (vis){
			/*
			if (this.nameNode)
			{
				if (top.active_box!=this.box&&this.box&&this.box.sid!=0&&this.box.typing)
				{
					this.nameNode.style.fontStyle='italic';
				}
				else
				{
					this.nameNode.style.fontStyle='';
				}
			}
			*/
			if (this.box&&this.box.sid)
			{
				if (this.box.new_messages>0>0)
				{
					this.nameNode.firstChild.nodeValue = this.name+' - ('+this.box.new_messages+')';
				}
				else
					this.nameNode.firstChild.nodeValue = this.name;
			}

			this.node.style.display = '';
			var icon;
			var title;
			if (this.level==1)
			{
				this.cur_pos = 2;
				if (this.new_messages>0)
				{
					icon = "operator";
				}
				else
				{
					icon = "operator2";
				}
				title = "";
			}
			else if (this.request){
				if (this.level==0 && this.loc==1)
				{
					var uf = id2node("users_f");
					var uDoc = uf.contentDocument;
					if (!uDoc)uDoc = uf.contentWindow.document;
					np = id2node("user_row",uDoc).parentNode;
					np.appendChild(this.node);
					this.loc = 0;
				}
				if (this.request_time>=60)
				{
					icon = "warning";
				}
				else
				{
					if (this.request[0]!=0)
					{
						icon = "wait2";
					}
					else
					{
						icon = "wait";
					}
				}
				title = "";
				this.cur_pos = 0;
			} 
			else {
				if (this.box && this.box.sid!=0){
					if (this.level==0 && this.loc==0)
					{
						var uf = id2node("users_f");
						var uDoc = uf.contentDocument;
						if (!uDoc)uDoc = uf.contentWindow.document;
						np = id2node("active_chats",uDoc).firstChild;
						np.appendChild(this.node);
						this.loc = 1;
					}
					if (top.multi_window)
					{
						icon = "active";
					}
					else if (top.active_box==this.box)
					{
						icon = "active";
						title = "";
					}
					else if (this.box.new_messages>0){
						icon = "new";
						title = "";
					}	
					else{
						icon = "talk";
						title = "";
					}
				}
				else {
					icon = "none";
					title = "";
				}
				this.cur_pos = 1;
			}
			if (this.status_node!=null){
				var src = this.status_node.src;
				var pos = src.indexOf("status_");
				src = src.substr(0,pos)+"status_"+icon+".png";

				if (top.chat_fix_png)
				{
					if (this.png_fix_node)this.png_fix_node.parentNode.removeChild(this.png_fix_node);
					this.png_fix_node = null;
					
					this.status_node.style.display = 'none';
					this.png_fix_node = this.status_node.ownerDocument.createElement("span");
					this.png_fix_node.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + src + "\', sizingMethod='scale');";
					this.png_fix_node.style.width = '16px';
					this.png_fix_node.style.height = '16px';
					this.png_fix_node.style.display = 'inline-block';
					this.status_node.parentNode.insertBefore(this.png_fix_node,this.status_node);
				}
				else
				{
					this.status_node.src = src;
					this.status_node.setAttribute("alt",title);
					this.status_node.setAttribute("title",title);
				}
			}
		}
		else {
			this.node.style.display = 'none';
			this.cur_pos = -1;
		}
		switch (this.cur_pos)
		{
		case 0:
			top.cnt_requests++;
			break;
		case 1:
			top.cnt_chats++;
			break;
		case 2:
			top.cnt_ops++;
			break;
		}
		id2node("no_requests",this.doc).style.display = (top.cnt_requests>0)?'none':'';
		id2node("no_chats",this.doc).style.display = (top.cnt_chats>0)?'none':'';
		id2node("no_operators",this.doc).style.display = (top.cnt_ops>0)?'none':'';
	}
			
	this.set_status = function(status){
		this.status = status;
		this.update_status();
	}
	
	this.request_time = 0;
	this.set_request = function(operator,message){
		if (this.request)
		{
			this.clear_request();
		}

		top.play_sound('new_request');

		this.request_time = 0;
		this.request = [operator,message];
		this.update_status();
		
		var cb = this.get_chat_box();
		cb.on_cancel_request = new Array();
		if (this.level==0){
			var msg = '';
			if (operator==0){
				msg = 'User [b]'+this.name+'[/b] is waiting for chat. Question: "'+unescape(message)+'". [ACCEPT]';
				top.focus_chat();
			}
			else {
				var op = find_user(operator);
				if (!op)return;
				msg = 'Operator [b]'+op.name+'[/b] needs an assistance. [ACCEPT]';
			}
			cb.add_message(0,0,msg);
		}
	}
	
	this.clear_request = function(){
		var cb = this.get_chat_box();
		if (cb.on_cancel_request)
		{
			for (var i=0; i<cb.on_cancel_request.length ; i++ )
			{
				cb.on_cancel_request[i].style.display = 'none';
			}
			cb.on_cancel_request = null;
			if (cb.message_count()==0)
			{
				cb.hide();
			}
		}
		if (cb.on_cancel_request2)
		{
			cb.on_cancel_request2.style.display = 'none';
			cb.on_cancel_request2 = null;
		}

		this.request_time = 0;
		this.request = null;
		this.update_status();
	}
	
	this.get_chat_box = function(){
		if (this.box==null){
			this.box = new chat_box(this);
		}
		return this.box;
	}
	
	this.select = function(){
		var cb = this.get_chat_box();
		cb.show();
	}

	this.process_node = function(node){
		if (node.getAttribute&&node.getAttribute("id")=="user_x"){
			node.setAttribute("id","user_"+this.uid);
		}
		if (node.nodeName=='#text' && node.nodeValue=='#name#')
		{
			this.nameNode = node.parentNode;
			node.nodeValue = this.name;
		}
		if (node.getAttribute&&node.getAttribute("id")=="status")
		{
			this.status_node = node;
		}

		var n = node.firstChild;
		while (n)
		{
			this.process_node(n);
			n = n.nextSibling;
		}
	}
	
	if (top.is_admin && uid!=top.userid){
		var tpl = null;
		var uf = id2node("users_f");
		var uDoc = uf.contentDocument;
		if (!uDoc)uDoc = uf.contentWindow.document;
		this.doc = uDoc;
		if (uDoc)
		{
			if (level==0){
				tpl = id2node("user_row",uDoc);
			}
			else {
				tpl = id2node("admin_row",uDoc);
			}
		}
		if (tpl)
		{
			this.node = tpl.cloneNode(true);
			this.node.id = '';
			this.process_node(this.node);
			tpl.parentNode.appendChild(this.node);
			this.update_status();
		}
		else
			this.node = null;
	}
}

function find_user(uid){
	if (!top.users)return null;
	for (var i=0;i<top.users.length;i++){
		if (top.users[i].uid==uid)return top.users[i];
	}
	return null;
}

function get_user_name(uid){
	if (uid==0)return 'SYSTEM';
	if (uid==top.userid)return top.username;
	var u = find_user(uid);
	if (u==null)return '';
	return u.name;
}

function get_user_level(uid){
	if (uid==0)return -1;
	if (uid==top.userid)return top.is_admin?1:0;
	var u = find_user(uid);
	if (u==null)return -1;
	return u.level;
}

function process_user(uid,level,status,name){
	var u = find_user(uid);
	if (u!=null){
		u.set_status(status);
		return;
	}
	
	u = new chat_user(uid,level,status,name);
	if (!top.users)top.users = new Array();
	top.users[top.users.length] = u;
}

function set_user_request(uid,operator,message){
	var u = find_user(uid);
	if (u!=null){
		u.set_request(operator,message);
	}
}

function clear_user_request(uid){
	var u = find_user(uid);
	if (u!=null){
		u.clear_request();
	}
}

function select_user(uid){
	var u = find_user(uid);
	if (u!=null){
		u.select();
	}
}

function chat_user_selected(node){
	var uid = node.id.substr(5);
	var u = find_user(uid);
	if (!u)return;
	u.get_chat_box().show();
}
