/* Copyright Electronic Arts, 2007 */

/** Global instances **/
var TABLE;
var GENERAL_DOMAIN;
var COOKIE_NAME;

/** Return the all games table.  Lazily initializes when necessary. */
function getTable() {
	if (!TABLE) {
		TABLE = new AllGamesTable("gamesTable");
	}
	return TABLE;
}

/** Set domain for use in cookies */
function setGeneralDomain(domain) {
	GENERAL_DOMAIN = domain;
}

function setCookieName(name) {
	COOKIE_NAME = name;
}

/** Function used to handle sorting, to ensure table object can be reused. **/
function runSort(col, defaultReverse) {
	return sortTableChangeDefault(getTable(), col, defaultReverse);
}

/** All Games page specific cookie implementation. Stores two pieces of data: 
 *  column number,  and reverse (a boolean, where true indicates the column's 
 *  sorting was reversed. */
function AllGamesCookie() {
	var SEPERATOR_CHAR = "|";
	var mCookie = getCookie(COOKIE_NAME);
	var mColumn;
	var mReverse = false;
	
	if (!mCookie.isEmpty()) {
		var cookieComponents = mCookie.getValue().split(SEPERATOR_CHAR);
		if (cookieComponents.length > 0) {
			mColumn = parseInt(cookieComponents[0]);
		}
		if (cookieComponents.length > 1) {
			if (cookieComponents[1] == "true") {
				mReverse = true;
			} else {
				mReverse = false;
			}
		}
	}	
	
	this.isEmpty = function () { return mCookie.isEmpty(); }
	this.setColumn = function (col) { mColumn = col; }
	this.getColumn = function () { return mColumn; }
	this.setReverse = function (rev) { mReverse = rev; }
	this.getReverse = function () { return mReverse; }
	
	this.write = function () {
		if (!mColumn) {
			return; 
		}
		mCookie.setExpiration(24*500);
		
		var strHref = window.location.href;
		var loc = strHref.indexOf("//");
		if (loc > -1 && loc + 2 < strHref.length) { // strip off 'http://'
			strHref = strHref.substring(loc + 2, strHref.length);
		}
		loc = strHref.indexOf("/");
		if (loc > -1) { // strip of domain, leaving leading '/'
			strHref = strHref.substring(loc, strHref.length);
		}
		loc = strHref.indexOf("?");
		if (loc > -1) { // strip off query parameters
			var path = strHref.substring(0, loc);
		} else {
			var path = strHref;
		}
		if (path.length == 0) {
			return; // if no path exists, abort rather than scope a cookie globally
		}
		mCookie.setPath(path);
		
		if (GENERAL_DOMAIN) {
			mCookie.setDomain(GENERAL_DOMAIN);
		}
		
		var value = mColumn + SEPERATOR_CHAR;
		if (mReverse) {
			value += "true";	
		} else {
			value += "false";
		}
		mCookie.setValue(value);
		
		mCookie.write();
	}
}

/** Implementation of Table object.  See tablesort.js for details. **/
function AllGamesTable(tablename) {
	/** Public members **/
	this.lastColumn = 1; /** The last column sorted defaults to the first column**/
	this.body = null;
	this.rowPrefix = "row";
	
	/** Private members **/
	var alternateClass = false;
	var tableNode = document.getElementById(tablename);
	var orderElements = new Array();
	var headerImgs = new Array();
	var arrowHide = "sort-arrow-hidden";
	var arrowShow = "sort-arrow-shown";
	var sortNodeName = "sortNodes";
	var numColumns = 6;
	
	/** 
	 * Initialization code.  This only needs to be run once for a page, 
	 * so use only once instance of this object per sorted table.
	 **/	
	var sortNode = document.getElementById(sortNodeName);
	var header = tableNode.tHead;
	this.body = tableNode.tBodies[0];
	
	for (var i = 0; i < header.rows[0].cells.length; i++) {
		var headerNodes = header.rows[0].cells[i].childNodes;
		for (var x = 0; x < headerNodes.length; x++) {
			var img = headerNodes[x];
			
			if (!img.id) { continue; }
			
			for (var y = 1; y <= numColumns; y++) {
				if (img.id != "up"+y && img.id != "down"+y && img.id != "null"+y) {
					continue;
				}
				headerImgs.push(img);
			}
		}
	}
	
	for (var x = 0; x < sortNode.childNodes.length; x++) {
		var child = sortNode.childNodes[x];
		if (child.nodeType == document.ELEMENT_NODE && child.id) {
			orderElements[child.id] = child;
		}
	}
	
	/** Functions **/
	this.startSort = function () { alternateClass = false; }
	
	this.createNewBody = function () {
		return document.createElement("tbody");
	}	
	
	this.replaceBody = function (newBody) {
		tableNode.replaceChild(newBody, this.body);
		this.body = newBody;
	}
	
	this.getOrderingNode = function (col, reverse) {
		var elemId;		
		if (reverse) {
			elemId = "order"+col+"b";
		} else {
			elemId = "order"+col+"a";
		}		
		return orderElements[elemId];
	}
	
	this.rowInsert = function (row) {
		if (alternateClass) {
			row.className="odd";
			alternateClass = false;
		} else {
			row.className="even";
			alternateClass = true;
		}
	}
	
	this.sortOrder = function (col, reverse) {
		if (!header) {
			return;
		}
		
		for ( var x=0; x < headerImgs.length; x++ ) {
			var img = headerImgs[x];
			if (reverse && img.id == "up"+col) {
				img.className=arrowShow;
			} else if (!reverse && img.id == "down"+col) {
				img.className=arrowShow;
			} else if (img.id.indexOf("null") != -1 && img.id != "null"+col) {
				// set null image for any column we didn't sort
				img.className=arrowShow;
			} else {
				img.className=arrowHide;
			}
		}
		
		// Now that we know the column and direction, set the cookie.
		var cookie = new AllGamesCookie();
		cookie.setColumn(col);
		cookie.setReverse(reverse);
		cookie.write();
	}
}