function get_base_page()
{
	text = '<p>Здесь вы можете посмотреть вашу общую статистику во всех наших играх. Введите ваш никнейм в поле ниже, и получите полную статистику.</p>';
	text += '<input id="usernameTextInput" type="text" />';
	text += '<button onclick="get_stat()"><img src="../img/user_art.png" width="30" height="30"/></button>';
	$('dataDiv').innerHTML = text;
}

function get_base_page_en()
{
	text = '<p>Here you can see your statistics all over our games. Put in your nickname in the field below and you\'ll get all data.</p>';
	text += '<input id="usernameTextInput" type="text" />';
	text += '<button onclick="get_stat_en()"><img src="../img/user_art.png" width="30" height="30"/></button>';
	$('dataDiv').innerHTML = text;
}

function get_full_stat()
{
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);

		text = "Сводная статистика:<br>";
		text += "Всего игроков играло в наши игры: "+data.count_all;
		text += "<br>Всего игроков играло больше чем в одну нашу игру: "+data.count_mult;
		
		
		$('all_stat').innerHTML = text;
		 
	 }
	 req.open("GET", "table_admin.php?action=fullstat", true);
	 req.send(null);
}

function get_stat_santa()
{

	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "Unique iphones:"+data.count+"<br><table style=\"margin-top:5px; font-size:14px; font-family:Verdana;\" border=1 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			text +="<tr  style=\"height:19px;\"><td style=\"padding-left:18px; width:210px; text-align:left;\">"+jsonData[i].playername+"</td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td></tr>";
		}
		text +="</table><br>";
		
		
		$('res').innerHTML = text;
		 
	 }
	 req.open("GET", "santa_model.php?gamename=santavsz&offset=0&limit=10000&from=admin", true);
	 req.send(null);

}

function get_stat_boker()
{

	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "Unique iphones:"+data.count+"<br><table style=\"margin-top:5px; font-size:14px; font-family:Verdana;\" border=1 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			text +="<tr  style=\"height:19px;\"><td style=\"padding-left:18px; width:210px; text-align:left;\">"+jsonData[i].playername+"</td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td><td>"+jsonData[i].device_model+"&nbsp;&nbsp;</td><td>&nbsp;&nbsp;"+jsonData[i].device_os+"</td></tr>";
		}
		text +="</table><br>";
		
		
		$('res').innerHTML = text;
		 
	 }
	 req.open("GET", "table_model.php?gamename=ibocker_1.0&offset=0&limit=10000&from=admin", true);
	 req.send(null);

}

function get_stat_dream()
{

	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "Unique iphones:"+data.count+"<br><table style=\"margin-top:5px; font-size:14px; font-family:Verdana;\" border=1 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			text +="<tr  style=\"height:19px;\"><td style=\"padding-left:18px; width:210px; text-align:left;\">"+jsonData[i].playername+"</td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td><td>"+jsonData[i].device_model+"&nbsp;&nbsp;</td><td>&nbsp;&nbsp;"+jsonData[i].device_os+"</td></tr>";
		}
		text +="</table><br>";
		
		
		$('res').innerHTML = text;
		 
	 }
	 req.open("GET", "dreamland_model.php?gamename=dreamland_1.0&offset=0&limit=10000&from=admin", true);
	 req.send(null);

}



function get_stat_st()
{
	
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "Unique iphones:"+data.count+"<br><table style=\"margin-top:5px; font-size:14px; font-family:Verdana;\" border=1 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			text +="<tr  style=\"height:19px;\"><td style=\"padding-left:18px; width:210px; text-align:left;\">"+jsonData[i].playername+"</td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td></tr>";
		}
		text +="</table><br>";
		
		
		$('res').innerHTML = text;
		 
	 }
	 req.open("GET", "table_admin.php?action=st", true);
	 req.send(null);
}

function get_stat_st_free()
{
	
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "Unique iphones:"+data.count+"<br><table style=\"margin-top:5px; font-size:14px; font-family:Verdana;\" border=1 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			text +="<tr  style=\"height:19px;\"><td style=\"padding-left:18px; width:210px; text-align:left;\">"+jsonData[i].playername+"</td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td></tr>";
		}
		text +="</table><br>";
		
		
		$('res').innerHTML = text;
		 
	 }
	 req.open("GET", "table_admin.php?action=stfree", true);
	 req.send(null);
}

