var webchatObj = new Object();
webchatObj.login = false;
webchatObj.ajax = true;
webchatObj.num = 0;
webchatObj.checkHN = null;
webchatObj.num_path = "commons/webchat/webchat.num.txt";
webchatObj.cgi_path = "commons/webchat/webchat.cgi";
webchatObj.se = false;
webchatObj.se_flag = true;
var _GET = new Array();
var _POST = new Array();
var GET_eq = '_%%eq%%_';
var GET_amp = '_%%amp%%_';

try {
	if(document.getElementsByTagName('BODY').length == 0)
		document.write('<body>');
	var bodyObj = document.getElementsByTagName("body").item(0);
	var audioObj = document.createElement('audio');
	if(navigator.userAgent.indexOf("Safari") > -1 && navigator.userAgent.indexOf("Chrome") == -1)
		audioObj.src = "images/chat.wav";
	else
		audioObj.src = "images/chat.ogg";
	
	audioObj.id = 'webchat_audio';
	audioObj.autobuffer = true;
	bodyObj.appendChild(audioObj);
	document.getElementById('webchat_audio').load();
	webchatObj.se = true;
}
catch (e) {
	webchatObj.se = false;
}
function WCHAT_INIT(){
	var getCookies = new Object();
	if(document.cookie.indexOf("WebChatLoginUserName") > -1){
		var cookies = new Array();
		cookies = document.cookie.split(';');
		for(i=0;i<cookies.length;i++){
			var ses = new Array();
			var vals = new Array();
			ses = cookies[i].split('=');
			vals = ses[0].split(' ');
			ses[0] = vals.join('');
			getCookies[ses[0]] = ses[1];
		}
		if(getCookies['WebChatLoginUserName']){
			webchatObj.login_username = unescape(getCookies['WebChatLoginUserName']);
			webchatObj.login = true;
			WCHAT_DISP('webchat_message');
			WCHAT_DISP('webchat_timeline');
			WCHAT_HIDE('webchat_login');
			//webchatObj.checkHN = setInterval("WCHAT_CHECK()",1000);
			var hash = new Date();
			var time = hash.getTime();
			wchatCheckObj = createXMLHttpRequest();
			wchatCheckObj.onreadystatechange = WCHAT_CHECK_GET_INIT;
			wchatCheckObj.open("GET",webchatObj.num_path+'?'+time,true);
			wchatCheckObj.send();
		}
	}
}
function WCHAT_WATCHER_CHECK_GET_INIT(){
	if(wchatCheckObj.readyState == 4 && wchatCheckObj.status == 200){
		webchatObj.num = wchatCheckObj.responseText;
		webchatObj.init = wchatCheckObj.responseText;
		webchatObj.checkHN = setInterval("WCHAT_CHECK()",1000);
	}
}

try{
	window.addEventListener("load",function(){WCHAT_INIT();},false);
}
catch(e){
	window.attachEvent("onload",function(){WCHAT_INIT();});
}


function WCHAT_LOGIN(obj) {
	var d = window.document;
	if(d.getElementById('webchat_login_username').value != "" && d.getElementById('webchat_login_username').value != d.getElementById('webchat_login_username').defaultValue){
		webchatObj.login = true;
		webchatObj.login_username = WCHAT_ESCAPE(d.getElementById('webchat_login_username').value);
		_POST["message"] = webchatObj.login_username + " さんがログインしました。";
		document.cookie = "WebChatLoginUserName=" + escape(webchatObj.login_username) + "; path=/;";
		XMLhttp_post();
		WCHAT_DISP('webchat_message');
		WCHAT_DISP('webchat_timeline');
		WCHAT_HIDE('webchat_login');
		webchatObj.checkHN = setInterval("WCHAT_CHECK()",1000);
	}
	else {
		d.getElementById('webchat_error').innerHTML = "LOGIN ERROR";
	}
	return false;
}
function WCHAT_MESSAGE(obj){
	var d = window.document;
	if(d.getElementById('webchat_message_text').value != ""){
		_POST["message"] = webchatObj.login_username + " &gt; " + d.getElementById('webchat_message_text').value;
		d.getElementById('webchat_message_text').value = "";
		XMLhttp_post();
	}
	return false;
}
function WCHAT_ADD_TIMELINE(msg){
	var d = window.document;
	d.getElementById('webchat_timeline').innerHTML = "<div class=\"webchat_message\">" + msg + "</div>" + d.getElementById('webchat_timeline').innerHTML;
	try{
		document.getElementById('webchat_message_text').focus();
	}
	catch(e){}
	showNotification(msg);
}
function showNotification(msg){
	msg = msg.replace(/<.*?>/gi,"");
	msg = msg.replace(/&gt;/gi,">");
	msg = msg.replace(/&lt;/gi,"<");
	try{
		if (window.webkitNotifications.checkPermission() > 0)
			WCHAT_RequestPermission(showNotification(msg));
		else
			window.webkitNotifications.createNotification("http://weblogs.trancedive.com/images/logo-touch-icon.png", "weblogs.trancedive.com",msg).show();
	}
	catch(e){
		
	}
}
function WCHAT_DISP(obj){
	var d = window.document;
	d.getElementById(obj).style.display = "block";
}
function WCHAT_HIDE(obj){
	var d = window.document;
	d.getElementById(obj).style.display = "none";
}
function WCHAT_ESCAPE(str){
	var befor = new Array("<",">","\n","\t","\\n");
	var after = new Array("&lt;","&gt;","<br />"," ","<br />");
	for(ei=0;ei<befor.length;ei++){
		var temp = new Array();
		temp = str.split(befor[ei]);
		str = temp.join(after[ei]);
	}
	return str;
}

