function Vehicle(make, model, count) {
	this.make = make;
	this.model = model;
	this.count = count;
}
function MakesAndModels() {
	//indexes the make names
	this.makes = new Array();
	//indexes the model arrays
	this.models = new Array();
	//indexes the counts for each make
	this.counts = new Array();
}
MakesAndModels.prototype.addVehicle = function(v) {
	//get the make index for each row
	var index = getIndex(v.make, this.makes)
	var modelArray = null;
	if (index == null) {
		index = this.makes.length;
		this.makes[this.makes.length] = v.make;
		modelArray = new Array();
		this.models[index] =  modelArray;
		this.counts[this.counts.length] = 0;
	} else {
		modelArray = this.models[index];
	}
	//add the model to its list
	modelArray[modelArray.length] = v;
	//now handle the count
	this.counts[index] += +v.count;	
}
MakesAndModels.prototype.getCountForMake = function(make) {
	var index = getIndex(make, this.makes);
	return index == null ? 0 : this.counts[index];
}
MakesAndModels.prototype.getCountForModel = function(make, model) {
	var index = getIndex(make, this.makeNames);
	if (index) {
		modelArray = this.models[index];
		for (i = 0; i < modelArray.length; i++) {
			if (modelArray[i].model == model) {
				return modelArray[i].count;
			}
		}
	}
	return 0;
}
var vehicles = new MakesAndModels();

function genMakeListFromBookId(id) {
	var u = "http://protein.informatics.indiana.edu/newpdb/user.jsp?type=bookSummary&bookID="+id;
	var x = getXmlHttp();
	x.onreadystatechange = function() {
		if (x.readyState == 4) {
			if (x.status == 200) {
				processMakeListXML(x.responseXML);
				ul = getMakeList();
				tit = document.getElementById("btitle");
				while (tit.childNodes.length > 0) {
					tit.removeChild(tit.childNodes[0]);
				}
				tit.appendChild(document.createTextNode("Some Title"));
				document.getElementById("body").appendChild(ul);
			} else {
				alert("An error occured while retreiving the book summary. Check user.jsp.\n\nHTML Status: "+x.status); 
			}
		}
	}
	x.open("GET", u, "true");
	x.send(null);	
}

function processMakeListXML(xml) {
	vehicles = new MakesAndModels();
	var vs = xml.getElementsByTagName("summary")[0].getElementsByTagName("vehicles")[0].childNodes;
	for (i = 0; i < vs.length; i++) {
		var make = vs[i].getAttribute("name");
		var models = vs[i].childNodes;
		for (j = 0; j < models.length; j++) {
			count = models[j].getAttribute("count");
			model = models[j].getAttribute("name");
			v = new Vehicle(make, model, count);
			vehicles.addVehicle(v);
		}
	}
}

function getMakeList() {
	var x = document.createElement("table");
	setCssClass(x, "vTable");
	var tb = document.createElement("tbody");
	x.appendChild(tb);
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	for (i = 0; i < vehicles.makes.length; i++) {
		make = vehicles.makes[i];
		tr = document.createElement("tr");
		td = document.createElement("td");
		img = document.createElement("img");
		img.setAttribute("src", "./img/plus.jpg");
		td.appendChild(img);
		setCssClass(td, "tog");
		tr.appendChild(td);
		td.onclick = function() {
			expandModels(this);
		}
		
		td = document.createElement("td");
		setCssClass(td, "make al");
		tr.appendChild(td);
		td.appendChild(document.createTextNode(make));
		td = document.createElement("td");
		tr.appendChild(td);
		td.appendChild(document.createTextNode(vehicles.counts[i]));
		setCssClass(td, "make ar");
		tb.appendChild(tr);
	}
	return x;
}
function expandModels(el) {
	var make = el.nextSibling.childNodes[0].nodeValue;
	var models = vehicles.models[getIndex(make, vehicles.makes)];
	var row = el.parentNode.nextSibling;
	for (i = 0; i < models.length; i++) {
		var tr = document.createElement("tr");
		var td = document.createElement("td");
		tr.appendChild(td);
		td.appendChild(document.createTextNode(""));
		tr.appendChild(td);
		td = document.createElement("td");
		td.appendChild(document.createTextNode(""+models[i].model));
		tr.appendChild(td);
		setCssClass(td, "model al");
		td = document.createElement("td");
		td.appendChild(document.createTextNode(""+models[i].count));
		setCssClass(td, "model ar");
		tr.appendChild(td);
		if (row) {
			row.parentNode.insertBefore(tr, row);
		} else {
			el.parentNode.parentNode.appendChild(tr);
		}
	}
	el.onclick = function() {
		collapseModels(this);
	}
	el.childNodes[0].setAttribute("src", "./img/minus.jpg");
}
function collapseModels(el) {
	var make = el.nextSibling.childNodes[0].nodeValue;
	for (var i = 0; i < vehicles.models[getIndex(make, vehicles.makes)].length; i++) {
		var row = el.parentNode;
		var rows = el.parentNode.parentNode.childNodes;

		el.parentNode.parentNode.removeChild(el.parentNode.nextSibling);
	}	
	el.onclick = function() {
		expandModels(this);
	}
	el.childNodes[0].setAttribute("src", "./img/plus.jpg");
}
function getXmlHttp() {
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		alert("Your browser does not support DOM");
	}
}
function contains(array, object) {
	return getIndex(object, array) != null;
}
function getIndex(object, array) {
	if (array.length) {
		for (i = 0; i < array.length; i++)	{
			if (array[i] == object) {
				return i;
			}
		}
	}
	return null;
}
function setCssClass(el, className) {
	el.setAttribute( window.ActiveXObject ? "className" : "class", className);
}

