// JavaScript Document
var canChangeScoreType = false;		// Is the user allowed to select the score type they want?
var scoreType = "overall";			// Which score type is currently selected?
var puzzleid = 0;					// The id of the current puzzle
var totalScores = 10;				// Total number of scores to display

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
var http = createRequestObject();

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
		
		var tmpArray = response.split("|");
		
		//Display the average time
		//document.getElementById("averageTime").innerHTML = tmpArray[0];
		
        //Create the high score table
        drawSolveTable(tmpArray[1]);
    }
}

//Sets the id of the current puzzle
function setPuzzleId(id)
{
	puzzleid = id;	
}

//Asks the server for the the solve times table
function getSolveTable(id)
{
	canChangeScoreType = true;
	
	if(id==undefined || id==null || id=="")
		id = puzzleid; 
	else
		puzzleid = id;
		
	
	refreshScoreTabs();
	
	createTableMessage("Loading fastest solve times...");
    http.open('get', 'scripts/ajax/getSolveTable.php?id='+puzzleid+"&rand="+Math.floor(Math.random()*50000)+"&type="+scoreType);
    http.onreadystatechange = handleResponse;
    http.send(null);
	
}


//Creates the score table populared with the highest scores
function drawSolveTable(scoreArray)
{
	scoreArray = scoreArray.split(",");

	if(scoreArray[0]=="")
		createTableMessage("There are no scores listed for this time period.");		
	else
	{
		
		var htmlText = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"296px\" align=\"center\">";
			
		var num = 1;
		var barClass = "LightBand";
		
		for(var i=0; i<totalScores * 2; i+=2)
		{	
			var color = "white";
			if(barClass == "DarkBand")
			{
				color = "lblue";	
			}
		
			htmlText += "<tr class=\""+barClass+"\">";	
			htmlText += "<td width=\"21%\" align=\"center\">"+num+".</td>";
			htmlText += "<td width=\"50%\"><img src=\"scripts/php/emoticon.php?user="+scoreArray[i]+"\" class=\"emoticon\">"+scoreArray[i]+"</td>";
			htmlText += "<td width=\"29%\">"+scoreArray[i+1]+"</td>";
			htmlText += "</tr>";
			num++;
			
			if(barClass=="LightBand")
				barClass = "DarkBand";
			else
				barClass = "LightBand";
		}
				
		htmlText += "</table>";
		htmlText += "<img src=\"images/spacer.gif\" width=\"1\" height=\"8\" />";
		
		document.getElementById("solveTable").innerHTML = htmlText;	
	}
}

//Creates the score table displaying a message instead of scores
function createTableMessage(message)
{
	htmlText = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"296px\" align=\"center\">";
			
	htmlText += "<tr class=\"LightBand\">";
	htmlText += "<td align=\"center\">&nbsp;</td>";
	htmlText += "</tr>";

	htmlText += "<tr class=\"DarkBand\">";
	htmlText += "<td align=\"center\">"+message+"</td>";
	htmlText += "</tr>";
	
	for(var i=0; i <= (totalScores / 2) - 2; i++)
	{
		htmlText += "<tr class=\"LightBand\">";
		htmlText += "<td align=\"center\">&nbsp;</td>";
		htmlText += "</tr>";
		
		htmlText += "<tr class=\"DarkBand\">";
		htmlText += "<td align=\"center\">&nbsp;</td>";
		htmlText += "</tr>";
	}
					
	htmlText += "</table>";
	htmlText += "<img src=\"images/spacer.gif\" width=\"1\" height=\"8\" />";
	
	document.getElementById("solveTable").innerHTML = htmlText;	
}


function changeScoreType(type)
{	
	if(canChangeScoreType==true)
	{	
		scoreType = type;
		refreshScoreTabs();
		getSolveTable();
	}
}

//Changes the score display
//type:String = daily, weekly, monthly, overall
function refreshScoreTabs()
{	
	//Clear all the tabs
	document.getElementById("daily_tab").src = "images/mods/top_players/daily_off.jpg";
	document.getElementById("weekly_tab").src = "images/mods/top_players/weekly_off.jpg";
	document.getElementById("monthly_tab").src = "images/mods/top_players/monthly_off.jpg";
	document.getElementById("overall_tab").src = "images/mods/top_players/overall_off.jpg";
		
	document.getElementById(scoreType+"_tab").src = "images/mods/top_players/"+scoreType+"_on.jpg";
}


function scoreTypeClicked(type)
{
	if(canChangeScoreType)
	{
		setScoreType(type);
		getScoreTable();
	}
}


//Preload the top players tab images
var preloadArray = new Array("images/mods/top_players/daily_on.jpg","images/mods/top_players/daily_off.jpg","images/mods/top_players/weekly_on.jpg","images/mods/top_players/weekly_off.jpg","images/mods/top_players/monthly_on.jpg","images/mods/top_players/monthly_off.jpg","images/mods/top_players/overall_on.jpg","images/mods/top_players/overall_off.jpg");

var imageArray = new Array();
for(var i=0; i<preloadArray.length; i++)
{
		imageArray[i] = new Image(70,20);
		imageArray[i].src = preloadArray[i];
}



