/* コンストラクタ */
function ChatGlimpse()
{
	this.messageArea = document.getElementById("chat_msg");
	this.roomName = document.getElementById("chat_msg_room");
	this.tbody = this.messageArea.getElementsByTagName("tbody")[0];
	this.sendURL = "glimpse.html";
	
	this.scrollPixel = 100;
	
	this.longReg = new RegExp("([a-z0-9:=_\\-&~/%\?\.]{80})([a-z0-9:=_\\-&~/%\?\.]+)", "gi");
	this.urlReg = new RegExp("https?://[a-z0-9\\-]+(\\.[a-z0-9\\-]+)+(/[a-z0-9=_\\-&~/%\\?\\.]*)?", "i");
	
	this.safari = (navigator.userAgent.indexOf("AppleWebKit") != -1);
	
	this.loading = false;
	this.pushed = null;
}

/* スクロール */
ChatGlimpse.prototype.scrollMessage = function()
{
	var target = this.messageArea;
	var top = target.scrollTop;
	top += this.scrollPixel;
	target.scrollTop = top;
	if (target.scrollTop == top) {
	    setTimeout("ChatGlimpse_scrollMessage()", 10);
	}
}

/* 発言者セル作成 */
ChatGlimpse.prototype.createMessageMember = function(data)
{
	var td = document.createElement("td");
	var div = document.createElement("div");
	var spanTime = document.createElement("span");
	var textName = document.createTextNode(data.nm);
	var textTime = document.createTextNode(data.tm);
	
	td.className = "name";
	td.style.color = data.cl;
	
	div.style.position = "relative";
	spanTime.className = "timestamp";
	spanTime.id = "msg" + data.id;
	spanTime.style.visibility = "hidden";
	spanTime.appendChild(textTime);
	
	div.appendChild(textName);
	if (data.cp) {
		var spanCap = document.createElement("span");
		var textCap = document.createTextNode("#" + data.cp);
		spanCap.className = "cap";
		spanCap.appendChild(textCap);
		div.appendChild(spanCap);
	}
	div.appendChild(spanTime);
	
	td.appendChild(div);
	return td;
}

/* メッセージノード作成 */
ChatGlimpse.prototype.createMessageNodes = function(message)
{
	var nodes = new Array();
	var leftText = message;
	if (!this.safari && this.urlReg.exec(leftText)) {
		leftText = RegExp.leftContext;
		var anchorText = RegExp.lastMatch;
		var rightText = RegExp.rightContext;
		
		var anode = document.createElement("a");
		anode.href = anchorText;
		anode.target = "_blank";
		
		if (anchorText.length > 80) {
			anchorText = anchorText.substring(0, 80) + "...";
		}
		anode.appendChild(document.createTextNode(anchorText));
		
		var rightNodes = this.createMessageNodes(rightText);
		var node;
		nodes.push(anode);
		while (node = rightNodes.shift()) {
			nodes.push(node);
		}
	}
	leftText = leftText.replace(this.longReg, "$1...");
	nodes.unshift(document.createTextNode(leftText));
	return nodes;
}

/* メッセージセル作成 */
ChatGlimpse.prototype.createMessageMessage = function(data)
{
	var td = document.createElement("td");
	var span = document.createElement("span");
	var nodes = this.createMessageNodes(data.m);
	var node;
	
	td.className = "message";
	td.style.color = data.cl;
	
	span.onmouseover = function()
	{
		document.getElementById("msg" + data.id).style.visibility = "visible";
	}
	
	span.onmouseout = function()
	{
		document.getElementById("msg" + data.id).style.visibility = "hidden";
	}
	
	while (node = nodes.shift()) {
		span.appendChild(node);
	}
	
	td.appendChild(span);
	return td;
}

/* メッセージ追加 */
ChatGlimpse.prototype.appendMessage = function(data)
{
	var tr = document.createElement("tr");
	var td1 = this.createMessageMember(data);
	var td2 = this.createMessageMessage(data);
	tr.appendChild(td1);
	tr.appendChild(td2);
	this.tbody.appendChild(tr);
}

/* 複数メッセージ追加 */
ChatGlimpse.prototype.appendMessages = function(messages)
{
	while (message = messages.shift()) {
		this.appendMessage(message);
	}
}

/* リクエスト送信 */
ChatGlimpse.prototype.sendRequest = function(control, room, roomName)
{
	if (this.loading) {
		return false;
	}
	this.pushed = control;
	this.pushed.disabled = true;
	this.loading = true;
	
	this.roomName.style.visibility = "visible";
	this.messageArea.style.visibility = "visible";
	this.messageArea.style.height = "200px";
	this.roomName.innerHTML = roomName + "ルーム";
	
	while (this.tbody.hasChildNodes()) {
		this.tbody.removeChild(this.tbody.lastChild);
	}
	
	var postData = {
		room: room
	};
	
	sendRequest(ChatGlimpse_loaded, postData, "POST", this.sendURL, true, false);
	return true;
}

/* 更新 */
ChatGlimpse.prototype.loaded = function(req)
{
	eval("var response = " + req.responseText);
	var status = response.status;
	
	// 更新
	newMessage = (response.messages.length > 0);
	this.appendMessages(response.messages);
	
	
	if (newMessage) {
		this.scrollMessage();
	}
	this.loading = false;
	this.pushed.disabled = false;
	this.pushed = null;
}

/* リロード */
ChatGlimpse.prototype.reload = function()
{
	this.sendRequest();
	return false;
}


var glimpse;

function ChatGlimpse_loaded(req)
{
	glimpse.loaded(req);
}

function ChatGlimpse_scrollMessage()
{
	glimpse.scrollMessage();
}