function get_stat_gk()
{
	var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "Unique iphones:"+data.count+"<br><table style=\"margin-top:5px; font-size:14px; font-family:Verdana;\" border=1 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			text +="<tr  style=\"height:19px;\"><td style=\"padding-left:18px; width:210px; text-align:left;\">"+jsonData[i].playername+"</td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td></tr>";
		}
		text +="</table><br>";
		
		
		$('res').innerHTML = text;
		 
	 }
	 req.open("GET", "table_admin.php?action=gk", true);
	 req.send(null);
}

function get_stat_boker_lite()
{
	
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "Unique iphones:"+data.count+"<br><table style=\"margin-top:5px; font-size:14px; font-family:Verdana;\" border=1 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			text +="<tr  style=\"height:19px;\"><td style=\"padding-left:18px; width:210px; text-align:left;\">"+jsonData[i].playername+"</td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td><td>"+jsonData[i].device_model+"&nbsp;&nbsp;</td><td>&nbsp;&nbsp;"+jsonData[i].device_os+"</td></tr>";
		}
		text +="</table><br>";
		
		
		$('res').innerHTML = text;
		 
	 }
	 req.open("GET", "table_model.php?gamename=ibocker_lite_1.0&offset=0&limit=10000&from=admin", true);
	 req.send(null);

}

function get_stat(playername)
{
	if (playername == null)                                                                                                        
	{
	 	playername = $('usernameTextInput').value;
	}
	 postData = 'playername=' + playername;
 // Передача данных
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	 {
		 if (req.readyState != 4) return;
		 data = JSON.parse(req.responseText);
		 text = "<h3>Статистика игрока "+playername+":</h3>";
		 for (i=0;i<data.length;i++)
		 {
		 	text += '<h4 style="margin-left:10px;">'+data[i].gamename+"</h4>";
		 	if (data[i].stat == null)
		 	{
		 		text += '<p style="margin-left:20px;">Данных нет. Вы еще не играли?</p>';
		 	}
		 	else
		 	{
		 		text += '<p style="margin-left:20px;">Всего рекордов: '+data[i].stat.cnt+"<br>Максимальный результат: "+data[i].stat.score;
		 		text += '<br><span style="cursor:pointer; font-size:x-small;" onclick="get_all_stat(\''+data[i].gamename+'\',\''+playername+'\');">посмотреть детальную статистику...</span></p>'
		 	}
		 }
		 $('dataDiv').innerHTML = text;
		 RefreshMenuButtonPressed('user');
	 }
 //асинхронный POST-метод
	 req.open("POST", "../lib/ajax/stat_loader.php", true);
	 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 req.setRequestHeader("Content-Length", postData.length);
 	req.send(postData);
}

function get_stat_en(playername)
{
	if (playername == null)
	{
	 	playername = $('usernameTextInput').value;
	}
	 postData = 'playername=' + playername;
 // Передача данных
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	 {
		 if (req.readyState != 4) return;
		 data = JSON.parse(req.responseText);
		 text = "<h3>Player "+playername+":</h3>";
		 for (i=0;i<data.length;i++)
		 {
		 	text += '<h4 style="margin-left:10px;">'+data[i].gamename+"</h4>";
		 	if (data[i].stat == null)
		 	{
		 		text += '<p style="margin-left:20px;">Our search-machine couldn\'t find any data. Maybe you donna play yet?</p>';
		 	}
		 	else
		 	{
		 		text += '<p style="margin-left:20px;">Score\'s count: '+data[i].stat.cnt+"<br>Max record: "+data[i].stat.score;
		 		text += '<br><span style="cursor:pointer; font-size:x-small;" onclick="get_all_stat_en(\''+data[i].gamename+'\',\''+playername+'\');">details...</span></p>'
		 	}
		 }
		 $('dataDiv').innerHTML = text;
		 RefreshMenuButtonPressed('user');
	 }
 //асинхронный POST-метод
	 req.open("POST", "../lib/ajax/stat_loader.php", true);
	 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 req.setRequestHeader("Content-Length", postData.length);
 	req.send(postData);
}

