viewController = null;

function OM_ViewClass(index) {
	this.index = index;
	this.info = null;
	this.hideInfo = function() {
		var listitem = document.getElementById('classItem' + this.index);
		listitem.style.height = "19px";
		var text = "<a class='listitem' href='#' onclick='return viewController.classView.clicked(" + this.index + ");'>";
		if(this.info.label == "") {
			text = text + this.info.uri + "</a><br />";
		}
		else {
			text = text + this.info.label + "</a><br />";
		}
		listitem.innerHTML = text;
		this.info = null;
	}
	this.expandSupers = function() {
		if(this.info == null) {
			viewController.showErrorDialog("An unknown error has occurred.");
			return;
		}
		var supers = this.info.getSupers();
		var classitem = document.getElementById('classItem' + this.index + 'Supers');
		classitem.innerHTML = "";
		classitem.style.height = "auto";
		var str = "<a class='sublistitem' href='#' onclick='return viewController.classView.retractSupers(" + this.index + ");'>Superclasses</a><br />";
		str = str + "<span style='font-size:16px;font-weight:normal;max-width:100%;background-color:#fff;'>";
		if(typeof(supers[0]) == "string") {
			str = str + supers[0] + "<br />";
		}
		else {
			for(i=0;i<supers.length;i++) {
				str = str + "<a href='#' onclick='return viewController.switchToClass(" + '"' + supers[i][0] + '"' + ");'>";
				if(supers[i][1] == "")
					str = str + supers[i][0] + "</a><br />";
				else
					str = str + supers[i][1] + "</a><br />";
			}
		}
		str = str + "</span>";
		classitem.innerHTML = str;
	}
	this.retractSupers = function() {
		var classitem = document.getElementById('classItem' + this.index + 'Supers');
		classitem.innerHTML = "<a class='sublistitem' href='#' onclick='return viewController.classView.expandSupers(" + this.index + ");'>Superclasses</a></li>";
		classitem.style.height = '19px';
	}
	this.expandSubs = function() {
		if(this.info === undefined) {
			viewController.showErrorDialog("An unknown error has occurred.");
			return;
		}
		var subs = this.info.getSubs();
		var classitem = document.getElementById('classItem' + this.index + 'Subs');
		classitem.innerHTML = "";
		classitem.style.height = "auto";
		var str = "<a class='sublistitem' href='#' onclick='return viewController.classView.retractSubs(" + this.index + ");'>Subclasses</a><br />";
		str = str + "<span style='font-size:16px;font-weight:normal;max-width:100%;background-color:#fff;'>";
		if(typeof(subs[0]) == "string") {
			str = str + subs[0] + "<br />";
		}
		else {
			for(i=0;i<subs.length;i++) {
				str = str + "<a href='#' onclick='return viewController.switchToClass(" + '"' + subs[i][0] + '"' + ");'>";
				if(subs[i][1] == "")
					str = str + subs[i][0] + "</a><br />";
				else
					str = str + subs[i][1] + "</a><br />";
			}
		}
		str = str + "</span>";
		classitem.innerHTML = str;
	}
	this.retractSubs = function() {
		var classitem = document.getElementById('classItem' + this.index + 'Subs');
		classitem.innerHTML = "<a class='sublistitem' href='#' onclick='return viewController.classView.expandSubs(" + this.index + ");'>Subclasses</a></li>";
		classitem.style.height = '19px';
	}
	this.expandInstances = function() {
		if(this.info === undefined) {
			viewController.showErrorDialog("An unknown error has occurred.");
			return;
		}
		var insts = this.info.getInstances();
		var classitem = document.getElementById('classItem' + this.index + 'Instances');
		classitem.innerHTML = "";
		classitem.style.height = "auto";
		var str = "<a class='sublistitem' href='#' onclick='return viewController.classView.retractInstances(" + this.index + ");'>Instances</a><br />";
		str = str + "<span style='font-size:16px;font-weight:normal;max-width:100%;background-color:#fff;'>";
		if(typeof(insts[0]) == "string") {
			str = str + insts[0] + "<br />";
		}
		else {
			for(i=0;i<insts.length;i++) {
				str = str + "<a href='#' onclick='return viewController.switchToIndividual(" + '"' + insts[i][0] + '"' + ");'>";
				if(insts[i][1] == "")
					str = str + insts[i][0] + "</a><br />";
				else
					str = str + insts[i][1] + "</a><br />";
			}
		}
		str = str + "</span>";
		classitem.innerHTML = str;
	}
	this.retractInstances = function() {
		var classitem = document.getElementById('classItem' + this.index + 'Instances');
		classitem.innerHTML = "<a class='sublistitem' href='#' onclick='return viewController.classView.expandInstances(" + this.index + ");'>Instances</a></li>";
		classitem.style.height = '19px';
	}
	this.displayInfo = function(sender, info) {
		viewController.hideWaitDialog();
		if(typeof(info) == "string") {
			viewController.showErrorDialog(info);
			return;
		}
		this.info = info;
		var classitem = document.getElementById('classItem' + this.index);
		classitem.innerHTML = '';
		classitem.style.height = "auto";
		var str = "<a class='listitem' href='#' onclick='return viewController.classView.hideInfo(" + this.index + ");'>";
		if(info.label == "")
			str = str + info.uri + "</a><br />";
		else
			str = str + info.label + "</a><br />";
		str = str + "<span style='font-size:16px;font-weight:normal;max-width:100%;background-color:#fff;'>";
		str = str + info.uri + "<br />";
		str = str + "<ul><li id='classItem" + this.index + "Supers'><a class='sublistitem' href='#' onclick='return viewController.classView.expandSupers(" + this.index + ");'>Superclasses</a></li>";
		str = str + "<li id='classItem" + this.index + "Subs'><a class='sublistitem' href='#' onclick='return viewController.classView.expandSubs(" + this.index + ");'>Subclasses</a></li>";
		str = str + "<li id='classItem" + this.index + "Instances'><a class='sublistitem' href='#' onclick='return viewController.classView.expandInstances(" + this.index + ");'>Instances</a></li></ul>";
		str = str + "</span>";		classitem.innerHTML = str;
	}
	this.clicked = function() {
		viewController.showWaitDialog();
		modelController.classList.getClassInfo(this.index, new OM_Callback(this, this.displayInfo));
	}
}

