MGCCbEvoNS.refresh_timeout 			= null;
MGCCbEvoNS.already_refreshing 		= 0;
MGCCbEvoNS.already_sending 			= 0;
MGCCbEvoNS.force_session_refresh 	= 0;
MGCCbEvoNS.force_scrolling 			= 0;
MGCCbEvoNS.current_chanid 			= 0;
MGCCbEvoNS.last_scroll_height 		= 0;
MGCCbEvoNS.last_scroll_sheight 		= 0;
MGCCbEvoNS.last_sticky_chatid 		= 0;
MGCCbEvoNS.in_archives 				= 0;
MGCCbEvoNS.first_load 				= 1;
MGCCbEvoNS.chatids_array 			= new Array();
MGCCbEvoNS.schatids_array 			= new Array();


dojo.addOnLoad(function() { MGCCbEvoNS.onload_chatbox(); });

MGCCbEvoNS.onload_chatbox = function()
{
	// Launch clock
	if (this.showtime && !this.small_version)
	{
		this.clock_update();
	}

	// Overflow problem with some browsers
	if (is_opera || is_ns4)
	{
		dojo.style('chats','overflow','auto');
		if (this.notifs_display > 1)
		{
			dojo.style('schats','overflow','auto');
		}
	}
	
	// Set default channel (MGCCbEvoNS.current_chanid is set as the channel changing ajax is processed afterwards)
	if (this.default_channel != 0)
	{
		this.display_channel(this.default_channel);
		this.current_chanid = this.default_channel;
	}
	else
	{
		// Display general channel text
		if (typeof(this.channel_names_array) != "undefined")
		{
			dojo.byId('mgc_cb_evo_channel_name').innerHTML = MGCCbEvoNS.channel_names_array[0];
		}
	}

	// Last but not least : Chats refresh
	if (!this.in_archives)
	{
		if (!this.small_version)
		{	
			// Chats retrieving loading when not in collapsed mode
			if (this.iscollapsed == 0)
			{
				if (this.initstate)
				{
					this.chatbox_refresh('forced');
				}
				else
				{
					// Show inactive message and refresh button (if refresh hidden option is set)
					dojo.byId('chats').innerHTML = "<div align=\center\>" + phrase_inactive + "</div>";
					if (this.hide_refreshimg)
					{
						dojo.byId('mgc_cb_evo_refresh_button').style.display = "";
					}
				}
			}
			else if (this.inistate)
			{
				// Chats retrieving loading when in collapsed mode
				this.get_last_chats_collasped();
			}
		}
		else
		{
			if (this.initstate)
			{
				this.chatbox_refresh('forced');
			}
			else
			{
				// Show inactive message and refresh button (if refresh hidden option is set)
				dojo.byId('chats').innerHTML = "<div align=\center\>" + phrase_inactive + "</div>";
				if (this.hide_refreshimg)
				{
					dojo.byId('mgc_cb_evo_refresh_button').style.display = "";
				}
			}		
		}
	}	
};

MGCCbEvoNS.clock_update = function ()
{
	var AM_PM;
	var d = new Date();
	var s = d.getSeconds();
	var m = d.getMinutes();
	var h = d.getHours();
	
	// Management of the 2 digits display for seconds and minutes
	if (s<10)
	{
		s = "0" + s;
	}
	
	if (m<10)
	{
		m = "0" + m;
	}
	
	// AM/PM management if not 24 hours clock
	if (this.time_24hours == 0)
	{
		if (h > 12)
		{
			h -= 12;
			AM_PM = "PM";
		}
		else
		{
			AM_PM = "AM";
		}
	}
	
	// Management of the 2 digits display for hours
	if (h < 10)
	{
		h = "0" + h;
	}
	
	// Display updated time
	if (this.time_24hours)
	{
		dojo.byId("mgc_cb_evo_clock").innerHTML = h + ":" + m + ":" + s;
	}
	else
	{
		dojo.byId("mgc_cb_evo_clock").innerHTML = h + ":" + m + ":" + s + " " + AM_PM;
	}
	
	// Reload time-out for time updating	
	setTimeout("MGCCbEvoNS.clock_update()",1000);
};

