//////////////////////////////////////////////////////
/////////////////////// GAME AJAX ////////////////////
//////////////////////////////////////////////////////

var ajaxCount = 1;

function refreshLoop() {
	ajaxCount++;
	$.get(ajaxURL, {id: gamesId, m:  + ajaxCount} , refreshResult, 'json');
}

function refreshResult(data) {
	var table = document.getElementById('match_results');

	while (table.rows.length > 1) {
		table.deleteRow(1);
	}

	var c = data.history.length;
	for (var i = 0; i < c; i++) {
		var item = data.history[i];
		var row = table.insertRow(-1);
		row.insertCell(0).innerHTML = item.set;
		row.insertCell(1).innerHTML = item.hours + ':' + item.minutes;
		row.insertCell(2).innerHTML = item.pointsA + ':' + item.pointsB;
		row.insertCell(3).innerHTML = item.resA + ':' + item.resB;
	}

	if (data.live) {
		setTimeout('refreshLoop();', 5000);
	}
}

//////////////////////////////////////////////////////
/////////////////////// GAME CHAT ////////////////////
//////////////////////////////////////////////////////

function initGameChat() {
	if (gamesId > 0 && chatLive == '1') {
		timerSet(0);
	}

	if (chatLive == '2') {
		readList(0);
	}

	document.onkeyup = KeyCheck;
}


function KeyCheck(e) {
	var evt = e || window.event;
	var KeyID = evt.keyCode;
	var msgTxt = document.getElementById('msgTxt').value;

	if (msgTxt.length == 0) return;

	switch(KeyID) {
		case 13:
			readList(msgTxt);
			break; 
	}
}

function timerSet(Sid) {
	readList(Sid);
	setTimeout("timerSet('0')",5000);
}


function readChanged(d) {
	divObject = document.getElementById('chat'); 
	divObject.innerHTML = d;
	divObject.scrollTop = divObject.scrollHeight;
}

function readList(sId) {
	var gamesId = document.getElementById('gamesId').value;
	if (sId != 0) {
		document.getElementById('msgTxt').value= "";
	}
	$.post(gamesChatURL, {read: sId, gamesId: gamesId}, readChanged);
}


//////////////////////////////////////////////////////
/////////////////////// GAME LIST ////////////////////
//////////////////////////////////////////////////////

function goTour() {
	var url = gamesTourURL + document.getElementById('season').value;
	window.location=url;
}


function updateListContent(content) {
	document.getElementById('gamesSelect').innerHTML = content;
}

function getOptionList() {
	var sId = document.getElementById('season').value;
	var fId = document.getElementById('stage').value;
	var kId = document.getElementById('teams').value;
	var oId = document.getElementById('dateFrom').value;
	var dId = document.getElementById('dateTo').value;

	var url = gamesListURL;
	url += 's/'+sId;
	url += '/f/'+fId;
	url += '/k/'+kId;
	url += '/o/'+oId;
	url += '/d/'+dId;

	$.get(url, null, updateListContent);
}

function updateSeasonContent(content) {
	document.getElementById('newSeason').innerHTML = content;
}

function getTeamsList() {

	var sId = document.getElementById('season').value;
	var fId = document.getElementById('stage').value;

	var url = gamesListURL;

	url += 'season/'+sId;
	url += '/f/'+fId;

	$.get(url, null, updateSeasonContent);

}
