/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/+\
|=-=!!!------------------------------------------------!!!=-=|
|=-=!!!  IF YOU USE THIS SCRIPT IN ANY WAY, YOU MUST   !!!=-=|
|=-=!!!           LEAVE THIS COMMENT INTACT            !!!=-=|
|=-=!!!------------------------------------------------!!!=-=|
|=-=!!! AUTHOR    : Russell J. Crevatas                !!!=-=|
|=-=!!! CODED FOR : http://worldswitcher.LovingLori.com!!!=-=|
|=-=!!! E-MAIL    : Admin@LovingLori.com               !!!=-=|
|=-=!!!------------------------------------------------!!!=-=|
\+/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
// BEGIN GLOBAL Array Setting
var WorldListSetObjects = new Array();
// END GLOBAL Array Setting
// Begin World CLASS
function World(WorldNumber,WorldPopulation,WorldLocation,WorldActivity,WorldLootshare,WorldType,WorldPermissions) {
	this.world		= new Number(WorldNumber);
	this.people		= new Number(WorldPopulation);
	this.location	= WorldLocation;
	this.activity	= WorldActivity;
	this.lootshare	= ((WorldLootshare == "TRUE")? true:false);
	this.type		= WorldType;
	this.permissions= new Number(WorldPermissions);
	this.content	= this.pack(new Array(this.world,this.people,this.location,this.activity,this.lootshare,this.type));
}
World.prototype.pack = function (arr) {
	var tr = document.createElement("tr");
	for (i in arr) {
		var td = document.createElement("td");
		td.colspan = 1;
		var content = document.createElement("img");
		if (i == "2") {
			content.src = "/bool/flag_"+arr[i].toLowerCase().replace("gb","uk")+".png";
		}
		else {
			if ((i == "4") || (i == "5")) {
				content.src = "/bool/"+arr[i]+".gif";
				td.style.width = "15px";
				td.className = "nb";
			}
			else {
				if (i == "0") arr[i] += String.fromCharCode(160);
				if (i == "3") td.className = "activity";
				var content = document.createTextNode(arr[i]);
			}
		}
		td.style.textAlign = "center";
		td.appendChild(content);
		tr.appendChild(td);
	}
	return tr;
}
// END World CLASS
// BEGIN WorldList Class
function WorldList() {
	this.index = WorldListSetObjects.length + 1;
	WorldListSetObjects[this.index] = this;
	this.xmlHttp;
	this.currentSort = 0;
	this.sortDirection = 1;
	this.worldArray = new Array;
	this.rawArray;
	this.loadCount = 0;
	this.awaitingSort = false;
	this.destinationElement;
}

WorldList.prototype.publishError = function (error) {
	this.destinationElement.innerHTML = error;
}

WorldList.prototype.gethttp = function () {
	try { a = new XMLHttpRequest(); } catch (e) { try { a = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { a = new activeXObject("Microsoft.XMLHTTP"); } catch (e) { return null; } } }
	return a;
}

WorldList.prototype.processRawArray = function () {
	this.worldArray = new Array();
	for (x in this.rawArray) {
		a = this.rawArray[x].split("_");
		this.worldArray.push(new World(a[0],a[1],a[2],a[3],a[4],a[5],a[6]));
	}
	this.xmlHttp = null;
	this.publishWorldList(true);
}

WorldList.prototype.statechange = function (event) {
	for (x in WorldListSetObjects) {
		a = WorldListSetObjects[x];
		if (a.xmlHttp.readyState == 4) {
			a.rawArray = a.xmlHttp.responseText.split("\n");
			a.processRawArray();
		}
	}
}

WorldList.prototype.request = function () {
	this.loadCount++;
	if (this.loadCount > 1) {
		div = document.createElement("div");
		div.style.textAlign = "center";
		i = document.createElement("i");
		i.appendChild(document.createTextNode("Loading.... Please Wait"));
		div.appendChild(i);
		div.appendChild(document.createElement("br"));
		img = document.createElement("img");
		img.src = "/loading.gif";
		div.appendChild(img);
		div.appendChild(document.createElement("br"));
		div.appendChild(document.createElement("br"));
		this.destinationElement.removeChild(this.destinationElement.firstChild);
		this.destinationElement.appendChild(div);
	}
	if ((this.xmlHttp = this.gethttp()) != null) {
		this.xmlHttp.onreadystatechange = this.statechange;
		this.xmlHttp.open("GET","rawra.php?echo=true",true);
		this.xmlHttp.send(null);
	}
	else {
		this.publishError("Could not load World List");
	}
}

