var relax_time = 3000;
var reactiontime = 500;
var in_chat = 'N';
var last_message_id = 0;
var debug_out = false;

chat_script = root_folder+'chat/chat.php';

function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function debug_eval(msg)
{
	var debug_div = document.getElementById('debug_div');
	eval(msg);
	
	if (debug_out!=null&&debug_div!=null&&debug_out== true) debug_div.innerHTML += msg;
	
	debug_out = false;
}

var TChatSystem = function (self,login,gender)
{
	this.self = self;
	this.login = login;
	this.gender = gender;
	this.chating = new Array();
	
	setTimeout(this.self+".refresh_state()",relax_time);
}

TChatSystem.prototype.refresh_state = function ()
{
	var rnd = Math.random();
	send_data(chat_script+'?last_message_id='+last_message_id+'&lng='+lang+'&rnd='+rnd,eval);
}

TChatSystem.prototype.refresh_user_on_site = function (users)
{
	var uosd = document.getElementById('users_on_site_div_out');
	if (uosd != null)
	{
		line = "<table>";
		for (var i=0;i<users.length;i++)
		{
			line += "<tr>";
			line += "<td class=ChatTabs_message_user><a href=\""+users[i].url+"\" target=\"_blank\" onmouseover=\"chatmouseover('"+users[i].login+"','"+users[i].user_photo+"');\" onmouseout=\"chatmouseout('"+users[i].login+"');\">"+users[i].login+"</a></td>";
			line += '<td width="10"></td>';
			line += "<td><a href=\"javascript:"+this.self+".user_call('"+users[i].login+"');\">общаться</a></td>";
			line += "<tr>";
		}
		line += "</table>";
		
		uosd.innerHTML = line;
	}
}

TChatSystem.prototype.user_call = function(oposite_login)
{
	var rnd = Math.random();
	send_data(chat_script+'?action=call&oposite_login='+oposite_login+'&lng='+lang+'&rnd='+rnd,alert);
}

TChatSystem.prototype.refresh_incoming_calls = function (users)
{
	var calls_div = document.getElementById('users_calls_div_out');
	if (calls_div!=null)
	{
		if (this.gender == 1) var uid = 48;
		else var uid = 42;
		
		var txt = '<table>';
		for (var i=0;i<users.length;i++)
		{
			txt += '<tr>';
			txt += '<td><a href="index.php?id='+users[i].id+'&uid='+uid+'" target="_blank">'+users[i].login+'</a></td>';
			txt += '<td><a href="javascript:'+this.self+'.accept_call('+"'"+users[i].login+"'"+');">принять</a></td>';
			txt += '<td><a href="javascript:'+this.self+'.deni_call('+"'"+users[i].login+"'"+');">отклонить</a></td>';
			txt += '</tr>';
		}
		txt += '<table>';
		calls_div.innerHTML = txt;
	}
}

TChatSystem.prototype.accept_call = function (accept_login)
{
	var rnd = Math.random();
	send_data(chat_script+'?action=accept_call&oposite_login='+accept_login+'&lng='+lang+'&rnd='+rnd,eval);
}

TChatSystem.prototype.deni_call = function (deni_login)
{
	var rnd = Math.random();
	send_data(chat_script+'?action=deni_call&oposite_login='+deni_login+'&lng='+lang+'&rnd='+rnd,alert);
}

TChatSystem.prototype.out_denied_calls = function (users)
{
	if (users.length>0)
	{
		var txt = 'Ваше приглашение было отклонено пользователями: ';
		txt += users.join(', ')+'.';
		alert(txt);
	}
}

TChatSystem.prototype.out_incoming_calls = function()
{
	var calls_div = document.getElementById('users_calls_div_out');
	if (calls_div == null) alert('Вас приглашают в чат.');
}

TChatSystem.prototype.add_user_to_dialog = function(oposite_login)
{
	if (this.chating[oposite_login] != null && this.chating[oposite_login] != "undefined") var ext = true;
	else var ext = false;
	
	if (ext == false && in_chat=='Y')
	{
		var buttons = new Array();
		var img = '<img id="webc_'+oposite_login+'" src="chat/video.gif" border="0" align="absmiddle" alt="Video">';
		buttons.push({action:"javascript:videochange('"+oposite_login+"');",caption: img,title:"Video"});
		
		var rsheet = ChatTabs.add_sheet(oposite_login,buttons);
		this.chating[oposite_login] = rsheet;
		
		var chatdiv = document.getElementById('ChatTabs_work'+rsheet+'_div');
		if (chatdiv!= null)
		{
			chatdiv.innerHTML = '';
			var messages_div = document.createElement("div");
			messages_div.className = "Chat_messages_div";
			messages_div.id = 'ChatTabs_messages_'+rsheet+'_div';
			chatdiv.appendChild(messages_div);
			
			var messages_input_div = document.createElement("div");
			messages_input_div.className = "Chat_messages_input_div";
			messages_input_div.innerHTML = '<input type="text" class="Chat_message_text" id="message_'+rsheet+'" value="">&nbsp;&nbsp;&nbsp;<input type="submit" value="Отправить" class="Chat_message_button" onclick="'+this.self+'.send_message(\''+rsheet+'\')">';
//			messages_input_div.innerHTML = '<textarea class="Chat_message_text" id="message_'+rsheet+'" value=""></textarea>&nbsp;&nbsp;&nbsp;<input type="submit" value="Отправить" class="Chat_message_button" onclick="'+this.self+'.send_message(\''+rsheet+'\')">';
			
			chatdiv.appendChild(messages_input_div);
		}
	}
}

