// We like ugly fixes.
is_ie = false;

var delete_url		= './ajax.php?m=delete&sid=9e5eae03808508ed7914960b221ade3a';
var edit_url    	= './ajax.php?m=edit&sid=9e5eae03808508ed7914960b221ade3a';
var version_url    	= './ajax.php?m=version&sid=9e5eae03808508ed7914960b221ade3a';
var print_ver		= 'Breizh Shoutbox v1.0.0';
var print_ver_alt 	= 'Breizh Shoutbox v1.0.0 © 2009';
var print_perm		= 'Vous devez être enregistré pour poster des messages';


function shout_popup(pop_url)
{
	popup(pop_url, '1300', '580', '_blank', "toolbar=0,menubar=0,scrollbars=0,statusbar=0,copyhistory=0,directories=0");
	return false;
}

function forum_url(url)
{
	window.open(url, '_blank'); 
	return false;
}

function load_shout()
{
	try
	{
		var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
		if (display_shoutbox == false)
		{
			return;
		}
		
		if (document.getElementById('shoutbox') == null)
		{
		
			var ev = err_msg('La div shoutbox ne peut pas être trouvée.', true);
	
			ev.name = 'E_CORE_ERROR';
			throw ev; 
			return;
		}
		else
		{
			div = document.getElementById('shoutbox');
			div.innerHTML = '';

			// Display message ;)
			message('Chargement...');
			// HTTP vars, required to relead/post things.
			
			hin = http();
			
			if (!hin)
			{
				return;
			}
			hin2 = http();
			huit = http();	
			hsmilies = http();
			hnr = http();
						// Div exists in the html, write it.
			write_main();
		}
	}
	catch (e)
	{
			handle(e);
			return;
	}
}

function write_main()
{
	try
	{
		// Write the base.

		var base = ce('ul');
		base.className = 'topiclist forums';
		base.id = 'base_ul'
		base.style.height = '97%';

		var li = ce('li');
		li.style.display = 'none';
		li.className = 'button_background button_background_green';
		var dl = ce('dl');
		dl.style.width = '100%';
		var posting_form = ce('dt');
		posting_form.id = 'post_message';
		posting_form.className = 'row';
		
		posting_form.style.display = 'none';
		posting_form.style.paddingtop = '5px';
		posting_form.height = '20px';
		posting_form.style.width = '99.5%';

		var posting_box = ce('form');
		posting_box.id = 'chat_form';

		
				li.style.display = 'block';
		li.style.padding = '1px 10px 2px 10px';
		li.style.borderBottom = '1px solid #00608F';
		
		posting_box.appendChild(tn(print_perm));
		posting_box.appendChild(tn(' '));
				posting_form.appendChild(posting_box);
		dl.appendChild(posting_form);
		li.appendChild(dl);
		base.appendChild(li);
		
		var msg_txt = ce('div');
		msg_txt.id = 'msg_txt';
		msg_txt.height = '180px';
		msg_txt.appendChild(tn(' '));
		base.appendChild(msg_txt);

		//In this div, the chats will be placed ;)
		var post = ce('div');
		post.style.display = 'block';
		post.id = 'msg';
		post.style.width = '99.5%';

		post.style.height = '220px';
			post.style.overflow = 'auto';		post.appendChild(tn('Chargement...'));
		base.appendChild(post);

		// pagin div - pagination
		var pagin = ce('li');
		pagin.id = 'nr';
		pagin.className = 'button_background button_background_green pagination';
		pagin.style.textAlign = 'left';
		pagin.style.marginTop = '0px';
		pagin.style.paddingTop = '2px';
		pagin.style.paddingBottom = '2px';
		pagin.style.height = '16px';
		pagin.style.width = '100%';
		base.appendChild(pagin);
		 
		div.innerHTML = '';
		div.appendChild(base);
		// Everyting loaded, lets select posts :)
		reload_post();
		reload_page();
	}
	catch (e)
	{
		handle(e);
		return;
	}
}

