var closings = new function() {
	this.region = null;
	this.group = null;
	this.requestOpen = false;
	this.ajax = new Ajax();
	this.debugging = false;
	
	this.groups = {};
	this.regions = {};
	this.items = {};
	this.regions = {};
	this.statuses = {};
	this.extras = {};
	this.types = {};
	this.letters = {};
	this.debug = {};
	this.curCount = 0;
	this.l = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","All"];

	
	this.currentLetter = "All";
	
	this.mode=null;
	
	this.changeMode = function(newMode) {
		var c = document.getElementById("closings_info");
		while(c.hasChildNodes()) c.removeChild(c.firstChild);
		
		// clear out old information
	
		closings.mode = newMode;

		switch(closings.mode) {
			case "search": 
				closings.search(closings.currentLetter);
				break;
			case "item": 
				closings.changeLetter(closings.currentLetter);
				break;
			case "region": 
				for(var i in closings.regions) {
					c.appendChild(closings._region_HTML(i));
					c.appendChild(html_tags["div"]({"id":"details_"+i}));
				}
				break;
		}
		
		if(closings.debugging) 
			for(var i in closings.debug) {
				c.appendChild(html_tags["p"](i+": " +closings.debug[i]));
			}
		
		return false;
	}
	
	this.changeLetter = function(newLetter) {
		var c = document.getElementById("closings_info");
		if(closings.currentLetter != newLetter) { 
			while(c.hasChildNodes()) c.removeChild(c.firstChild);
		}
		closings.currentLetter = newLetter;
		c.appendChild(closings._letterHTML());
		c.appendChild(html_tags["div"]({"id":"closings_details"}));
		
		var newTable = html_tags["table"]({"cellspacing":"0","class":"closings_table"}, [
			html_tags["thead"]([
				html_tags["tr"]([
					html_tags["th"]("Name"),
					html_tags["th"]({"class":"status"}, "Status")
				])
			])
		]);
		
		newTable.cellSpacing=0;
		
		closings.curCount = 0;
		var theseItems = html_tags["tbody"]();
		if(newLetter == "All") {
			for(var j in closings.l) {
				var letter = closings.l[j];
				for(var i in closings.letters[letter]) {
					theseItems.appendChild(closings._closingsHTML(closings.items[closings.letters[letter][i]],"letter"));
					closings.curCount++;
				}
			}
		} else {
			for(var i in closings.letters[newLetter]) {
				theseItems.appendChild(closings._closingsHTML(closings.items[closings.letters[newLetter][i]],"letter"));
				closings.curCount++;
			}
		}
		
		if(closings.curCount == 0) {
			theseItems.appendChild(html_tags["tr"]([
				html_tags["td"]([
					html_tags["p"]("No closings reported at this time")
				]),
				html_tags["td"](),
			]));
		} 
		
		newTable.appendChild(theseItems);
		
		c.appendChild(newTable);
	};
	
	this.openRegion = function(region_id) {
		closings.group = null;
		if(closings.region != null) {
			var c = document.getElementById("details_"+closings.region);
			while(c.hasChildNodes()) c.removeChild(c.firstChild);
		}
		
		if(closings.region == region_id ) {
			closings.region = null;
		} else {
			closings.region = region_id;
			
			var c = document.getElementById("details_"+closings.region);
			var i = closings.region;
			
			// populate container
			var count = 0;
			for(var j in closings.regions[i]["groups"]) {
				var thisGroupId = closings.regions[i]["groups"][j];
				if(closings.groups[thisGroupId]["items"].length > 0) {
					var theseItems = html_tags["tbody"]({"id":"items_"+i+"_"+thisGroupId});
					var thisGroup = html_tags["thead"]({"class":"clickable"});
					var thisTable = html_tags["table"]({"class":"group_table"},
						[thisGroup,theseItems]);
					thisTable.cellSpacing=0;
					thisGroup.appendChild(closings._closingsGroupHTML(closings.groups[thisGroupId], "group", thisGroupId));
					c.appendChild(thisTable);
					count++;
				} else {
					var theseItems = html_tags["tbody"]({"id":"items_"+i+"_"+thisGroupId});
					var thisGroup = html_tags["thead"]();
					var thisTable = html_tags["table"]({"class":"group_table"},
						[thisGroup,theseItems]);
					thisTable.cellSpacing=0;
					thisGroup.appendChild(closings._closingsGroupHTML(closings.groups[thisGroupId], "group", thisGroupId));
					c.appendChild(thisTable);
					count++;
				}
			}
			
			if(closings.regions[i]["items"].length > 0) {
				var extraItems = html_tags["table"]({"cellspacing":"0","class":"group_table"});
				extraItems.cellSpacing=0;
				extraItems.appendChild(html_tags["thead"]({"class":"clickable"}, [
						closings._closingsGroupHTML({"name":"Ungrouped","region":i}, "groupless")
					]));
				extraItems.cellSpacing=0;
				var theseItems = html_tags["tbody"]({"id":"items_"+i+"_0"});
				extraItems.appendChild(theseItems);
				c.appendChild(extraItems);
				count++;
			}
			
			if(count==0)
				c.appendChild(html_tags["p"]({"class":"message"}, "No closings reported at this time"));
		}

		return false;
	};
	
	this.openGroup = function(group_id) {
		if(closings.group != null) {
			document.getElementById("folder_"+closings.region+"_"+closings.group).className="expandable";
			var c = document.getElementById("items_"+closings.region+"_"+closings.group);
			while(c.hasChildNodes()) c.removeChild(c.firstChild);
		}
		
		closings.curCount = 0;
		if(closings.group == group_id ) {
			closings.group = null;
		} else {
			closings.group = group_id;
			var c = document.getElementById("items_"+closings.region+"_"+closings.group);
			document.getElementById("folder_"+closings.region+"_"+closings.group).className="expanded";
			
			if(group_id == 0) {
				for(var j in closings.regions[closings.region]["items"]) {
					c.appendChild(closings._closingsHTML(closings.items[closings.regions[closings.region]["items"][j]], "item"));
					closings.curCount++;
				}
			} else {
				for(var j in closings.groups[group_id]["items"]) {
					c.appendChild(closings._closingsHTML(closings.items[closings.groups[group_id]["items"][j]], "item"));
					closings.curCount++;
				}
			}
			
		}
//		return false;
	};
	
	this.openRequest = function() {
		var div = document.getElementById("request_box");
		if(closings.requestOpen) {
			div.style.display = "none";
			closings.requestOpen = false;
		} else {
			div.style.display = "block";
			closings.requestOpen = true;
		}
		return false;
	};
	
	this.makeRequest = function(req) {
		if(req) {
			closings.ajax.setMimetype="text/xml";
			closings.ajax.responseFormat="xml";
			closings.ajax.doGet("/includes/closings/src/index.php?xaction=make+request&request="+escape(req), closings.finishRequest, "xml"); 
		}
	};
	
	this.finishRequest = function(ajax_xml) {
		var xmlDom=ajax_xml;
		var root=xmlDom.documentElement;
		
		for(var i = 0; i < root.childNodes.length; i++) {
			switch(root.childNodes[i].nodeName) {
//				case "error": alert("error!"); break;
				case "success": 
					var c = document.getElementById("request_box");
					while(c.hasChildNodes()) c.removeChild(c.firstChild);
					c.appendChild(html_tags["p"]({"class":"message"}, "Your request has been received."));
				break;
			}
		}
	};
	
	this.checkBoxes = function(mybool, c) {
		for(var i = 0; i < c.elements.length; i++) {
			if(c.elements[i].name == "edits[]") {
				c.elements[i].checked=mybool;
			}
		}
		return false;
	};
	
	this.init = function(mode) {
		closings.mode = mode;
		closings.ajax.setMimetype="text/xml";
		closings.ajax.responseFormat="xml";
		closings.ajax.doGet("/includes/closings/src/index.php?xaction=get+info", closings.finishGet, "xml"); 
	};
	
	this.finishGet = function(ajax_xml) {
		var xmlDom=ajax_xml;
		var root=xmlDom.documentElement;
		
		var regions = {};
		var groups = {};
		var items = {};
		
		for(var i = 0; i < root.childNodes.length; i++) {
			switch(root.childNodes[i].nodeName) {
				case "groups": 
				case "items":
					for(var j = 0; j < root.childNodes[i].childNodes.length; j++) { 
						if(root.childNodes[i].childNodes[j].nodeType == 1) {
							var thisId = root.childNodes[i].childNodes[j].getAttribute("id");
							var thisItem = {
									"group":root.childNodes[i].childNodes[j].getAttribute("group"),
									"region":root.childNodes[i].childNodes[j].getAttribute("region"),
									"extra":root.childNodes[i].childNodes[j].getAttribute("extra"),
									"status":root.childNodes[i].childNodes[j].getAttribute("status"),
									"type":root.childNodes[i].childNodes[j].getAttribute("type")
								};
							
							for(var k = 0; k < root.childNodes[i].childNodes[j].childNodes.length; k++) {
								switch(root.childNodes[i].childNodes[j].childNodes[k].nodeName) {
									case "name": 
										var thisLetter = root.childNodes[i].childNodes[j].childNodes[k].firstChild.nodeValue.substring(0, 1).toUpperCase();
										if(!closings.letters.hasOwnProperty(thisLetter)) {
											closings.letters[thisLetter] = [];
										}
										closings.letters[thisLetter].push(thisId);
									case "remarks":
									case "updated":
										thisItem[root.childNodes[i].childNodes[j].childNodes[k].nodeName] = root.childNodes[i].childNodes[j].childNodes[k].hasChildNodes()
											? root.childNodes[i].childNodes[j].childNodes[k].firstChild.nodeValue
											: "";
										break;
										
								}
							}
							
							
 							if(closings.debugging) closings.debug[thisId] = root.childNodes[i].childNodes[j].nodeName;
							
							switch(root.childNodes[i].childNodes[j].nodeName) {
								case "group": 
									closings.groups[thisId] = thisItem; 
									closings.groups[thisId]["items"] = [];
								case "item":  
									closings.items[thisId] = thisItem; 
									break;
							}
						}
					}
					break;

				default:
					for(var j = 0; j < root.childNodes[i].childNodes.length; j++) { 
						if(root.childNodes[i].childNodes[j].nodeType == 1) {
							var thisId = root.childNodes[i].childNodes[j].getAttribute("id");
							switch(root.childNodes[i].childNodes[j].nodeName) {
								case "region": regions[thisId] = root.childNodes[i].childNodes[j].firstChild.nodeValue; break;
								case "type": closings.types[thisId] = root.childNodes[i].childNodes[j].firstChild.nodeValue; break;
								case "status": closings.statuses[thisId] = root.childNodes[i].childNodes[j].firstChild.nodeValue; break;
								case "extra": closings.extras[thisId] = root.childNodes[i].childNodes[j].firstChild.nodeValue; break;
							}
						}
					}
					break;
			}
		}
		
		for(var i in regions) {
			closings.regions[i] = {};
			closings.regions[i]["name"] = regions[i];
			closings.regions[i]["groups"] = [];
			closings.regions[i]["items"] = [];
		}
		
		if(!closings.letters.hasOwnProperty("All")) {
			closings.letters["All"] = [];
		}
		for(var i in closings.items) {
			if(closings.items[i]["group"]) 
				closings.groups[closings.items[i]["group"]]["items"].push(i);
			else if(!closings.groups.hasOwnProperty(i))
				closings.regions[closings.items[i]["region"]]["items"].push(i);
		}
		
		for(var i in closings.groups) {
			closings.regions[closings.groups[i]["region"]]["groups"].push(i);
		}
		
		document.getElementById("loading_message").style.display="none";
		document.getElementById("browse_by").style.display="block";
		
		closings.changeMode(closings.mode);
	};
	
	this._region_HTML = function(id) {
		var thisLink = html_tags["a"]({"href":"#"}, closings.regions[id]["name"]);
		thisLink.onclick = function() { return closings.openRegion(id); };
		return html_tags["h3"]([thisLink]);
	};
	
	this._closingsHTML = function(itemInfo, type, id) {
		var thisItemRow = html_tags["tr"]({"class":("row_"+(closings.curCount%2+1))});
//		thisItemRow.onmouseover = function() {thisItemRow.className="highlighted";};
//		thisItemRow.onmouseout = function() {thisItemRow.className="normal";};
		
		var thisItem;
		switch(type) {
			case "letter":
				thisItem = html_tags["td"](); 
				thisItem.appendChild(html_tags["p"](itemInfo["name"]));
				break;
			case "item":
				thisItemRow.appendChild(html_tags["td"]({"class":"sub"}, "\u00a0"));
				thisItem = html_tags["td"](); 
				thisItem.appendChild(html_tags["p"](itemInfo["name"]));
			break;
		}
		
		switch(type) {
			case "item":
			case "letter":
				if(itemInfo["remarks"]) {
					if(itemInfo["extra"]) {
						thisItem.appendChild(html_tags["p"]({"class":"remarks"}, "- "+itemInfo["remarks"]+" ("+closings.extras[itemInfo["extra"]]+")"));
					} else {
						thisItem.appendChild(html_tags["p"]({"class":"remarks"}, "- "+itemInfo["remarks"]));
					}
				} else if(itemInfo["extra"]) {
					thisItem.appendChild(html_tags["p"]({"class":"remarks"}, "- "+closings.extras[itemInfo["extra"]]));
				}
				
				if(itemInfo["updated"]!= "" ) thisItem.appendChild(html_tags["p"]({"class":"small"},"- Updated: "+itemInfo["updated"]));
				
				thisItemRow.appendChild(thisItem);
				thisItemRow.appendChild(html_tags["td"]({"class":"status"}, closings.statuses[itemInfo["status"]]));
				break;
		}
		return thisItemRow;
	};	

	this._closingsGroupHTML = function(itemInfo, type, id) {
		var thisItemRow = html_tags["tr"]();
		
		var thisItem;
		switch(type) {
			case "groupless":
				thisItemRow.onclick = function() { closings.openGroup(0); };
				thisItemRow.appendChild(html_tags["td"]({"id":"folder_"+itemInfo["region"]+"_0","class":"expandable"}, "\u00a0"));
				thisItemRow.appendChild(html_tags["td"]({"class":"group_name"},[
						html_tags["p"]("Ungrouped ("+closings.regions[itemInfo["region"]]["items"].length+" items)")
					]));
				thisItemRow.appendChild(html_tags["td"]("\u00a0"));
				break;
			case "group": 
				thisItem = html_tags["td"]({"class":"group_name"}); 
				if(closings.groups[id]["items"].length > 0) {
					thisItemRow.appendChild(html_tags["td"]({"id":"folder_"+itemInfo["region"]+"_"+id,"class":"expandable"}, "\u00a0"));
					thisItem.onclick = function() { closings.openGroup(id); };
					thisItem.appendChild(html_tags["p"](itemInfo["name"]+" ("+closings.groups[id]["items"].length+" items)"));
			} else {
					thisItemRow.appendChild(html_tags["td"]({"id":"folder_"+itemInfo["region"]+"_"+id}));
					thisItem.appendChild(html_tags["p"](itemInfo["name"]+" (No Closings Reported)"));
				}
				break;
			break;
		}
		
		switch(type) {
			case "group":
				if(itemInfo["remarks"]) {
					if(itemInfo["extra"]) {
						thisItem.appendChild(html_tags["p"]({"class":"remarks"}, "- "+itemInfo["remarks"]+" ("+closings.extras[itemInfo["extra"]]+")"));
					} else {
						thisItem.appendChild(html_tags["p"]({"class":"remarks"}, "- "+itemInfo["remarks"]));
					}
				} else if(itemInfo["extra"]) {
					thisItem.appendChild(html_tags["p"]({"class":"remarks"}, "- "+closings.extras[itemInfo["extra"]]));
				}
				
				if(itemInfo["updated"]!= "" ) thisItem.appendChild(html_tags["p"]({"class":"small"},"- Updated: "+itemInfo["updated"]));
				
				thisItemRow.appendChild(thisItem);
				thisItemRow.appendChild(html_tags["td"]({"class":"status"}, closings.statuses[itemInfo["status"]]));
				break;
		}
		return thisItemRow;
	};	

	this._letterHTML = function() {
		var out = html_tags["h4"]();

		for(var i in closings.l) {
			var thisLetter = closings.l[i];
			if(closings.l[i] == closings.currentLetter) {
				out.appendChild(html_tags["span"]([
					html_tags["span"]({"class":"activeLetter"}, " "+thisLetter+" "),
					"|"
				]));
			} else if(closings.letters.hasOwnProperty(closings.l[i])) {
				var thisLink = html_tags["a"]({"href":"javascript: closings.changeLetter('"+thisLetter+"')"}, " "+thisLetter+" ");
				out.appendChild(html_tags["span"]({"class":"availLetter"},[
					thisLink,
					"|"
				]));
			} else {
				out.appendChild(html_tags["span"]([
					html_tags["span"]({"class":"inactiveLetter"}, " "+thisLetter+" "),
					"|"
				]));
			}
		}
		return out;
	};
}