function OM_ViewProperty(index) {
	this.index = index;
	this.info = null;
	this.hideInfo = function() {
		var listitem = document.getElementById('propItem' + this.index);
		listitem.style.height = "19px";
		var text = "<a class='listitem' href='#' onclick='return viewController.propertyView.clicked(" + this.index + ");'>";
		if(this.info.label == "")
			text = text + this.info.uri + "</a><br />";
		else
			text = text + this.info.label + "</a><br />";
		listitem.innerHTML = text;
		this.info = null;
	}
	this.expandSupers = function() {
		if(this.info == null) {
			viewController.showErrorDialog("An unknown error has occurred.");
			return;
		}
		var supers = this.info.getSupers();
		var propitem = document.getElementById('propItem' + this.index + 'Supers');
		propitem.innerHTML = '';
		propitem.style.height = 'auto';
		var str = "<a class='sublistitem' href='#' onclick='return viewController.propertyView.retractSupers(" + this.index + ");'>Superproperties</a><br />";
		str = str + "<span style='font-size:16px;font-weight:normal;max-width:100%;background-color:#fff;'>";
		if(typeof(supers[0]) == "string") {
			str = str + supers[0] + "<br />";
		}
		else {
			for(i=0;i<supers.length;i++) {
				str = str + "<a href='#' onclick='return viewController.switchToProperty(" + '"' + supers[i][0] + '"' + ");'>";
				if(supers[i][1] == "")
					str = str + supers[i][0] + "</a><br />";
				else
					str = str + supers[i][1] + "</a><br />";
			}
		}
		str = str + "</span>";
		propitem.innerHTML = str;
	}
	this.retractSupers = function() {
		var propitem = document.getElementById('propItem' + this.index + 'Supers');
		propitem.innerHTML = "<a class='sublistitem' href='#' onclick='return viewController.propertyView.expandSupers(" + this.index + ");'>Superproperties</a>";
		propitem.style.height = '19px';
	}
	this.expandSubs = function() {
		if(this.info === undefined) {
			viewController.showErrorDialog("An unknown error has occurred.");
			return;
		}
		var subs = this.info.getSubs();
		var propitem = document.getElementById('propItem' + this.index + 'Subs');
		propitem.innerHTML = '';
		propitem.style.height = 'auto';
		var str = "<a class='sublistitem' href='#' onclick='return viewController.propertyView.retractSubs(" + this.index + ");'>Subproperties</a><br />";
		str = str + "<span style='font-size:16px;font-weight:normal;max-width:100%;background-color:#fff;'>";
		if(typeof(subs[0]) == "string") {
			str = str + subs[0] + "<br />";
		}
		else {
			for(i=0;i<subs.length;i++) {
				str = str + "<a href='#' onclick='return viewController.switchToProperty(" + '"' + subs[i][0] + '"' + ");'>";
				if(subs[i][1] == "")
					str = str + subs[i][0] + "</a><br />";
				else
					str = str + subs[i][1] + "</a><br />";
			}
		}
		str = str + "</span>";
		propitem.innerHTML = str;
	}
	this.retractSubs = function() {
		var propitem = document.getElementById('propItem' + this.index + 'Subs');
		propitem.innerHTML = "<a class='sublistitem' href='#' onclick='return viewController.propertyView.expandSubs(" + this.index + ");'>Subproperties</a>";
		propitem.style.height = '19px';
	}
	this.expandExtras = function() {
		if(this.info === undefined) {
			displayError("An unknown error has occurred.");
			return;
		}
		var extras = this.info.getExtras();
		var propitem = document.getElementById('propItem' + this.index + 'Extras');
		propitem.innerHTML = '';
		propitem.style.height = 'auto';
		var str = "<a class='sublistitem' href='#' onclick='return viewController.propertyView.retractExtras(" + this.index + ");'>Extras</a><br />";
		str = str + "<span style='font-size:16px;font-weight:normal;max-width:100%;background-color:#fff;'>";
		if(extras.length == 0)
			str = str + "<i>None</i>";
		else for(i=0;i<extras.length;i++) {
			str = str + extras[i] + "<br />";
		}
		str = str + "</span>";
		propitem.innerHTML = str;
	}
	this.retractExtras = function() {
		var propitem = document.getElementById('propItem' + this.index + 'Extras');
		propitem.innerHTML = "<a class='sublistitem' href='#' onclick='return viewController.propertyView.expandExtras(" + this.index + ");'>Extras</a>";
		propitem.style.height = '19px';
	}
	this.displayInfo = function(sender, info) {
		viewController.hideWaitDialog();
		if(typeof(info)=="string") {
			viewController.showErrorDialog(info);
			return;
		}
		if(info===undefined) {
			viewController.showErrorDialog("This property doesn't seem to exist in this ontology.");
			return;
		}
		this.info = info;
		var propitem = document.getElementById('propItem' + this.index);
		propitem.style.height = 'auto';
		var str = "<a class='listitem' href='#' onclick='return viewController.propertyView.hideInfo(" + this.index + ");'>";
		if(info.label == "")
			str = str + info.uri + "</a><br />";
		else
			str = str + info.label + "</a><br />";
		str = str + "<span style='font-size:16px;font-weight:normal;width:100%;background-color:#fff;'>";
		str = str + info.uri + "<br />";
		str = str + info.proptype + "<br />";
		str = str + "Domain: ";
		if(typeof(info.domain)!="string") {
			str = str + "<a href='#' onclick='return viewController.switchToClass(" + '"' + info.domain[0] + '"' + ");'>";
			if(info.domain[1] == "")
				str = str + info.domain[0] + "</a><br />";
			else
				str = str + info.domain[1] + "</a><br />";
		}
		else {
			str = str + info.domain + "<br />";
		}
		str = str + "Range: ";
		if(typeof(info.range)!="string") {
			str = str + "<a href='#' onclick='return viewController.switchToClass(" + '"' + info.range[0] + '"' + ");'>";
			if(info.range[1] == "")
				str = str + info.range[0] + "</a><br />";
			else
				str = str + info.range[1] + "</a><br />";
		}
		else {
			str = str + info.range + "<br />";
		}
		if(typeof(info.inverse)!="string") {
			str = str + "Inverse of <a href='#' onclick='return viewController.switchToProperty(" + '"' + info.inverse[0] + '"' + ");'>";
			if(info.inverse[1] == "")
				str = str + info.inverse[0] + "</a><br />";
			else
				str = str + info.inverse[1] + "</a><br />";
		}
		str = str + "<ul><li id='propItem" + this.index + "Supers'><a class='sublistitem' href='#' onclick='return viewController.propertyView.expandSupers(" + this.index + ");'>Superproperties</a></li>";
		str = str + "<li id='propItem" + this.index + "Subs'><a class='sublistitem' href='#' onclick='return viewController.propertyView.expandSubs(" + this.index + ");'>Subproperties</a></li>";
		str = str + "<li id='propItem" + this.index + "Extras'><a class='sublistitem' href='#' onclick='return viewController.propertyView.expandExtras(" + this.index + ");'>Extras</a></li></ul>";
		str = str + "</span>";
		propitem.innerHTML = str;
	}
	this.clicked = function() {
		viewController.showWaitDialog();
		modelController.propertyList.getPropertyInfo(this.index, new OM_Callback(this, this.displayInfo));
	}
}

