/**
 * @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>
 */
function makeSeo(username) {
	//username=username.toLowerCase();
	do{
		lastusername=username;
		username=username.replace(/<(.*?)>/i,"");
	}while(lastusername!=username);
	zeichen="abcdefghijklmnopqrstuvwxyz";
	for (i=0;i<username.length;i++){
		if (zeichen.toLowerCase().indexOf(username.toLowerCase().substr(i,1))==-1){
			username.replace(username.substr(i,1),"");
		}
	}
	return username;
};
function showContainerShoutbox(id,nr){
	if (nr==1){
		found=0;
		for (i=1;i<=3;i++){
			if (document.getElementById("shoutboxContainer"+i).style.display!="none"){
				found=1;hideContainerShoutbox(i,id,1);
			}
		}
		if (!found){
			showContainerShoutbox(id,0);
		}
	}else{
		document.getElementById("shoutboxContainer"+id).style.display="block";
		newheight=(document.getElementById("shoutboxContainer"+id).offsetHeight*2);
		if (newheight>document.getElementById("shoutboxContent"+id).offsetHeight) newheight=document.getElementById("shoutboxContent"+id).offsetHeight;
		if (newheight<=0) newheight=1;
		document.getElementById("shoutboxContainer"+id).style.height=newheight+"px";
		if (document.getElementById("shoutboxContainer"+id).offsetHeight<document.getElementById("shoutboxContent"+id).offsetHeight){
			setTimeout('showContainerShoutbox('+id+',0)',10);
		}
	}
}
function hideContainerShoutbox(id,showID,first){
	document.getElementById("shoutboxContainer"+id).style.height=(document.getElementById("shoutboxContainer"+id).offsetHeight/1.5)+"px";
	containerheight=document.getElementById("shoutboxContainer"+id).offsetHeight;
	if (containerheight>1){
		setTimeout('hideContainerShoutbox('+id+','+showID+', 0)',10);
	}else{
		document.getElementById("shoutboxContainer"+id).style.display="none";
		if(id!=showID || first==1){
			document.getElementById("shoutboxContainer"+id).style.height=document.getElementById("shoutboxContent"+id).offsetHeight;
			showContainerShoutbox(showID,0);
		}
	}
}
ShoutboxIsActive=1;updateTimer=0;setInterval("updateTimeInterval()",1000);
var Shoutbox = Class.create({
	/**
	 * Inits Shoutbox.
	 */
	initialize: function(entries, lastUpdateTime) {
		this.entries = entries;
		this.lastUpdateTime = lastUpdateTime;
		this.lastEntryString = '';
		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() {
		updateTimer=0;
		// 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+"&rnd="+Math.random(), {
			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+'">'+(document.getElementsByName("optTime")[0].checked ? '<span class="light" style="font-size:9px;">['+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="/Benutzer'+entry.userID+'_'+makeSeo(entry.username)+'.html">'+entry.username+'</a>' : entry.username)+': '+entry.message+'</p>';
			}
			
			if (newEntryString!=this.lastEntryString){
				this.lastEntryString=newEntryString;
				shoutboxMessageDiv.innerHTML='<div id="shoutboxActivate" style="display:none;overflow:hidden;font-weight:bold;text-align:center;"><img src="/icon/refresh.png" alt="" />&nbsp;<a href="javascript:updateTimeInterval(1);void(0);">Die Shoutbox wurde pausiert! Aktivieren?</a></div><div id="shoutboxContentArea">'+this.lastEntryString+'</div>';
				
				// focus last entry
				if (this.options.entrySortOrder == 'ASC') {
					shoutboxMessageDiv.scrollTop = shoutboxMessageDiv.scrollHeight - shoutboxMessageDiv.offsetHeight + 100;
				}
			}
		}
	},
	
	setOptions: function() {
		this.lastUpdateTime=0;
		this.lastEntryString='';
		loader = document.createElement('script');
		loader.type = 'text/javascript';
		if (document.getElementsByName("optDirection")[0].checked){
			this.options.entrySortOrder='ASC';
		}else{
			this.options.entrySortOrder='DESC';
			$('shoutboxContent').scrollTop=0;
		}
		loader.src = "index.php?page=ShoutboxAction&action=setOptions&Time="+document.getElementsByName("optTime")[0].checked+"&Username="+document.getElementsByName("optUsername")[0].checked+"&Smilies="+document.getElementsByName("optSmilies")[0].checked+"&URLParser="+document.getElementsByName("optUrlparser")[0].checked+"&BBCodes="+document.getElementsByName("optBbcodes")[0].checked+"&Direction="+document.getElementsByName("optDirection")[0].checked+"&HideIgnored="+document.getElementsByName("optHideIgnored")[0].checked;
		document.body.appendChild(loader);
		document.getElementById('setOptionsShoutbox').style.display='block';
		setTimeout("document.getElementById('setOptionsShoutbox').style.display='none';",1000);
		this.updateEntries();
	}
});
function updateTimeInterval(activate){
	if (activate) updateTimer=0;
	if (updateTimer<60*3) {
		if (!ShoutboxIsActive) {
			Tween(document.getElementById("shoutboxActivate"),"height",20,1,100,-1,"hideObject");
			Tween(document.getElementById("shoutboxContentArea"),"alpha",25,100,100);
			ShoutboxIsActive=1;
			shoutbox.updateEntries();
		}
		updateTimer++;
	}else if(ShoutboxIsActive){
		ShoutboxIsActive=0;
		document.getElementById("shoutboxActivate").style.height="1px";
		document.getElementById("shoutboxActivate").style.display="block";
		Tween(document.getElementById("shoutboxActivate"),"height",1,20,100);
		Tween(document.getElementById("shoutboxContentArea"),"alpha",100,25,100);
		shoutbox.stopEntryUpdate();
	}
}