WorldList.prototype.publishWorldList = function (fromraw) {
	if (fromraw) {
		this.order(this.currentSort,this.sortDirection);
		return;
	}
	d = document;
	table = d.createElement("table");
	table.style.padding = "0";
	table.style.borderSpacing = "0";
	table.style.border = "3px";
	table.style.borderCollapse = "collapse";
	tbody = d.createElement("tbody");

	tr = d.createElement("tr");
	tr.className = "mainWorldRow";
	captions = new Array("w#","ppl#","flag","activity","LS","?");
	for (i in captions) {
		td = d.createElement("td");
		tt = d.createTextNode(captions[i]);
		td.appendChild(tt);
		tr.appendChild(td);
	}
	tbody.appendChild(tr);

	tr = d.createElement("tr");
	tr.className = "midMainRow";
	numz = new Array("0:1","1:1","2:1","3:1","8:1","7:1");	
	for (i in numz) {
		td = d.createElement("td");
		for (r = 0; r < 2; r++) {
			arra = d.createElement("a");
			arra.href = "javascript:WorldListSetObjects["+this.index+"].order("+new Number(numz[i].split(":")[0])+","+r+")";
			arri = d.createElement("img");
			arri.src = "/bool/sort"+((r == 0)? "up":"dn")+".gif";
			arra.appendChild(arri);
			td.appendChild(arra);
			td.colSpan = numz[i].split(":")[1];
		}
		tr.appendChild(td);
	}
	tbody.appendChild(tr);
	o = 0;
	a = this.worldArray;
	for (i in a) {
		x = a[i].content;
		x.className = (((o % 2) == 1)? "u":"d");
		tbody.appendChild(x);
		o++;
	}
	table.appendChild(tbody);
	this.destinationElement.innerHTML = "";
	this.destinationElement.appendChild(table);
}

WorldList.prototype.order = function (arryIndex,dir) {
	this.currentSort = new Number(arryIndex);
	this.sortDirection = new Number(dir);
	this.awaitingSort= true;
	switch (arryIndex.toString()) {
		case "2":
			this.worldArray = this.worldArray.sort(this.sortAlpha);
		break;
		case "3":
			this.worldArray = this.worldArray.sort(this.sortAlpha);
		break;
		case "7":
			this.worldArray = this.worldArray.sort(this.sortAlpha);
		break;
		default:
			this.worldArray = this.worldArray.sort(this.sortNumeric);
		break;
	}
	this.awaitingSort = false;
	if (this.sortDirection.toString() == "0") {
		this.worldArray = this.worldArray.reverse();
	}
	this.publishWorldList();
}
WorldList.prototype.sortNumeric = function (a,b) {
	for (ob in WorldListSetObjects) {
		ob = WorldListSetObjects[ob];
		if (ob.awaitingSort == true) {
			cs = ob.currentSort;
			a = ((cs == 0)? a.world:((cs == 1)? a.people:((cs == 8)? a.permissions:"")));
			b = ((cs == 0)? b.world:((cs == 1)? b.people:((cs == 8)? b.permissions:"")));
			if (new Number(ob.sortDirection) == 0) {
				if (a == 0) return -1;
				if (b == 0) return 1;
			}
			else {
				if (a == 0) return 1;
				if (b == 0) return -1;
			}
			return a - b;
		}
	}
}
WorldList.prototype.sortAlpha = function (a,b) {
	for (ob in WorldListSetObjects) {
		ob = WorldListSetObjects[ob];
		if (ob.awaitingSort) {
			h = a;
			i = b;
			cs = ob.currentSort;
			a = ((cs == 2)? a.location:((cs == 3)? a.activity:((cs == 7)? a.type:"")));
			b = ((cs == 2)? b.location:((cs == 3)? b.activity:((cs == 7)? b.type:"")));
			if (ob.sortDirection == 0) {
				if (a == "-") return -1;
				if (b == "-") return 1;
			}
			else {
				if (a == "-") return 1;
				if (b == "-") return -1;
			}
			try {
				r = 0;
				loopto = Math.max(a.length,b.length);
				for (i = 0; i < loopto; i++) {
					x = a.charCodeAt(i);
					y = b.charCodeAt(i);
					if (x == y) {
						continue;
					}
					else {
						if (x > y) r = 1;
						else r = -1;
						break;
					}
				}
				return r;
			}
			catch (e) {
				alert("error caught   ~"+ob.currentSort+"~  "+a+":"+h.type+"~"+b+":"+i.type);
				return 0;
			}
		}
	}
}