function WCHAT_FOCUS(obj){
	if(obj.value == obj.defaultValue){
		obj.value = "";
		obj.style.color = "#009900";
	}
}
function WCHAT_BLUR(obj){
	if(obj.value == ""){
		obj.value = obj.defaultValue;
		obj.style.color = "#003300";
	}
}

function WCHAT_CHECK(){
	if(webchatObj.se){
		if(document.getElementById('webchat_audio').ended && (!webchatObj.se_flag)){
			var hash = new Date();
			if(navigator.userAgent.indexOf("Safari") > -1 && navigator.userAgent.indexOf("Chrome") == -1)
				document.getElementById('webchat_audio').src = "images/chat.wav?" + parseInt(hash.getTime() / 10000);
			else
				document.getElementById('webchat_audio').src = "images/chat.ogg?" + parseInt(hash.getTime() / 10000);
			document.getElementById('webchat_audio').autobuffer = true;
			document.getElementById('webchat_audio').load();
			webchatObj.se_flag = true;
		}
	}
	var hash = new Date();
	var time = hash.getTime();
	wchatCheckObj = createXMLHttpRequest();
	wchatCheckObj.onreadystatechange = WCHAT_CHECK_GET;
	wchatCheckObj.open("GET",webchatObj.num_path+'?'+time,true);
	wchatCheckObj.send();
}
function WCHAT_CHECK_GET(){
	if(wchatCheckObj.readyState == 4 && wchatCheckObj.status == 200){
		if(wchatCheckObj.responseText != webchatObj.num){
			_POST["get"] = webchatObj.num;
			XMLhttp_post();
			if(!sbt_webchat_toggle_flag){
				sbt_webchat_toggle();
				if(webchatObj.se){
					document.getElementById('webchat_audio').play();
					webchatObj.se_flag = false;
				}
			}
		}
	}
}
function WCHAT_RequestPermission(callback){
	window.webkitNotifications.requestPermission(callback);
}
function XMLhttp_post(){
	if(webchatObj.ajax){
		webchatObj.ajax = false;
		XMLhttpObj = createXMLHttpRequest();
		XMLhttpObj.onreadystatechange = XMLhttp_get;
		XMLhttpObj.open("POST",webchatObj.cgi_path,true);
		XMLhttpObj.send(XMLhttpPostQuerys());
	}
}
function XMLhttp_get(){
	if(XMLhttpObj.readyState == 4 && XMLhttpObj.status == 200){
		XMLhttpGetBuffer(XMLhttpObj.responseText);
		webchatObj.ajax = true;
		try{
			document.getElementById('webchat_message_text').focus();
		}
		catch(e){
			
		}
	}
}
function XMLhttpPostQuerys(){
	var postQuerys = new Array;
	for (i in _POST) postQuerys.push(i+GET_eq+_POST[i]);
	return postQuerys.join(GET_amp);
}
function XMLhttpGetBuffer(buffer){
	var d = window.document;
	_GET = new Array();
	XMLhttp_buffer = new Array();
	XMLhttp_buffer = buffer.split(GET_amp);
	for(i=0;i < XMLhttp_buffer.length;i++){
		var g = new Array();
		g = XMLhttp_buffer[i].split(GET_eq);
		_GET[g[0]] = g[1];
	}
	if(_GET['num'] != undefined)
		webchatObj.num = _GET['num'];
	if(_GET['message'] != undefined)
		WCHAT_ADD_TIMELINE(_GET['message']);
	_POST = new Object();
}

function createXMLHttp() {
	try {
		return new ActiveXObject ("Microsoft.XMLHTTP");
	}catch(e){
		try {
			return new XMLHttpRequest();
		}catch(e) {
			return null;
		}
	}
	return null;
}
function createXMLHttpRequest(){
	var XMLhttpObject = null;
	try{
		XMLhttpObject = new XMLHttpRequest();
	}
	catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				return null;
			}
		}
	}
	return XMLhttpObject;
}

// watch
webchatObj.init = null;
function WCHAT_WATCHER_CHECK(){
	var hash = new Date();
	var time = hash.getTime();
	wchatCheckObj = createXMLHttpRequest();
	wchatCheckObj.onreadystatechange = WCHAT_WATCHER_CHECK_GET;
	wchatCheckObj.open("GET",webchatObj.num_path+'?'+time,true);
	wchatCheckObj.send();
}
function WCHAT_WATCHER_CHECK_GET(){
	if(wchatCheckObj.readyState == 4 && wchatCheckObj.status == 200){
		if(webchatObj.init == null || webchatObj.init == wchatCheckObj.responseText){
			webchatObj.init = wchatCheckObj.responseText;
			setTimeout("WCHAT_WATCHER_CHECK()",1000);
		}
		else if(webchatObj.init != wchatCheckObj.responseText){
			webchatObj.num = webchatObj.init;
			_POST["get"] = webchatObj.num;
			XMLhttp_post();
			webchatObj.checkHN = setInterval("WCHAT_CHECK()",1000);
			WCHAT_DISP('webchat_timeline');
			if(!sbt_webchat_toggle_flag)
				sbt_webchat_toggle();
			if(webchatObj.se){
				document.getElementById('webchat_audio').play();
				webchatObj.se_flag = false;
			}
		}
	}
}
WCHAT_WATCHER_CHECK();

