vBulletin.events.systemInit.subscribe(function()
{	
	ragtek_chat.init();
});

var ragtek_chat = {
	init:function()
	{
		YAHOO.util.Event.on("showchat", "click", this.send,this,true);
		this.update();
		
	},
	send:function(e)
	{
		YAHOO.util.Event.stopEvent(e);
		this.pf = new vB_Hidden_Form('ajax.php?do=showchat');
		this.pf.add_variable('ajax', 1);
		this.pf.add_variable('s', fetch_sessionhash());
		this.pf.add_variable('securitytoken', SECURITYTOKEN);
		this.pf.add_variable('do', 'showchat');
		YAHOO.util.Connect.asyncRequest("POST", 'ajax.php?do=showchat', {
			success: this.showchat,
			  failure: vBulletin_AJAX_Error_Handler,
			  timeout: vB_Default_Timeout,
		},this.pf.build_query_string());
	},
	showchat:function(a)
	{
		YAHOO.util.Dom.get('chatcontainer').innerHTML = '<iframe src="ajax.php?do=showchat" height="400px" width="100%"></iframe>';
	},
	update:function()
	{
		YAHOO.util.Connect.asyncRequest("POST", "ajax.php?do=updatechatuser", {
			success: this.showupdate,
		    failure: vBulletin_AJAX_Error_Handler,
		    timeout: vB_Default_Timeout
		},'securitytoken='+SECURITYTOKEN);
	},
	showupdate:function(a)
	{
		if (a.responseXML)
		{
			YAHOO.util.Dom.get('ragtek_ajaxchater').innerHTML = a.responseXML.getElementsByTagName('template')[0].firstChild.nodeValue;
			YAHOO.util.Dom.get('chatstatheader').innerHTML = a.responseXML.getElementsByTagName('tableheader')[0].firstChild.nodeValue;
		}
		setTimeout("ragtek_chat.update()", 90000);
	}
};