var cardSmallWidths = { '0':16, '1':10, '2':16, '3':14, '4':16, '5':15, '6':15, '7':14, '8':15, '9':14  };
var divTopScores1 = "";
var divTopScores2 = "";
var topScoresCurrent = 1;
var displayTopScores = true;
var interval = null;


	
setTopScores = function(period) {
	//alert('javascript/gwap/setTopScores period: ' + period);
	if (!displayTopScores) {
		return;
	}
	window.clearInterval(interval);
	$("top_scores").innerHTML = "";
	divTopScores1 = "";
	divTopScores2 = "";
	var req = null;
	var url = null;
	if (period == "today") {
		url = "http://www.gwap.com/gwap/service?method=topScoresToday";
	} else {
		url = "http://www.gwap.com/gwap/service?method=top10ScoresAllTime";
	}
	var topScores = "";
	
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			//req = null;
		}
	} 
	// branch for IE/Windows ActiveX version
	else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {			
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				//req = null;
			}
		}
	}
	if(req != null) {
		req.open("GET", url, false);
		req.send(null);
		topScores = req.responseText;	
	}
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		} else {
			winW = window.innerWidth;
			winH = window.innerHeight; 
		}
	}	

	// generating the results
	if(topScores != null) {
		var dataEntries = topScores.split(",");
		var scr = "";
		var als = "";
		var commaCounter = 0;
		var dataHTML1 = "";
		var dataHTML2 = "";
		
		//  generating the results for top players 1-5
		for(var i = 0; i < dataEntries.length && i < 10; i++) {
			//alert(dataEntries[i]);
			scr = dataEntries[i].split("|")[1];		 
			commaCounter = 0;
			var topScorePointsString = scr + "";
			//alert(topScorePointsString);
			var scrHTMLContent = "";
			if (topScorePointsString.length <= 4) { 
				//print all the digits with commas
				for(var j = topScorePointsString.length-1; j>=0; j--) {   				
					if (commaCounter == 3) {
						scrHTMLContent = "<img src=\"http://gwap.cachefly.net/gwap-web-017/img/profile/smallCards/cardComma_small.png\" width=\"8\"  height=\"22\"/>" + scrHTMLContent;
						commaCounter = 0;
					} else {
						commaCounter++;
					}
					scrHTMLContent = "<img src=\"http://gwap.cachefly.net/gwap-web-017/img/profile/smallCards/card" + topScorePointsString.charAt(j) + "_small.png\" width = \""+cardSmallWidths[topScorePointsString.charAt(j)]+"\"  height=\"22\"/>" + scrHTMLContent;	
				}   					
			} else if (topScorePointsString.length <= 7){
				//Need to truncate using 'k' to represent thousands
				topScorePointsString = topScorePointsString.substring(0,topScorePointsString.length -3);
				scrHTMLContent = "<img src=\"http://gwap.cachefly.net/gwap-web-017/img/profile/smallCards/cardThousand_small.png\" width=\"16\"  height=\"22\"/>" + scrHTMLContent;
				for(var j = topScorePointsString.length-1; j>=0; j--) {
					scrHTMLContent = "<img src=\"http://gwap.cachefly.net/gwap-web-017/img/profile/smallCards/card" + topScorePointsString.charAt(j) + "_small.png\" width = \""+cardSmallWidths[topScorePointsString.charAt(j)]+"\"  height=\"22\"/>" + scrHTMLContent;
				}
			} else {
				//Need to truncate using 'm' to represent millions
				topScorePointsString = topScorePointsString.substring(0,topScorePointsString.length -6);
				scrHTMLContent = "<img src=\"http://gwap.cachefly.net/gwap-web-017/img/profile/smallCards/cardMillion_small.png\" width=\"16\"  height=\"22\"/>" + scrHTMLContent;
				for(var j = topScorePointsString.length-1; j>=0; j--) {
					scrHTMLContent = "<img src=\"http://gwap.cachefly.net/gwap-web-017/img/profile/smallCards/card" + topScorePointsString.charAt(j) + "_small.png\" width = \""+cardSmallWidths[topScorePointsString.charAt(j)]+"\"  height=\"22\"/>" + scrHTMLContent;
				}
			}	   	
		
			als = dataEntries[i].split("|")[0];
			pID = dataEntries[i].split("|")[2];
			link = "<a href='http://www.gwap.com/gwap/profile/?id=" + pID + "'>" + als + "</a>";
			if(als.length >= 15) {
				als = als.substr(0,12) + "...";
			}
			// generating the sub tables to display the scores
			if (i < 5) {
				divTopScores1 += "" +
					"<td>" +
						"<div class='top_score_cell'>" + 
						"<table border=0>" +
						"<tr>" +
							"<th rowspan=2 valign=\"top\"><div class=\"top_score_rank\">" + (i+1) + "</div></th>" +
							"<td><div class=\"top_score_alias\">" + link + "</div></td>" +
						"</tr>" +
						"<tr>" +
							"<td><div class=\"top_score_alias\">" + scrHTMLContent + "</div></td>" +
						"</tr>" +
						"</table>" +
						"</div>" + 
					"</td>";
			} else {
				divTopScores2 += 
					"<td>" +
					"<div class='top_score_cell'>" + 
						"<table border=0>" +
						"<tr>" +
							"<th rowspan=2 valign=\"top\"><div class=\"top_score_rank\">" + (i+1) + "</div></th>" + 
							"<td><div class=\"top_score_alias\">" + link + "</div></td>" +
						"</tr>" +
						"<tr>" +
							"<td><div class=\"top_score_alias\">" + scrHTMLContent + "</div></td>" +
						"</tr>" +
						"</table>" +
					"</div>" +
					"</td>";
			}

		}	
		periodImage = null;
		if (period == "today") {
			periodImage = "<div style=\"margin-top:12px;\"><a href='#' onclick=javascript:setTopScores('all')><img src='http://gwap.cachefly.net/gwap-web-017/img/footer/todaysOn.png' width='103px' height='15px'/></a></div>"
		} else {
			periodImage = "<div style=\"margin-top:12px;\"><a href='#' onclick=javascript:setTopScores('today')><img src='http://gwap.cachefly.net/gwap-web-017/img/footer/allTimeOn.png' width='103px' height='15px'/></a></div>"
		}
		dataHTML1 = "" +
			"<div id='divTopScores1' class=\"div_top_scores\" style=\"z-index:101\">" +
				"<table border=0>" +
				"<tr>" +
					"<td width='92px'>" + periodImage +
					"</td>" +
					"<td width='102px'>" +
						"<center><div style=\"margin-top:12px; margin-left:2px;\"><img src='http://gwap.cachefly.net/gwap-web-017/img/footer/top10logo.png' width='72px' height='30px'/></div></center>" +
					"</td>" +
					"<div id='topscoresInner' style='z-index=100'>";
		dataHTML1 += divTopScores1;
		dataHTML1 += "" + 
					"</div>" +
				"</tr>" + "" +
				"</table>" +
			"</div>";

		dataHTML2 = 
			"<div id='divTopScores2' class=\"div_top_scores\" style=\"z-index:100\">" +
				"<center>" +
				"<table border=0>" +
				"<tr>" +
					"<td width='92px'>" + periodImage +
					"</td>" +				
					"<td width='102px'>" +
						"<center><div style=\"margin-top:12px; margin-left:2px;\"><img src='http://gwap.cachefly.net/gwap-web-017/img/footer/top10logo.png' width='72px' height='30px'/></div></center>" +
					"</td>" +
					"<div id='topscoresInner' style='z-index=100'>";
		dataHTML2 += divTopScores2;
		dataHTML2 += "</div>" +
				"</tr>" +
				"</table>" +
				"</center>" +
			"</div>";
		
		$("top_scores").innerHTML = dataHTML1 + dataHTML2;

		correctPNG();
		
		interval = setInterval('fadeTopScores()', 8000)
	}	
},
fadeTopScores = function() {
	
	var topScoresNew;
	
	if (topScoresCurrent == 1) {
		topScoresNew = 2;
	} else {
		topScoresNew = 1;
	}
	
	new Effect.Opacity("divTopScores" + topScoresCurrent, {duration:1.0, from:1.0, to:0.0});
	new Effect.Opacity("divTopScores" + topScoresNew, {duration:1.0, from:0.0, to:1.0});
	$("divTopScores" + topScoresCurrent).style.zIndex = 100;
	$("divTopScores" + topScoresNew).style.zIndex = 101;
	
	topScoresCurrent = topScoresNew;
	
},
correctPNG = function() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters))
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }
}
