var ping_request = false;
var responsed = false;
var last_ping = false;
var chat_timer = false;
var windows = new Array();

function chatWnd(id,focus)
{
	this.id = id;
	this.wnd = window.open("http://x/sol-o.ru/operdialog/" + id + ".chat","_blank","width=318,height=250,toolbar=no");
	if (focus) this.wnd.focus();
}

function send_ping()
{
	if (ping_request) clearTimeout(ping_request);
	if (responsed) response();
	else no_response();
	window.frames.ping.location.href = "http://x/sol-o.ru/oper_ping/" + last_ping + ".chat?rnd=" + Math.random(0,9999);
	responsed = false;
	ping_request = setTimeout(send_ping,document.getElementById("delay").value);
}

function receive_ping(stamp)
{
	response();
	responsed = true;
	last_ping = stamp;
	var temp = document.getElementById("hist").options;
	while (temp.length) temp.remove(0);
	temp = document.getElementById("conts");
	var back = document.getElementById("backup");
	for (x = back.rows.length - 1; x >= 0; x--) back.deleteRow(x);
	for(x = temp.rows.length - 1; x > 0; x--)
	{
		row = temp.rows[x].cloneNode(true);
		back.appendChild(row);
		temp.deleteRow(x);
	}
}

function response()
{
	document.getElementById("connected").className = "note";
	document.getElementById("not_connected").className = "hide";
}

function no_response()
{
	document.getElementById("connected").className = "hide";
	document.getElementById("not_connected").className = "note";
}

function addVisitor(id,time,title,away,referer)
{
	var temp = document.createElement("OPTION");
	document.getElementById("hist").options.add(temp);
	document.getElementById("hib").disabled = false;
	temp.value = id;
	temp.innerHTML = title;
	temp = "";
	for (x = 0; x < windows.length; x++) if (windows[x].id == id)
	{
		try { temp = windows[x].wnd.length; } catch(e) {}
		if (temp > 0)
		{
			temp = " but";
			break;
		}
		else temp = "";
	}
	var x = document.getElementById("c" + id);
	var vd = "2d";
	if (x)
	{
		if (x.parentNode.parentNode.parentNode.className == "hide") vd = "hide";
		if (x.style.color == "#ffffff") x = "\" style=\"color:#FFFFFF;background-color:#E10915;";
		else x = "";
	}
	else x = "";
	var row = document.getElementById("conts").insertRow();
	row.id = "v" + id;
	row.className = vd;
	var cell = row.insertCell();
	cell.innerHTML = "<div class=\"" + vd + "\"><input type=\"button\" id=\"c" + id + "\" title=\"Зашел в " + time + (away ? "; оффлайн" : "") + "\" value=\"" + title + "\" class=\"bug" + temp + x + "\" onClick='gotoVisitor(" + id + ");'" + (away ? " disabled" : "") + " /></div>";
	cell = row.insertCell();
	cell.innerHTML = "<div class=\"" + vd + "\">" + time + "</div>";
	cell = row.insertCell();
	cell.innerHTML = "<div class=\"" + vd + "\" align=\"center\">" + (referer ? "<a href=\"" + referer + "\" target='_blank'>есть</a>" : "нет") + "</div>";
	cell = row.insertCell();
	cell.innerHTML = "<div class=\"" + vd + "\" align=\"center\"><input type=\"button\" value=\"X\" title=\"Зашел в " + time + "\" class=\"but\" onClick='offVisitor(" + id + ")' /></div>";
}

function addCommonVisitor(id,time,title,referer)
{
	var temp = document.createElement("OPTION");
	document.getElementById("hist").options.add(temp);
	document.getElementById("hib").disabled = false;
	temp.value = id;
	temp.innerHTML = title;
	temp.className = "gr";
	temp = "";
	for (x = 0; x < windows.length; x++) if (windows[x].id == id)
	{
		try { temp = windows[x].wnd.length; } catch(e) {}
		if (temp > 0)
		{
			temp = " but";
			break;
		}
		else temp = "";
	}
	var x = document.getElementById("c" + id);
	var vd = "2d";
	if (x) if (x.parentNode.parentNode.parentNode.className == "hide") vd = "hide";
	var row = document.getElementById("conts").insertRow();
	row.id = "v" + id;
	row.className = vd;
	var cell = row.insertCell();
	cell.innerHTML = "<div class=\"" + vd + "\"><input type=\"button\" id=\"c" + id + "\" title=\"Зашел в " + time + "\" value=\"" + title + "\" class=\"bug gr" + temp + "\" onClick='gotoVisitor(" + id + ");' /></div>";
	cell = row.insertCell();
	cell.innerHTML = "<div class=\"" + vd + "\">" + time + "</div>";
	cell = row.insertCell();
	cell.innerHTML = "<div class=\"" + vd + "\" align=\"center\">test</div>";
	cell = row.insertCell();
	cell.innerHTML = "<div class=\"" + vd + "\" align=\"center\"><input type=\"button\" value=\"X\" title=\"Зашел в " + time + "\" class=\"but\" onClick='offVisitor(" + id + ")' /></div>";
}