MGCCbEvoNS.chatbox_collapse = function()
{
	if (this.isvb4)
	{
		// Object management
		if (dojo.style('mgc_cb_evo_opened','display') != 'none')
	   	{
			removeFromCookie	= 1;
		}
	   	else
	   	{
			removeFromCookie	= 0;
		}	

		// Cookie save
		var vBCookie = fetch_cookie("vbulletin_collapse");
		var TableauCollapsed = new Array();
		if(vBCookie != null)
		{
			vBCookie = vBCookie.split("\n");
			for(var vBCookieElement in vBCookie)
			{
				if(YAHOO.lang.hasOwnProperty(vBCookie,vBCookieElement) && vBCookie[vBCookieElement] != "mgc_cb_evo" && vBCookie[vBCookieElement] != "")
				{
					TableauCollapsed[TableauCollapsed.length] = vBCookie[vBCookieElement];
				}
			}
		}
		
		if (removeFromCookie == 1) // Remove from collapsed
		{
			TableauCollapsed[TableauCollapsed.length] = "mgc_cb_evo";
		}
		
		expires=new Date();
		expires.setTime(expires.getTime()+(1000*86400*365));
		set_cookie("vbulletin_collapse",TableauCollapsed.join("\n"),expires);
		docollapse = false;
	}
	else
	{
		// Get vB toggle collapse function (for registry)
		var docollapse = toggle_collapse('mgc_cb_evo');	
	}
	
	// Clear chats
   	this.clear_chatbox(0);
   	
	// Exec collapse
   	if(dojo.style('mgc_cb_evo_opened','display') != 'none')
   	{
   		dojo.style('mgc_cb_evo_opened','display','none');
   		dojo.style('mgc_cb_evo_title_opened','display','none');
   		dojo.style('mgc_cb_evo_title_closed','display','');
		clearTimeout(this.refresh_timeout);

     	if(this.chats_when_collapsed)
      	{
        	dojo.style('mgc_cb_evo_closed','display','');
        	this.get_last_chats_collasped ();
      	}
   	}
   	else
   	{
   		dojo.style('mgc_cb_evo_opened','display','');
   		dojo.style('mgc_cb_evo_closed','display','none');
   		dojo.style('mgc_cb_evo_title_opened','display','');
   		dojo.style('mgc_cb_evo_title_closed','display','none');
    	this.chatbox_refresh('forced');
   	}
	
	return docollapse;
};

MGCCbEvoNS.clear_chatbox = function(reload)
{
	var cbel = null;

	// Empty chatids array
	while(this.chatids_array.length>0)
	{
		this.chatids_array.pop();
	}
	this.last_sticky_chatid = 0;

	// Remove all child nodes from last chat table
	if (!this.small_version)
	{
		cbel = dojo.empty('lchats');
	}
	
	// Remove all child nodes from chat table
	cbel = dojo.empty('chats');

	// If special chats separate => remove all of them
	if ((((this.notifs_display > 1) && reload) || (this.notifs_display == 2)) && !this.small_version)
	{
		this.clear_notifications();
	}

	/* Refresh chatbox if requested */
	if (reload)
	{
		this.force_scrolling = 1;
		this.chatbox_refresh('forced');
	}
};

