/*
		+-----------------------------------------------------------------------+
		| Penlyte Web Application Framework                                     |
		+-----------------------------------------------------------------------+
		| Copyright (c) 2009 David Turner                                       |
		+-----------------------------------------------------------------------+
		| Authors: David Turner <dave@turneris.com>                             |
		+-----------------------------------------------------------------------+
		| Subversion Version Control Info:                                      |
		| $Id:: pnmessage.js 202 2010-03-09 23:35:34Z dturner                 $ |
		+-----------------------------------------------------------------------+
*/
var pnMessages = Class.create({
	curr: 0,
	prev: 0,
	next: 0,
	count: 0,
	visible: 0,
	initialize: function() {
		this.loadHandler = this.loadHandler.bind(this);
		this.ackHandler = this.ackHandler.bind(this);
		this.seekHandler = this.seekHandler.bind(this);
		this.seek = this.seek.bind(this);
		$('pnmsg_prev').observe('click', this.prev.bindAsEventListener(this));
		$('pnmsg_next').observe('click', this.next.bindAsEventListener(this));
		$('pnmsg_ack').observe('click', this.ack.bindAsEventListener(this));
		new PeriodicalExecuter(this.seek, 30);
		this.seek(Object());
	},
	load: function() {
		new Ajax.Request('/messages/ajax/get/module/messages/m/' + this.curr.toString(), {
			method:'get',
			onSuccess: this.loadHandler
		});
	},
	loadHandler: function (t){
		var json = t.responseText.evalJSON();
		if((json.id > 0)) {
			$('pnmsg_body').innerHTML = json.body;
			//$('pnmsg_list').show();
			if(this.visible < 1) {
				Effect.SlideDown('pnmsg_container', { duration: 0.5 });
				this.visible = 1;
			}
			this.curr = json.id;
			this.prev = json.prev;
			this.next = json.next;
			if(this.prev < 1) {
				$('pnmsg_prev').className = 'disabled';
			} else {
				$('pnmsg_prev').className = 'enabled';
			}
			if(this.next < 1) {
				$('pnmsg_next').className = 'disabled';
			} else {
				$('pnmsg_next').className = 'enabled';
			}
		} else {
			//$('pnmsg_list').hide();
			if(this.visible> 0) {
				Effect.SlideUp('pnmsg_container');
				this.visible = 0;
			}
		}
		this.count = 0;
	},
	prev: function () {
		if(this.prev > 0) {	this.curr = this.prev; this.load(); }
		return false;
	},
	next: function() {
		if(this.next > 0) {	this.curr = this.next; this.load(); }
		return false;
	},
	ack: function() {
		new Ajax.Request('/messages/module/messages/ajax/ack/m/' + this.curr.toString(), {
			method: 'get',
			onSuccess: this.ackHandler
		});
	},
	ackHandler: function(t){
		var json = t.responseText.evalJSON();
		if(false === json.newm) {
			//$('pnmsg_list').hide();
			if(this.visible> 0) {
				Effect.SlideUp('pnmsg_container');
				this.visible = 0;
			}
		} else {
			this.curr = json.newm;
			this.load();
		}
		this.count = 0;
	},
	seek: function(pe) {
		if(this.count > 10) {
			pe.stop();
			return;
		}
		new Ajax.Request('/messages/module/messages/ajax/seek/cid/' + this.curr, {
			method:'get',
			onSuccess: this.seekHandler
		});
	},
	seekHandler: function(t){
		var json = t.responseText.evalJSON();
		if(false === json.newm) {
			//$('pnmsg_list').hide();
			if(this.visible> 0) {
				Effect.SlideUp('pnmsg_container');
				this.visible = 0;
			}
			this.count = this.count + 1;
		} else {
			if((json.newm > 0) && (json.newm != this.curr)) {
				this.curr = json.newm;
				this.load();
				this.count = 0;
			} else {
				this.count = this.count + 1;
			}
		}
	}
});