function OM_ViewIndividual(index) {
	this.index = index;
	this.info = null;
	this.hideInfo = function() {
		var listitem = document.getElementById('instItem' + this.index);
		listitem.style.height = "19px";
		var text = "<a class='secondaryWLink' href='#' onclick='return viewController.switchToClass(" + '"' + this.info.typeuri + '"' + ");'>";
		if(this.info.typelabel == "")
			text = text + "View class</a><br />";
		else
			text = text + this.info.typelabel + "</a>";
		text = text + "<a class='listitem' href='#' onclick='return viewController.individualView.clicked(" + this.index + ");'>";
		if(this.info.label == "")
			text = text + this.info.uri + "</a><br />";
		else
			text = text + this.info.label + "</a><br />";
		listitem.innerHTML = text;
	}
	this.expandProperty = function(propid) {
		if(this.info === undefined) {
			viewController.showErrorDialog("An unknown error has occurred.");
			return;
		}
		var values = this.info.getPropertyValuesByIndex(propid);
		var prop = this.info.getPropertyByIndex(propid);
		if(!values) {
			viewController.showErrorDialog("An unknown error has occurred.");
			return;
		}
		var institem = document.getElementById('instItem' + this.index + 'Prop' + propid);
		institem.innerHTML = '';
		institem.style.height = 'auto';
		var str = "<a class='secondaryWLink' href='#' onclick='return viewController.switchToProperty(" + '"' + prop[0] + '"' + ");'>View</a>";
		str = str + "<a class='sublistitem' href='#' onclick='return viewController.individualView.retractProperty(" + this.index + ", " + propid + ");'>";
		if(prop[1] == "") str = str + prop[0] + "</a><br />";
		else str = str + prop[1] + "</a><br />";
		str = str + "<span style='font-size:16px;font-weight:normal;max-width:100%;background-color:#fff;'>";
		for(i=0;i<values.length;i++) {
			if(values[i][2] == "false")
				str = str + "<a href='#' onclick='viewController.switchToClass(" + '"' + values[i][0] + '"' + ");'>";
			if(values[i][1] == "")
				if(matchNumber(values[i][0])===false)
					str = str + values[i][0];
				else
					str = str + "<a href='tel:" + values[i][0] + "'>" + values[i][0] + "</a>";
			else
				str = str + values[i][1];
			if(values[i][2] == "false")
				str = str + "</a>";
			str = str + "<br />";
		}
		str = str + "</span>";
		institem.innerHTML = str;
	}
	this.retractProperty = function(propid) {
		var prop = this.info.getPropertyByIndex(propid);
		var institem = document.getElementById('instItem' + this.index + 'Prop' + propid);
		var str = "<a class='secondaryWLink' href='#' onclick='return viewController.switchToProperty(" + '"' + prop[0] + '"' + ");'>";
		str = str + "View</a>";
		str = str + "<a class='sublistitem' href='#' onclick='return viewController.individualView.expandProperty(" + this.index + ", " + propid + ");'>";
		if(prop[1] == "") str = str + prop[0] + "</a><br />";
		else str = str + prop[1] + "</a><br />";
		institem.innerHTML = str;
		institem.style.height = '19px';
	}
	this.displayInfo = function(sender, info) {
		viewController.hideWaitDialog();
		if(typeof(info) == "string") {
			viewController.showErrorDialog(info);
			return;
		}
		this.info = info;
		var institem = document.getElementById('instItem' + this.index);
		institem.innerHTML = '';
		institem.style.height = 'auto';
		var str = "<a class='secondaryWLink' href='#' onclick='return viewController.switchToClass(" + '"' + info.typeuri + '"' + ");'>";
		if(info.typelabel == "")
			str = str + "View class</a><br />";
		else
			str = str + info.typelabel + "</a>";
		str = str + "<a class='listitem' href='#' onclick='return viewController.individualView.hideInfo(" + this.index + ");'>";
		if(info.label == "")
			str = str + info.uri + "</a><br />";
		else
			str = str + info.label + "</a><br />";
		str = str + "<span style='font-size:16px;font-weight:normal;width:100%;background-color:#fff;'>";
		str = str + info.uri + "<br />";
		str = str + "<ul>";
		var props = info.properties;
		for(i=0;i<props.length;i++) {
			str = str + "<li id='instItem" + this.index + "Prop" + i + "'>";
			str = str + "<a class='secondaryWLink' href='#' onclick='return viewController.switchToProperty(" + '"' + props[i][0] + '"' + ");'>";
			str = str + "View</a>";
			str = str + "<a class='sublistitem' href='#' onclick='return viewController.individualView.expandProperty(" + this.index + ", " + i + ");'>";
			if(props[i][1] == "") str = str + props[i][0] + "</a></li>";
			else str = str + props[i][1] + "</a></li>";
		}
		str = str + "</ul>";
		str = str + "</span>";
		institem.innerHTML = str;
	}
	this.clicked = function() {
		viewController.showWaitDialog();
		modelController.individualList.getIndividualInfo(this.index, new OM_Callback(this, this.displayInfo));
	}
}

