// AQUA Omni

// Set Omni Functions
window.onblur = OmniBlur;
window.onfocus = OmniFocus;

// Variables
var VisitorId = null;
var isFocused = true;
var ChatId = null;

function OmniBlur() {
	if (!isFocused)	return;
	isFocused = false;
	if (!VisitorId) return;
	//document.title = "BLURRED";
	request ('scripts/aqua_omni_setblur.php', 'GET', {id:VisitorId, blurred:"YES"}, null );
}
function OmniFocus() {
	if (isFocused)	return;
	isFocused = true;
	if (!VisitorId) return;
	request ('scripts/aqua_omni_setblur.php', 'GET', {id:VisitorId, blurred:"NO"}, null);
}

function OmniStart() {
	request ('scripts/aqua_omni.php', 'GET', {ref:OmniReferer, id:OmniPage, lang:OmniLanguage}, OmniPageStart);
}

function OmniPageStart(http) {
	VisitorId = http.responseText;
	setTimeout(OmniTimer, 1000);
}

function OmniTimer() {
	request ('scripts/aqua_omni_keepalive.php', 'GET', {id:VisitorId}, OmniPopup);
	//var now = new Date();
	//document.title = now.toString();
	setTimeout(OmniTimer, 1000);
}

function OmniPopup(http) {
	if (http.responseText.length < 10) return;
	document.getElementById('OmniSupportButton').style.display = 'block';
	document.getElementById('OmniSupportWindow').getElementsByTagName('h2')[0].innerHTML  = http.responseText.split('*end*')[0];
	document.getElementById('OmniSupportWindow').getElementsByTagName('div')[0].innerHTML  = http.responseText.split('*end*')[1];
	if (http.responseText.split('*end*')[0] == "Support Chat") {
		ChatId = http.responseText.split('*end*')[2];
		document.getElementById('OmniSupportButton').style.display = 'hidden';
		document.getElementById('OmniSupportWindow').style.display = 'block';
		document.getElementById('OmniSupportYea').style.display="none";
		document.getElementById('OmniSupportNay').value="Close";
		document.getElementById('OmniSupportInp').style.display="block";
		OmniStartChat();
	}
}

function OmniDenyChat() {
	request ('scripts/aqua_omni_deny.php', 'GET', {id:VisitorId}, null);
	document.getElementById('OmniSupportWindow').style.display = 'none';
}

// Chat functions
function OmniAcceptChat() {
	request ('scripts/aqua_omni_accept.php', 'GET', {id:VisitorId}, function (http) { 
		ChatId = http.responseValue;
		document.getElementById('OmniSupportYea').style.display="none";
		document.getElementById('OmniSupportNay').value="Close";
		document.getElementById('OmniSupportInp').style.display="block";
		OmniStartChat(); 
		} );
}
function OmniStartChat() {
	if (!ChatId) return false;
	request ('scripts/aqua_omni_chat.php', 'GET', {id:ChatId}, OmniUpdateChat );
	setTimeout(OmniStartChat, 500);  
}

function OmniUpdateChat(http ) {
	document.getElementById('OmniSupportWindow').getElementsByTagName('h2')[0].innerHTML  = http.responseText.split('*end*')[0];
	document.getElementById('OmniSupportWindow').getElementsByTagName('div')[0].innerHTML  = http.responseText.split('*end*')[1];
}

function OmniAddChat(obj) {
	var txt = '<span style="color:#f96;">&lt;User&gt; </span>'+obj.value;
	request ('scripts/aqua_omni_add.php', 'GET', {id:ChatId, text:txt}, null );
	obj.value = "";
} 

// External functions

function request(url, method, data, callback) {
	var http = new XMLHttpRequest;
	if (!http)
		return false;
	var _data;
	if (data != null && typeof data == "object") {
		_data = [];
		for (var i in data)
			_data.push(i + "=" + encodeURIComponent(data[i]));
		_data = _data.join("&");
	} else {
		_data = data;
	}
	method = method.toUpperCase();
	if (method == "POST") {
		http.open(method, url, true);
		http.setRequestHeader("Method", "POST "+url+" HTTP/1.1");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	} else {
		if (_data)
			url += (url.indexOf("?") == -1 ? "?" : "&") + _data;
		_data = "";
		http.open(method, url, true);
	}
	if (callback)
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				http.onreadystatechange = function(){};
				callback(http);
			}
		};
	http.send(_data);
	return http;
}