function reload_page()
{
	if (hnr.readyState == 4 || hnr.readyState == 0)
	{
	    if (error_number > 8)
	    {
	        return;
		}

		// Lets got some nice things :D
		hnr.open('GET','./ajax.php?m=number&sid=9e5eae03808508ed7914960b221ade3a&rand='+Math.floor(Math.random() * 1000000),true);

		hnr.onreadystatechange = function()
		{
			try
			{
				if (hnr.readyState != 4)
				{
					return;
				}
				if (hnr.readyState == 4)
				{
					xml = hnr.responseXML;
					
					if (xml.getElementsByTagName('error') && xml.getElementsByTagName('error').length != 0)
					{
						err = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
						message(err, true);
						return;
					}

					if (typeof xml != 'object' || xml.getElementsByTagName('nr').length <= 0)
					{
						throw err_msg(lang['SERVER_ERR']);
						return;
					}

					var nr = xml.getElementsByTagName('nr')[0].childNodes[0].nodeValue;
					var f = document.getElementById('nr');
					f.innerHTML = '';
					var d = ce('div');

					if (nr < 15){return;}

					var per_page = 15;
					var total_pages = Math.ceil(nr / per_page);
					var is_page = 'Page N° ';
					if (total_pages == 1 || !nr){return;}
					on_page = Math.floor(count / per_page) + 1;

					var p = ce('span');
					var a = ce('a');
					var b = ce('strong');

					if (on_page == 1)
					{
						b.appendChild(tn('1'));b.title = is_page+'1';
						p.appendChild(b);
						b = ce('strong');
					}
					else
					{
						a.c = ((on_page - 2) * per_page);a.href = 'javascript:;';a.title = 'Précédente';
						a.onclick = function(){count = this.c;last = 0;clearTimeout(timer_in);reload_post();reload_page();}
						a.appendChild(tn('Précédente'));
						p.appendChild(a);p.appendChild(tn(' '));

						a = ce('a');a.c = 0;a.href = 'javascript:;';a.title = is_page+'1';
						a.onclick = function()
						{
							count = this.c;
							last = 0;
							clearTimeout(timer_in);
							reload_post();
							reload_page();
						}
						a.appendChild(tn('1'));
						p.appendChild(a);
						a = ce('a');
					}

					if (total_pages > 5)
					{
						var start_cnt = Math.min(Math.max(1, on_page - 4), total_pages - 5);
						var end_cnt = Math.max(Math.min(total_pages, on_page + 4), 6);

						p.appendChild((start_cnt > 1) ? tn(' ... ') : cp());

						for (var i = start_cnt + 1; i < end_cnt; i++)
						{
							if (i == on_page)
							{
								b.appendChild(tn(i));b.title = is_page+on_page;
								p.appendChild(b);
								b = ce('strong');
							}
							else
							{
								a.c = (i - 1) * per_page;a.href = 'javascript:;';a.title = is_page+i;
								a.onclick = function()
								{
									count = this.c;
									last = 0; // Reset last, otherwise it will not be loaded.
									clearTimeout(timer_in);
									reload_post();
									reload_page();
								}
								a.appendChild(tn(i));
								p.appendChild(a);
								a = ce('a');
							}
							if (i < end_cnt - 1)
							{
								p.appendChild(cp());
							}
						}
						p.appendChild((end_cnt < total_pages) ? tn(' ... ') : cp());
					}
					else
					{
						p.appendChild(cp());
						for (var i = 2; i < total_pages; i++)
						{
							if (i == on_page)
							{
								b.appendChild(tn(i));b.title = is_page+on_page;
								p.appendChild(b);
								b = ce('strong');
							}
							else
							{
								a.c = (i - 1) * per_page;a.href = 'javascript:;';
								a.onclick = function()
								{
									count = this.c;
									last = 0; // Reset last, otherwise it will not be loaded.
									clearTimeout(timer_in);
									reload_post();
									reload_page();
								}
								a.appendChild(tn(i));
								p.appendChild(a);
								a = ce('a');
							}
							if (i < total_pages)
							{
								p.appendChild(cp());
							}
						}
					}

					if (on_page == total_pages)
					{
						b.appendChild(tn(total_pages));b.title = is_page+on_page;
						p.appendChild(b);
						b = ce('strong');
					}
					else
					{
						a = ce('a');a.c = ((total_pages - 1) * per_page);a.href = 'javascript:;';a.title = is_page+total_pages;
						a.onclick = function()
						{
							count = this.c;
							last = 0; // Reset last, otherwise it will not be loaded.
							clearTimeout(timer_in);
							reload_post();
							reload_page();
						}
						a.appendChild(tn(total_pages));

						p.appendChild(a);
						a = ce('a');a.c = ((on_page) * per_page);a.href = 'javascript:;';a.title = 'Suivante';
						a.onclick = function()
						{
							count = this.c;
							last = 0; // Reset last, otherwise it will not be loaded.
							clearTimeout(timer_in);
							reload_post();
							reload_page();
						}
						a.appendChild(tn('Suivante'));
						p.appendChild(tn(' '));p.appendChild(a);
						a = ce('a');
					}
					f.appendChild(p);
				}
			}
			catch (e)
			{
				handle(e);
				clearTimeout(timer_in);
				return;
			}
		}
		hnr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

		hnr.send(null);
	}
}