MGCCbEvoNS.chatbox_refresh = function(type)
{
	/* Not already refreshing */
	if ((this.auto_refresh || (type == 'forced')) && !this.already_refreshing && !this.already_sending)
	{
		clearTimeout(this.refresh_timeout);
		this.already_refreshing = 1;

		/* If chatbox is inactive and notification in sidebar : clear them before refresh as they were kept */
		if (this.chatbox_inactive && ((this.notifs_display == 3) || (this.notifs_display == 4)))
		{
			this.clear_notifications();
		}

		dojo.byId('mgc_cb_evo_refresh_img').style.visibility = 'visible';

    	var cids = this.chatids_array.join(',');

    	if (this.notifs_display > 1)
    	{
    		var scids = this.schatids_array.join(',');
    	}
    	else
    	{
    		var scids = '';
    	}

		// If leaving from inactive mode and refresh img hidden option is set : hide refresh button
		if (this.hide_refreshimg)
		{
			dojo.byId('mgc_cb_evo_refresh_button').style.display = "none";
		}
		
    	/* Chatbox is inactive, refresh forced => session update */
    	if (this.chatbox_inactive || this.force_session_refresh)
    	{
			dojo.xhrPost({
				url : this.jsloc + 'mgc_cb_evo_ajax.php',
				handleAs : 'xml',
				preventCache : true,
				content :
				{
					securitytoken : SECURITYTOKEN,
					action : 'ajax_refresh_chat',
					status : (this.small_version?'small':'open'),
					channel_id : this.current_chanid,
					location : cb_location, // A Voir
					first_load : 1,
					chatids : cids,
					schatids : scids,
					this_script : this.this_script
				},
		        error: function (error) {
	                console.error ('Error: ', error);
				},
				load : dojo.hitch(this,'wait_chatbox_refresh')
			});
		}
		else
		{		
			dojo.xhrPost({
				url : this.jsloc + 'mgc_cb_evo_ajax.php',
				handleAs : 'xml',
				preventCache : true,
				content :
				{
					securitytoken : SECURITYTOKEN,
					action : 'ajax_refresh_chat',
					status : (this.small_version?'small':'open'),
					channel_id : this.current_chanid,
					location : cb_location, // A VOIR
					first_load : this.first_load,
					chatids : cids,
					schatids : scids,
					this_script : this.this_script
				},
		        error: function (error) {
	                console.error ('Error: ', error);
				},
				load : dojo.hitch(this,'wait_chatbox_refresh')
			});
		}
	}
	return false;
};

