

Chat = Class.create();
Object.extend(Chat.prototype, {
	initialize:				function (contid) {
		this.messages = new Array();
		this.last_id = null;
		this.requesting = false;
		this.contid = contid;
		this.scrollid = contid;
		this.scrollHeight = null;
		this.container = null;
		this.max_lines = 100;
		this.fail_count = 0;
		this.alias = null;
	},

	start:					function () {
		this.container = document.getElementById(this.contid);
		this.scroll    = document.getElementById(this.scrollid);
		this.scrollHeight = this.scroll.scrollHeight;
		this.getLastId();
	},

	getLastId:				function () {
		if (this.last_id != null) return;
		var me = this;
		var ajax = new Chat.API('last_id', function (data) { me._handle_last_id(data) } );
//		ajax.debug = true;
		ajax.post(); // POST macht einen Request auf die chat/eingine.pl
	},
	
	_handle_last_id:		function (data) {
		if (data && data.success) {
//			document.getElementById(this.buttonid + '_loading').style.display = 'none';
//			document.getElementById(this.buttonid + '_ready').style.display = 'block';
			if (data.not_logged_in) {
				this.system_message("Bitte logge dich erst mal ein.");
			} else {
				this.last_id = data.last_id;
				this.alias   = data.alias;
				this.request();
			}
		} else {
			var me = this;
			setTimeout(function () {me.getLastId()}, 1000);
		}
	},

	delayedRequest:			function (id) {
		var me = this;
		setTimeout(function () { me.request(id) }, 2000);
	},
	
	request:				function () {
		if (this.last_id == null || this.requesting) return;
		var me = this;
		var id = this.last_id + 1;
		var ajax = new Chat.API(id, function (data, status) { me._handle_request(data, status) } );
//		ajax.debug = true;
		ajax.get(); // GET macht einen Request auf die chat/messages/XXX.json
		this.requesting = true;
	},
	
	_handle_request:		function (data, status) {
		this.requesting = false;
		if (!data || !data.id) {
			if (status != 404) this.fail_count++;
			if (this.fail_count > 5) {
				this.last_id++;
				this.fail_count = 0;
			}
			this.delayedRequest();
		} else {
			this.fail_count = 0;
			this.last_id = data.id;
			this.update_messages(data);
			this.request()
		}
	},
	
	submit:					function (message) {
		if (this.last_id == null) return 0;
		var me = this;
		var ajax = new Chat.API('submit', function (data) { me._handle_submit(data) } );
//		ajax.debug = true;
		ajax.post({ "message": message }); // POST macht einen Request auf die chat/engine.pl
		return 1;
	},
	
	_handle_submit:			function (data) {
//		for (var x in data) 	this.system_message(x + ": "+data[x]);
		if (data && data.alias != null) {
			this.alias = data.alias;
		}
		if (data && data.sys_message) {
			this.update_messages(data);
		}
		if (data && data.not_logged_in) {
			this.system_message("Bitte logge dich erst mal ein.");
		} else if (data && data.muted) {
			this.system_message("Sorry, aber du bist in diesem Chat nur noch stummer Beobachter.");
		}
	},

	_nick_in_content:		function (content) {
		var result = false;
		try {
			if (this.alias != null && !this.alias.match(/^\s*$/)) {
				if (content.match(this.alias) != null) result = true;
			}
		}
		catch (e) {}
		return result;
	},
	
	system_message:			function (msg) {
		this.update_messages({
			message:		msg,
			sys_message:	true
		});
	},
	
	update_messages:		function (data) {
		var do_scroll = false;

		if (this.scroll.scrollTop >= (this.scroll.scrollHeight - this.scrollHeight -10)) do_scroll = true;

		var line = document.createElement('div');
		var content;

		var relevant = this._nick_in_content(data.message) ? 'relevant' : '';

		if (data.sys_message) {
			content = document.createElement('span');
			content.className = 'sysmessage ' + relevant;
			content.innerHTML = data.message;
			line.appendChild(content);
		} else {
			var nick = document.createElement('span');
			nick.className = 'nick ' + relevant;
			nick.innerHTML = data.person_alias + ':';
			line.appendChild(nick);
	
			content = document.createElement('span');
			content.className = 'message ' + relevant;
			content.innerHTML = data.message;
			line.appendChild(content);
		}

		this.messages.push(line);
		this.container.appendChild(line);

/*		var width = this.container.offsetWidth;
		if ((width-20) < line.offsetWidth) {
			content.innerHTML = this._resize_message(content.innerHTML);
		}
*/
		while (this.messages.length > this.max_lines) {
			this.container.removeChild(this.messages.shift());
		}
		
		if (do_scroll) this.scroll.scrollTop = this.scroll.scrollHeight - this.scrollHeight + 20;
	},
	
	_resize_message:			function (message) {
		var mitte = Math.round(message.length / 2);
		
		return message.substr(0, mitte) + "<br>"+message.substr(mitte, message.length - mitte);
	}
	
});