function OM_ViewTabManager() {
	this.viewTab = document.getElementById("viewTab");
	this.loadTab = document.getElementById("loadTab");
	this.optionTab = document.getElementById("optionTab");
	this.classTab = document.getElementById("classTab");
	this.propertyTab = document.getElementById("propertyTab");
	this.individualTab = document.getElementById("individualTab");
	this.oldtab = 'loadTab';
	this.subtab = 'classTab';
	this.selectTab = function(tab) {
		tab.style.WebkitBorderImage = "url(tabSelected.png) 0 14 0 14";
		tab.style.color = "#000";
		tab.style.textShadow = "#fff 0px 0.1em 0.1em";
	}
	this.deselectTab = function(tab) {
		tab.style.WebkitBorderImage = "url(tabUnselected.png) 0 14 0 14";
		tab.style.color = "#fff";
		tab.style.textShadow = "#000 0px 0.1em 0.1em";
	}
	this.switchToTab = function(tab, primary) {
		if(tab == this.oldtab) return;
		if(tab == "classTab") viewController.switchToView(viewController.classView);
		else if(tab == "propertyTab") viewController.switchToView(viewController.propertyView);
		else if(tab == "individualTab") viewController.switchToView(viewController.individualView);
		else if(tab == "optionTab") viewController.switchToView(viewController.optionView);
		else if(tab == "viewTab") this.switchToTab(this.viewtab, false);
		else if(tab == "loadTab") viewController.switchToView(viewController.loadView);
		return false;
	}
	this.setActiveTab = function(tab) {
		this.deselectTab(this.viewTab);
		this.deselectTab(this.loadTab);
		this.deselectTab(this.optionTab);
		this.deselectTab(this.classTab);
		this.deselectTab(this.propertyTab);
		this.deselectTab(this.individualTab);
		if(tab == 'classTab' || tab == 'propertyTab' || tab == 'individualTab') {
			this.selectTab(this.viewTab);
			if(tab == 'classTab') this.selectTab(this.classTab);
			else if(tab == 'propertyTab') this.selectTab(this.propertyTab);
			else if(tab == 'individualTab') this.selectTab(this.individualTab);
			this.oldtab = 'viewTab';
			this.subtab = tab;
			return;
		}
		if(tab == 'viewTab') {
			this.setActiveTab(this.subtab);
			return;
		}
		if(tab == 'loadTab')
			this.selectTab(this.loadTab);
		else if(tab == 'optionTab')
			this.selectTab(this.optionTab);
		this.oldtab = tab;
	}
}

