////FONCTIONS DE MAJ DES CHAMPS////
// variables : id, value, url, bubble_text, list
function changeMP(newVal, oldVal){
	if (newVal.list.length != '0')
	{
		document.getElementById('nbMP').innerHTML = '<span class="alert_Compteur">'+newVal.list.length+'</span>';
	}
}

// variables : id, value, bubble_text
function changeFlags(newVal, oldVal){
	if (parseInt(newVal.value) > 0)
	{
		document.getElementById('nbFlag').innerHTML = '<span class="alert_Compteur">'+newVal.value+'</span>';
	}
}

// variables : aucune, les valeurs sont newVal et oldVal
function changeAvatar(newVal, oldVal){
	document.getElementById('avatarBarreDyn').innerHTML = '<img src="'+newVal+'" />';
}

// variables : id, value, url, bubble_text, list
function changeFriendsAsks(newVal, oldVal){
	if (newVal.list.length != '0')
	{
		document.getElementById('amis').innerHTML = '<span class="alert_Compteur">'+newVal.list.length+'</span>';
	}
}

// variables : id, value, bubble_text, list
function changeFirendsOnline(newVal, oldVal){
	if (newVal.list.length != '0')
	{
		document.getElementById('amis_online').innerHTML = '<span class="alert_Compteur">'+newVal.list.length+'</span>';
	}
}

//variable : pending_alerts, url, bubble_text
function changeModeratorNotification(newVal, oldVal){
	if (newVal.pending_alerts.length != '0')
	{
		document.getElementById('moderateur').innerHTML = '<span class="alert_Compteur alert_Modo">'+newVal.pending_alerts.length+'</span>';
	}
}
////FIN FONCTIONS DE MAJ DES CHAMPS////
////CONFIG////
var CF_refresh_time = 30000 ;
////FIN CONFIG////


function lit_cook(nom) {
        var deb,fin;
        deb = document.cookie.indexOf(nom + "=");
        if (deb >= 0)
        {
               deb += nom.length + 1;
               fin = document.cookie.indexOf(";",deb);
               if (fin < 0)
                       fin = document.cookie.length;
               return unescape(document.cookie.substring(deb,fin));
        }
        return "";
}

////GESTION DU FOCUS (repris de MD)////
var CF_tabFocus =   {
	init : function () {
		CF_tabFocus.isFocused 	= true ;
		CF_tabFocus.miss 		= false;
		CF_tabFocus.hash 		= lit_cook('CF_user_hash');
		CF_tabFocus.affiche_moderator = false;//affichage du <li> de moderation a ne faire que la premiere fois
		if ( navigator.userAgent.indexOf ( 'MSIE' ) != - 1 ) {
			window.attachEvent ( 'onfocus' , CF_tabFocus.onWindowFocus );
			window.attachEvent ( 'onfocusin' , CF_tabFocus.onWindowFocus );
			document.attachEvent ( 'onfocusout' , CF_tabFocus.onWindowBlur );
			CF_tabFocus.isFocused = false ; /* onfocusout is not called the first time with IE */
		} else {
			  window.addEventListener ( 'blur' , CF_tabFocus.onWindowBlur , false );
			  window.addEventListener ( 'focus' , CF_tabFocus.onWindowFocus , false );
		}
	},
	onWindowBlur : function () {
		CF_tabFocus.isFocused = false ;
	},
	onWindowFocus : function () {
		CF_tabFocus.isFocused = true ;
		if(CF_tabFocus.miss)//si on a raté au moins un raffraichissement
			refresh();
	}
};
////FIN GESTION DU FOCUS////

////GESTION DE L'UPDATE////
function updateToolBar ( force ) {
	if ( CF_tabFocus.isFocused || force ) {
		refresh();
		CF_tabFocus.miss = false;
	}
	else {
		CF_tabFocus.miss = true;
	}
	window.setTimeout ( updateToolBar , CF_refresh_time , false );
}

function refresh(){

	var cf_hash = lit_cook('CF_user_hash');
	var md_hash = lit_cook('md_hash');

	callExternalScript(url_club_tb + '?callback=updateToolbar_callback&rd='+ Math.random ()+'&hash_check='+CF_tabFocus.hash);
}

//CF_CURRENT_OBJECT : nom imposé par MD pour la fonction de callback
var CF_CURRENT_OBJECT = {};
CF_CURRENT_OBJECT.updateToolbar_callback = function(ajax) {


	if (ajax.responseText) {
		var json = eval('(' + ajax.responseText.replace(/\\/g,'\\\\') + ')');
	} else {
		var json = eval('(' + ajax.replace(/\\/g,'\\\\') + ')');
	}
	
	//changement avatar
	if(typeof(prev_json) == 'undefined' || json.toolbar_avatar_src 		!= prev_json.toolbar_avatar_src){
		changeAvatar(				json.toolbar_avatar_src, 		typeof(prev_json) == 'undefined'?'undefined':prev_json.toolbar_avatar_src);
	}
	
	if (typeof(json.moderator_notification) != 'undefined'){
		if(typeof(prev_json) == 'undefined' || json.moderator_notification.pending_alerts != prev_json.moderator_notification.pending_alerts){
			if(CF_tabFocus.affiche_moderator == false){
				document.getElementById('notification_alerts').style.display = 'block';
				CF_tabFocus.affiche_moderator = true;
			}
			changeModeratorNotification(json.moderator_notification, 	typeof(prev_json) == 'undefined'?'undefined':prev_json.moderator_notification);
		}
	}

	
	//on parcourt les notifications
	for (var i=0;i<json.notification_update.length;i++) {
		if(typeof(prev_json) == 'undefined' || prev_json.notification_update[i].value != json.notification_update[i].value){
			switch (json.notification_update[i].id) {
				case 'personnal_messages':
					changeMP(			json.notification_update[i], typeof(prev_json) == 'undefined'?'undefined':prev_json.notification_update[i]);
				break;
				case 'forum_flags':
					changeFlags(		json.notification_update[i], typeof(prev_json) == 'undefined'?'undefined':prev_json.notification_update[i]);
				break;
				case 'friends_asks':
					changeFriendsAsks(	json.notification_update[i], typeof(prev_json) == 'undefined'?'undefined':prev_json.notification_update[i]);
				break;
				case 'friends_online':
					changeFirendsOnline(json.notification_update[i], typeof(prev_json) == 'undefined'?'undefined':prev_json.notification_update[i]);
				break;
				case 'chat':
				break;
				default : break;
			}
		}
	}
	prev_json = json;
}

function toolbar_load(url) {
	url_club_tb = url+'/widget/toolbar/toolbar_update.php';
	//bouchon
	//url_club_tb = 'http://dev-caradisiac.jbourdaire.caradisiac.dev/service/toolbarUpdate/';
	CF_tabFocus.init();
	updateToolBar(true);
}
////FIN GESTION DE L'UPDATE////

function callExternalScript(url){
	var n = document.createElement("script");
	n.setAttribute("type", "text/javascript");
	n.setAttribute("src", url);
	document.getElementsByTagName("head")[0].appendChild(n);
}
