var div,hin,huit,hin2, hsmilies, hinfo, hdelete, hnr;
var config = new Array();
var post_info = timer_in = last = null;
var display_shoutbox = false;
var start = reload = first = true;
var smilies = false;
var count = 0;
function err_msg(title)
{
 	var err = new Error(title);
 	
 	if(!err.message)
	{
		err.message = title; 
	}
	 	
	err.name = "E_USER_ERROR";//Php error?!? :D
	return err;
}

function handle(e)
{
 	switch(e.name)
	 {
 	 	//Is it our error? :)
 	 	case "E_USER_ERROR":
 	 	case "E_CORE_ERROR":
 	 		message(e.message, true);			
 	 	 	return;
 	 	break;
 	 	default:
		{
 	 		
 	 		tmp = 'Es gab einen JavaScript Fehler. \nFehler: ';
 	 		tmp += e.message;
 	 		if(e.lineNumber)
			{
 	 			tmp += '\nZeile: ';
 	 			tmp += e.lineNumber;
 	 		}
 	 		if(e.fileName)
			{
 	 		 	tmp += '\nDatei: ';
 	 		 	tmp += e.fileName;
 	 		}
 	 		message(tmp, true);			
 	 		return;
 	 	}
 	}
}

function load_shout()
{
	try
	{
		
		if(display_shoutbox == false)
		{
		 	return;
		}
		
		if(document.getElementById('shoutbox') == null)
		{
		
			var ev = err_msg('The shoutbox div cann\'t be found.');	
	
			ev.name = "E_CORE_ERROR";
			throw ev; 
			return;
		}
		else
		{
			div = document.getElementById('shoutbox');

			//Display message ;)
			message('Loading');
			//HTTP vars, required to relead/post things.
			
			hin = http();
			hin2 = http();
			huit = http();	
			hsmilies = http();
			hnr = http();
					 	//Element exists.
		 	write_main();
		}
	}
	catch(e)
	{
		  handle(e);
		  return;
	}
}

function write_main()
{
	try
	{
	 	//Write the base.
	 	var basis = ce('div');
	 	basis.style.height = '200px';
//	 	basis.style.overflow = 'auto';


	 			var msg_txt = ce('div');
		
		
		msg_txt.id = 'msg_txt';
		msg_txt.height = '180px';
		msg_txt.appendChild(tn(' '));
		
		basis.appendChild(msg_txt);	 	

	 	var post = ce('div');//In this div, the chat will be placed ;)
	 	post.style.display = 'block';
	 	post.id = 'msg';
	 	post.style.overflow = 'auto';
	 	post.style.width = '100%';
	 	post.style.height = '160px';
	 	
	 	post.appendChild(tn('Loading'));
	 	basis.appendChild(post);
	 	
	 	//Nr div
	 	var nr_d = ce('div');
	 	nr_d.id = 'nr';
	 	
	 	basis.appendChild(nr_d);
		 
	 	div.innerHTML = '';
	 	div.appendChild(basis);
	 	//Everyting loaded, lets select posts :)
	 	reload_post();
	 	reload_page();
	 	
	}
	catch(e)
	{
	 	handle(e);
	 	return;
	}
}

