var NoizeShoutbox = new Class({
	
	Extends: Options,
	
	options: {
		// Default Options
		shoutbox: null,
		shouts: '.aShout',
		refresh_ms: 5000,
		status_url: '/shoutbox',
		update_url: '/shoutbox/post'
	},
	
	initialize: function(options) {
		var that = this;
		
		this.buildMatrix();
		
		this.status_requestor = new Request({
			url:this.options.status_url,
			onSuccess: that.updateShoutbox.bind(that),
			onFailure: that.updateError.bind(that)
		});

		if ( $('postsubmit') ){
			this.update_requestor = new Request({
				method:'post',
				url:this.options.update_url,
				onSuccess: that.sendShout.bind(that),
				onFailure: that.cantSendError.bind(that)
			});
			
			this.setEventHandles();
		}
		
		(function(){
			that.checkForUpdates(that);
		}).periodical(this.options.refresh_ms);
	
	},
	
	buildMatrix: function() {
		var that = this;
		this.shouts = [];

		$$(this.options.shouts).each(function(i) {
			that.shouts.include(i.get('rel').toInt());
		});
	},
	
	setEventHandles: function() {
		var that = this;
		$('postsubmit').addEvent('click', function(e) {
			e.stop();
			var message = $('msgToPost').get('value');
			console.log('message=' + message);
			that.update_requestor.send({data: 'message=' + message});
		});
		
	},
	
	getLowestId: function(a) {
		var lowest_id = 999999999999;
		a.each(function(i) {
			lowest_id = (i.get('rel').toInt() < lowest_id) ? i.get('rel').toInt() : lowest_id;
		});
		
		return lowest_id;
	},
	
	checkForUpdates: function(that) {
		this.status_requestor.send();
	},
	
	updateShoutbox: function(resp) {
		var shouts = JSON.decode(resp);
		var that = this;
		var shoutIds = [];
		
		shouts.each(function(aShoutMessage) {
			shoutIds.include( aShoutMessage.uid.toInt() );
		});
		
		this.shouts.each(function(id) {
			if ( !shoutIds.contains(id) ) {
				that.removeShout( id );
			}
		});
		
		shouts.each(function(aShoutMessage) {
			var id = aShoutMessage.uid.toInt();
			if ( !$('shout_' + id)) {
				that.addShout(aShoutMessage, that);
			}
		});
	},
	
	updateError: function() {
		//console.log(this.shouts);
		// no update required
	},
	
	sendShout: function(resp) {
		this.updateShoutbox(resp);
	},
	
	cantSendError: function() {
		alert('You can not submit more then one shout each minute.');
	},
	
	addShout: function(message, that) {
		that.shouts.include(message.uid.toInt());
		var shoutDiv = new Element('div', {
			'class': 'aShout',
			'rel': message.uid,
			'id': 'shout_' + message.uid
		});
		shoutDiv.adopt(
			new Element('span', {
				'class': 'shoutUser',
				'html': '<strong>' + message.username + '</strong> <small>' + message.age + '</small>'
			})
		);
		shoutDiv.adopt(
			new Element('br')
		);
		shoutDiv.adopt(
			new Element('span', {
				'class': 'shoutMessage',
				'html': message.message
			})
		);
		shoutDiv.inject($('shoutbox'),'top');

		var messageSpan = shoutDiv.getElement('span[class=shoutMessage]');
		messageSpan.set('tween', {duration: 2000});
		messageSpan.tween('color', ['#C6822A','#fff']);
	},
	
	removeShout: function(id) {
		var ref = 'shout_' + id;
		$(ref).destroy();
		this.shouts.erase(id);
	}
	
});