TChatSystem.prototype.send_message = function (i)
{
	var pp = document.getElementById('message_'+i);
	if (pp!=null)
	{
		var text = pp.value;
		if (text != "")
		{
			var oposite_login = ChatTabs.sheets[i];//this.active_sheet
			var rnd = Math.random();
			send_post_data(chat_script+'?action=send_message&oposite_login='+oposite_login+'&nomber='+i+'&lng='+lang+'&rnd='+rnd,'message='+text,eval,true);
		}
	}
}

TChatSystem.prototype.add_to_addressbook = function (oposite_login)
{
	alert(oposite_login+" добавлен в адресную книгу.");
}

TChatSystem.prototype.messages_draw = function (messages)
{
	if (in_chat == 'Y')
	{
		for (var i=0;i<messages.length;i++)
		{
			if (messages[i][0]==this.login)
			{
				var class_name_h = "ChatTabs_self_head";
				var class_name_m = "ChatTabs_self_message";
				var	oposite_login = messages[i][1];
			}
			else
			{
				var class_name_h = "ChatTabs_opposite_head";
				var class_name_m = "ChatTabs_opposite_message";
				var	oposite_login = messages[i][0];
			}
			
			if (this.chating[oposite_login] != null && this.chating[oposite_login] != "undefined")
			{
				var rsheet = this.chating[oposite_login];
				messages_div = document.getElementById('ChatTabs_messages_'+rsheet+'_div');
				if (messages_div != null)
				{
					var newDiv = document.createElement("div");
					newDiv.className = class_name_h;
					newDiv.innerHTML = messages[i][0] + ' ('+messages[i][2]+')';
					messages_div.appendChild(newDiv);
					var newDiv = document.createElement("div");
					newDiv.className = class_name_m;
					newDiv.innerHTML = messages[i][3];
					messages_div.appendChild(newDiv);
					var ppp = document.getElementById('scrolling');
					if (ppp!=null && ppp.checked == true) messages_div.scrollTop += 1500;
				}
			}
			
		}
	}
}

video_login = '';
loaded_video = true;
in_chat = 'Y';

function LoadImage()
{
	if (video_login != '')
	{
		uniq1 = Math.random();
		loaded_video = false;
		var picture = document.getElementById('video_pic');
		picture.src = "chat/get_picture.php?need_login="+video_login+"&uniq="+uniq1;
	}
}

function videochange(oposite_login)
{
	if (video_login != oposite_login)
	{
		if (video_login!='')
		{
			var img = document.getElementById('webc_'+video_login);
			img.src = 'chat/video.gif';
		}
		
		var img = document.getElementById('webc_'+oposite_login);
		img.src = 'chat/video_a.gif';
		video_login = oposite_login;
		LoadImage();
	}
	else
	{
		var img = document.getElementById('webc_'+oposite_login);
		img.src = 'chat/video.gif';
		video_login = '';
		loaded_video = true;
		var picture = document.getElementById('video_pic');
		picture.src = "chat/null.jpg";
	}
}

function controll_video()
{
	if (video_login != '' && loaded_video == false) LoadImage();
	uniq_last = uniq_new;
}
var previews = new Array();

function chatmouseover(login,file)
{
	previews[login] = 1;
	setTimeout("Chat_system.chatloadpreview('"+login+"','"+file+"');",reactiontime);
	
	posX = event.clientX+getBodyScrollLeft()-100;
	posY = event.clientY+getBodyScrollTop()-10;
}

function chatmouseout(login)
{
	if (previews[login]==1)	previews[login] = 0;
	var div = document.getElementById('previewchat');
	div.style.display = 'none';
}

TChatSystem.prototype.chatloadpreview = function (login,file)
{
	if (previews[login]==null) return false;
	
	if (previews[login]==1)
	{
		var div = document.getElementById('previewchat');
		var picture = document.getElementById('previewchatimg');
		picture.style.display = 'none';
		if (this.gender==2) var path = 'photos/';
		else var path = 'photos_male/pre_';
		picture.src = '/pictures/'+path+file;
		
		div.style.left = posX;
		div.style.top = posY;
		div.style.display = '';
	}
}