MGCCbEvoNS.wait_chatbox_refresh = function(refresh_xml)
{
   	if(refresh_xml)
   	{
   		var do_scrolling 	= 0;
   		var do_sscrolling 	= 0;
		var playsound 		= 0;

		var jchats = refresh_xml.getElementsByTagName('chat');

		if (this.first_load || this.chatbox_inactive)
		{
			this.clear_chatbox(0);
		}

		// Let's check if we should scroll at end of new shouts adding when in new shouts at bottom
		if (MGCCbEvoNS.chats_order)
		{
			if (this.first_load || this.force_session_refresh || this.chatbox_inactive || this.force_scrolling)
			{
				do_scrolling = 1;
			}
			else if (this.last_scroll_height == dojo.byId('chats_container').scrollTop)
			{
				// Scrollbar is at bottom we can scroll !
				do_scrolling = 1;
			}
			else
			{
				do_scrolling = 0;
			}

			if (this.notifs_display > 1)
			{
				if (this.first_load || this.force_session_refresh || this.chatbox_inactive || this.force_scrolling)
				{
					do_sscrolling = 1;
				}
				else if (this.last_scroll_sheight == dojo.byId('schats_container').scrollTop)
				{
					// Scrollbar is at bottom we can scroll !
					do_sscrolling = 1;
				}
				else
				{
					do_sscrolling = 0;
				}
			}
		}

		/* Reset inactive mode */
		if (this.chatbox_inactive)
		{
			this.chatbox_inactive = 0;
			this.inactive_timeout = setTimeout("MGCCbEvoNS.activate_idle_mode()", this.inactive_mode_delay);
		}

		if (jchats.length)
		{
			/* Some var for chat add/edit manipulation */
			var chatrefreshel	= dojo.byId('chats');
			var eledit 			= null;

			if (this.notifs_display > 1)
			{
				var schatrefreshel = dojo.byId('schats');
			}

			/* Let's parse the ajax retrievals */
			for (i=0;i<jchats.length;i++)
			{
				var type 		= jchats[i].getElementsByTagName('type')[0].firstChild.nodeValue;
				var issticky 	= jchats[i].getElementsByTagName('sticky')[0].firstChild.nodeValue;
				var chatid 		= jchats[i].getElementsByTagName('chatid')[0].firstChild.nodeValue;
				var oldchat		= jchats[i].getElementsByTagName('oldchat')[0].firstChild.nodeValue;
				var specialchat	= jchats[i].getElementsByTagName('specialchat')[0].firstChild.nodeValue;

				if ((this.notifs_display > 1) && (specialchat == 1))
				{
					/* Whole page refresh requested */
					if (type == 2)
					{
						window.location.reload();
					}
					/* Edit chat */
					else if (type == 1)
					{
						/* Create the new chat row */
						var newchat = this.create_chatrow(jchats[i],1);

						/* Do the replacement */
						schatrefreshel.replaceChild(newchat,dojo.byId('chatrow_' + chatid));
					}
					/* Add notification */
					else if (type == 0)
					{
						/* We have a chat or notification => can play sound */
						playsound = 1;

						/* Add chatid to chatids array */
						this.schatids_array.push(chatid);

						/* Create the new chat row */
						var newchat = this.create_chatrow(jchats[i],1); // TODO

						/* Older at top */
						if (this.chats_order == 1)
						{
							/* Add chat to the chatbox */
							schatrefreshel.appendChild(newchat);
						}
						/* Newer at top*/
						else
						{
							/* Older chat, append at the bottom */
							if (oldchat == 1)
							{
								schatrefreshel.appendChild(newchat);
							}
							/* First load => always prepend and check if first sticky chat received for recording */
							else if (MGCCbEvoNS.first_load == 1)
							{
								if (schatrefreshel.hasChildNodes())
								{
									schatrefreshel.insertBefore(newchat,schatrefreshel.firstChild);
								}
								else
								{
									schatrefreshel.appendChild(newchat);
								}
							}
							else
							{
								schatrefreshel.insertBefore(newchat,schatrefreshel.firstChild);
							}
						}
					}
					/* Remove chat */
					else if (type == 3)
					{
						schatrefreshel.removeChild(dojo.byId('chatrow_' + chatid));
						this.remove_chatid_from_array(chatid,1);
					}
				}
				/* No separation between chats and notifications or normal chats management */
				else
				{
					/* Whole page refresh requested */
					if (type == 2)
					{
						window.location.reload();
					}
					/* Edit chat */
					else if (type == 1)
					{
						/* Create the new chat row */
						var newchat = this.create_chatrow(jchats[i],0); // TODO

						/* Do the replacement */
						chatrefreshel.replaceChild(newchat,dojo.byId('chatrow_' + chatid));
					}
					/* Add chat */
					else if (type == 0)
					{
						/* We have a chat or notification => can play sound */
						playsound = 1;

						/* Add chatid to chatids array */
						this.chatids_array.push(chatid);

						/* Create the new chat row */
						var newchat = this.create_chatrow(jchats[i],specialchat); // TODO

						/* Older at top */
						if (this.chats_order == 1)
						{
							/* Older chat, insert at the top */
							if (oldchat == 1)
							{
								chatrefreshel.appendChild(newchat);
							}
							/* If no sticky chat yet -> append and check if first sticky chat received for recording */
							else if (!this.last_sticky_chatid)
							{
								/* Add chat to the chatbox */
								chatrefreshel.appendChild(newchat);

								/* If sticky set as last sticky */
								if (issticky == 1)
								{
									this.last_sticky_chatid = chatid;
								}
							}
							else
							{
								/* If sticky chat -> continue to append (first load) */
								if (issticky == 1)
								{
									chatrefreshel.appendChild(newchat);
								}
								/* Else, add before the first sticky chat */
								else
								{
									chatrefreshel.insertBefore(newchat,dojo.byId('chatrow_' + this.last_sticky_chatid));
								}
							}
						}
						/* Newer at top*/
						else
						{
							/* Older chat, append at the bottom */
							if (oldchat == 1)
							{
								chatrefreshel.appendChild(newchat);
							}
							/* First load => always prepend and check if first sticky chat received for recording */
							else if (this.first_load == 1)
							{
								if (chatrefreshel.hasChildNodes())
								{
									chatrefreshel.insertBefore(newchat,chatrefreshel.firstChild);
								}
								else
								{
									chatrefreshel.appendChild(newchat);
								}


								if (!this.last_sticky_chatid && issticky == 1)
								{
									this.last_sticky_chatid = chatid;
								}
							}
							else
							{
								/* If no sticky chat => prepend */
								if (!this.last_sticky_chatid)
								{
									chatrefreshel.insertBefore(newchat,chatrefreshel.firstChild);
				
									if (!this.last_sticky_chatid && issticky == 1)
									{
										this.last_sticky_chatid = chatid;
									}									
								}
								/* Add after the last sticky chat */
								else
								{
									/* Insert after using insertBefore if next child, otherwise append */
									if (dojo.byId('chatrow_' + this.last_sticky_chatid).nextSibling)
									{
										chatrefreshel.insertBefore(newchat,dojo.byId('chatrow_' + this.last_sticky_chatid).nextSibling);
									}
									else
									{
										chatrefreshel.appendChild(newchat);
									}
								}
							}
						}
					}
					/* Remove chat */
					else if (type == 3)
					{
						chatrefreshel.removeChild(dojo.byId('chatrow_' + chatid));
						this.remove_chatid_from_array(chatid,0);
					}
				}
			}
		}
		
		/* Scroll ? */
		if (do_scrolling)
		{
			dojo.byId('chats_container').scrollTop = dojo.byId('chats').offsetHeight;
			this.last_scroll_height = dojo.byId('chats_container').scrollTop;
		}

		if (this.notifs_display > 1)
		{
			if (do_sscrolling)
			{
				dojo.byId('schats_container').scrollTop = dojo.byId('schats').offsetHeight;
				this.last_scroll_sheight = dojo.byId('schats_container').scrollTop;
			}
		}		
		
		/* Check for new messages if active */
		if (this.first_load && this.warn_active)
		{
			this.check_new_messages();
		}

		/* Hide refresh */
		dojo.byId('mgc_cb_evo_refresh_img').style.visibility = 'hidden';

		/* Update row alternate if requested */
      	if (MGCCbEvoNS.alt_chat_colors)
      	{
			this.update_row_color_alt(0);
		}
		else if (this.notifs_display == 3 || this.notifs_display == 4)
		{
			this.update_row_color_alt(1);
		}

		/* Numerically sort chatids array */
		this.chatids_array.sort(function(a,b){return b - a;});
		if (this.notifs_display > 1)
		{
			this.schatids_array.sort(function(a,b){return b - a;});
		}

		/* At last play sound if needed and active */
		if (this.sound && playsound && !this.first_load)
		{
			dojo.byId("mgc_cb_evo_sound_span").innerHTML= '<embed src="mgc_cb_evo/clientscript/sound/new_chat.swf" hidden="true" autostart="true" loop="false">';
		}

		this.first_load 			= 0;
		this.force_session_refresh 	= 0;
		this.force_scrolling 		= 0;
		this.already_refreshing 	= 0;

		/* Relaunch timeout if active */
		if (this.autorefresh_on)
		{
			this.refresh_timeout = setTimeout("MGCCbEvoNS.chatbox_refresh()", this.refresh_delay);
		}
	}
};

