// Temporary replacement of original script to isolate bug.
//
var MostViewed = function() {
	var DATA = {
		aboutUs: [
			{ pageGroup: "aboutUs", page: "Statistics", url: "/resources/statistics/index.shtml", order: 1 }, 
			{ pageGroup: "aboutUs", page: "Fees and Taxes", url: "/resources/fees/index.shtml", order: 2 }, 
			{ pageGroup: "aboutUs", page: "Publications and Resources", url: "/resources/aboutUs/index.shtml", order: 3 }
		],
		licensing: [
			{ pageGroup: "licensing", page: "Publications and Resources", url: "/resources/gamingLicensing/index.shtml", order: 1 }, 
			{ pageGroup: "licensing", page: "Competitions and Raffles", url: "/licensing/gamingLicensing/competition/index.shtml", order: 2 }, 
			{ pageGroup: "licensing", page: "Queensland Gaming Commission", url: "/licensing/commission/index.shtml", order: 3 }
		],
		compliance: [ 
			{ pageGroup: "compliance", page: "Publications and Resources", url: "/resources/compliance/index.shtml", order: 1 }, 
			{ pageGroup: "compliance", page: "Venue Compliance", url: "/compliance/gamingCompliance/compliancePrograms/index.shtml", order: 2 }, 
			{ pageGroup: "compliance", page: "Fees and Taxes", url: "/resources/fees/index.shtml", order: 3 }
		], 
		grants: [
			{ pageGroup: "grants", page: "Grants", url: "/grants/index.shtml", order: 1 }, 
			{ pageGroup: "grants", page: "GCBF", url: "/grants/gcbf//index.shtml", order: 2 }, 
			{ pageGroup: "grants", page: "JCCBF", url: "/grants/JCCBF/index.shtml", order: 3 }
		],
		responsibleGambling: [
			{ pageGroup: "responsibleGambling", page: "Publications and Resources", url: "/resources/resGambling/index.shtml", order: 1 }, 
			{ pageGroup: "responsibleGambling", page: "Gambling Help", url: "/responsibleGambling/communityInfo/helpServices/index.shtml", order: 2 }, 
			{ pageGroup: "responsibleGambling", page: "Player Info", url: "/responsibleGambling/communityInfo/playerInfo/index.shtml", order: 3 }
		],
		resources: [
			{ pageGroup: "resources", page: "Statistics", url: "/resources/statistics/index.shtml", order: 1 }, 
			{ pageGroup: "resources", page: "Gaming Licensing Resources", url: "/resources/gamingLicensing/index.shtml", order: 2 }, 
			{ pageGroup: "resources", page: "Responsible Gambling Resources", url: "/resources/resGambling/index.shtml", order: 3 }
		],
		gamingServices: [
			{ pageGroup: "gamingServices", page: "Statistics", url: "/resources/statistics/index.shtml", order: 1 }, 
			{ pageGroup: "gamingServices", page: "Publications and Resources", url: "/resources/gamingServices/index.shtml", order: 2 }, 
			{ pageGroup: "gamingServices", page: "Definitions", url: "/gamingServices/definitions/index.shtml", order: 3 }
		]
	};
	
	var DEBUG          = false;
	var DEFAULT_FILTER = "aboutUs";
	var PRELOADER_ID   = "most-viewed-preloader";
	var RESULTS_ID     = "most-viewed";
	var ERROR_MESSAGE  = "Unable to retrieve data.";
	var XML_PATH       = "/_assets/data/mostViewed.xml";
	
	var _hidePreloader = function() {
		var div = document.getElementById(PRELOADER_ID);
		if (div) {
			div.style.display = "none";
		}
	};
	
	var _insertHTML = function(list) {
		var div = document.getElementById(RESULTS_ID);
		if (div) {
			var ul = document.createElement("UL");
			
			for (var i = 0; i < list.length; i++) {
				var a = document.createElement("A");
				var li = document.createElement("LI");
				
				a.href = list[i].url;
				a.appendChild(document.createTextNode(list[i].page));
				li.appendChild(a);
				ul.appendChild(li);
			}
			
			div.appendChild(ul);
			_hidePreloader();
		}
	};
	
	var _error = function(message) {
		if (DEBUG) {
			alert("ERROR: " + message);
		} else {
			var div = document.getElementById(RESULTS_ID);
			if (div) {
				div.appendChild(document.createTextNode(ERROR_MESSAGE));
				_hidePreloader();
			}
		}
	};
	
	var _getFilter = function() {
		var path = document.location.pathname;
		var index = 1;
		
		// Modify logic so the code will preview in TeamSite.
		if (path.indexOf("/iw/") == 0) {
			index = 11;
		}
		if (path.indexOf("/iw-mount/") == 0) {
			index = 7;
		}
		
		// Find the first directory name.
		var parts = path.split("/");
		
		if (parts.length < index) {
			return DEFAULT_FILTER;
		}
		return parts[index];
	};
	
	var _processXML = function(xml, filter) {};
	var _callback = function(xml) {};
	
	this.init = function() {
		if (!MostViewed.isSupported()) { return; }
		
		var filter = _getFilter();
		var files  = (DATA[filter]) ? DATA[filter] : DATA[DEFAULT_FILTER];
		
		_insertHTML(files);
	};
};

MostViewed.isSupported = function() {
	if (document.getElementById && document.createElement) {
		return true;
	}
	return false;
};


// ===========================================================================
// Initialisation code.
// ===========================================================================
document.write('<div id="most-viewed"></div>');

(new MostViewed()).init();
