

games = new Array();
genres = new Array();
loadingdata = true;
totalgames=35;/**/
oldScrollX=0;
oldScrollY=0;
table_sortedby = "name";
descmode=false;

arrow_up_loc="images/arrow-up.gif";
arrow_down_loc="images/arrow-down.gif";
arrow_none_loc="images/arrow-none.gif";
wait16="images/wait16.gif";

function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function filter() {
	findObj("filter").style.backgroundImage="url("+wait16+")";
	window.setTimeout("actualfilter();",7);
}

function actualfilter(){	
	var namesorticon_data = arrow_none_loc;
	var playedsorticon_data = arrow_none_loc;
	var ratingsorticon_data = arrow_none_loc;
	var genresorticon_data = arrow_none_loc;
	var command;
	
	if (descmode==true) {
		command=table_sortedby+"sorticon_data='"+arrow_up_loc+"'";
	} else {
		command=table_sortedby+"sorticon_data='"+arrow_down_loc+"'";
	}
	eval(command);
	
	var searchquery=findObj("filter").value.toLowerCase();
	newtext="<table class=\"tborder\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" width=\"98%\">";
	newtext+="<thead>";
	newtext+="<tr>";
	newtext+="<td class=\"thead\" colspan=\"5\">";
	newtext+="";
	newtext+="<div><strong>Games</strong><br /><div class=\"smalltext\"></div></div>";
	newtext+="</td>";
	newtext+="</tr>";
	newtext+="<tr>";
	newtext+="<td class=\"tcat\" width=\"35\">&nbsp;</td>";
	newtext+="<td class=\"tcat\"><strong><a href=\"#\" onclick=\"sorttable('name'); return false;\">Game Name <img id=\"namesorticon\" src=\""+namesorticon_data+"\" alt=\"\"></a></strong></td>";
	newtext+="<td class=\"tcat\" style=\"white-space: nowrap;\" align=\"center\" width=\"85\"><strong><a href=\"#\" onclick=\"sorttable('played'); return false;\">Played <img id=\"playedsorticon\" src=\""+playedsorticon_data+"\" alt=\"\"></a></strong></td>";
	newtext+="<td class=\"tcat\" style=\"white-space: nowrap;\" align=\"center\" width=\"85\"><strong><a href=\"#\" onclick=\"sorttable('rating'); return false;\">Rating <img id=\"ratingsorticon\" src=\""+ratingsorticon_data+"\" alt=\"\"></a></strong></td>";
	newtext+="<td class=\"tcat\" style=\"white-space: nowrap;\" align=\"center\" width=\"85\"><strong><a href=\"#\" onclick=\"sorttable('genre'); return false;\">Genre <img id=\"genresorticon\" src=\""+genresorticon_data+"\" alt=\"\"></a></strong></td>";
	newtext+="</tr>";
	newtext+="</thead>";
	newtext+="<tfoot>";
	newtext+="<tr style=\"height:0px; background-color:#FFFFFF;\">";
	newtext+="<td colspan=\"5\"></td>";
	newtext+="</tr>";
	newtext+="</tfoot>";
	newtext+="<tbody>";
	/**/
	if (searchquery!="") {
		for (var i=0;i<totalgames;i++) {
			if (games[i]["isdeleted"]==true)
				continue;
			var genre_matches;
			genre_matches=true;
			genre_index=findObj('filter_genre').selectedIndex;
			if (genre_index==-1) { genre_index=0; }
			if (findObj('filter_genre').options[genre_index].value=="showall") {
				genre_matches=true;
			} else {
				genre_selected=findObj('filter_genre').options[genre_index].value;
				genre_matches=(genre_selected==games[i]["genre"]);
			}
			if ((games[i]["keywords"].indexOf(searchquery)!=-1) && (genre_matches)) {
/**/
					newtext+="<tr>";
					newtext+="<td class=\"trow1\" align=\"center\" valign=\"middle\">&nbsp;</td>";
					newtext+="<td class=\"trow1\" valign=\"middle\" align=\"left\"><a href='#' onclick='showgame("+games[i]["id"]+","+i+");'>"+games[i]["name"]+"</a></td>";
					newtext+="<td class=\"trow1\" valign=\"middle\" align=\"center\" style=\"white-space: nowrap\">"+games[i]["played"]+"</td>";
					newtext+="<td class=\"trow1\" valign=\"middle\" align=\"center\" style=\"white-space: nowrap\">"+games[i]["rating"]+"</td>";
					newtext+="<td class=\"trow1\" valign=\"middle\" align=\"center\" style=\"white-space: nowrap\">"+games[i]["genre"]+"</td>";
					//newtext+="<td class=\"trow1\" valign=\"middle\" align=\"right\" style=\"white-space: nowrap\"><a href='#' onclick='showgame("+games[i]["id"]+","+i+"); return false;'>Play</a></td>";
					newtext+="</tr>";
/**/
			}
		}
	} else {
		for (var i=0;i<totalgames;i++) {
			if (games[i]["isdeleted"]==true)
				continue;
			var genre_matches;
			genre_matches=true;
			genre_index=findObj('filter_genre').selectedIndex;
			if (genre_index==-1) { genre_index=0; }
			if (findObj('filter_genre').options[genre_index].value=="showall") {
				genre_matches=true;
			} else {
				genre_selected=findObj('filter_genre').options[genre_index].value;
				genre_matches=(genre_selected==games[i]["genre"]);
			}
			if (genre_matches) {
/**/
				newtext+="<tr>";
				newtext+="<td class=\"trow1\" align=\"center\" valign=\"middle\">&nbsp;</td>";
				newtext+="<td class=\"trow1\" valign=\"middle\" align=\"left\"><a href='#' onclick='showgame("+games[i]["id"]+","+i+");'>"+games[i]["name"]+"</a></td>";
				newtext+="<td class=\"trow1\" valign=\"middle\" align=\"center\" style=\"white-space: nowrap\">"+games[i]["played"]+"</td>";
				newtext+="<td class=\"trow1\" valign=\"middle\" align=\"center\" style=\"white-space: nowrap\">"+games[i]["rating"]+"</td>";
				newtext+="<td class=\"trow1\" valign=\"middle\" align=\"center\" style=\"white-space: nowrap\">"+games[i]["genre"]+"</td>";
				//newtext+="<td class=\"trow1\" valign=\"middle\" align=\"right\" style=\"white-space: nowrap\"><a href='#' onclick='showgame("+games[i]["id"]+","+i+"); return false;'>Play</a></td>";
				newtext+="</tr>";
/**/
			}
		}
	}
	newtext+="</tbody>";
	newtext+="</table>";
	findObj("gameslist").innerHTML=newtext;
	findObj("filter").style.backgroundImage="none";
}

