var myData = '';
var myFile;
var myScriptUrl;
var myLastModified;
var myFilter = {
	'ttd'  : 0,
	'team' : 0
};
var myObj;


function onBeforeSend(req)
{
	if ( myLastModified )
		req.setRequestHeader("If-Modified-Since", myLastModified);
}

function onData(data)
{
	if ( myData != data )
	{
		myData = data;

		myObj = $.evalJSON(data);
		document.getElementById("spb_cmp_reel_chat_text_txta").innerHTML   = myObj['log'];
		document.getElementById("spb_cmp_reel_chat_stats_tbls").innerHTML  = myObj['stat'];
		if ( myObj['info'] )
			document.getElementById("spb_cmp_reel_chat_info_tbls").innerHTML  = myObj['info'];
		if ( myObj['stat1'] )
			document.getElementById("spb_cmp_reel_chat_stats1_tbls").innerHTML = myObj['stat1'];
		document.getElementById("spb_cmp_reel_chat_stats_score").innerHTML = myObj['score'];
		myApplyFilter();
	}
}

function onComplete(req, textStatus)
{
	if ( req.status == 200 )
	{
		myLastModified = req.getResponseHeader('Last-Modified');
		var data       = req.responseText;
		onData(data);
	}
}

function onError()
{
	setTimeout("myRequest()", 180000);
}

function myAjaxRequest()
{
	$.ajax({
		url:        myFile,
		cache:      false,
		error:      onError,
		complete:   onComplete,
		beforeSend: onBeforeSend
	});
}

function myScriptRequest()
{
	var d          = new Date();
	var tail       = d.getTime();
	var newScript  = document.createElement('script');
	newScript.type = "text/javascript";
	newScript.src  = myScriptUrl + '?' + tail;

	document.getElementById('crossdomain-holder').appendChild(newScript);
}

function myRequest()
{
	if ( document.getElementById('crossdomain-holder') )
		myScriptRequest();
	else
		myAjaxRequest();

	setTimeout("myRequest()", 180000);
}

function myClickFilter(e)
{
	if ( e.id.indexOf("labeling_fil_team_") == 0 )
	{
		var t = e.id.substr(18);
		for (var i = 0; i < 3; ++i)
			document.getElementById("labeling_filter_team_" + i).className = "";
		document.getElementById("labeling_filter_team_" + t).className = "spb_cmp_reel_chat_stats_txt_choice_s";
		myFilter.team = t;
	} else
	if ( e.id.indexOf("labeling_fil_ttd_") == 0 )
	{
		var t = e.id.substr(17);
		for (var i = 0; i < 11; ++i)
		{
			if ( document.getElementById("labeling_filter_ttd_" + i) )
				document.getElementById("labeling_filter_ttd_" + i).className = "";
		}
		document.getElementById("labeling_filter_ttd_" + t).className = "spb_cmp_reel_chat_stats_txt_choice_s";
		myFilter.ttd = t;
	}

	myApplyFilter();
}

function myApplyFilter()
{
	document.getElementById("spb_cmp_reel_chat_text_txta").innerHTML = myObj['log'];

	var filter_ttd  = myFilter.ttd;
	var filter_team = myFilter.team;

	var filterExpression = "tr.labeling_row";
	if ( filter_team * 1 )
	{
		filterExpression += ":not(.labeling_team_class_" + filter_team + ")";
		$(filterExpression).css('display', 'none');
	}

	filterExpression = "tr.labeling_row";
	if ( filter_ttd * 1 )
	{
		filterExpression += ":not(.labeling_ttd_class_" + filter_ttd + ")";
		$(filterExpression).css('display', 'none');
	}
}

