var SCORE_SCRIPT = 'highscores.php';
var score = 0;
var recvComplete = 0;

function get_scores() {
    recvComplete = 0;
    new Ajax.Request(SCORE_SCRIPT, {
        method: 'get',
        onComplete: show_scores,
        onFailure: show_error
    });
}

function post_score(playerNname, playerScore)
{
	recvComplete = 0;
    new Ajax.Request(SCORE_SCRIPT, {
        method: 'post',
        parameters: $H({
                        'name':  playerNname,
                        'level': '1',
                        'score': playerScore
                    }).toQueryString(),
        onComplete: swap_scores,
        onFailure: show_error
    });
}

function swap_scores(req)
{
    recvComplete = 1;
    show_scores(req);
}

//var low_high_score = 100;

function show_scores(req)
{
    var scores = $A(req.responseXML.getElementsByTagName('score'));
    var i = 1;
    var elem;
    scores.each(function(node)
	{
		//  Element.update($('place_' + i), node.getAttribute('place'));
		elem = document.getElementById('place_' + i);
		elem.innerHTML = node.getAttribute('place');

        //Element.update($('high_score_' + i), node.getAttribute('score'));
		elem = document.getElementById('high_score_' + i);
		elem.innerHTML = node.getAttribute('score');
        //Element.update($('high_name_' + i), node.getAttribute('name'));
		elem = document.getElementById('high_name_' + i);
		elem.innerHTML = node.getAttribute('name');
        $('high_row_' + i).className = node.getAttribute('age'); // 'old' or 'new'
        i++;
    });
   // low_high_score = parseInt(scores[scores.length -1].getAttribute('score'));
	recvComplete = 1;
}


function getMinMax()
{
	recvComplete = 0;
    new Ajax.Request('minmax.php', {
        method: 'get',
        onComplete: getMinMaxComoleted,
        onFailure: show_error
    });
}

function show_error(req)
{
    //$('result').value = 'error';
    recvComplete = 1;
}

var top10Min = 0;
var top10Max = 0;

function getMinMaxComoleted(req)
{
	recvComplete = 1;
    var scores = $A(req.responseXML.getElementsByTagName('score'));
    scores.each(function(node)
    {
   		top10Min = node.getAttribute('min');
   		top10Max = node.getAttribute('max');
   });
}

function showMinMax(req)
{
    var scores = $A(req.responseXML.getElementsByTagName('score'));
    scores.each(function(node)
    {
   	top10Min = node.getAttribute('min');
   	top10Max = node.getAttribute('max');
   });
//   window.alert("min: " + min + "max: " + max);
}