MGCCbEvoNS.remove_chatid_from_array = function(id_chat,is_notif)
{
	var j;

	if (is_notif == 1)
	{
		for(j = 0;j < this.schatids_array.length;j++)
		{
			if (this.schatids_array[j] == id_chat)
			{
				this.schatids_array.splice(j,1);
				return;
			}
		}
	}
	else
	{
		for(j = 0; j < this.chatids_array.length;j++)
		{
			if (this.chatids_array[j] == id_chat)
			{
				this.chatids_array.splice(j,1);
				return;
			}
		}
	}
};

MGCCbEvoNS.update_row_color_alt = function(do_only_schats)
{
	if (do_only_schats == 0)
	{
		var chatbits = dojo.byId('chats');
	
		if (chatbits.hasChildNodes())
		{
			var chatalt_cur = "alt2";
			var chatbits_childnodes = chatbits.childNodes;
	
			for(var cnt = 0;cnt < chatbits_childnodes.length;cnt++)
			{
				chatbits_childnodes[cnt].className = chatalt_cur;
				if (chatalt_cur == "alt2")
				{
					chatalt_cur = "alt1";
				}
				else
				{
					chatalt_cur = "alt2";
				}
			}
		}
	}
	
	// If special chats separate => update row alt
	if (this.notifs_display > 1)
	{
		var chatbits = dojo.byId('schats');

		if (chatbits.hasChildNodes())
		{
			var chatalt_cur = "alt2";
			var chatbits_childnodes = chatbits.childNodes;

			for(var cnt = 0;cnt < chatbits_childnodes.length;cnt++)
			{
				chatbits_childnodes[cnt].className = chatalt_cur;
				if (chatalt_cur == "alt2")
				{
					chatalt_cur = "alt1";
				}
				else
				{
					chatalt_cur = "alt2";
				}
			}
		}
	}
};