function validate_overige(xml)
{
	try
	{
		if(xml.childNodes.length == 0)
		{
			return tn("");
		}
		else if(xml.childNodes.length == 1 && xml.childNodes[0].nodeValue != null)
		{//With a tag in it, its bigger as 1?

			return tn(xml.childNodes[0].nodeValue);
		}
		else
		{
			var div = ce("span");
			loop:
			
			for(var i = 0; i < xml.childNodes.length ;i++)
			{

				switch(xml.childNodes[i].nodeType)
				{
					case 3:
						div.appendChild(document.createTextNode(xml.childNodes[i].nodeValue));
					break;
					case 9:
					case 8:
					case 10:
					case 11:
						//continue;
					break;
					case 1:
						if(xml.childNodes[i].childNodes.length == 0 && xml.childNodes[i].nodeName != 'br' && xml.childNodes[i].nodeName != 'img' && xml.childNodes[i].nodeName != 'hr')
						{
							break;
						}					
						//This is a difficult one :)
						switch(xml.childNodes[i].nodeName)
						{
							case "br":
								div.appendChild(ce("br"));
							break;
							case "blockquote":
								var q = ce("blockquote");
								q.className = "quote";
								q.appendChild(validate_overige(xml.childNodes[i]));
								div.appendChild(q);
							break;
							case "a":
								var a = ce("a");
								
								a.href = xml.childNodes[i].getAttribute("href");
								a.appendChild(validate_overige(xml.childNodes[i]));

								a.target = "_blank";
								
								div.appendChild(a);
							break;
							case "img":
								var img = ce("img");

								img.alt = xml.childNodes[i].getAttribute("alt");
								img.src = xml.childNodes[i].getAttribute("src");
								img.border = 0;
								
								div.appendChild(img);
							break;

							break;
							case "strong":
							case "b":
								var b = ce("strong");

								b.appendChild(validate_overige(xml.childNodes[i]));
								div.appendChild(b);
							break;
							case "hr":
								div.appendChild(ce('hr'));
							break;
							case "h1":
							case "h2":
							case "h3":
							case "h4":
							case "h5":
							case "h6":
								var h = ce(xml.childNodes[i].nodeName);
								h.appendChild(validate_overige(xml.childNodes[i]));
								div.appendChild(h);
							break;
							case 'span':
							
								var s = ce('span');
								s.appendChild(validate_overige(xml.childNodes[i]));
								// 
								f = xml.childNodes[i].getAttribute("style");
								if(f.indexOf(";") == -1)
								{
									f += ";";
								}
								f = f.split(";");
								for(var j = 0; j < f.length; j++)
								{
									f2 = f[j].split(": ");
									switch(f2[0])
									{
										case 'font-style':
											s.style.fontStyle = f2[1];
										break;
										case 'font-weight':
											s.style.fontWeight = f2[1];
										break;
										case 'font-size':
											try
											{
												s.style.fontSize = f2[1];
											}
											catch(e){}
										break;
										case 'line-height':
											s.style.lineHeigt = f2[1];
										break;
										case 'color':
											s.style.color = f2[1];
										break;
									}
								}
								div.appendChild(s);
							break;																
							default:
							{
								throw err_msg("Ungültiges Element: "+xml.childNodes[i].nodeName);
							}
						}
					break;
				}
			}
		}
		return div;
	}
	catch(e)
	{
		handle(e);
		return div;
	}
}

function reload_page()
{
	if(!reload)
	{
		//No reload needed, return.
		return;
	}
 	if(hnr.readyState == 4 || hnr.readyState == 0)
	{
		
 	 	//Lets got some nice things :D
 	 	hnr.open('GET','ajax.php?m=nr&rand='+Math.floor(Math.random() * 1000000),true);
 	 	
 	 	hnr.onreadystatechange = function()
		{		 	 	
 			try
			{
 	 	 		if(hnr.readyState == 4)
				{
					reload = false;
 	 	 			xml = hnr.responseXML;
 	 	 			if(xml.getElementsByTagName("error") && xml.getElementsByTagName("error").length != 0)
					{
 	 	 				err = xml.getElementsByTagName("error")[0].childNodes[0].nodeValue;
	 	 	 			document.getElementById('posts').innerHTML = '';			 	 	 				
 	 	 				document.getElementById('posts').appendChild(tn(err));
						clearTimeout(timer_in);
						 	 	 				
						setTimeout("reload_post();",5000);			 	 	 				
 	 	 			}
					else
					{
						var nr = xml.getElementsByTagName("nr")[0].childNodes[0].nodeValue;
						var f = document.getElementById('nr');
						f.innerHTML = '';
						var d = ce('div');
						var j = 1;
	 	 	 	 		for(var i = 0;i < nr ;i = i + 20)
						{

							a = ce('a');
							a.appendChild(tn(j));
							a.c = i;
							a.href = "javascript:;";
							a.onclick = function()
							{
								count = this.c;
								last = 0;//Reset last, otherwise it will not be loaded.
								reload = true;
								clearTimeout(timer_in);
								reload_post();
							}
							d.appendChild(a);

							d.appendChild(tn(' '));
							j++;
							
	 	 	 	 		}	 		
	 	 	 	 		f.appendChild(d);
					}
				}	 	 	
 	 	 	}
 	 	 	catch(e)
			{
 	 	 	 	handle(e);
 	 	 	 	return;
 	 	 	}
 	 	}						 	 	
		hnr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

 	 	hnr.send(null);
 	}	
 	setTimeout("reload_page();", 500);
}

