var xmlDoc = null;
var scoreDIV = "scorelistbox";

function getScores(){
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }else{// code for IE6, IE5
	 	 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.open("GET","global_scores/listScoreXML.php?version=1&app=fluxon&ver=1",false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML;
}

function parseScoreArrayXML(xml, mode){
	var nodes = xml.childNodes;
	var str="";
	for (var i=0;i<nodes.length;i++){ 
		var c = nodes[i];
		if (c.nodeType==1){
			str += "<tr id=\"scoreitem\"><td>" + c.getAttribute("owner") + "</td><td>" + c.getAttribute("value") + "</td>";
			//<td>c.getAttribute("date") + "</td>";
			str+="</tr>";
		 }
	}
	return str;
}

function showScores(mode){
	
	var dst = document.getElementById(scoreDIV);
	var str = "";
	
	if(!xmlDoc)
		getScores();
		
	if(xmlDoc){
		var nodes = xmlDoc.documentElement.childNodes;
		str += "<table border=\"0\" cellspacing=\"1\" width=\"100%\">";
		for (var i=0;i<nodes.length;i++){ 
			var c = nodes[i];
			if (c.nodeType==1){
				if(c.nodeName=="array"){
					var cmode=parseInt(c.getAttribute("mode") );
					if(cmode==mode){
						str += parseScoreArrayXML(c);
						break;
					}
				}
			 }
		}
		str += "</table>";
		dst.innerHTML=str;
	}
}


function resetIcons(){
	var ids=["easy_icn", "normal_icn","hard_icn"];
	for(var i=0; i<iconButtons.length; i++){
		if(iconButtons[i]){
			iconButtons[i].setAttribute("id", ids[i]);
		}
	}
}
	
function iconClick(e){
	var node =  e.target;
	
	if(node.nodeName=='IMG'){
		node = node.parentNode;
	}
	var id = node.getAttribute("id");
	
	resetIcons();
	
	if(node==iconButtons[0]){
		showScores(0);
		iconButtons[0].setAttribute("id", "easy_icn_high");
	}else if(node==iconButtons[1]){
		showScores(1);
		iconButtons[1].setAttribute("id", "normal_icn_high");
	}else if(node==iconButtons[2]){
		showScores(2);
		iconButtons[2].setAttribute("id", "hard_icn_high");
	}
}

function linkIcons(){

	var ids=["easy_icn", "normal_icn_high","hard_icn"];
	iconButtons = new Array();
	
	for(var i=0; i<ids.length; i++){
		iconButtons[i] = document.getElementById(ids[i]);
		if(iconButtons[i]){
			iconButtons[i].addEventListener("click", iconClick, false);
		}
	}
}

function checkDocReady(){
	//alert("checkDocReady");
	var dst = document.getElementById(scoreDIV);
	if(dst){
		linkIcons();
		showScores(1);
	}else{
		// set timeout to check later
		setTimeout("checkDocReady()", 200);
	}
}


/*
<table border="0" cellspacing="1" width="100%">
<tr id="scoreitem"><td>username</td><td>mode</td><td>98966573</td></tr>
<tr id="scoreitem"><td>username</td><td>mode</td><td>98966573</td></tr>
<tr id="scoreitem"><td>username</td><td>mode</td><td>98966573</td></tr>
<tr id="scoreitem"><td>username</td><td>mode</td><td>98966573</td></tr>
<tr id="scoreitem"><td>username</td><td>mode</td><td>98966573</td></tr>
<tr id="scoreitem"><td>username</td><td>mode</td><td>98966573</td></tr>
<tr id="scoreitem"><td>username</td><td>mode</td><td>98966573</td></tr>
<tr id="scoreitem"><td>username</td><td>mode</td><td>98966573</td></tr>
</table>
*/
checkDocReady();