MGCCbEvoNS.clear_notifications = function()
{
	while(this.schatids_array.length>0)
	{
		this.schatids_array.pop();
	}
	
	cbel = dojo.byId('schats');

	if (cbel.hasChildNodes())
	{
		while(cbel.childNodes.length >= 1)
		{
			cbel.removeChild(cbel.firstChild);
		}
	}	
};

MGCCbEvoNS.get_last_chats_collasped = function()
{
	dojo.xhrPost({
		url : this.jsloc + 'mgc_cb_evo_ajax.php',
		handleAs : 'xml',
		preventCache : true,
		content :
		{
			securitytoken : SECURITYTOKEN,
			action : 'ajax_refresh_chat',
			status: 'closed',
			channel_id: this.current_chanid 
		},
        error: function (error) {
            console.error ('Error: ', error);
		},
		load : dojo.hitch(this,'wait_get_last_chats_collasped')
	});
};

MGCCbEvoNS.wait_get_last_chats_collasped = function(get_last_answer_xml)
{
	if(get_last_answer_xml)
	{
		if (this.first_load)
		{
			this.clear_chatbox(0);
		}

		var jchats 			= get_last_answer_xml.getElementsByTagName('chat');
		var lchatrefreshel 	= dojo.byId('lchats');
		var chatid			= 0;

		for (i=0;i<jchats.length;i++)
		{
			/* Get chatid */
			chatid = jchats[i].getElementsByTagName('chatid')[0].firstChild.nodeValue;

			/* Create the new chat row */
			var newchat = this.create_chatrow(jchats[i],0);

        	/* First at bottom => append */
        	if (this.chats_order)
        	{
	        	lchatrefreshel.appendChild(newchat);
	      	}
	      	/* Else => prepend */
		  	else
	      	{
	      		lchatrefreshel.insertBefore(newchat,lchatrefreshel.firstChild);
	      	}
      	}
    }
};

MGCCbEvoNS.display_channel = function(channel)
{
	// Check if the user has the right to access the channel
	dojo.xhrPost({
		url : this.jsloc + 'mgc_cb_evo_ajax.php',
		handleAs : 'xml',
		preventCache : true,
		content :
		{
			securitytoken : SECURITYTOKEN,
			action : 'ajax_change_channel',
			channel_id : channel
		},
        error: function (error) {
            console.error ('Error: ', error);
		},
		load : dojo.hitch(this,'wait_ajax_change_channel')
	});
	
	return false;
};

MGCCbEvoNS.wait_ajax_change_channel = function(ajax_change_channel_xml)
{
	if (ajax_change_channel_xml)
	{
		var display_channel_status = ajax_change_channel_xml.getElementsByTagName('status')[0];
		
		if (display_channel_status.getElementsByTagName('canview')[0].firstChild.nodeValue == 1)
		{
			var channel = display_channel_status.getElementsByTagName('channel_id')[0].firstChild.nodeValue;
				
		  	/* General channel */
			if (channel == 0)
			{
				dojo.style('channel_gen_on','display','');
				dojo.style('channel_gen_off','display','none');
				dojo.style('channel_gen_new','display','none');
		
				for (i = 0; i < channels_ids.length; i++)
				{
					dojo.style('channel_' + channels_ids[i] + '_on','display','none');
						
					/* Was actually blinking */
					if (dojo.style('channel_' + channels_ids[i] + '_new','display') != 'none')
					{
						dojo.style('channel_' + channels_ids[i] + '_off','display','none');
						dojo.style('channel_' + channels_ids[i] + '_new','display','');
					}
					else
					{
						dojo.style('channel_' + channels_ids[i] + '_off','display','');
						dojo.style('channel_' + channels_ids[i] + '_new','display','none');
					}
				}
			}
			else
			{
				dojo.style('channel_gen_on','display','none');
			
				/* Was blinking */
				if (dojo.style('channel_gen_off','display') != 'none')
				{
					dojo.style('channel_gen_off','display','none');
					dojo.style('channel_gen_new','display','');
				}
				else
				{
					dojo.style('channel_gen_off','display','');
					dojo.style('channel_gen_new','display','none');
				}
		
				for (i = 0; i < channels_ids.length; i++)
				{
					if (channels_ids[i] == channel)
					{
						dojo.style('channel_' + channels_ids[i] + '_on','display','');
						dojo.style('channel_' + channels_ids[i] + '_off','display','none');
						dojo.style('channel_' + channels_ids[i] + '_new','display','none');
					}
					else
					{
						dojo.style('channel_' + channels_ids[i] + '_on','display','none');
		
						/* Was actually blinking */
						if (dojo.style('channel_' + channels_ids[i] + '_new','display') != 'none')
						{
							dojo.style('channel_' + channels_ids[i] + '_off','display','none');
							dojo.style('channel_' + channels_ids[i] + '_new','display','');
						}
						else
						{
							dojo.style('channel_' + channels_ids[i] + '_off','display','');
							dojo.style('channel_' + channels_ids[i] + '_new','display','none');
						}
					}
				}
			}
		
			this.current_chanid = channel;
			this.force_session_refresh = 1;
		
			/* Change channel names if object exist only */
			if (typeof(this.channel_names_array) != "undefined")
			{
				dojo.byId('mgc_cb_evo_channel_name').innerHTML = this.channel_names_array[channel];
			}
		
			/* Clean chat box and refresh if not first load */
			if (this.first_load == 0)
			{
				this.clear_chatbox(1);
			}
		}
	}
};