function reload_post()
{
	// If there is a open edit field, we dont reload.
	// If we reload, this edit field will be closed, and data lost.
	if (one_open)
	{
	    timer_in = setTimeout('reload_post();',7000);
		return;
	}
	
    if (error_number > 8)
    {
        return;
	}
	
	// First check if there new posts.
	if (hin2.readyState == 4 || hin2.readyState == 0)
	{
		hin2.open('GET','./ajax.php?m=check&sid=9e5eae03808508ed7914960b221ade3a&last=' + last + '&rand='+Math.floor(Math.random() * 100000),true);
		hin2.onreadystatechange = function()
		{
			try
			{
				if (hin2.readyState != 4)
				{
					return;
				}
				if (hin2.readyState == 4)
				{
					if (!hin2.responseXML)
					{
						throw err_msg('Erreur XML.');
					}
					var xml = hin2.responseXML;		
					
					if (typeof xml != 'object')
					{
						throw err_msg('Quelque chose s’est mal déroulé après avoir envoyé une requête au serveur');
						return;
					}
					
					if (xml.getElementsByTagName('error') && xml.getElementsByTagName('error').length != 0)
					{
						err = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
						throw err_msg(err, true);
						return;
					}
					
					if (xml.getElementsByTagName('time').length <= 0 || xml.getElementsByTagName('time')[0].childNodes.length <= 0)
					{
						throw err_msg('Quelque chose s’est mal déroulé après avoir envoyé une requête au serveur');
						return;
					}
					
					var t = xml.getElementsByTagName('time')[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('Il n’y a aucun message.'));
						}
					}
					else
					{
						if (hin.readyState == 4 || hin.readyState == 0)
						{
							last = xml.getElementsByTagName('last')[0].childNodes[0].nodeValue;
							// Lets got some nice things :D
							hin.open('GET','./ajax.php?m=view&sid=9e5eae03808508ed7914960b221ade3a&start=' + count + '&rand='+Math.floor(Math.random() * 1000000),true);
							hin.onreadystatechange = function()
							{
								try
								{
									if (hin.readyState != 4)
									{
										return;
									}
									if (hin.readyState == 4)
									{
										if (!hin.responseXML)
										{
											throw err_msg('Erreur XML.');
											return;
										}
										var xml = hin.responseXML;
										
										if (typeof xml != 'object')
										{
											throw err_msg(lang['SERVER_ERR']);
											return;
										}
										
										if (xml.getElementsByTagName('error') && xml.getElementsByTagName('error').length != 0)
										{
											var msg = xml.getElementsByTagName('error')[0].childNodes[0].nodeValue;
											throw err_msg(msg, true);
											return;
										}
										else
										{
											start = false;
											var tmp = xml.getElementsByTagName('posts');
											if (tmp.length == 0)
											{
																							
												var posts = document.getElementById('msg');
												posts.innerHTML = '';
												posts.appendChild(tn('Il n’y a aucun message.'));
												timer_in = setTimeout('reload_post();',5000);
												return;
											} 
											var posts = document.getElementById('msg');
											posts.innerHTML = '';
													
											var row = false;
											for (var i = 0; i < tmp.length ; i++)
											{
												var li = ce('li');
												li.className = (!row) ? 'row row1' : 'row row2';
												row = !row;
												
												var dl = ce('dl');
												var dd = ce('dt');
												var dt = ce('dd');
												var inh = tmp[i];
												dt.style.width = 'auto';
												dd.style.width = 'auto';
												dt.style.styleFloat = dt.style.cssFloat = 'left';
												dd.className = 'button_background button_background_green';
												dd.style.styleFloat = dd.style.cssFloat = 'left';
												dt.style.paddingLeft = '3px';
												dt.style.paddingRight = '5px';

												var s = ce('span');
												var msg = parse_xml_to_html(inh.getElementsByTagName('shout_text')[0]);
												
												dt.appendChild(parse_xml_to_html(inh.getElementsByTagName('shout_time')[0]));
												dt.appendChild(tn(' | '));
												dt.appendChild(parse_xml_to_html(inh.getElementsByTagName('username')[0]));
												dt.appendChild(tn(': '));

																								dd = ce('dt');
																								dl.appendChild(dt);
												var msg2 = ce('span');
												msg2.id = 'shout' + i;
												msg2.i = i;
												msg2.appendChild(msg);
												edit_form = ce('span');
												
												
												dd.appendChild(msg2);
												dd.appendChild(edit_form);
												dd.id = 'msgbody';
												dd.style.width = 'auto';
												dd.style.styleFloat = dd.style.cssFloat = 'none';
												dd.style.paddingLeft = '3px';
												dd.style.paddingRight = '5px';

												dl.appendChild(dd);
												li.appendChild(dl);
												posts.appendChild(li);

											}

																						if (first)
											{
												document.getElementById('post_message').style.display = 'inline';
												first = false;
											}
																					}
									}
								}
								catch (e)
								{
//									timer_in = setTimeout('reload_post();',7000);
									handle(e);
									return;
								}
							}
							hin.send(null);
						}
					}
					timer_in = setTimeout('reload_post();',7000);
				}
			}
			catch (e)
			{
				handle(e);
				return;
			}
		}
		hin2.send(null);
	}
}

lang['COMMA_SEPARATOR'] = ', ';
lang['NO_AJAX'] = 'Non ajax';
lang['SERVER_ERR'] = 'Quelque chose s’est mal déroulé après avoir envoyé une requête au serveur';
lang['JS_ERR'] = 'Il y a eu une erreur JavaScript. Erreur: ';
lang['LINE'] = 'Ligne';
lang['FILE'] = 'Fichier';
lang['MSG_DEL_DONE'] = 'Message en cours de suppression...';
lang['ONLY_ONE_OPEN'] = 'Vous ne pouvez avoir qu’une seule boite d’édition ouverte';
lang['EDIT'] = 'Éditer';
lang['SHOUT_EDIT'] = 'Éditer le message';
lang['SENDING_EDIT'] = 'Publication après édition...';
lang['EDIT_DONE'] = 'Le message a été édité';
lang['CANCEL'] = 'Abandonner';