if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

function add_genre(genre) {
	if (genres.indexOf(genre)==-1) {
		genres[genres.length]=genre;
	}
}

function update_genres() {
	findObj("filter_genre").options.length=0;
	genres.length=0;
	findObj("filter_genre").options[0]=new Option("-- Show All --","showall");
	for (var i=0;i<totalgames;i++) {
		add_genre(games[i]["genre"]);
	}
	sort_genres();
	for (var i=0;i<genres.length;i++) {
		findObj("filter_genre").options[findObj("filter_genre").options.length] = new Option(genres[i],genres[i]);
	}
}

function sort_genres() {
	genres.sort();
}

function showgame(id,i) {
	//gamebox=findObj("game_box");
	//loadintoIframe("game_frame", "playgame.php?id="+id+"&editorid="+i);
	findObj("game_frame").src="playgame.php?id="+id+"&editorid="+i;
}

function update_download_count(id,editid,url,flash) {
	if (flash==true) { ff='0'; } else { ff='1'; }
	findObj("download_count_updater").src="idc.php?id="+id+"&editorid="+editid+"&url="+url+"&flash="+ff;
	findObj("download_count_updater_icon").innerHTML="<img src=\""+wait16+"\" />";
}

function openflash(id,i) {
	findObj("game_frame").src="playgame.php?id="+id+"&editorid="+i+"&showflash=true";
}

function showtrailer(id,i) {
	findObj("game_frame").src="playgame.php?id="+id+"&editorid="+i+"&showtrailer=true";
}

function sorttablefirsttime() {
	var sortfn=ts_name;
	newgames=games.sort(sortfn);
	//findObj("namesorticon").src=arrow_down_loc;
	table_sortedby="name";
	newgames.reverse();
	games=newgames;
	filter();
}

function resorttable() {
	descmode=!descmode;
	sorttable(table_sortedby);
}

function sorttable(mode) {
	var sortfn;
	if (mode=="name") {
		sortfn=ts_name;
	} else if (mode=="played") {
		sortfn=ts_played;
	} else if (mode=="rating") {
		sortfn=ts_rating;
	} else if (mode=="genre") {
		sortfn=ts_genre;
	}
	newgames=games.sort(sortfn);
	findObj("namesorticon").src=arrow_none_loc;
	findObj("playedsorticon").src=arrow_none_loc;
	findObj("ratingsorticon").src=arrow_none_loc;
	findObj("genresorticon").src=arrow_none_loc;
	if ((table_sortedby==mode) && (descmode==false)) { descmode=true; findObj(mode+"sorticon").src=arrow_up_loc;} else {  newgames.reverse(); descmode=false; findObj(mode+"sorticon").src=arrow_down_loc; }
	table_sortedby=mode;
	games=newgames;
	filter();
}