function get_all_stat(gamename,playername)
{
		 postData = 'playername=' + playername + '&gamename=' + gamename;
 // Передача данных
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	 {
		 if (req.readyState != 4) return;
		 data = JSON.parse(req.responseText);
		 
		 
		 text = "<h3>Detailed statistic</h3>";
		 text += '<h4 style="margin-left:10px;">Игрок '+data.playername+' <span style="cursor:pointer; font-size:x-small; font-weight:normal; text-decoration:underline;" onclick="get_base_page()">другой игрок?</span></h4>';
		 text += '<h4 style="margin-left:10px;">Игра '+data.gamename+' <span style="cursor:pointer; font-size:x-small; font-weight:normal; text-decoration:underline;" onclick="get_stat(\''+playername+'\')">другая игра?</span></h4>';
		 if (data.stat == null)
		 	{
		 		text += '<p style="margin-left:20px;">Данных нет. Вы еще не играли?</p>';
		 	}
		 else
		 {
		 	text += '<table border=0 cellpaddin=5 cellspacing=0 width=400><tr><td width=100 style="background-color:#c0c0c0; text-align:center;">Позиция</td><td width=300 style="background-color:#c0c0c0; text-align:right;">Результат</td></tr>';
		 	for (i=0;i<data.stat.length;i++)
		 	{
		 		if (i%2 == 0)
		 		{
		 			text += '<tr><td style="background-color:#ccccff; text-align:center;">'+data.stat[i].pos+'</td><td style="background-color:#ccccff; text-align:right;">'+data.stat[i].score+'</td></tr>';
		 		}
		 		else
		 		{
		 			text += '<tr><td style="background-color:#c0c0c0; text-align:center;">'+data.stat[i].pos+'</td><td style="background-color:#c0c0c0; text-align:right;">'+data.stat[i].score+'</td></tr>';
		 		}
		 	}
		 	text += '</table>';
		 }
		 
		 $('dataDiv').innerHTML = text;
		 RefreshMenuButtonPressed('user');
	 }
 //асинхронный POST-метод
	 req.open("POST", "../lib/ajax/stat_loader.php", true);
	 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 req.setRequestHeader("Content-Length", postData.length);
 	req.send(postData);
}

function get_all_stat_en(gamename,playername)
{
		 postData = 'playername=' + playername + '&gamename=' + gamename;
 // Передача данных
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	 {
		 if (req.readyState != 4) return;
		 data = JSON.parse(req.responseText);
		 
		 
		 text = "<h3>Detailed statistic</h3>";
		 text += '<h4 style="margin-left:10px;">Player '+data.playername+' <span style="cursor:pointer; font-size:x-small; font-weight:normal; text-decoration:underline;" onclick="get_base_page_en()">another player?</span></h4>';
		 text += '<h4 style="margin-left:10px;">Game '+data.gamename+' <span style="cursor:pointer; font-size:x-small; font-weight:normal; text-decoration:underline;" onclick="get_stat_en(\''+playername+'\')">another game?</span></h4>';
		 if (data.stat == null)
		 	{
		 		text += '<p style="margin-left:20px;">Cannot find any data. Don\'t play yet?</p>';
		 	}
		 else
		 {
		 	text += '<table border=0 cellpaddin=5 cellspacing=0 width=400><tr><td width=100 style="background-color:#c0c0c0; text-align:center;">Position</td><td width=300 style="background-color:#c0c0c0; text-align:right;">Score</td></tr>';
		 	for (i=0;i<data.stat.length;i++)
		 	{
		 		if (i%2 == 0)
		 		{
		 			text += '<tr><td style="background-color:#ccccff; text-align:center;">'+data.stat[i].pos+'</td><td style="background-color:#ccccff; text-align:right;">'+data.stat[i].score+'</td></tr>';
		 		}
		 		else
		 		{
		 			text += '<tr><td style="background-color:#c0c0c0; text-align:center;">'+data.stat[i].pos+'</td><td style="background-color:#c0c0c0; text-align:right;">'+data.stat[i].score+'</td></tr>';
		 		}
		 	}
		 	text += '</table>';
		 }
		 
		 $('dataDiv').innerHTML = text;
		 RefreshMenuButtonPressed('user');
	 }
 //асинхронный POST-метод
	 req.open("POST", "../lib/ajax/stat_loader.php", true);
	 req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 req.setRequestHeader("Content-Length", postData.length);
 	req.send(postData);
}

