var ChatFriend = Class.create({
	initialize: function( p_id, p_name, p_icon,p_numOnList){
		this.id = p_id;
		this.window = false;
		this.icon = p_icon
		this.name = p_name;
		this.num = p_numOnList;
		this.currentLineNum = 0;
		this.numLines = 0;
		this.status = 999;
		this.lines = [];
		this.maxSize = 125;
		g_chat.friends[this.id] = this;
		this.wname = 'KazM_FW_'+this.id+'_In';
		this.stored = [];
		this.offList = true;
		++ g_chat.numUsers;
		this.CreateOnList();
	},
	UpdateStatus: function(){
		if (this.status > 10 && ! this.offList)
		{
			this.offList = true;
			$('KazM_FriendsOff').insert($('KazM_F_'+this.id));
		}
		else if (this.status < 10)
		{
			this.offList = false;
			$('KazM_FriendsOn').insert($('KazM_F_'+this.id));
		}
		$('KazM_FI_'+this.id).src = (this.status > 10?'/i/s_error.png':'/i/s_okay.png');
	},
	_removeLine: function(){
		if (this.numLines >= this.maxSize)
		{
			var i = this.currentLineNum - this.maxSize;
			var l_temp = $(this.wname + i);
			l_temp.parentNode.removeChild( l_temp);
			-- this.numLines;
		}
	},
	AddLine: function( p_line){
		if (this.numLines >= this.maxSize)
		{
			this._removeLine();
		}
		var l_temp = new Element('div');
		l_temp.setAttribute( 'id', this.wname + this.currentLineNum);
		l_temp.update( p_line);
		$( this.wname).insert( l_temp);
		$( this.wname).scrollTop = $( this.wname).scrollHeight;
		++ this.currentLineNum;
		++ this.numLines;
	},
	ShowWindow: function(){
		if (this.window === false)
		{
			this.Create();
		}
		this.UnHl();
		$('KazM_FW_'+this.id).show();
		$('KazM_F_'+this.id).setStyle('background:#FFFFFF');
	},
	Create: function(){
		this.window = new ChatWindow( 'KazM_FW_'+this.id, this.name, this.icon, {x:260,y:300},{x:this.num * 10,y:100},{x:260,y:150},'g_chat.CloseW('+this.id+');return false;');
		$('KazM_FW_'+this.id+'_In').setStyle('font-size:75%;line-height:13px;');
		$('KazM_FW_'+this.id).insert('<div><a href="javascript:;"onclick="return g_chat.OpenSmileysFrom('+this.id+')"onmouseover="tooltip.show(\'Smileys\')"onmouseout="tooltip.hide()"><img src="/i/j/sm/SMILEY01.png" class="i24m"/></a><input type="text" id="KazM_I_'+this.id+'"style="width:200px;"><a href="javascript:;"onclick="g_chat.PostFrom('+this.id+')"><img src="/i/rar.png"class="i24m"onmouseover="tooltip.show(\'Envoyer\')"onmouseout="tooltip.hide()"></a></div>');
		$('KazM_I_'+this.id).onkeydown = this.ValidateInput.bind( this);
		$('KazM_FW_'+this.id).onclick = this.Focus.bind(this);
		if (this.stored.length > 0)
		{
			for (var i = this.stored.length-1 ; i >=0 ; -- i)
			{
				this.AddLine( this.stored[i]);
			}
		}
	},
	Hl: function(){
		if (g_chat.cwFocus == this.wname){return;}
		g_chat.Hl();
		if($('KazM_FW_'+this.id+'T'))
		{
			$('KazM_FW_'+this.id+'T').setStyle('background:#FF9999');
		}
		$('KazM_FIN_'+this.id).setStyle('font-weight:bold;color:red');
	},
	UnHl: function(){
		if($('KazM_FW_'+this.id+'T'))
		{
			$('KazM_FW_'+this.id+'T').setStyle('background:#FFFFFF');
		}
		$('KazM_FIN_'+this.id).setStyle('font-weight:normal;color:black');
	},
	AddSmiley: function(p_txt){
		$('KazM_I_'+this.id).value += p_txt;
	},
	ValidateInput: function( p_event){
		p_event = p_event || window.event;
		if (ConsumeEnter(p_event))
		{
			this.Post();
			return false;
		}
		else if (p_event.keyCode == 9)
		{
			p_event.returnValue = false;
			p_event.cancel = true;
			$('KazM_I_'+this.id).value += g_chat.CompleteName( $('KazM_I_'+this.id).value);
			return false;
		}
		return true;
	},
	Post: function(){
		this.RTalk($('KazM_I_'+this.id).value);
		g_chat.WhispTalk($('KazM_I_'+this.id).value, this.id);
		$('KazM_I_'+this.id).value = '';
	},
	Talk: function( p_txt, p_priv, p_first){
		if ( ! p_priv){return;}

		if (!p_first && (!$(this.wname) || !$(this.wname).visible())){this.Hl();}
		if (this.window)
		{
			this.AddLine( this.name + ' &gt; ' + p_txt);
		}
		else
		{
			this.stored.push( this.name + ' &gt; ' + p_txt);
		}
	},
	Focus: function(){
		g_chat.UnFocus();
		$(this.wname).setStyle({zIndex:2500});
		g_chat.cwFocus = this.wname;
		this.UnHl();
	},
	RTalk: function( p_txt){
		if (g_city.loaded)
		{
			g_city.users[g_mainChar.id].ShowText( g_chat.ApplySmileys(p_txt));
		}
		this.AddLine( g_mainChar.name + ' &gt; ' + g_chat.ApplySmileys(p_txt));
	},
	MouseOver: function(){
		tooltip.show( g_text.chat['user_dbl']);
	},
	CreateOnList: function(){
		var ele = new Element( 'li',{'class':'blackHov',id:'KazM_F_'+this.id});
		ele.onmousedown = function(){return false;};
		ele.ondblclick = this.ShowWindow.bind(this);
		ele.setStyle( 'width:220px;margin:5px;line-height:32px;');
		ele.onmouseover = this.MouseOver.bind(this);
		ele.onmouseout = tooltip._hide;
		ele.update('<img id="KazM_FI_'+this.id+'"src="/i/s_error.png" style="float:right"class="i20m"/><img src="'+this.icon+'" class="i32m"><span id="KazM_FIN_'+this.id+'">'+this.name+'</span>');
		$('KazM_FriendsOff').insert(ele);
	},
	Delete: function(){
		deleteNode('KazM_FW_'+this.id);
		deleteNode('KazM_F_'+this.id);
	}
});
var ChatGroup = Class.create({
	initialize: function( p_id, p_name, p_numOnList){
		this.id = p_id;
		this.window = false;
		this.name = p_name;
		this.num = p_numOnList;
		this.currentLineNum = 0;
		this.numLines = 0;
		this.lines = [];
		this.maxSize = 125;
		g_chat.groups[this.id] = this;
		this.wname = 'KazM_GW_'+this.id+'_In';
		this.stored = [];
		++ g_chat.numUsers;
		this.CreateOnList();
	},
	CreateOnList: function(){
		var ele = new Element( 'li',{'class':'blackHov',id:'KazM_G_'+this.id});
		ele.onmousedown = function(){return false;};
		ele.ondblclick = this.ShowWindow.bind(this);
		ele.setStyle( 'width:220px;margin:5px;line-height:32px;');
		ele.onmouseover = this.MouseOver.bind(this);
		ele.onmouseout = tooltip._hide;
		ele.update(this.name);
		$('KazM_Groups').insert(ele);
	},
	ShowWindow: function(){
		if (this.window === false)
		{
			this.Create();
		}
		$('KazM_GW_'+this.id).show();
		$('KazM_G_'+this.id).setStyle('background:#FFFFFF');
		$('KazM_GW_'+this.id+'T').setStyle('background:#FFFFFF');
		this.UnHl();
	},
	MouseOver: function(){
		tooltip.show( g_text.chat['gr_dbl']);
	},
	Create: function(){
		this.window = new ChatWindow( 'KazM_GW_'+this.id, this.name, false, {x:260,y:300},{x:this.num * 10,y:100},{x:260,y:150},'g_chat.CloseW('+this.id+',true);return false;');
		$('KazM_GW_'+this.id+'_In').setStyle('font-size:75%;line-height:13px;');
		$('KazM_GW_'+this.id).insert( '<div><a href="javascript:;"onclick="return g_chat.OpenSmileysFrom('+this.id+')"onmouseover="tooltip.show(\'Smileys\')"onmouseout="tooltip.hide()"><img src="/i/j/sm/SMILEY01.png" class="i24m"/></a><input type="text" id="KazM_GI_'+this.id+'"style="width:200px;"><img src="/i/rar.png"class="i24m"onmouseover="tooltip.show(\'Envoyer\')"onmouseout="tooltip.hide()"></div>');
		$('KazM_GI_'+this.id).onkeydown = this.ValidateInput.bind( this);
		$('KazM_GW_'+this.id).onclick = this.Focus.bind(this);
		if (this.stored.length > 0)
		{
			for (var i = this.stored.length-1 ; i >=0 ; -- i)
			{
				this.AddLine( this.stored[i]);
			}
		}
	},
	Hl: function(){
		if (g_chat.cwFocus == this.wname){return;}
		if($('KazM_GW_'+this.id+'T'))
		{
			$('KazM_GW_'+this.id+'T').setStyle('background:#FF9999');
		}
		if ($('KazM_G_'+this.id))
		{
			$('KazM_G_'+this.id).setStyle('background:#FF9999');
		}
	},
	UnHl: function(){
		if($('KazM_GW_'+this.id+'T'))
		{
			$('KazM_GW_'+this.id+'T').setStyle('background:#FFFFFF');
		}
		if ($('KazM_G_'+this.id))
		{
			$('KazM_G_'+this.id).setStyle('background:#FFFFFF');
		}
	},
	Focus: function(){
		g_chat.UnFocus();
		$(this.wname).setStyle({zIndex:2500});
		g_chat.cwFocus = this.wname;
		this.UnHl();
	},
	RTalk: function( p_txt){
		if (g_city.loaded)
		{
			g_city.users[g_mainChar.id].ShowText( g_chat.ApplySmileys(p_txt));
		}
		this.AddLine( g_mainChar.name + ' &gt; ' + g_chat.ApplySmileys(p_txt));
	},
	Talk: function( p_txt, p_first){
		if (!p_first){this.Hl();}
		if (this.window)
		{
			this.AddLine( this.name + ' &gt; ' + p_txt);
		}
		else
		{
			this.stored.push( this.name + ' &gt; ' + p_txt);
		}
	},
	Post: function(){
		this.RTalk($('KazM_GI_'+this.id).value);
		g_chat.WhispTalk($('KazM_GI_'+this.id).value, this.id, true);
		$('KazM_GI_'+this.id).value = '';
	},
	ValidateInput: function( p_event){
		p_event = p_event || window.event;
		if (ConsumeEnter(p_event))
		{
			this.Post();
			return false;
		}
		return true;
	},
	AddSmiley: function(p_txt){
		$('KazM_I_'+this.id).value += p_txt;
	},
	_removeLine: function(){
		if (this.numLines >= this.maxSize)
		{
			var i = this.currentLineNum - this.maxSize;
			var l_temp = $(this.wname + i);
			l_temp.parentNode.removeChild( l_temp);
			-- this.numLines;
		}
	},
	AddLine: function( p_line){
		if (this.numLines >= this.maxSize)
		{
			this._removeLine();
		}
		var l_temp = new Element('div');
		l_temp.setAttribute( 'id', this.wname + this.currentLineNum);
		l_temp.update( p_line);
		$( this.wname).insert( l_temp);
		++ this.currentLineNum;
		++ this.numLines;
	},
	UpdateStatus: function(){
		$('KazM_FI_'+this.id).src = (this.status > 10?'/i/s_error.png':'/i/s_okay.png');
	}
});
var ChatMainW = Class.create({
	initialize: function( p_id, p_name, p_numOnList){
		this.id = p_id;
		this.name = p_name;
		this.num = p_numOnList;
		this.currentLineNum = 0;
		this.numLines = 0;
		this.lines = [];
		this.maxSize = 125;
		this.wname = 'ChatGenTalk';
		g_chat.groups[this.id] = this;
		this.stored = [];
		++ g_chat.numUsers;
	},
	Hl: function(){

	},
	UnHl: function(){
	},
	Focus: function(){
	},
	RTalk: function( p_txt,who, p_col){
		++g_chat.numMessagesSinceAuto;
		if (p_txt.toLowerCase().indexOf(g_mainChar.name.toLowerCase()) != -1 && !$('ChatGenTalkOut').visible())
		{
			g_chat.ToggleGen();
		}
		this.AddLine( (p_col?'<font style="color:'+p_col+'">':'')+'<strong>'+who + ' &gt;</strong>  ' + g_chat.ApplySmileys(p_txt)+(p_col?'</font>':''));
		$('ChatGenTalk').scrollTop = $('ChatGenTalk').scrollHeight;
	},
	AddSmiley: function(p_txt){
		clearDefault( $( 'ChatText'));
		$('ChatText').value += p_txt;
	},
	_removeLine: function(){
		if (this.numLines >= this.maxSize)
		{
			var i = this.currentLineNum - this.maxSize;
			var l_temp = $(this.wname + i);
			l_temp.parentNode.removeChild( l_temp);
			-- this.numLines;
		}
	},
	AddLine: function( p_line){
		if (this.numLines >= this.maxSize)
		{
			this._removeLine();
		}
		var l_temp = new Element('div');
		l_temp.setAttribute( 'id', this.wname + this.currentLineNum);
		l_temp.update( p_line);
		$( this.wname).insert( l_temp);
		$( this.wname).scrollTop = $( this.wname).scrollHeight;
		++ this.currentLineNum;
		++ this.numLines;
	},
	ValidateInput: function( p_event){
		p_event = p_event || window.event;
		if (ConsumeEnter(p_event))
		{
			g_chat.GenTalk();
			return false;
		}
		else if (p_event.keyCode == 9)
		{
			p_event.returnValue = false;
			p_event.cancel = true;
			$('ChatText').value += g_chat.CompleteName( $('ChatText').value);
			return false;
		}
		return true;
	}
});
var ChatInviter = Class.create({
	initialize: function(p_id, p_name, p_icon, p_func){
		this.id = p_id;
		this.name = p_name;
		this.icon = p_icon;
		this.Activate = p_func || false;
		this.CreateOnList();
	},
	CreateOnList: function(){
		var ele = new Element( 'li',{'class':'blackHov',id:'KazM_Iv_'+this.id});
		var e = new Element('div');
		e.onmousedown = function(){return false;};
		e.setStyle( 'width:220px;margin:5px;line-height:32px;');
		e.onmouseover = this.MouseOver.bind(this);
		e.onmouseout = tooltip._hide;
		//Note : ie doesn't like dblclicks on list items -_-
		e.ondblclick = this.Activate;
		e.update('<img src="'+this.icon+'" class="i32m">'+this.name);
		ele.update(e);
		$('KazM_Invites').insert(ele);
	},
	MouseOver: function(){
		tooltip.show(this.name);
	}
});
var g_chat = {	//javascript: g_chat.Add([29,19,6,'miaou']);
	windows: {},
	cwFocus: false,
	friends: {},
	groups: {},
	smileys: [],
	users: {},
	ignored: {},
	numUsers: 0,
	gen: false,
	official: 0,
	roomId:0,
	neverTalked: true,
	smileyOrigin: 0,
	autoMessages: {},
	numMessagesSinceAuto: 99,
	selectedUser: 0,
	shown: false,
	sound: false,
	toggled: {f:true,i:true,g:true},
	AddUsers: function(){
		for (var i = 0 ; i < arguments.length; ++ i)
		{
			var l = arguments[i];
			if ( ! this.users[l[0]])
			{
				this._addUser(parseInt(l[0]), l[1], parseInt(l[2]));
			}
		}
	},
	RemoveUsers: function(){
		for (var i = 0 ; i < arguments.length; ++ i)
		{
			var l = arguments[i];
			if ($('ChatList_'+l) && this.users[l])
			{
				deleteNode( 'ChatList_' + l);
				delete this.users[l];
				if (this.friends[l])
				{
					this.friends[l].status = 999;
				}
			}
		}
	},
	_addUser:function(id,name,status){
		var col = this.friends[id]?'#DDFFDD':'#DDDDFF';
		var st = (status == 1?'@':status==2?'%':status==3?'+':'');
		$('ChatGenList').insert('<a href="javascript:;"onclick="g_chat.SelectUser('+id+');"><div id="ChatList_'+id+'"style="colour:'+col+'">'+st+name+'</div></a>');
		this.users[id] = name;
	},
	SelectUser: function(p_id){
		this.selectedUser=p_id;
		var u = this.users[p_id];
		if (!u){return;}
		$('ChatGenTalkUser').show();
	},
	SetIgnored: function(){
		for (var i = 0 ; i < arguments.length; ++ i)
		{
			var l = arguments[i];
			this.AddIgnored(l[0],l[1],true);
		}
	},
	IgnoreSelected: function(){
		var u = this.users[this.selectedUser];
		if(!u){return;}
		if (this.ignored[this.selectedUser])
		{
			g_confirm.Show( g_text.chat['unignoresel'], 'Main',function(){$('ChatGenTalkUser').hide();g_chat.Unignore( g_chat.selectedUser);});
		}
		else
		{
			g_confirm.Show( g_text.chat['ignoresel'], 'Main',function(){$('ChatGenTalkUser').hide();g_chat.AddIgnored( g_chat.selectedUser, u);});
		}
	},
	FriendSelected: function(){
		var u = this.users[this.selectedUser];
		if(!u){return;}
		$('ChatGenTalkUser').hide();
		_openUrl2('profile',{c:'ami',action:'ask',id_user:this.selectedUser});
	},
	ProfileSelected: function(){
		var u = this.users[this.selectedUser];
		if(!u){return;}
		$('ChatGenTalkUser').hide();
		_openUrl2('profile',{c:'ami',id_user:this.selectedUser});
	},
	VisitSelected: function(){
		var u = this.users[this.selectedUser];
		if(!u){return;}
		$('ChatGenTalkUser').hide();
		_openUrl('home',{id:this.selectedUser});
	},
	TrackSelected: function(){
		var u = this.users[this.selectedUser];
		if(!u){return;}
		$('ChatGenTalkUser').hide();
		_openUrl2('track',{id:this.selectedUser});
	},
	AddIgnored: function( p_id, p_name, p_first){
		if (this.ignored[p_id]){return;}
		this.ignored[p_id] = true;
		$('ChatIgnoredList').insert('<div id="ChatIgnore_'+p_id+'"style="position:relative;"><a href="javascript:;"onclick="g_chat.Unignore('+p_id+')"><img src="/i/b_drop.png"class="i24m"/></a>&nbsp;'+p_name+'</div>');
		if (!p_first)
		{
			new Ajax.Request('/callback/chat_ignore.php',{parameters:{target:p_id,type:1}});
		}
	},
	Unignore: function( p_id){
		p_id = parseInt(p_id);
		if (!this.ignored[p_id]){return;}
		delete this.ignored[p_id];
		new Ajax.Request('/callback/chat_ignore.php',{parameters:{target:p_id,type:2}});
		deleteNode('ChatIgnore_'+p_id);
	},
	ToggleSound: function(){
		this.sound = !this.sound;
		$('ChatSound').src = '/i/iconesanstexte/voix_'+(this.sound?'on':'off')+'.png';
	},
	Hl: function(){
		if (this.shown){return;}
		$('KazMIcoMenu').setStyle('background:red');
		if(this.sound){/*g_sound.Play();*/}
	},
	TogglePart: function(p_part){
		switch(p_part)
		{
			case 0:$('KazM_Friends').toggle();this.toggled.f = ! this.toggled.f;$('KazM_TF').update(this.toggled.f?'-':'+');break;
			case 1:$('KazM_Invites').toggle();this.toggled.i = ! this.toggled.i;$('KazM_TI').update(this.toggled.i?'-':'+');break;
			case 2:$('KazM_Groups').toggle();this.toggled.g = ! this.toggled.g;$('KazM_TG').update(this.toggled.g?'-':'+');break;
		}
	},
	Toggle: function(){
		this.shown = !this.shown;
		$('KazM_Main').toggle();
		if (this.shown)
		{
			$('KazMIcoMenu').setStyle('background:white');
		}
	},
	ToggleGen: function(){
		$('ChatGenToggler').src=$('ChatGenTalkOut').visible()?'/i/flechedown.png':'/i/flecheup.png';
		$('ChatGenTalkOut').toggle();
		$('ChatGenTalk').scrollTop = $('ChatGenTalk').scrollHeight;
	},
	Load: function( p_id){
		this.roomId = p_id;
		this.window = new ChatWindow( 'KazM_Main', '<img src="/i/'+g_mainChar.language+'/menu/t/kazumess.png" style="height:32px";>', false, {x:260,y:300},{x:100,y:300},{x:260,y:150},'g_chat.Toggle();return false;');
		$('KazM_Main_In').insert('<a onclick="g_chat.TogglePart(0)"><span id="KazM_TF">-</span> '+g_text.chat['friends']+'</a><br/><div id="KazM_FriendsOn"style="padding-left:10px;"></div><div id="KazM_FriendsOff"style="padding-left:10px;"></div>');
		$('KazM_Main_In').insert('<a onclick="g_chat.TogglePart(2)"><span id="KazM_TG">-</span> '+g_text.chat['groups']+'</a><br/><div id="KazM_Groups"style="padding-left:10px;"></div>');
		$('KazM_Main_In').insert('<a onclick="g_chat.TogglePart(1)"><span id="KazM_TI">-</span> '+g_text.chat['invites']+'</a><br/><div id="KazM_Invites"style="padding-left:10px;"></div>');
		Recenter('KazM_Main');
		var ele = new Element('div',{id:'SmileyList','class':'Chat_Smileys'});
		ele.hide();
		$('body').insert(ele);
		new ChatInviter(1,'Kazulife','/i/icone_kazu32x32.png',function(){return _openUrl2('profile');});
		new ChatInviter(0,'Facebook','/i/facebook-icon.png',function(){facebook.Invites( g_text.chat['invite1'],g_text.chat['invite2']);});
		this.gen = new ChatMainW();
	
		this.interval = setInterval( this.Check.bind(this), 5000);
		this.Check();
		this._initSmileys();
		setTimeout( this.CheckAutoMessages.bind(this),60000);

		$('ChatText').onkeydown = this.gen.ValidateInput.bind(this.gen);
	},
	UnFocus: function(){
		if (this.cwFocus)
		{
			$(this.cwFocus).setStyle({zIndex:2400});
			this.cwFocus = false;
		}
	},
	SetFriends: function(){
		for (var i = 0 ; i < arguments.length;++i)
		{
			var l = arguments[i];
			var f = new ChatFriend( l[0], l[1], l[2], i);
		}
	},
	AddFriend: function( p_id, p_name, p_icon){
		var j=0;
		for (var i in this.friends) {++ j;}
		new ChatFriend( p_id, p_name, p_icon, j);
	},
	DelFriend: function(p_id){
		if (!this.friends[p_id]){return;}
		delete this.friends[p_id];
	},
	SetGroups: function(){
		for (var i = 0 ; i < arguments.length;++i)
		{
			var l = arguments[i];
			var f = new ChatGroup( l[0], l[1], i);
		}
	},
	Banned: function(){
		clearInterval( this.interval);
		this.interval = 0;
		this.gen.RTalk( g_text.chat['banned'],'Kazulife');
	},
	Check: function(){
		new Ajax.Request( '/callback/chat_read.php', { method: 'post', parameters: {room : this.roomId, x:g_mainChar.x, y:g_mainChar.y, r:g_mainChar.rid, z:g_mainChar.zid}});
	},
	GenTalk: function(){
		var l_text = $('ChatText').value;
		if (l_text == $('ChatText').defaultValue)
		{
			$('ChatGenToggler').src = '/i/flechedown.png';
			$('ChatGenTalkOut').show();
			$('ChatGenTalk').scrollTop = $('ChatGenTalk').scrollHeight;
			$('ChatText').value = '';
			return;
		}
		$('ChatText').value = '';
		
		if (this.official)
		{
			l_text = this.EvalCommands(l_text) || '';
		}
		if (l_text.length == 0){return;}
		
		this.gen.RTalk(l_text, g_mainChar.name);
		if (g_city.loaded && g_city.users[g_mainChar.id])
		{
			g_city.users[g_mainChar.id].ShowText( this.ApplySmileys(l_text));
		}
		new Ajax.Request( '/callback/chat_write.php', { method: 'post', parameters: {room : this.roomId, text: l_text,type:8}} );
		return false;
	},
	WhispTalk: function( p_text, p_targetId, p_group){
		if (p_group)
		{
			new Ajax.Request( '/callback/chat_write.php', { method: 'post', parameters: {room: this.roomId, text: p_text, target:p_targetId,type:7}});
		}
		else
		{
			new Ajax.Request( '/callback/chat_write.php', { method: 'post', parameters: {room: this.roomId, text: p_text, target:p_targetId,type:6}});
		}
	},
	Add: function(){
		for (var i = 0 ; i < arguments.length;++i)
		{
			var l = arguments[i];
			//id user, target, special, text
			var id = parseInt( l[0]);
			if (this.ignored[id]){continue;}//Ignore if needed
			var type = parseInt(l[2]);
			switch (type)
			{
				case 1:break;//Global announcement
				case 2:g_qm.PrintAdvice( l[3]);break;//cmsg
				case 6:{//pv to current user
					if (parseInt(l[1]) == g_mainChar.id && this.friends[id])
					{
						this.friends[id].Talk( this.ApplySmileys( l[3]), true, this.neverTalked);
					}
				}break;
				case 7:{//pv to current group
					
					if (this.groups[parseInt(l[1])])
					{
						this.groups[parseInt(l[1])].Talk( this.ApplySmileys( l[3]), this.neverTalked);
					}
				}break;
				case 8:{//gen talk
					this.gen.RTalk(this.ApplySmileys( l[3]),l[4]);

				}break;
			}
			if (g_city.users[id])
			{
				g_city.users[id].ShowText( this.ApplySmileys( l[3]));
			}
			/*
			if (this.users[id])
			{
				this.users[id].Talk( l[1], l[2], l[3], this.ApplySmileys( l[4]), this.neverTalked);
			}
			*/
		}
		this.neverTalked = false;
	},
	ApplySmileys: function(p_text){
		if ( ! p_text){return '';}
		var l_line = p_text;
		for (var i = 0 ; i < this.smileys.length ; ++ i)
		{
			l_line = l_line.replace( this.smileys[i].reg, '<img class="chsm" src="' + this.smileys[i].img + '">');
		}
		return l_line;
	},
	_initSmileys: function(){
		new Smiley(':grin:','SMILEY01.png');
		new Smiley(':gne:','SMILEY02.png');
		new Smiley(':-_-:','SMILEY03.png');
		new Smiley(':cry:','SMILEY04.png');
		new Smiley(':anger:','SMILEY05.png');
		new Smiley(':blush:','SMILEY06.png');
		new Smiley(':confused:','SMILEY07.png');
		new Smiley(':urgh:','SMILEY08.png');
		new Smiley(':eww:','SMILEY09.png');
		new Smiley(':argh:','SMILEY10.png');
		new Smiley(':yeahh:','SMILEY11.png');
		new Smiley(':bleh:','SMILEY12.png');
		new Smiley(':8D:','SMILEY13.png');
		new Smiley(':kiss:','SMILEY14.png');
		new Smiley(':cofee:','SMILEY15.png');
		new Smiley(':demon:','SMILEY16.png');
		new Smiley(';)','SMILEY17.png');
		new Smiley(':kdo:','SMILEY18.png');
		new Smiley(':cake:','SMILEY19.png');
		new Smiley(':rip:','SMILEY20.png');
		new Smiley(':fish:','SMILEY21.png');
		new Smiley(':cat:','SMILEY22.png');
		new Smiley(':lum:','SMILEY23.png');
		new Smiley(':bunny:','SMILEY24.png');
		new Smiley(':vamp:','SMILEY25.png');
		new Smiley(':saint:','SMILEY26.png');
		new Smiley(':fleur:','SMILEY27.png');
		new Smiley(':coeur:','coeur.png');
		
		for (var i = 0 ; i < this.smileys.length ; ++ i)
		{
			this.smileys[i].Load();
		}
	},
	OpenSmileysFrom: function( p_id){
		this.smileyOrigin = p_id;
		if (p_id > 0)
		{
			$('KazM_FW_'+this.smileyOrigin).insert($('SmileyList'));
		}
		else
		{
			$('ChatGenInput').insert($('SmileyList'));
		}
		$('SmileyList').show();
	},
	PostFrom: function( p_id){
		if (this.friends[p_id])
		{
			this.friends[p_id].Post();
		}
	},
	AddSmiley: function(p_txt){
		$('SmileyList').hide();
		if (this.smileyOrigin > 0)
		{
		this.friends[this.smileyOrigin].AddSmiley(p_txt);
		}
		else
		{
			this.gen.AddSmiley(p_txt);
		}
	},
	CloseW: function( p_id, p_group){
		if ( ! p_group)
		{
			if (this.cwFocus == 'KazM_FW_'+p_id)
			{
				this.UnFocus();
			}
			$('KazM_FW_'+p_id).hide();
		}
		else
		{
			if (this.cwFocus == 'KazM_GW_'+p_id)
			{
				this.UnFocus();
			}
			$('KazM_GW_'+p_id).hide();
		}
	},
	EvalCommands: function( p_text){
		var l_max = p_text.indexOf( ' ');
		if (l_max == -1){l_max = p_text.length;}
		var l_command = p_text.substr( 0, l_max);
		switch (l_command)
		{
			case '/reqhelp':
			{
				if (g_mainChar.status == 1)
				{
					l_max = p_text.indexOf( ':');
					if (l_max > 8 && l_max < p_text.length)
					{
						var l_name = p_text.substr( 9, l_max-9);
						for (var i = 0; i < this.chatters.length ; ++ i)
						{
							if (this.chatters[i].name == l_name)
							{
								new Ajax.Request( '/callback/admin.php', { method: 'post', parameters: {room : this.roomId, text:'/reqhelp K',target:this.chatters[i].id,text2:l_text.substr( l_max + 1)}} );
								return;
							}
						}
					}
				}
				return;
			}
			case '/cmsg':
			{
				if (g_mainChar.status == 1)
				{
					l_max = p_text.indexOf( ':');
					if (l_max > 5 && l_max < p_text.length)
					{
						var l_name = p_text.substr( 6, l_max-6);
						for (var i = 0; i < this.chatters.length ; ++ i)
						{
							if (this.chatters[i].name == l_name)
							{
								new Ajax.Request( '/callback/admin.php', { method: 'post', parameters: {room : this.roomId, text:'/cmsg K',target:this.chatters[i].id,text2:l_text.substr( l_max + 1)}} );
								return;
							}
						}
					}
				}
				return;
			}
			case '/tele':
			{
				if (g_mainChar.status == 1)
				{
					l_command = parseInt(p_text.substr( l_max + 1, 3));
					g_city.ChangeRoom( g_mainChar.id, l_command, g_mainChar.x, g_mainChar.y);
					$('ChatText').value = '/tele ';
				}
				return;
			}
			case '/tcl':
			{
				if (g_mainChar.status == 1)
				{
					g_city.tcl = ! g_city.tcl;
				}
				return;
			}
			case '/meubles':
			{
				if (g_mainChar.status == 1)
				{
					_openUrl('furnit',{all:1});
				}
				return;
			}
			case '/custom':
			{
				if (g_mainChar.status == 1)
				{
					_openUrl2('avatar',{id:-1});
				}
				return;
			}
			case '/dcoll':
			{
				if (g_mainChar.status == 1)
				{
					g_colls.Draw();
					$('ChatText').value = '/tele ';
				}
				return;
			}
			case '/dcoff':
			{
				if (g_mainChar.status == 1)
				{
					$('CollD').hide();
				}
				return;
			}
			case '/cban':
			case '/uncban':
			case '/sban':
			case '/unsban':
			case '/amsg':
			{
				if (g_mainChar.status == 1 || g_mainChar.status == 2){
					new Ajax.Request( '/callback/admin.php', { method: 'post', parameters: {room : this.roomId, text: p_text}} );
				}
				return;
			}
		}
		
		if (l_command[0] && l_command[0] == '/'){return;}
		
		return p_text;
	},
	UpdateUsers: function(){
		for (var i in this.friends)
		{
			++ this.friends[i].status;
		}
		for (var i = 0 ; i < arguments.length;++i)
		{
			if (this.friends[arguments[i]])
			{
				this.friends[arguments[i]].status = 0;
			}
		}
		for (var i in this.friends)
		{
			this.friends[i].UpdateStatus();
		}
	},
	CompleteName: function( p_text){
		var l_text = p_text;
		var l_pos = l_text.lastIndexOf(' ');
		var l_text = l_text.substr( l_pos+1);

		if (l_text.length <= 0){return;}

		for (var i in this.users)
		{
			if (this.users[i].substr(0, l_text.length).toLowerCase() == l_text.toLowerCase())
			{
				return this.users[i].substr(l_text.length);
			}
		}

		return '';
	},
	SetAutoMessages: function(){
		for (var i = 0 ; i < arguments.length; ++ i)
		{
			var l = arguments[i];
			new ChatAutoMessage(l[0],l[1],l[2],l[3]);
		}
	},
	CheckAutoMessages: function(){
		if (this.numMessagesSinceAuto < 25)
		{
			setTimeout( this.CheckAutoMessages.bind(this),60000);
			return;
		}
		var total = 0;
		for (var i in this.autoMessages)
		{
			total += this.autoMessages[i].proba;
		}
		if (total == 0){return;}
		var r=Math.floor(Math.random() * total)
		for (var i in this.autoMessages)
		{
			r -= this.autoMessages[i].proba;
			if(r<0)
			{
				this.autoMessages[i].Activate();
				return;
			}
		}
		
	}
};
var ChatAutoMessage = Class.create({
	initialize: function( p_id, p_text, p_time, p_proba){
		this.id = p_id;
		this.text = p_text;
		this.time = p_time;
		this.proba = p_proba;
		g_chat.autoMessages[this.id] = this;
	},
	Activate: function(){
		g_chat.numMessagesSinceAuto = 0;
		setTimeout( g_chat.CheckAutoMessages.bind(g_chat),this.time*1000);
		g_chat.gen.RTalk('<b>'+this.text+'</b>','Kazulife','#880000');
	}
});
var ChatWindow = Class.create({
	initialize: function( p_id, p_title, p_icon, p_baseDims, p_basePos, p_minDims, p_closeFunc){
		this.id = p_id;
		this.title = p_title;
		this.icon = p_icon || false;
		this.baseDims = p_baseDims;
		this.minDims = p_minDims;
		this.basePos = p_basePos;
		this.closeFunc = p_closeFunc || false;
		this.Create();
	},
	Create: function(){
		var ele = new Element('div',{id:this.id,'class':'SpcCadre'});
		ele.style.background='url(/i/fondKazM.png)';
		ele.setStyle('position:absolute;display:none;line-height:24px;left:'+this.basePos.x+'px;top:'+this.basePos.y+'px;z-index:2400');
		ele.onclick = this.Focus.bind( this);
		ele.insert('<div id="'+this.id+'_In"style="border-top:1px solid black;overflow:auto;width:'+this.baseDims.x+'px;height:'+this.baseDims.y+'px;text-align:left;">');
		$('body').insert(ele);
	
		Windowise( this.id, this.closeFunc, true,(this.icon?'<img src="'+this.icon+'"class="i20m"style="margin-right:12px;">':'')+this.title);
		new Resizable(this.id, this.id+'_In');
	},
	Focus: function(){
		g_chat.UnFocus();
		$(this.id).setStyle({zIndex:2500});
		g_chat.cwFocus = this.id;
	}
});
var Smiley = Class.create({
	initialize: function( p_text, p_img){
		this.txt = p_text;
		this.img = '/i/j/sm/' + p_img;
		this.reg = new RegExp( RegExp.quote(p_text), 'g');
		g_chat.smileys.push( this);
	},
	Load: function(){
		$('SmileyList').insert( '<img src="' + this.img + '" class="h24" onclick="g_chat.AddSmiley(\''+this.txt+'\')"/>');
	}
});
