function utf8_encode ( string ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");
 
    var utftext = "";
    var start, end;
    var stringl = 0;
 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }
 
    if (end > start) {
        utftext += string.substring(start, string.length);
    }
 
    return utftext;
}

function utf8_decode ( str_data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    var tmp_arr = [], i = ac = c1 = c2 = c3 = 0;
 
    str_data += '';
 
    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
 
    return tmp_arr.join('');
}

function updateTeamspeakPanel()
{
	if ($('teamspeak-panel')) {
		new Ajax.Request('/forum/TS_start.php', {
		  method: 'get',
		  onSuccess: function(transport) {
			if (transport.responseText.length > 0) {
			  $('teamspeak-panel').removeClassName('ajax-loading');
			  $('teamspeak-flexcroll').update(transport.responseText);
			  CSBfleXcroll('teamspeak-flexcroll');

			} else	{
			  $('teamspeak-panel').update("Chargement impossible");
			}
		  }
		});
		          $('teamspeak-refreshlink').observe('click', function(event) {
		              var a = new Element('div', { 'id': 'teamspeak-flexcroll' }).update('');
		              $('teamspeak-flexcroll').replace(a);
		              $('teamspeak-panel').addClassName('ajax-loading');
		              updateTeamspeakPanel();
		              Event.stop(event);
		          });
	}
}

function loadjscssfile(filename, filetype){
    if (filetype=="js"){ //if filename is a external JavaScript file
	var fileref=document.createElement('script')
	    fileref.setAttribute("type","text/javascript")
	    fileref.setAttribute("src", filename)
	    }
    else if (filetype=="css"){ //if filename is an external CSS file
	var fileref=document.createElement("link")
	    fileref.setAttribute("rel", "stylesheet")
	    fileref.setAttribute("type", "text/css")
	    fileref.setAttribute("href", filename)
	    }
    if (typeof fileref!="undefined")
	document.getElementsByTagName("head")[0].appendChild(fileref)
}

function getImgSize(imgSrc)
{
	var newImg = new Image();
	newImg.src = imgSrc;
	return newImg;
}

Event.observe(window, 'load', function() {

	if ($$('.section-portal').length > 0)
	{
		updateTeamspeakPanel();
	}

	/*
	if ($('menu_connexion'))
	{
		Event.observe($('menu_connexion'), 'click', function(event) {
			Event.stop(event);
			Lightview.show({
			href: './ajax/form_login.php',
			rel: 'ajax',
			options: {
				autosize: true,
				topclose: true,
				ajax: {
				method: 'get',
					evalScripts: true,
					onComplete: function(){
						$('name').focus();
					}
				}
			}
			});
		});
	}
	
	*/
	
	if ($$('.chatform input#message').length > 0)
	{
		new Ajax.Autocompleter($$('.chatform input#message')[0], 'chatbox_autocomplete', "/forum/ajax/chatbox_autocomplete.php");
		Event.observe($$('.chatform input#message')[0], 'keydown', function(event) {
			var el = Event.element(event);
			if ($('chatbox_autocomplete').getStyle('display') == 'none') {
				var key = event.which || event.keyCode;
				if (key == Event.KEY_DOWN) {
					if (chatbox_histo_current_pos == 0) {
						chatbox_histo[0] = el.value;
					}
					if (chatbox_histo_current_pos + 1 < chatbox_histo.length) {
						chatbox_histo_current_pos += 1;
						el.value = chatbox_histo[chatbox_histo_current_pos];
					}
				}
				else if (key == Event.KEY_UP) {
					if (chatbox_histo_current_pos > 0) {
						chatbox_histo_current_pos -= 1;
						el.value = chatbox_histo[chatbox_histo_current_pos];
					}
				}
			}
		});
	}

	if ($('navbar_unread_msg'))
	{
		new PeriodicalExecuter(function(pe) {
			Effect.Pulsate($('navbar_unread_msg'), { pulses : 2, duration: 1 });
			pe.stop();
		}, 1);

		new PeriodicalExecuter(function(pe) {
			Effect.Pulsate($('navbar_unread_msg'), { pulses : 2, duration: 1 });
		}, 8);
	}

	if ($('back2top_footer_left')) {
		Event.observe($('back2top_footer_left'), 'click', function(event) { 
			Event.stop(event); Effect.ScrollTo('top', { duration: 0.5 }); 
		});
	}

	if ($('back2top_footer_right')) {
		Event.observe($('back2top_footer_right'), 'click', function(event) { 
			Event.stop(event); Effect.ScrollTo('top', { duration: 0.5 }); 
		});
	}

	$$('img.autoresize').each(function(el){
		var src = el.readAttribute('src');
		if (src) {
			var image = getImgSize(src);
			el.setStyle({ display : 'inline' });
			if (image.width > autosize_max_width - 2)
			{
				var max_width = autosize_max_width - 2;
				el.setStyle({ 
					width : max_width + 'px', 
					border : autosize_border
				});
				el.addClassName('autoresized');
				el.observe('click', function(event) {
					var img = Event.element(event);
					var src = img.readAttribute('src');
					Lightview.show({
					    href: src,
					    rel: 'image',
					    title: 'Image',
					    options: {
					      autosize: true,
					      topclose: true
					    }
					});
				});
			}
		}
	});

	loadjscssfile("http://www.wowhead.com/widgets/power.js", "js");

});