	if (MGCCbEvoNS.pm_tabs_on)
{
	dojo.require("dijit.layout.TabContainer");
	dojo.require("dijit.layout.ContentPane");
}

MGCCbEvoNS.pm_active_tab 			= 0;
MGCCbEvoNS.refreshPM_timeout		= null;
MGCCbEvoNS.pm_first_load 			= 0;
MGCCbEvoNS.already_refreshing_pms	= 0;
MGCCbEvoNS.pm_userids_array 		= new Array();
MGCCbEvoNS.pm_chatids_array 		= new Array();

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

MGCCbEvoNS.onload_pm = function()
{
	// Select tab events for the PM tabs
	if (MGCCbEvoNS.pm_tabs_on)
	{
		// Subscribe switch tab event
		dojo.subscribe('tabs_mgc_cb_evo-selectChild', function(child) { MGCCbEvoNS.pm_switch_tab(child.id);});
		
		// Trick height bug by opening and immediately closing a tab
		var tabs = dijit.byId('tabs_mgc_cb_evo');
		var tab_pane = new dijit.layout.ContentPane({
			title: '',
			id: 'tab_mgc_cb_evo_0',
			closable: true,
			content : ''
		});
		tabs.addChild(tab_pane);
		tabs.selectChild(tab_pane);	
		tabs.selectChild(dijit.byId('tab_mgc_cb_evo'));
		tabs.removeChild(tab_pane);
	}
};

MGCCbEvoNS.pm_switch_tab = function(tab_id)
{
	this.force_scrolling = 1;
	
	if (tab_id == 'tab_mgc_cb_evo')
	{
		// Back to chatbox tab
		clearTimeout(this.refreshPM_timeout);
		this.pm_active_tab = 0;
		this.chatbox_refresh('forced');
	}
	else
	{
		// Back to a PM tab
		clearTimeout(this.refreshPM_timeout);
		MGCCbEvoNS.pm_active_tab = tab_id.split('tab_mgc_cb_evo_')[1];
		MGCCbEvoNS.chatbox_pm_refresh();
	}
};

MGCCbEvoNS.open_pm_tab = function(tabuserid,tabusername)
{
	// Check if tab doesn't already exist
	if (dojo.byId('tab_mgc_cb_evo_' + tabuserid))
	{
		var tabs = dijit.byId('tabs_mgc_cb_evo');
		var seeked_tab = dijit.byId('tab_mgc_cb_evo_' + tabuserid);
		tabs.selectChild(seeked_tab);
	}
	else
	{
		// Check if maximum number of tabs is not reached
		if (this.pm_maxtab)
		{
			// Number of pm tabs retrieval
			var numberofpmtabs = dojo.query('div[id^="tab_mgc_cb_evo_"]').length;
			
			if (numberofpmtabs >= this.pm_maxtab)
			{
				this.show_dialog(phrase_pm_maxtabs_reached,'');
				return false;
			}
		}

		// Clear refresh timeouts
		clearTimeout(this.refresh_timeout);
		clearTimeout(this.refreshPM_timeout);

		// Set created tab as current PM tab
		this.pm_active_tab = tabuserid;
		
		// Prepare the chats retrieval flags
		this.pm_chatids_array[this.pm_active_tab] = 0;		
		this.pm_first_load = 1;
	
		// Instance of the tabs container
		var tabs = dijit.byId('tabs_mgc_cb_evo');	

		// Create the new tab
		var tab_content = '\
			<div style="width:100%; height: ' + this.height + 'px;overflow-y: auto;border: none; z-index: 1;float: left;" id="pm_container_' + tabuserid + '"> \
				<table width="95%" align="' + this.left + '" cellspacing="0"> \
					<tbody id="pm_chats_' + tabuserid + '"> \
					</tbody> \
				</table> \
			</div>';
			
		var tab_pane = new dijit.layout.ContentPane({
			title: tabusername,
			id: 'tab_mgc_cb_evo_' + tabuserid,
			closable: true,
			content : tab_content
		});

		// Add and select the new tab to trigger pm retrieval and display
		tabs.addChild(tab_pane);
		tabs.selectChild(tab_pane);
	}
	return false;
};

MGCCbEvoNS.chatbox_pm_refresh = function()
{
	var numberofpmtabs = dojo.query('div[id^="tab_mgc_cb_evo_"]').length;
	
	if (!this.already_sending && !this.already_refreshing_pms && (numberofpmtabs > 1))
	{
		this.already_refreshing_pms = 1;
	
		dojo.style('mgc_cb_evo_refresh_img','visibility','visible');
		dojo.xhrPost({
			url : this.jsloc + 'mgc_cb_evo_ajax.php',
			handleAs : 'xml',
			preventCache : true,
			content :
			{
				securitytoken : SECURITYTOKEN,
				action : 'ajax_get_pms',
				lastchatid : this.pm_chatids_array[this.pm_active_tab],
				tabuserid : this.pm_active_tab
			},
	        error: function (error) {
                console.error ('Error: ', error);
			},
			load : dojo.hitch(this,'wait_chatbox_pm_refresh')
		});
	}
};