function OM_ViewLoadView() {
	this.display = function(area) {
		viewController.showWaitDialog();
		var temp = function(text) {
			viewController.hideWaitDialog();
			area.innerHTML = text;
			this.populateDocuments();
			viewController.tabManager.setActiveTab('loadTab');
		}
		sendRequest(this, temp, "GET", "loadPage.php", true, "")
	}
	this.populateDocuments = function() {
		var ontlist = document.getElementById('ontologyList');
		var ontcount = modelController.ontologyManager.getOntologyCount();
		if(ontcount==0) {
			ontlist.innerHTML = "<i>None</i>";
			return;
		}
		ontlist.innerHTML = '';
		var str = "<ul>";
		for(i=0;i<ontcount;i++) {
			var onto = modelController.ontologyManager.getOntology(i);
			var myoid = onto.oid;
			var myuri = onto.uri;
			str = str + "<li id='ontologyItem" + myoid + "'>";
			str = str + "<a class='listitem' href='#' onclick='return viewController.loadView.viewOntInfo(" + myoid + ");'>" + shortenURI(myuri) + "</a>";
			str = str + "</li>"
		}
		str = str + "</ul>"
		ontlist.innerHTML = str;
	}
	this.addedOntology = function(sender, data) {
		viewController.hideWaitDialog();
		this.populateDocuments();
	}
	this.addOntology = function(useselect) {
		var text="";
		var name="";
		if(useselect==true)
			text = document.getElementById('selectowldocument').value;
		else {
			text = document.getElementById('openowldocument').value;
			name = prompt("Please give a name to this ontology:", "");
		}
		document.getElementById('openowldocument').value = "";
		viewController.showWaitDialog();
		modelController.ontologyManager.loadOntology(text, name, new OM_Callback(this, this.addedOntology));
	}
	this.removedOntology = function(sender, data) {
		viewController.hideWaitDialog();
		if(data!=null) {
			viewController.showErrorDialog(data);
			return;
		}
		this.populateDocuments();
	}
	this.removeOntology = function(oid) {
		viewController.showWaitDialog();
		modelController.ontologyManager.removeOntology(oid, new OM_Callback(this, this.removedOntology));
	}
	this.hideOntInfo = function(myoid) {
		var listitem = document.getElementById('ontologyItem' + myoid);
		listitem.style.height = "19px";
		listitem.innerHTML = "<a class='listitem' href='#' onclick='return viewController.loadView.viewOntInfo(" + myoid + ");'>" + shortenURI(modelController.ontologyManager.getOntologyById(myoid).uri) + "</a><br />";
	}
	this.displayOntInfo = function(sender, info) {
		viewController.hideWaitDialog();
		var myoid = sender.oid;
		var listitem = document.getElementById('ontologyItem' + myoid);
		var totProps = info.datapropcount + info.objpropcount;
		listitem.innerHTML = '';
		listitem.style.height = "auto";
		var str = "<a class='listitem' href='#' onclick='return viewController.loadView.hideOntInfo(" + myoid + ");'>" + shortenURI(sender.uri) + "</a><br />";
		str = str + "<span style='font-weight:normal;width:100%;background-color:#fff;'><table width='100%'>";
		str = str + "<tr>";
		str = str + "<td>Classes: " + info.classcount + "</td>";
		str = str + "</tr><tr>"
		str = str + "<td>Properties: " + totProps + "</td>";
		str = str + "</tr><tr>"
		str = str + "<td>&nbsp;&nbsp;&nbsp;Data Props.: " + info.datapropcount + "</td>";
		str = str + "</tr><tr>"
		str = str + "<td>&nbsp;&nbsp;&nbsp;Object Props.: " + info.objpropcount + "</td>";
		str = str + "</tr><tr>"
		str = str + "<td>Individuals: " + info.instcount + "</td>";
		str = str + "</tr><tr>"
		str = str + "<td>Inconsistent reasoner? " + info.invalid + "</td>"
		str = str + "</tr></table></span>";
		str = str + "<span class='grayButton button' style='float:right;' onclick='viewController.loadView.removeOntology(" + myoid + ");'>Remove</span><br /><br />"
		listitem.innerHTML = str;
	}
	this.viewOntInfo = function(myoid) {
		viewController.showWaitDialog();
		modelController.ontologyManager.getOntologyById(myoid).getInfo(new OM_Callback(this, this.displayOntInfo));
	}
}

function OM_ViewOptionView() {
	this.selectPerPage = function(view) {
		var str;
		str = '<option' + (view.perPage == 5 ? ' selected' : '') + '>5</option>'
		str = str + '<option' + (view.perPage == 10 ? ' selected' : '') + '>10</option>'
		str = str + '<option' + (view.perPage == 20 ? ' selected' : '') + '>20</option>'
		str = str + '<option' + (view.perPage == -1 ? ' selected' : '') + '>All</option>'
		return str;
	}
	this.display = function(area) {
		var str = '<font size="6">Classes per page: </font><select id="doClassesPerPage" onchange="CPPChanged();">';
		str = str + this.selectPerPage(viewController.classView);
		str = str + "</select><br /><br />"
		str = str + '<font size="6">Properties per page: </font><select id="doPropertiesPerPage" onchange="return PPPChanged();">'
		str = str + this.selectPerPage(viewController.propertyView);
		str = str + "</select><br /><br />"
		str = str + '<font size="6">Individuals per page: </font><select id="doIndividualsPerPage" onchange="return IPPChanged();">'
		str = str + this.selectPerPage(viewController.individualView);
		str = str + "</select><br /><br />"
		str = str + '<font size="6">Cache Ontology Queries: </font>'
		str = str + '<input type="Checkbox" style="height:32px;width:32px;" id="doCache" onchange="return CacheChanged();" ' + (checkAlways ? '' : 'checked ') + '/>'
		area.innerHTML = str;
		viewController.tabManager.setActiveTab("optionTab");
	}
}

