/**
 * @author	Sebastian Öttl
 * @copyright	2009 WCF Solutions <http://www.wcfsolutions.com/index.php>
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 */
var Shoutbox = Class.create({
	/**
	 * Inits Shoutbox.
	 */
	initialize: function(entries, lastUpdateTime) {
		this.entries = entries;
		this.lastUpdateTime = lastUpdateTime;
		this.options = Object.extend({
			langDeleteEntry:	'',
			langDeleteEntrySure:	'',
			imgDeleteEntrySrc:	'',
			entryReloadInterval: 	0,
			entrySortOrder: 	'ASC'
		}, arguments[2] || { });
		
		// set entries
		this.setEntries(this.entries);
		
		// start entry update
		this.startEntryUpdate();
	},
	
	/**
	 * Starts the entry update.
	 */
	startEntryUpdate: function() {
		if (this.options.entryReloadInterval != 0) {
			this.executer = new PeriodicalExecuter(this.updateEntries.bind(this), this.options.entryReloadInterval);
		}
	},
	
	/**
	 * Stops the entry update.
	 */
	stopEntryUpdate: function() {
		if (this.options.entryReloadInterval != 0) {
			this.executer.stop();
		}	
	},
	
	/**
	 * Inserts a smiley.
	 */
	insertSmiley: function(code) {
		var shoutboxMessage = $('shoutboxMessage');
		shoutboxMessage.value = shoutboxMessage.value+' '+code+' ';
		shoutboxMessage.focus();
	},
	
	/**
	 * Adds a new entry.
	 */
	addEntry: function() {
		// stop update
		this.stopEntryUpdate();

		// get message
		var message = $('shoutboxMessage').value;
		
		// reset message
		$('shoutboxMessage').value = '';
		$('shoutboxMessage').focus();
		
		// get username
		var username = '';
		if ($('shoutboxUsername')) username = $('shoutboxUsername').value;

		// add entry
		new Ajax.Request('index.php?page=ShoutboxAction&action=add'+SID_ARG_2ND, {
			method: 'post',
			parameters: {
				message: message,
				username: username
			},
			onSuccess: function() {
				// update entries
				this.updateEntries();
				
				// restart entry update
				this.startEntryUpdate();
			}.bind(this)
		});
	},

	/**
	 * Deletes an entry.
	 */	
	deleteEntry: function(id) {
		new Ajax.Request('index.php?page=ShoutboxAction&action=delete'+SID_ARG_2ND, {
			method: 'post',
			parameters: {
				entryID: id
			},
			onSuccess: function() {
				// unset entry
				this.entries.unset(id);
				
				// set entries
				this.setEntries(this.entries);
			}.bind(this)
		});
	},
	
	/**
	 * Updates the entries.
	 */
	updateEntries: function() {
		new Ajax.Request('index.php?page=ShoutboxAction&action=getEntries'+SID_ARG_2ND, {
			method: 'post',
			parameters: {
				startTime: this.lastUpdateTime
			},
			onSuccess: function(response) {				
				// get entries
				var entries = response.responseXML.getElementsByTagName('entries');
				if (entries.length > 0) {
					for (var i = 0; i < entries[0].childNodes.length; i++) {
						this.entries.set(entries[0].childNodes[i].childNodes[0].childNodes[0].nodeValue, {
							userID: entries[0].childNodes[i].childNodes[1].childNodes[0].nodeValue,
							username: entries[0].childNodes[i].childNodes[2].childNodes[0].nodeValue,
							time: entries[0].childNodes[i].childNodes[3].childNodes[0].nodeValue,
							message: entries[0].childNodes[i].childNodes[4].childNodes[0].nodeValue,
							canDelete: entries[0].childNodes[i].childNodes[5].childNodes[0].nodeValue
						});
						
						// set last update time
						if (i == entries[0].childNodes.length - 1) {
							this.lastUpdateTime = entries[0].childNodes[i].childNodes[3].childNodes[0].nodeValue;
						}
					}
					this.setEntries(this.entries);
				}
			}.bind(this)
		});
	},
	
	/**
	 * Sets the given entries in the shoutbox.
	 */
	setEntries: function(entries) {
		var shoutboxMessageDiv = $('shoutboxContent');
		if (shoutboxMessageDiv) {
			// update shoutbox content
			var newEntryString = '';
			var idArray = entries.keys();
			if (this.options.entrySortOrder == 'DESC') {
				idArray.reverse();
			}
			for (var i = 0; i < idArray.length; i++) {
				var id = idArray[i];
				var entry = this.entries.get(id);
				
				// update entry string
				newEntryString += '<p id="shoutboxEntry'+id+'"><span class="light">['+entry.time+']</span>'+(entry.canDelete != 0 ? ' <a href="javascript:shoutbox.deleteEntry('+id+')" onclick="return confirm(\''+this.options.langDeleteEntrySure+'\')" title="'+this.options.langDeleteEntry+'"><img src="'+this.options.imgDeleteEntrySrc+'" alt="" /></a>' : '')+' '+(entry.userID != 0 ? '<a href="'+'index.php?page=User&userID='+entry.userID+SID_ARG_2ND+'">'+entry.username+'</a>' : entry.username)+': '+entry.message+'</p>';
			}
			shoutboxMessageDiv.update(newEntryString);
			
			// focus last entry
			if (this.options.entrySortOrder == 'ASC') {
				shoutboxMessageDiv.scrollTop = shoutboxMessageDiv.scrollHeight - shoutboxMessageDiv.offsetHeight + 100;
			}
		}
	}
});