var xml;
	function Browser() {
	this.isIE    = false;
	if (window.ActiveXObject) {
		this.isIE = true;
	}
}

var browser = new Browser();
var mouseLoc = new Object();

function mousePressed(event) {
	var el;
	var x, y;
	// Get cursor position with respect to the page.
	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
			+ document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
			+ document.body.scrollTop;
	} else {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}

	// Capture mousemove and mouseup events on the page.
	if (browser.isIE) {
		document.attachEvent("onmousemove", dragging);
		document.attachEvent("onmouseup",   mouseReleased);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		document.addEventListener("mousemove", dragging,   true);
		document.addEventListener("mouseup",   mouseReleased, true);
		event.preventDefault();
	}
	window.mouseLoc.x = x;
	window.mouseLoc.y = y;
}

function dragging(event) {
	var x, y;
	// Get cursor position with respect to the page.
	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
		  + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
		  + document.body.scrollTop;
	} else {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	var ele = document.getElementById("summary");

	// Move drag element by the same amount the cursor has moved.
	var dX = x - window.mouseLoc.x;
	var dY = y - window.mouseLoc.y;
	
	ele.style.left = parseInt(ele.style.left) + dX + "px";
	ele.style.top  = parseInt(ele.style.top) + dY + "px";
	if (browser.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		event.preventDefault();
	}
	window.mouseLoc.x = x;
	window.mouseLoc.y = y;
}

function mouseReleased(event) {
	if (browser.isIE) {
		document.detachEvent("onmousemove", dragging);
		document.detachEvent("onmouseup",   mouseReleased);
	} else if (document.removeEventListener) {
		document.removeEventListener("mousemove", dragging,   true);
		document.removeEventListener("mouseup",   mouseReleased, true);
	}
}	
function load() {
	var url = 'http://protein.informatics.indiana.edu/newpdb/user.jsp?type=booklist';
	xml = getHttpXml();
	xml.onreadystatechange=function() {
		if (xml.readyState == 4) {
			if (xml.status == 200) {
				displayBooks(xml.responseXML);
			} else {
				alert("An error occured while getting the booklist. Server status: "+xml.status);
			}	
		}
	}
	xml.open("GET", url, true);
	xml.send(null);
}

function displayBooks(xml) {
    var table = document.createElement("table");
    table.setAttribute("id","bookTable");
    table.setAttribute("border", "0");
    var tbod = document.createElement("tbody");
    table.appendChild(tbod);
    table.style.marginBottom = "50px";
    var list = document.getElementById("list");
    while ( list.childNodes.length > 0) {
        list.removeChild( list.childNodes[0] );
    }
    list.appendChild(table);
    
    var bookList = xml.getElementsByTagName("bookList")[0];
    
    for (var i = 0; i < bookList.childNodes.length; i++) {
        addPublisher(tbod, bookList.childNodes[i]);
    } 
}

