$(function(){
	$('#chatupdate').hide();
	if(ili()&&!/mobile|android|iphone|blackberry|symbian/i.test(navigator.userAgent)){
		var uname=$('a.mainmenu')[6].title.substring(10).replace(/ \]/,'');
		$('a.mainmenu')[5].href+='?u='+uname;
		if($.browser.msie){$('#chatupdate').css('position','absolute').css('bottom','auto').css('top','0px').css('width','100%')}
		var lastId=0;
		var timer;
		var timer2;
		getUpdate(lastId,timer,uname,timer2);
		$('#chatupdate').show();
	}
});
function getUpdate(id,t,name,t2) {
	$.post('/chatupdate', {'lastID': id, 'rand': Math.floor(Math.random()*1000000), update: 1}, function(data) {
		if(data.length>2){
			if(data!='can\'t get data'){
				var results=data.split('---');
				var rid=results[0];
				if(rid!=id){
					$('#chatupdate').show();
					var rname=results[1].replace(/:00} /,'}');
					var rtext=results[2].replace(/images\//ig,'http://www.cbchat.co.uk/images/');
					$('#chatupdate').html("<span style='color:#F39B1F'>" + rname + ":</span> " + rtext + " <a href='http://www.cbchat.co.uk?u="+name+"' target='_blank'>{enter chat}</a>");
					if(/^CoventryBikers/i.test(document.title)){document.title='[Active Chat] '+document.title}
					clearTimeout(t2)
					t2 = setTimeout(function(){$('#chatupdate').fadeOut(1000)},25000);
				}			
			} else {
				if(!/^Nothing recent in the chat/i.test($('#chatupdate').html())){
					$('#chatupdate').show();
					// var ucount=0;var us='';$($('span.gensmall')[58]).find('a').each(function(){us+=this.innerHTML.replace(/<\/?b>/ig,'')+',';ucount++}); // this gives a list of users
					$('#chatupdate').html("Nothing recent in the chat, why not start the conversation "+name+"? <a href='http://www.cbchat.co.uk?u="+name+"' target='_blank'>{enter chat}</a>");
					if(/^\[Active Chat\] /i.test(document.title)){document.title=document.title.substring(14)}
					clearTimeout(t2);
					setTimeout(function(){$('#chatupdate').fadeOut(1000)},5000);
				}
			}
			clearTimeout(t);
			t = setTimeout(function(){getUpdate(rid||id,t,name,t2);},10000);
		}
	});
}
