var con = undefined;
var xmAPP = 'office';
var xmHTB = '/xmpp-httpbind';
var xmCOO = 'xmext';

//$(document).ready(function () {
	//$(window).bind('beforeunload', function () { cSuspend(); });
	//cConnect();
//});


function cSuppOnline(b) {
	if (b) {
		$('#off-support').show();
	}
	else {
		$('#off-support').hide();
		$('#chatform input').attr('disabled', 'disabled');
	}
}
function cShowChat(b) {
	$('#person').hide();
	$('#personsml').hide();
	if (b) {
		$('#chatform input').removeAttr('disabled');
		if ($('#chatform').is(':visible')) {
			return;
		}
		$('#chatformwrap').show();
		$('#msgtype').focus();
	}
	else {
		$('#chatform input').attr('disabled', 'disabled');
		$('#chatformwrap').hide();
	}
	return false;
}
function cSendMsg() {
	var s = $.trim($('#msgtype').val());
	if (s.length < 1) {
		return false;
	}
	var m = new JSJaCMessage();
	m.setTo(xmCHT + '@' + xmJBD);
	m.setBody(s);
	con.send(m);

	cPrintMsg(s, false);
	$('#msgtype').val('');
	$('#msgtype').focus();

	return false;
}
function cPrintMsg(s, b) {
	var o = document.createElement('div');
	$(o).css('margin', '4px 5px');
	$('#msglist').append(o);
	if (b) {
		$(o).html(s);
	}
	else {
		$(o).text(s);
	}
	o.scrollIntoView();
}



// xmpp related
function cConnect() {
	var r = false;
	try { // restore existing session
		if (JSJaCCookie.read(xmCOO).getValue() == 'binding') {
			con = new JSJaCHttpBindingConnection({});
			cSetup();
			if (con.resume()) {
				r = true;
				cHello();
			}
		}
	}
	catch (e) {}

	if (!r) { // or start new one
		try {
			con = new JSJaCHttpBindingConnection({'httpbase':xmHTB, 'timerval':2000});
			cSetup();
			con.connect({'domain':xmJBD, authtype: 'saslanon', 'resource':xmAPP});
		}
		catch (e) {alert(e);}
	}
}
function cSetup() {
	con.registerHandler('onconnect', hConnect);
	con.registerHandler('ondisconnect', hDisconnect);
	con.registerHandler('onerror', hError);

	con.registerHandler('presence', hPresence);
	con.registerHandler('message', hMessage);

	con.registerHandler('iq', hIQ);
	con.registerIQGet('query', NS_VERSION, hIqVersion);
	con.registerIQGet('query', NS_TIME, hIqTime);
}
function cSuspend() {
	if (typeof(con) == 'undefined' || !con || !con.connected()) {
		return;
	}
	cBye();
	if (con._hold) {
		(new JSJaCCookie(xmCOO, 'binding')).write();
		if (con.suspend) {
			con.suspend();
		}
	}
}

function cHello() {
	var p = new JSJaCPresence();
	p.setTo(new JSJaCJID(xmCHT + '@' + xmJBD));
	p.setStatus(cdIP + "\n" + cdCNT + "\n" + window.location.href + "\n" + document.title);
	con.send(p);
}
function cBye() {
	var p = new JSJaCPresence();
	p.setTo(new JSJaCJID(xmCHT + '@' + xmJBD));
	p.setStatus('bye');
	con.send(p);
}
function hMessage(p) {
	cShowChat(true);
	cPrintMsg('<strong>support:</strong> ' + p.getBody(), true);
}
function hPresence(p) {
	if (!p.getType() && !p.getShow()) {
		var j = p.getFromJID();
		if (j.getNode() != xmCHT) {
			return;
		}
		if (p.getStatus() == 'welcome') {
			cSuppOnline(true);
		}
		else if (p.getStatus() == 'bye') {
			cSuppOnline(false);
		}
	}
}

function hConnect() {
	con.send(new JSJaCPresence());
	cHello();
}
function hDisconnect() {
	cSuppOnline(false);
}
function hError(e) {
	if (con.connected()) {
		con.disconnect();
	}
	cSuppOnline(false);
}
function hIQ(aIQ) {
	con.send(aIQ.errorReply(ERR_FEATURE_NOT_IMPLEMENTED));
}
function hIqVersion(iq) {
  con.send(iq.reply([
					 iq.buildNode('name', 'jsjac ' + xmAPP),
					 iq.buildNode('version', JSJaC.Version),
					 iq.buildNode('os', navigator.userAgent)
					 ]));
  return true;
}
function hIqTime(iq) {
  var now = new Date();
  con.send(iq.reply([iq.buildNode('display', now.toLocaleString()),
					 iq.buildNode('utc', now.jabberDate()),
					 iq.buildNode('tz', now.toLocaleString().substring(now.toLocaleString().lastIndexOf(' ')+1))
					 ]));
  return true;
}