function reload_post()
{
	//First check if there new posts.
	if(hin2.readyState == 4 || hin2.readyState == 0)
	{
		hin2.open('GET','ajax.php?m=check&last=' + last + '&rand='+Math.floor(Math.random() * 1000000),true);
		hin2.onreadystatechange = function()
		{
			try
			{
	 	 	 	if(hin2.readyState == 4)
				{
		 	 		if(!hin2.responseXML)
					{
						throw err_msg('XML Fehler.');
					}
					var xml = hin2.responseXML;		
					if(xml.getElementsByTagName('error').length > 0 && xml.getElementsByTagName('error')[0].childNodes.length > 0)
					{
					 	err = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
					 	throw err_msg(err);
					}
					
					var t = xml.getElementsByTagName('tijd')[0].childNodes[0].nodeValue;
					if(t == "0")
					{
						//If start is true, we let notice that there are no messages
						if(start == true)
						{
						 							
	 	 	 	 		  	var posts = document.getElementById('msg');
	 	 	 	 		  	posts.innerHTML = '';
		 	 	 	 			posts.appendChild(tn('Es gibt keine Nachrichten.'));	 							
						}
						else
						{
							reload = false;
						}
					}
					else
					{
					 	if(hin.readyState == 4 || hin.readyState == 0)
						{
							reload = true;
					 		last = xml.getElementsByTagName('last')[0].childNodes[0].nodeValue;
					 	 	//Lets got some nice things :D
					 	 	hin.open('GET','ajax.php?m=view&start=' + count + '&rand='+Math.floor(Math.random() * 1000000),true);
					 	 	hin.onreadystatechange = function()
							{
					 	 		try
								{
						 	 	 	if(hin.readyState == 4)
									{
										reload = true;
						 	 	 		if(!hin.responseXML)
										{
						 	 	 		 	throw err_msg('XML Fehler.');
						 	 	 		}
						 	 	 	 	var xml = hin.responseXML;
					 	 	 	 	 	var err = xml.getElementsByTagName('error');
						 	 	 	 	if(err && err[0] && err[0].childNodes.length > 0)
										{
						 	 	 	 		var msg = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
						 	 	 	 	 	throw err_msg(msg);
						 	 	 	 	}
										else
										{
											start = false;
						 	 	 	 		var tmp = xml.getElementsByTagName('posts');
						 	 	 	 		if(tmp.length == 0)
											{
											 												
						 	 	 	 		  var posts = document.getElementById('msg');
						 	 	 	 		  posts.innerHTML = '';
						 	 	 	 			posts.appendChild(tn('Es gibt keine Nachrichten.'));	 	 	 	 		  
						 	 	 	 			setTimeout('reload_post();',5000);
												return;
											}
						 	 	 	 		var basis = ce('table');
						 	 	 	 		basis.width = '100%';
						 	 	 	 		basis.className = 'forumline';
						 	 	 	 		var rows = 0;
						 	 	 	 		 // tmp.reverse();
						 	 	 	 		  
						 	 	 	 		  
						 	 	 	 		for(var i = (tmp.length - 1);i >= 0 ;i--)
											{
												var inh = tmp[i]
						 	 	 	 		  	var row = basis.insertRow(rows);
						 	 	 	 		  	var cell = row.insertCell(0);
						 	 	 	 		  	cell.className = inh.getElementsByTagName('row_class')[0].childNodes[0].nodeValue;
						 	 	 	 		  	cell.style.height = cell.style.width = '100%';
						 	 	 	 		  	
						 	 	 	 		  	var s = ce('span');
						 	 	 	 		  	//<span class="gensmall">
						 	 	 	 		  	s.className = 'gensmall';
						 	 	 	 		  							 	 	 	 		  	
						 	 	 	 		  	s.appendChild(tn(inh.getElementsByTagName('shout_time')[0].childNodes[0].nodeValue + ' | '));
						 	 	 	 		  	var b = ce('strong');
						 	 	 	 		  	var a = ce('a');
						 	 	 	 		  	a.href = 'profile.php?mode=viewprofile&u=' + inh.getElementsByTagName('shout_user_id')[0].childNodes[0].nodeValue;
						 	 	 	 		  	
						 	 	 	 		  	a.appendChild(validate_overige(inh.getElementsByTagName('username')[0]));

						 	 	 	 		  	b.appendChild(a);
						 	 	 	 		  	
						 	 	 	 		  	b.appendChild(tn(': '));
						 	 	 	 		  	
						 	 	 	 		  	s.appendChild(b);
													var msg = validate_overige(inh.getElementsByTagName('shout_text')[0]);
													s.appendChild(msg);
													cell.appendChild(s);
					
						 	 	 	 		  	//basis.appendChild(row);
						 	 	 	 		}
						 	 	 	 		var posts = document.getElementById('msg');
						 	 	 	 		posts.innerHTML = '';
						 	 	 	 		posts.appendChild(basis);
						 	 	 	 		//basis = row = null;

										 							 	 	 		}
						 	 		}
					 	 		}
					 	 	 	catch(e)
								{
					 	 	 		timer_in = setTimeout('reload_post();',5000);
					 	 	 	 	handle(e);
					 	 	 	 	return;
					 	 	 	}
					 	 	}
					 	 	hin.send(null);
					 	}
					}
					timer_in = setTimeout('reload_post();',5000);
				}
			}
			catch(e)
			{
				handle(e);
				return;			
			}			
		}
		hin2.send(null);
	}
}