MGCCbEvoNS.warn_new_message_in_channel = function(chanid)
{
  	/* General channel */
	if (chanid == 0)
	{
	   if (dojo.style('channel_gen_new','display') == 'none')
	   {
         dojo.style('channel_gen_on','display','none');
         dojo.style('channel_gen_off','display','none');
         dojo.style('channel_gen_new','display','');
      }
   }
   else
   {
      if (dojo.style('channel_' + chanid + '_new','display') == 'none')
      {

         dojo.style('channel_' + chanid + '_on','display','none');
         dojo.style('channel_' + chanid + '_off','display','none');
         dojo.style('channel_' + chanid + '_new','display',''); 
      }
   }
};

MGCCbEvoNS.activate_idle_mode = function()
{
	// Configure chatbox for idle
	this.chatbox_inactive = 1;
   	clearTimeout(this.refresh_timeout);

   	// Empty chat box and clear table
   	this.clear_chatbox(0);

	// If refresh img hidden option is set : show refresh button
	if (this.hide_refreshimg)
	{
		dojo.style('mgc_cb_evo_refresh_button','display','');
	}

   	// Show inactive message
   	var chatrefreshel = dojo.byId('chats');   	
   	var idletr = document.createElement('tr');
   	
	dojo.create(
		"td",
		{
			align: 'center',
			valign: this.chat_valign,
			innerHTML: phrase_inactive
		},
		idletr
	);	
	
	chatrefreshel.appendChild(idletr);
};

MGCCbEvoNS.check_new_messages = function()
{
	if (this.warn_active)
	{
		dojo.xhrPost({
			url : this.jsloc + 'mgc_cb_evo_ajax.php',
			handleAs : 'xml',
			preventCache : true,
			content :
			{
				securitytoken : SECURITYTOKEN,
				action : 'ajax_check_messages',
				channel_id : this.current_chanid
			},
	        error: function (error) {
                console.error ('Error: ', error);
			},
			load : dojo.hitch(this,'wait_check_new_messages')
		});
	}
}

MGCCbEvoNS.wait_check_new_messages = function(new_messages_ajax_xml)
{
	if(new_messages_ajax_xml)
	{
		var play_sound 	= 0;
		var statuses 	= new_messages_ajax_xml.getElementsByTagName('status');

		for (i=0;i<statuses.length;i++)
		{
        	chanid 		= statuses[i].getAttribute('chanid');
         	chanstatus 	= statuses[i].firstChild.nodeValue;
         	if (chanid != this.current_chanid && chanstatus == 1)
         	{
         		play_sound = 1;
            	this.warn_new_message_in_channel(chanid);
         	}
      	}
      	
      	if (this.channel_sound && play_sound)
      	{
			dojo.byId("mgc_cb_evo_sound_span").innerHTML= '<embed src="mgc_cb_evo/clientscript/sound/new_chat.swf" hidden="true" autostart="true" loop="false">';
      	}

      	setTimeout("MGCCbEvoNS.check_new_messages()", this.warn_delay);
   	}
};