function OM_ViewClassView() {
	this.perPage = 10;
	this.startindex = 0;
	this.classList = [];
	this.doList = function(thediv) {
		thediv.innerHTML = '';
		var mydivtext = ''
		var count;
		if(this.perPage == -1 || this.perPage >= this.classList.length) {
			count = this.classList.length;
			mydivtext = "<center>1 - " + count + " of " + count + "</center><br />";
		}
		else {
			count = this.perPage;
			mydivtext = "<center><div class='backButton button' id='backButton' onclick='viewController.currentView.doBackward();'>Backward</div>";
			mydivtext = mydivtext + (this.startindex+1) + " - ";
			if(this.startindex+count > this.classList.length) {
				mydivtext = mydivtext + this.classList.length;
				count = this.classList.length - this.startindex;
			}
			else
				mydivtext = mydivtext + (this.startindex+count);
			mydivtext = mydivtext + " of " + this.classList.length;
			mydivtext = mydivtext + "<div class='forwardButton button' id='forwardButton' onclick='viewController.currentView.doForward();'>Forward</div></center><br/>";
		}
		mydivtext = mydivtext + "<ul>"
		for(var i=0;i<count;i++) {
			mydivtext = mydivtext + "<li id='classItem" + (this.startindex+i) + "'><a class='listitem' href='#' onclick='viewController.classView.clicked(" + (this.startindex + i) + ");'>";
			var info = modelController.classList.getClass(this.startindex+i);
			if(info.label == "") {
				mydivtext = mydivtext + info.uri + "</a></li>";
			}
			else {
				mydivtext = mydivtext + info.label + "</a></li>";
			}
		}
		mydivtext = mydivtext + "</ul>"
		thediv.innerHTML = mydivtext;
	}
	this.display = function(area) {
		var temp = function(sender, data) {
			this.display2(area);
		}
		modelController.getClassList(new OM_Callback(this, temp));
	}
	this.display2 = function(area) {
		if(this.classList.length != modelController.classList.length) {
			this.classList = new Array(modelController.classList.length);
			for(var i=0;i<this.classList.length;i++)
				this.classList[i] = new OM_ViewClass(i);
		}
		this.doList(area);
		viewController.tabManager.setActiveTab("classTab");
	}
	this.switchToClass = function(uri, area) {
		var temp = function(sender, data) {
			this.switchToClass2(uri, area);
		}
		modelController.getClassList(new OM_Callback(this, temp));
	}
	this.switchToClass2 = function(uri, area) {
		if(this.classList.length != modelController.classList.length) {
			this.classList = new Array(modelController.classList.length);
			for(var i=0;i<this.classList.length;i++)
				this.classList[i] = new OM_ViewClass(i);
		}
		for(var i=0;i<modelController.classList.length;i++) {
			if(modelController.classList.getClass(i).uri == uri) {
				if(this.perPage == -1)
					this.startindex = 0;
				else {
					var index = i % this.perPage;
					this.startindex = (i - index);
				}
				this.doList(area);
				setTimeout("viewController.classView.clicked("+i+");",200);
				viewController.tabManager.setActiveTab("classTab");
				viewController.hideWaitDialog();
				return;
			}
		}
		if(uri.length < 7) return;
		if(uri.substring(0, 7) == "http://") {
			window.open(uri);
			return;
		}
		if(uri.substring(0, 7) == "mailto:") {
			window.location.href = uri;
			return;
		}
		if(matchNumber(uri) == true) {
			window.location.href = "tel:" + uri;
			return;
		}
	}
	this.clicked = function(clsid) {
		this.classList[clsid].clicked();
	}
	this.hideInfo = function(clsid) {
		this.classList[clsid].hideInfo();
	}
	this.expandSupers = function(clsid) {
		this.classList[clsid].expandSupers();
	}
	this.retractSupers = function(clsid) {
		this.classList[clsid].retractSupers();
	}
	this.expandSubs = function(clsid) {
		this.classList[clsid].expandSubs();
	}
	this.retractSubs = function(clsid) {
		this.classList[clsid].retractSubs();
	}
	this.expandInstances = function(clsid) {
		this.classList[clsid].expandInstances();
	}
	this.retractInstances = function(clsid) {
		this.classList[clsid].retractInstances();
	}
	this.doBackward = function() {
		var result = this.startindex - this.perPage;
		if(result < 0) result = 0;
		this.startindex = result;
		this.doList(document.getElementById("content"));
	}
	this.doForward = function() {
		if(this.perPage == -1) return;
		var result = this.startindex + this.perPage;
		if(result >= this.classList.length) return;
		this.startindex = result;
		this.doList(document.getElementById("content"));
	}
}