function get_results_santa()
{
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "<table style=\"margin-top:5px; font-size:14px; font-family:Verdana;\" border=0 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			j = i+1;
			text +="<tr style=\"height:19px;\"><td style=\"width:30px; text-align:right;\">"+j+"</td><td style=\"padding-left:18px; width:210px; text-align:left;\"><span>"+jsonData[i].playername+"</span></td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td></tr>";
		}
		text +="</table>";
		
		
		$('boker_score_table').innerHTML = text;
		$('boker_best_player').innerHTML = jsonData[0].playername;
		$('boker_best_score').innerHTML = jsonData[0].score;
		 
	 }
	 req.open("GET", "../santa_model.php?gamename=santavsz&offset=0&limit=16&from=site", true);
	 req.send(null);
}

function get_results_dream()
{
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "<table style=\"margin-top:5px; font-size:14px; font-family:Verdana;\" border=0 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			j = i+1;
			text +="<tr style=\"height:19px;\"><td style=\"width:30px; text-align:right;\">"+j+"</td><td style=\"padding-left:18px; width:210px; text-align:left;\"><span>"+jsonData[i].playername+"</span></td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td></tr>";
		}
		text +="</table>";
		
		
		$('boker_score_table').innerHTML = text;
		$('boker_best_player').innerHTML = data.topplayer;
		$('boker_best_score').innerHTML = data.topscore;
		 
	 }
	 req.open("GET", "../dreamland_model.php?gamename=dreamland_1.0&offset=0&limit=16&from=site", true);
	 req.send(null);
}

function get_results_boker()
{
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "<table style=\"margin-top:5px; color: #ffffff; font-size:14px; font-family:Verdana;\" border=0 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			j = i+1;
			text +="<tr style=\"height:19px;\"><td style=\"width:30px; text-align:right;\">"+j+"</td><td style=\"padding-left:18px; width:210px; text-align:left;\"><span  onmouseover=\"getHint(event,'"+jsonData[i].id+"')\" onmouseout=\"freeHint()\">"+jsonData[i].playername+"</span></td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td></tr>";
		}
		text +="</table>";
		
		
		$('boker_score_table').innerHTML = text;
		$('boker_best_player').innerHTML = data.topplayer;
		$('boker_best_score').innerHTML = data.topscore;
		 
	 }
	 req.open("GET", "../table_model.php?gamename=ibocker_1.0&offset=0&limit=16&from=site", true);
	 req.send(null);
}

function get_result_boker_admin()
{
	{
	 var req = getXmlHttpRequest();
	 req.onreadystatechange = function()
	{
		 if (req.readyState != 4) return;
		data = JSON.parse(req.responseText);
		jsonData = data.scores;
		
		
		text = "Unique iphones:"+data.count+"<br><table style=\"margin-top:5px; font-size:14px; font-family:Verdana;\" border=0 cellpadding=0 cellspacing=0>";
		for (i=0;i<jsonData.length;i++)
		{
			j = i+1;
			text +="<tr  style=\"height:19px;\"><td style=\"width:30px; text-align:right;\">"+j+"</td><td style=\"padding-left:18px; width:210px; text-align:left;\">"+jsonData[i].playername+"</td><td style=\"width:110px; text-align:left;\">"+jsonData[i].score+"</td><td>"+jsonData[i].device_model+"&nbsp;&nbsp;</td><td>&nbsp;&nbsp;"+jsonData[i].device_os+"</td></tr>";
		}
		text +="</table><br>";
		
		
		$('result').innerHTML = text;
		 
	 }
	 req.open("GET", "table_model.php?gamename=ibocker_1.0&offset=0&limit=10000&from=admin", true);
	 req.send(null);
}
}


function getXmlHttpRequest()
{
 if (window.XMLHttpRequest)
 {
 try
 {
 return new XMLHttpRequest();
 }
 catch (e){}
 }
 else if (window.ActiveXObject)
 {
 try
 {
 return new ActiveXObject('Msxml2.XMLHTTP');
 } catch (e){}
 try
 {
 return new ActiveXObject('Microsoft.XMLHTTP');
 }
 catch (e){}
 }
 return null;
}