function addPublisher(tableBody, publisherXML) {
    var tr = document.createElement("tr");
    var td = document.createElement("td");
    var text;
    //header row
    td.setAttribute("colspan", "5");
    text = publisherXML.getElementsByTagName("name")[0];
    text = (text.hasChildNodes() ? text.childNodes[0].nodeValue : text.nodeValue);
    text = document.createTextNode( text );
    td.appendChild(text);
    td.setAttribute( (window.ActiveXObject ? "className": "class"), "publisher");
    tr.appendChild(td);

    tr.setAttribute("align", "left");
    tr.setAttribute("valign", "top");
    tr.appendChild(td);
    tableBody.appendChild(tr);
    
    tr = document.createElement("tr");
    tr.setAttribute( (window.ActiveXObject ? "className" : "class"), "titleHeaderTR");
    td = document.createElement("td");
    td.appendChild( document.createTextNode("Title"));
    tr.appendChild( td );
    td.setAttribute( (window.ActiveXObject ? "className" : "class"), "titleHeaderTD");
    
    td = document.createElement("td");
    td.appendChild( document.createTextNode("Author"));
    tr.appendChild( td );
    td.setAttribute( (window.ActiveXObject ? "className" : "class"), "titleHeaderTD");
    
    td = document.createElement("td");
    td.appendChild( document.createTextNode("ISBN"));
    tr.appendChild( td );
    td.setAttribute( (window.ActiveXObject ? "className" : "class"), "titleHeaderTD");
    
    //td = document.createElement("td");
    //td.appendChild( document.createTextNode("Edition"));
    //tr.appendChild( td );
    //td.setAttribute( (window.ActiveXObject ? "className" : "class"), "titleHeaderTD");
    
    td = document.createElement("td");
    td.appendChild( document.createTextNode("Photos Catalogued"));
    tr.appendChild( td );
    td.setAttribute( (window.ActiveXObject ? "className" : "class"), "titleHeaderTD");
    tableBody.appendChild(tr);
    
    var books = publisherXML.getElementsByTagName("book");
    for (var i = 0; i < books.length; i++) {
        tr = document.createElement("tr");
        td = document.createElement("td");
        var title = (books[i].childNodes[0].hasChildNodes() ? books[i].childNodes[0].childNodes[0].nodeValue : books[i].childNodes[0].nodeValue);
        var author = (books[i].childNodes[1].hasChildNodes() ? books[i].childNodes[1].childNodes[0].nodeValue : books[i].childNodes[1].nodeValue);
        var isbn = (books[i].childNodes[2].hasChildNodes() ? books[i].childNodes[2].childNodes[0].nodeValue : books[i].childNodes[2].nodeValue);
        //var ed = (books[i].childNodes[3].hasChildNodes() ? books[i].childNodes[3].childNodes[0].nodeValue : books[i].childNodes[3].nodeValue);
        var last = (books[i].childNodes[4].hasChildNodes() ? books[i].childNodes[4].childNodes[0].nodeValue : books[i].childNodes[4].nodeValue);
        
        td = document.createElement("td");
        td.setAttribute((window.ActiveXObject ? "className": "class"), "title");
        td.setAttribute("id","bookID"+books[i].getAttribute("id"));
        td.appendChild( document.createTextNode(title) );
        td.style.cursor = "pointer";
        td.onmouseover = function() {
			this.style.background = "rgb(255, 255, 222)";
        }
        td.onmouseout = function() {
			this.style.background = "rgb(224, 211, 185)";
        }
        td.onclick = function() {
			showSummary(this);
        }	
        tr.appendChild( td );
        
        td = document.createElement("td");
        td.appendChild( document.createTextNode(author) );
        td.setAttribute((window.ActiveXObject ? "className": "class"),"author");
        tr.appendChild( td );
        
        td = document.createElement("td");
        td.appendChild( document.createTextNode(isbn) );
        td.setAttribute((window.ActiveXObject ? "className": "class"), "isbn");
        tr.appendChild( td );
        
        //td = document.createElement("td");
        //td.appendChild( document.createTextNode(ed) );
        //td.setAttribute((window.ActiveXObject ? "className": "class"), "ed");
        //tr.appendChild( td );
        
        td = document.createElement("td");
        td.appendChild( document.createTextNode(last) );
        td.setAttribute((window.ActiveXObject ? "className": "class"), "lastPage");
        if (last > 0) {
			td.style.background = "#B4CECC";
        }
        tr.appendChild( td );
        tr.setAttribute("align", "left");
        tr.setAttribute("valign", "top");
        tableBody.appendChild(tr);    
    }
}

function getHttpXml() {
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		alert("Your browser does not support DOM");
	}
}
function findAbsX(obj) {
	var leftOff = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			leftOff += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		leftOff += obj.x;
	}
	return leftOff;
}

function findAbsY(obj) {
	var topOff = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			topOff += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		topOff += obj.y;
	}
	return topOff;
}
function showSummary(titleEl) {
	var id = titleEl.getAttribute("id");
	id = id.substring(6, id.length);
	var u = "http://protein.informatics.indiana.edu/newpdb/user.jsp?type=bookSummary&bookID="+id;
	var x = getHttpXml();
	x.onreadystatechange = function() {
		if (x.readyState == 4) {
			if (x.status == 200) {
				processMakeListXML(x.responseXML);
				var t = getMakeList();
				var pop = document.getElementById("summary");
				
				var tit = document.getElementById("btit");
				while (tit.childNodes.length > 0) {
					tit.removeChild(tit.childNodes[0]);
				}
				tit.appendChild(document.createTextNode(titleEl.childNodes[0].nodeValue));
						
				var slist = document.getElementById("slist");
				while (slist.childNodes.length > 0) {
					slist.removeChild(slist.childNodes[0]);
				}
				slist.appendChild(t);
				
				var sum = x.responseXML.getElementsByTagName("summary")[0];
				var selected = document.getElementById("bookID"+sum.getAttribute("bookID"));
				pop.style.left = (+findAbsX(selected) +50)+"px";
				pop.style.top = findAbsY(selected) + "px";
				pop.style.visibility = "visible";
			} else {
				alert("An error occured while retreiving the book summary. Check user.jsp.\n\nHTML Status: "+x.status); 
			}
		}
	}
	x.open("GET", u, "true");
	x.send(null);
} 