var imgPoolPath = "/images/pool";
var ex; // Expander
		
/* Background image */
dojo.event.connect(window, "onload", function() {
	var imgNum = Math.floor(Math.random() * numImages + 1);
	imgNum = (imgNum < 10) ? "0" + imgNum : imgNum;
	var bgp = dojo.byId("bg-pool");
	if (bgp) { bgp.src = imgPoolPath + "/" + imgNum + ".jpg"; }
});

/* Module expansion */
dojo.event.connect(window, "onload", function() {
	ex = new Expander(numExpandModules);
	
	/* Callback creators */
	function createExpandFunc(/* integer */id) {
		return function() { ex.expand(id); };
	}
	
	function createContractFunc(/* integer */id) {
		return function() { ex.contract(id); };
	}
	
	for (var i = 1; i <= numExpandModules; i++) {
		var step = i;
		var expandCallbackFunc = createExpandFunc(step);
		dojo.event.connect(dojo.byId("link-item-expand-" + step), "onclick", expandCallbackFunc);
	}
	
	if (doExpandOnLoad && "all" == doExpandOnLoad) {
		ex.expandAll();
	} else if (doExpandOnLoad) {
		ex.expand(doExpandOnLoad);
	}
});

/* Expander: handle and track module expansion */
function Expander(/* integer */num) {
	this.max = num;
	
	this.contract = contract;
	this.expand = expand;
	this.expandAll = expandAll;
	
	var states = [];
	
	function contract(/* integer */id) {
		dojo.debug("Contracting ", id);
		dojo.byId("item-detail-" + id).className = "hide";
		dojo.byId("link-item-expand-" + id).className = "link-item-expand";
		states[id] = 0;
	}
	
	function expand(/* integer */id) {
		if (!states[id]) {
			dojo.debug("Expanding ", id);
			dojo.byId("item-detail-" + id).className = "item-detail";
			dojo.byId("link-item-expand-" + id).className = "link-item-contract";
			dojo.byId("link-item-expand-" + id).href = "javascript:void(0)";
			states[id] = 1;
		} else {
			contract(id);
		}
	}
	
	function expandAll() {
		for (var i = 1; i <= this.max; i++) {
			expand(i);
		}
	}
}