function http()
{
	try
	{
		var http_request = false;
		if (window.XMLHttpRequest) 
		{ // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) 
			{
				http_request.overrideMimeType('text/xml');
			}
		} 
		else if (window.ActiveXObject) 
		{ // IE
			try 
			{
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e) 
			{
				try 
				{
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch (e) 
				{
				}
			}
		}
	
		if (!http_request) 
		{
			 throw err_msg('Kein Ajax');
		}
		return http_request;

	}
	catch(e)
	{
		handle(e);
	 	return "ERR";
	}
}

function message(msg, color)
{
	if(document.getElementById('msg_txt') != null)
	{
	 	document.getElementById('msg_txt').innerHTML = '';
	 	var tmp = ce('p');
	 	tmp.appendChild(tn(msg));
	 	if(color)
	 	{
	 		tmp.style.color = 'red';
	 	}
	 	document.getElementById('msg_txt').appendChild(tmp);
	}
	else
	{
	 	div.innerHTML = '';
	 	var tmp = ce('p');
	 	tmp.appendChild(tn(msg));
	 	if(color)
	 	{
	 		tmp.style.color = 'red';
	 	}	 	
	 	div.appendChild(tmp);
	}
}

function ce(e)
{
 	return document.createElement(e);
}

function tn(e)
{
 	return document.createTextNode(e);
}
	