MGCCbEvoNS.wait_chatbox_pm_refresh = function(pm_xml)
{
	if(pm_xml)
	{
		var pmchats = pm_xml.getElementsByTagName('chat');
		var chatid_saved = 0;
		var playsound = 0;
		
		if (pmchats.length)
		{
			var chatrefreshel = dojo.byId('pm_chats_' + this.pm_active_tab);

			if (chatrefreshel.hasChildNodes())
			{
				var firstchild 	= chatrefreshel.firstChild;
			}

			for (i=0;i<pmchats.length;i++)
			{
				var newchat 	= this.create_pm_chatrow(pmchats[i]);
				var edited_chat = pmchats[i].getElementsByTagName('edited')[0].firstChild.nodeValue;
					
				if (chatid_saved == 0)
				{	
					chatid_saved =1;
					var chatid	= pmchats[i].getElementsByTagName('chatid')[0].firstChild.nodeValue;
					this.pm_chatids_array[this.pm_active_tab] = chatid;
				}
				
				/* Edited chat */
				if (edited_chat == 1)
				{
					chatrefreshel.replaceChild(newchat,dojo.byId('pm_chatrow_' + chatid));
				}
				/* Newer at top */
				else if (this.chats_order == 0)
				{
					/* Add chat to the chatbox */
					if (this.pm_first_load || !chatrefreshel.hasChildNodes())
					{
						chatrefreshel.appendChild(newchat);
					}
					else
					{
						chatrefreshel.insertBefore(newchat,firstchild);
					}
				}
				/* Older at top*/
				else
				{
					/* First load => always prepend and check if first sticky chat received for recording */
					if (this.pm_first_load == 1)
					{
						if (chatrefreshel.hasChildNodes())
						{
							chatrefreshel.insertBefore(newchat,chatrefreshel.firstChild);
						}
						else
						{
							chatrefreshel.appendChild(newchat);
						}
					}
					else
					{
						if (chatrefreshel.hasChildNodes())
						{
							chatrefreshel.insertBefore(newchat,chatrefreshel.lastChild);
						}
						else
						{
							chatrefreshel.appendChild(newchat);
						}
					}
				}			
			}		
		
			/* Scroll ? */
			if (this.chats_order)
			{
				dojo.byId('pm_container_' + this.pm_active_tab).scrollTop = dojo.byId('pm_chats_' + this.pm_active_tab).offsetHeight;
			}
		}
		else if ((this.force_scrolling) && (this.pm_active_tab != null))
		{
			dojo.byId('pm_container_' + this.pm_active_tab).scrollTop = dojo.byId('pm_chats_' + this.pm_active_tab).offsetHeight;
		}

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

		/* Update row alternate if requested */
      	if (this.alt_chat_colors)
      	{
			this.update_row_color_alt_pm('pm_chats_' + this.pm_active_tab);
		}

		/* At last play sound if needed and active */
		if (this.sound && playsound && !this.pm_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.pm_first_load = 0;
		this.force_scrolling = 0;		
		this.already_refreshing_pms = 0;

		/* Relaunch timeout if active */
		if (this.autorefresh_on)
		{
			this.refreshPM_timeout = setTimeout("MGCCbEvoNS.chatbox_pm_refresh()", this.pm_refresh_delay);
		}
	}
};

MGCCbEvoNS.create_pm_chatrow = function(xmlnode)
{
	var chatid	= xmlnode.getElementsByTagName('chatid')[0].firstChild.nodeValue;

	/* Creation of the row */
	var nchatrow = document.createElement('tr');
	nchatrow.setAttribute('id','pm_chatrow_' + chatid);

	/* Creation of the avatar column if any */
	if (this.avatar_on)
	{
		dojo.create(
			"td",
			{
				width: "0",
				valign: this.chat_valign,
				innerHTML: xmlnode.getElementsByTagName('col_avatar')[0].firstChild.nodeValue
			},
			nchatrow
		);
	}

	/* Manage the username/date/chat columns depending of the wished alignment */
	if (this.align_chats)
	{
		/* Creation of the date column */
		dojo.create(
			"td",
			{
				width: "0",
				align: this.left,
				valign: this.chat_valign,
				noWrap: "nowrap",
				innerHTML: xmlnode.getElementsByTagName('col_date')[0].firstChild.nodeValue
			},
			nchatrow
		);

		/* Creation of the username column */
		dojo.create(
			"td",
			{
				width: '0',
				align: this.left,
				valign: this.chat_valign,
				noWrap: "nowrap",
				innerHTML: xmlnode.getElementsByTagName('col_uname')[0].firstChild.nodeValue
			},
			nchatrow
		);
		
		/* Creation of the chat column */
		dojo.create(
			"td",
			{
				width: "100%",
				align: this.left,
				valign: this.chat_valign,
				innerHTML: xmlnode.getElementsByTagName('col_chat')[0].firstChild.nodeValue
			},
			nchatrow
		);
	}
	else
	{
		/* Creation of the date column */
		dojo.create(
			"td",
			{
				width: "0",
				align: this.left,
				valign: this.chat_valign,
				noWrap: "nowrap",
				style: {
					whiteSpace: "nowrap"
				},
				innerHTML: xmlnode.getElementsByTagName('col_date')[0].firstChild.nodeValue
			},
			nchatrow
		);
		
		/* Creation of the username + chat column */
		dojo.create(
			"td",
			{
				width: "100%",
				align: this.left,
				valign: this.chat_valign,
				innerHTML: xmlnode.getElementsByTagName('col_uname')[0].firstChild.nodeValue + xmlnode.getElementsByTagName('col_chat')[0].firstChild.nodeValue
			},
			nchatrow
		);
	}
	return nchatrow;
};

MGCCbEvoNS.update_row_color_alt_pm = function(pm_table_container)
{	
	var chatbits = dojo.byId(pm_table_container);

	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";
			}
		}
	}
};