function offVisitor(id)
{
	var temp = document.getElementById("v" + id);
	if (temp) temp.className = "hide";
	temp = false;
	for (x = 0; x < windows.length; x++) if (windows[x].id == id)
	{
		try { windows[x].wnd.close(); } catch(e) {}
	}
}

function gotoVisitor(id)
{
	temp = document.getElementById("c" + id);
	temp.style.color = "";
	temp.style.backgroundColor = "";
	var temp = false;
	for (x = 0; x < windows.length; x++) if (windows[x].id == id)
	{
		try { temp = windows[x].wnd.focus(); } catch(e) {}
		if (temp > 0) return true;
	}
	windows[windows.length] = new chatWnd(id,true);
}

function addMessage(stamp,id,dat,text)
{
	for(x = 0; x < windows.length; x++) if (windows[x].id == id)
	{
		try { temp = windows[x].wnd.length } catch(e) {}
		if (temp > 0)
		{
			if (window.GetAttention) windows[x].wnd.GetAttention();
			temp = document.getElementById("c" + id);
			temp.style.color = "#FFFFFF";
			temp.style.backgroundColor = "#E10915";
			windows[x].wnd.addMes(stamp,id,dat,text);
			return true;
		}
	}
	temp = document.getElementById("v" + id);
	if (temp)
	{
		temp.className = "2d";
		temp = document.getElementById("c" + id);
		temp.style.color = "#FFFFFF";
		temp.style.backgroundColor = "#E10915";
	}
	windows[windows.length] = new chatWnd(id,!windows.length);
	addMesDelayed(windows.length - 1,stamp,id,dat,text);
}

function addMes(stamp,id,dat,text)
{
	var mes = window.frames.chat.document.createElement("DIV");
	mes.id = stamp;
	mes.className = (id ? "cmes" : "omes");
	mes.innerHTML = "<a name=\"" + stamp + "\" /><span class=\"time\">[" + dat + "]</span>" + text;
	var temp = window.frames.chat.document.getElementById("root");
	for (x = 0; x < temp.childNodes.length; x++) if (temp.childNodes[x].id > stamp)
	{
		temp.insertBefore(mes,temp.childNodes[x]);
		return true;
	}
	temp.appendChild(mes);
	window.frames.chat.location.href = "http://x/sol-o.ru/empty.chat#" + stamp;
	window.focus();
	return true;
}

function addMesDelayed(wnd,stamp,id,dat,text)
{
	var temp = false;
	try { temp =  windows[wnd].wnd.addMes(stamp,id,dat,text); } catch(e) {}
	if (temp != true) setTimeout("addMesDelayed(" + wnd + "," + stamp + "," + id + ",\"" + dat + "\",\"" + text + "\");",400);
}

function flush()
{
	var temp = false;
	for (x = 0; x < windows.length; x++)
	{
		try { windows[x].wnd.close(); } catch(e) {}
	}
}

function word_sent(stamp,time)
{
	addMes(stamp,0,time,document.getElementById("word").value);
	var temp = document.getElementById("word");
	temp.value = "";
	temp.readonly = false;
	document.getElementById("subm").disabled = false;
	document.getElementById("word").focus();
}

function word_notsent()
{
	document.getElementById("word").readonly = false;
	document.getElementById("subm").disabled = false;
	if (chat_timer)
	{
		clearTimeout(chat_timer);
		chat_timer = false;
	}
	document.getElementById("word").focus();
}

function word_process()
{
	if (!document.getElementById("word").value)
	{
		event.returnValue = false;
		return false;
	}
	if (chat_timer) clearTimeout(chat_timer);
	chat_timer = setTimeout(word_notsent,12000);
}

function openHistory()
{
	var temp = document.getElementById("hist");
	if (temp.value) window.open("http://x/sol-o.ru/chathistory/" + temp.value + ".chat","hist" + temp.value,"width=318,height=430,toolbar=no,resizable=yes,scrollbars=yes").focus();
}

function dialogKey(event)
{
	if (event.keyCode == 27) window.close();
}

function processLogout()
{
	window.frames.ping.location.href = "http://x/sol-o.ru/oper_logout.chat?rnd=" + Math.random(0,9999);
	chat_timer = window.setTimeout(afterLogout,12000);
}

function afterLogout(success)
{
	if (ping_request) window.clearTimeout(ping_request);
	if (chat_timer) window.clearTimeout(chat_timer);
	no_response();
	if (success != true) alert("Выход выполнен некорректно");
	parent.window.close();
}