function OM_ViewPropertyView() {
	this.perPage = 10;
	this.startindex = 0;
	this.propertyList = [];
	this.doList = function(thediv) {
		thediv.innerHTML = '';
		var mydivtext = ''
		var count;
		if(this.perPage == -1 || this.perPage >= this.propertyList.length) {
			count = this.propertyList.length;
			mydivtext = "<center>1 - " + count + " of " + count + "</center><br />";
		}
		else {
			count = this.perPage;
			mydivtext = "<center><div class='backButton button' id='backButton' onclick='viewController.currentView.doBackward();'>Backward</div>";
			mydivtext = mydivtext + (this.startindex+1) + " - ";
			if(this.startindex+count > this.propertyList.length) {
				mydivtext = mydivtext + this.propertyList.length;
				count = this.propertyList.length - this.startindex;
			}
			else
				mydivtext = mydivtext + (this.startindex+count);
			mydivtext = mydivtext + " of " + this.propertyList.length;
			mydivtext = mydivtext + "<div class='forwardButton button' id='forwardButton' onclick='viewController.currentView.doForward();'>Forward</div></center><br/>";
		}
		mydivtext = mydivtext + "<ul>"
		for(var i=0;i<count;i++) {
			mydivtext = mydivtext + "<li id='propItem" + (this.startindex+i) + "'><a class='listitem' href='#' onclick='viewController.propertyView.clicked(" + (this.startindex + i) + ");'>";
			var info = modelController.propertyList.getProperty(this.startindex+i);
			if(info.label == "") {
				mydivtext = mydivtext + info.uri + "</a></li>";
			}
			else {
				mydivtext = mydivtext + info.label + "</a></li>";
			}
		}
		mydivtext = mydivtext + "</ul>"
		thediv.innerHTML = mydivtext;
	}
	this.display = function(area) {
		var temp = function(sender, data) {
			this.display2(area);
		}
		modelController.getPropertyList(new OM_Callback(this, temp));
	}
	this.display2 = function(area) {
		if(this.propertyList.length != modelController.propertyList.length) {
			this.propertyList = new Array(modelController.propertyList.length);
			for(var i=0;i<this.propertyList.length;i++)
				this.propertyList[i] = new OM_ViewProperty(i);
		}
		this.doList(area);
		viewController.tabManager.setActiveTab("propertyTab");
	}
	this.switchToProperty = function(uri, area) {
		var temp = function(sender, data) {
			this.switchToProperty2(uri, area);
		}
		modelController.getPropertyList(new OM_Callback(this, temp));
	}
	this.switchToProperty2 = function(uri, area) {
		if(this.propertyList.length != modelController.propertyList.length) {
			this.propertyList = new Array(modelController.propertyList.length);
			for(var i=0;i<this.propertyList.length;i++)
				this.propertyList[i] = new OM_ViewProperty(i);
		}
		/*alert(uri);*/
		for(var i=0;i<modelController.propertyList.length;i++) {
		/*alert(modelController.propertyList.getProperty(i).uri);*/
			if(modelController.propertyList.getProperty(i).uri == uri) {
				if(this.perPage == -1) {
					this.startindex = 0;
					this.doList(area);
					this.propertyList[i].clicked();
				}
				else {
					var index = i % this.perPage;
					this.startindex = (i - index);
					this.doList(area);
					this.propertyList[i].clicked();
				}
				viewController.tabManager.setActiveTab("propertyTab");
				return;
			}
		}
	}
	this.clicked = function(propid) {
		this.propertyList[propid].clicked();
	}
	this.hideInfo = function(propid) {
		this.propertyList[propid].hideInfo();
	}
	this.expandSupers = function(propid) {
		this.propertyList[propid].expandSupers();
	}
	this.retractSupers = function(propid) {
		this.propertyList[propid].retractSupers();
	}
	this.expandSubs = function(propid) {
		this.propertyList[propid].expandSubs();
	}
	this.retractSubs = function(propid) {
		this.propertyList[propid].retractSubs();
	}
	this.expandExtras = function(propid) {
		this.propertyList[propid].expandExtras();
	}
	this.retractExtras = function(propid) {
		this.propertyList[propid].retractExtras();
	}
	this.doBackward = function() {
		var result = this.startindex - this.perPage;
		if(result < 0) result = 0;
		this.startindex = result;
		this.doList(document.getElementById("content"));
	}
	this.doForward = function() {
		if(this.perPage == -1) return;
		var result = this.startindex + this.perPage;
		if(result >= this.propertyList.length) return;
		this.startindex = result;
		this.doList(document.getElementById("content"));
	}
}

function OM_ViewIndividualView() {
	this.perPage = 10;
	this.startindex = 0;
	this.individualList = [];
	this.doList = function(thediv) {
		thediv.innerHTML = '';
		var mydivtext = ''
		var count;
		if(this.perPage == -1 || this.perPage >= this.individualList.length) {
			count = this.individualList.length;
			mydivtext = "<center>1 - " + count + " of " + count + "</center><br />";
		}
		else {
			count = this.perPage;
			mydivtext = "<center><div class='backButton button' id='backButton' onclick='viewController.currentView.doBackward();'>Backward</div>";
			mydivtext = mydivtext + (this.startindex+1) + " - ";
			if(this.startindex+count > this.individualList.length) {
				mydivtext = mydivtext + this.individualList.length;
				count = this.individualList.length - this.startindex;
			}
			else
				mydivtext = mydivtext + (this.startindex+count);
			mydivtext = mydivtext + " of " + this.individualList.length;
			mydivtext = mydivtext + "<div class='forwardButton button' id='forwardButton' onclick='viewController.currentView.doForward();'>Forward</div></center><br/>";
		}
		mydivtext = mydivtext + "<ul>"
		for(var i=0;i<count;i++) {
			var info = modelController.individualList.getIndividual(this.startindex+i);
			mydivtext = mydivtext + "<li id='instItem" + (this.startindex+i) + "'>";
			mydivtext = mydivtext + "<a class='secondaryWLink' href='#' onclick='viewController.switchToClass(" + '"' + info.typeuri + '"' + ");'>";
			if(info.typelabel == "")
				mydivtext = mydivtext + "View Class</a>";
			else
				mydivtext = mydivtext + info.typelabel + "</a>";
			mydivtext = mydivtext + "<a class='listitem' href='#' onclick='viewController.individualView.clicked(" + (this.startindex + i) + ");'>";
			if(info.label == "") {
				mydivtext = mydivtext + info.uri + "</a></li>";
			}
			else {
				mydivtext = mydivtext + info.label + "</a></li>";
			}
		}
		mydivtext = mydivtext + "</ul>"
		thediv.innerHTML = mydivtext;
	}
	this.display = function(area) {
		var temp = function(sender, data) {
			this.display2(area);
		}
		modelController.getIndividualList(new OM_Callback(this, temp));
	}
	this.display2 = function(area) {
		if(this.individualList.length != modelController.individualList.length) {
			this.individualList = new Array(modelController.individualList.length);
			for(var i=0;i<this.individualList.length;i++)
				this.individualList[i] = new OM_ViewIndividual(i);
		}
		this.doList(area);
		viewController.tabManager.setActiveTab("individualTab");
	}
	this.switchToIndividual = function(uri, area) {
		var temp = function(sender, data) {
			this.switchToIndividual2(uri, area);
		}
		modelController.getIndividualList(new OM_Callback(this, temp));
	}
	this.switchToIndividual2 = function(uri, area) {
		if(this.individualList.length != modelController.individualList.length) {
			this.individualList = new Array(modelController.individualList.length);
			for(var i=0;i<this.individualList.length;i++)
				this.individualList[i] = new OM_ViewIndividual(i);
		}
		for(var i=0;i<modelController.individualList.length;i++) {
			if(modelController.individualList.getIndividual(i).uri == uri) {
				if(this.perPage == -1) {
					this.startindex = 0;
					this.doList(area);
					this.individualList[i].clicked();
				}
				else {
					var index = i % this.perPage;
					this.startindex = (i - index);
					this.doList(area);
					this.individualList[i].clicked();
				}
				viewController.tabManager.setActiveTab("individualTab");
				return;
			}
		}
	}
	this.clicked = function(instid) {
		this.individualList[instid].clicked();
	}
	this.hideInfo = function(instid) {
		this.individualList[instid].hideInfo();
	}
	this.expandProperty = function(instid, propid) {
		this.individualList[instid].expandProperty(propid);
	}
	this.retractProperty = function(instid, propid) {
		this.individualList[instid].retractProperty(propid);
	}
	this.doBackward = function() {
		var result = this.startindex - this.perPage;
		if(result < 0) result = 0;
		this.startindex = result;
		this.doList(document.getElementById("content"));
	}
	this.doForward = function() {
		if(this.perPage == -1) return;
		var result = this.startindex + this.perPage;
		if(result >= this.individualList.length) return;
		this.startindex = result;
		this.doList(document.getElementById("content"));
	}
}