MGCCbEvoNS.exec_collaspe_online = function()
{
	icon = dojo.byId('icon_mgc_cb_evo_online');

	if (MGCCbEvoNS.isvb4)
	{
		if (dojo.style('collapseobj_mgc_cb_evo_online','display') == 'none')
		{
			this.get_online_users();
			
			icon_re = new RegExp("\\.gif$");
			icon.src = icon.src.replace(icon_re, '_collapsed.gif');
			
			dojo.style('chats_container','display','none');
			if (this.notifs_display > 1)
			{
				dojo.style('schats_container','display','none');
				dojo.style('chatsmaincontainer','display','none');
			}
			dojo.style('collapseobj_mgc_cb_evo_online','display','');
		}
	   	else
	   	{
	   		clearTimeout(whoisonlineTimeout);
	   		
			icon_re = new RegExp("_collapsed\\.gif$");
			icon.src = icon.src.replace(icon_re, '.gif');
			
			dojo.style('chats_container','display','');
			if (this.notifs_display > 1)
			{
				dojo.style('schats_container','display','');
				dojo.style('chatsmaincontainer','display','');
			}
			
			dojo.style('collapseobj_mgc_cb_evo_online','display','none');
		}		
		return false;
	}
	else
	{
		if (dojo.style('collapseobj_mgc_cb_evo_online','display') == 'none')
		{
			this.get_online_users();
			
			icon_re = new RegExp("\\.gif$");
			icon.src = icon.src.replace(icon_re, '_collapsed.gif');
			
			dojo.style('chats_container','display','none');
			if (this.notifs_display > 1)
			{
				dojo.style('schats_container','display','none');
			}
		}
	   	else
	   	{
	   		clearTimeout(whoisonlineTimeout);
	   		
			icon_re = new RegExp("_collapsed\\.gif$");
			icon.src = icon.src.replace(icon_re, '.gif');
			
			dojo.style('chats_container','display','');
			
			if (this.notifs_display > 1)
			{
				dojo.style('schats_container','display','');
			}
		}
		return toggle_collapse('mgc_cb_evo_online');
	}
};

MGCCbEvoNS.get_online_users = function()
{
	if (MGCCbEvoNS.whoisonline_active)
	{
		dojo.xhrPost({
			url : this.jsloc + 'mgc_cb_evo_ajax.php',
			handleAs : 'xml',
			preventCache : true,
			content :
			{
				securitytoken : SECURITYTOKEN,
				action : 'ajax_get_online_users'
			},
	        error: function (error) {
                console.error ('Error: ', error);
			},
			load : dojo.hitch(this,'wait_get_online_users')
		});
	}
}

MGCCbEvoNS.wait_get_online_users = function(get_online_users_ajax_xml)
{
	if(get_online_users_ajax_xml)
	{
		var online = get_online_users_ajax_xml.getElementsByTagName('online');

		var online_nb = online[0].getAttribute('nb');

      	dojo.byId('mgc_cb_evo_nb_online_users').innerHTML = online_nb;

      	if (online_nb && online[0].hasChildNodes())
      	{
        	var online_list = online[0].firstChild.nodeValue;
         	dojo.byId('mgc_cb_evo_online_users').innerHTML = '<br />' + online_list;
      	}
     	else
      	{
        	dojo.byId('mgc_cb_evo_online_users').innerHTML = " ";
      	}

      	whoisonlineTimeout = setTimeout("MGCCbEvoNS.get_online_users()", MGCCbEvoNS.whoisonline_delay);
   }
};

MGCCbEvoNS.cmd_open_ignore_popup = function()
{
   window.open(MGCCbEvoNS.bburl + "/" + MGCCbEvoNS.filename + "?" + SESSIONURL + "do=showignore" , "show ignore", "toolbar=no,scrollbars=no,resizable=yes,left=0 top=0,width=750,height=500");
};

MGCCbEvoNS.cmd_open_ban_popup = function()
{
   window.open(MGCCbEvoNS.bburl + "/" + MGCCbEvoNS.filename + "?" + SESSIONURL + "do=showban" , "showman", "toolbar=no,scrollbars=no,resizable=yes,left=0 top=0,width=425,height=500");
};