function ts_name(a,b) {
	for (i=0;i<a["name"].length;i++) {
		if (a["name"].charCodeAt(i)!=b["name"].charCodeAt(i)) {
			return b["name"].charCodeAt(i) - a["name"].charCodeAt(i);
		}
	}
}

function ts_genre(a,b) {
	if (a["genre"]==b["genre"]) {
		for (i=0;i<a["name"].length;i++) {
			if (a["name"].charCodeAt(i)!=b["name"].charCodeAt(i)) {
				return b["name"].charCodeAt(i) - a["name"].charCodeAt(i);
			}
		}
	} else {
		for (i=0;i<a["genre"].length;i++) {
			if (a["genre"].charCodeAt(i)!=b["genre"].charCodeAt(i)) {
				return b["genre"].charCodeAt(i) - a["genre"].charCodeAt(i);
			}
		}
	}
}

function ts_played(a,b) {
	if (a["played"]==b["played"]) {
		for (i=0;i<a["name"].length;i++) {
			if (a["name"].charCodeAt(i)!=b["name"].charCodeAt(i)) {
				return b["name"].charCodeAt(i) - a["name"].charCodeAt(i);
			}
		}
	} else {
		return a["played"] - b["played"];
	}
}

function ts_rating(a,b) {
	if (a["rating"]==b["rating"]) {
		for (i=0;i<a["name"].length;i++) {
			if (a["name"].charCodeAt(i)!=b["name"].charCodeAt(i)) {
				return b["name"].charCodeAt(i) - a["name"].charCodeAt(i);
			}
		}
	} else {
		return a["rating"] - b["rating"];
	}
}

/***********************************************
* IFrame SSI script II- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of original DHTML scripts
* This notice must stay intact for legal use
**********************************************

//Input the IDs of the IFRAMES you wish to dynamically resize to match its content height:
//Separate each ID with a comma. Examples: ["myframe1", "myframe2"] or ["myframe"] or [] for none:
var iframeids=["game_frame"]

//Should script hide iframe from browsers that don't support this script (non IE5+/NS6+ browsers. Recommended):
var iframehide="no"

var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
var FFextraHeight=parseFloat(getFFVersion)>=0.1? 0 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers

function resizeCaller() {
var dyniframe=new Array()
for (i=0; i<iframeids.length; i++){
if (document.getElementById)
resizeIframe(iframeids[i])
//reveal iframe for lower end browsers? (see var above):
if ((document.all || document.getElementById) && iframehide=="no"){
var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i])
tempobj.style.display="block"
}
}
}

function resizeIframe(frameid){
var currentfr=document.getElementById(frameid)
if (currentfr){
currentfr.style.display="block"
if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; 
else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
currentfr.height = currentfr.Document.body.scrollHeight;
if (currentfr.addEventListener)
currentfr.addEventListener("load", readjustIframe, false)
else if (currentfr.attachEvent){
currentfr.detachEvent("onload", readjustIframe) // Bug fix line
currentfr.attachEvent("onload", readjustIframe)
}
}
}

function readjustIframe(loadevt) {
var crossevt=(window.event)? event : loadevt
var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
if (iframeroot)
resizeIframe(iframeroot.id);
}

function loadintoIframe(iframeid, url){
if (document.getElementById)
document.getElementById(iframeid).src=url
}

if (window.addEventListener)
window.addEventListener("load", resizeCaller, false)
else if (window.attachEvent)
window.attachEvent("onload", resizeCaller)
else
window.onload=resizeCaller

*/

function loadintoIframe(iframeid, url){
findObj(iframeid).src=url;
}

/**/

function vote(id)
{
	var num = prompt("Please enter what you wish to vote (between 0 and 5):", "3");
	if (num == null)
		return 1;
	 
	n = parseInt(num,10);
	if (isNaN(n)) 
	{
		alert("Please enter a valid number.");
	}
	else
	{
		if (n<0 || n>5)
		{
			alert("Please enter a number between 0 and 5.");
		}
		else
		{
			// valid number.
			loadintoIframe("vote_frame","playgame.php?action=setrating&amount="+n+"&id="+id);
		}
	}
}