function OM_ViewController() {
	this.tabManager = new OM_ViewTabManager();
	this.loadView = new OM_ViewLoadView();
	this.optionView = new OM_ViewOptionView();
	this.classView = new OM_ViewClassView();
	this.propertyView = new OM_ViewPropertyView();
	this.individualView = new OM_ViewIndividualView();
	this.currentView = this.loadView;
	this.switchToView = function(newView) {
		this.currentView = newView;
		this.currentView.display(document.getElementById("content"));
	}
	this.waitDialog = document.getElementById('waitdialog');
	this.showWaitDialog = function() {
		this.waitDialog.style.visibility = 'visible';
	}
	this.hideWaitDialog = function() {
		this.waitDialog.style.visibility = 'hidden';
	}
	this.errorDialog = document.getElementById('errordialog');
	this.showErrorDialog = function(msg) {
		var str = "<table width=100% height=100%><tr><td rowspan=2 valign='middle'><img src='error.png' /></td>";
		str = str + "<td colspan=2><b>An error occurred:</b><br />" + msg + "<tr><td></td>";
		str = str + "<td align=right><a href='#' onclick='viewController.hideErrorDialog();'>Close</a></td></tr></table>"
		this.errorDialog.innerHTML = str;
		this.errorDialog.style.visibility = "visible";
	}
	this.hideErrorDialog = function() {
		this.errorDialog.style.visibility = "hidden";
	}
	this.aboutWindow = document.getElementById('about');
	this.showAboutWindow = function() {
		this.aboutWindow.style.visibility = "visible";
		this.aboutWindow.style.opacity = "100%";
	}
	this.hideAboutWindow = function() {
		this.aboutWindow.style.opacity = "0%";
		this.aboutWindow.style.visibility = "hidden";
	}
	this.switchToClass = function(index) {
		this.currentView = this.classView;
		this.classView.switchToClass(index, document.getElementById("content"));
	}
	this.switchToProperty = function(index) {
		this.currentView = this.propertyView;
		this.propertyView.switchToProperty(index, document.getElementById("content"));
	}
	this.switchToIndividual = function(index) {
		this.currentView = this.individualView;
		this.individualView.switchToIndividual(index, document.getElementById("content"));
	}
	this.initialize = function(sender, data) {
		this.hideWaitDialog();
		if(!data.success) this.showErrorDialog(data.error);
	}
}

function CPPChanged() {
	var myctrl = document.getElementById('doClassesPerPage');
	if(myctrl.selectedIndex == 0) viewController.classView.perPage = 5;
	if(myctrl.selectedIndex == 1) viewController.classView.perPage = 10;
	if(myctrl.selectedIndex == 2) viewController.classView.perPage = 20;
	if(myctrl.selectedIndex == 3) viewController.classView.perPage = -1;
}

function PPPChanged() {
	var myctrl = document.getElementById('doPropertiesPerPage');
	if(myctrl.selectedIndex == 0) viewController.propertyView.perPage = 5;
	if(myctrl.selectedIndex == 1) viewController.propertyView.perPage = 10;
	if(myctrl.selectedIndex == 2) viewController.propertyView.perPage = 20;
	if(myctrl.selectedIndex == 3) viewController.propertyView.perPage = -1;
}

function IPPChanged() {
	var myctrl = document.getElementById('doIndividualsPerPage');
	if(myctrl.selectedIndex == 0) viewController.individualView.perPage = 5;
	if(myctrl.selectedIndex == 1) viewController.individualView.perPage = 10;
	if(myctrl.selectedIndex == 2) viewController.individualView.perPage = 20;
	if(myctrl.selectedIndex == 3) viewController.individualView.perPage = -1;
}

function CacheChanged() {
	var myctrl = document.getElementById('doCache');
	if(myctrl.checked == true) checkAlways = false;
	else checkAlways = true;
}

function initialize() {
	modelController = new OM_ModelController();
	viewController = new OM_ViewController();
	modelController.initialize(new OM_Callback(viewController, viewController.initialize));
}
