function Table(container, explicitInclude) {
	this.container = document.getElementById(container);
	
	this.explicitInclude = explicitInclude;
	this.prefix = container + "_";
} 

Table.prototype.setData = function (map, data) {
	this.data = data;
	this.map = map;
	
	this.createTable();
	this.selected = Array();
	this.excluded = Array();
	this.included = Array();
	
}

Table.prototype.performSearch = function (term) {
	term = term.toUpperCase();
		for(id in data) {
			var item = document.getElementById(this.prefix + id);
			if(item == undefined) {
				continue;
			}
			
			if(this.explicitInclude) {
				if(this.included.contains(id)) {
					css.removeClassFromElement(item, "hide");
					
				} else {
					css.addClassToElement(item, "hide");
				}
				
				return;
			}
			css.removeClassFromElement(item, "hide");
			
			match = false;
	
			if(!this.excluded.contains(id)) {
				for(var entry = 0; entry < mapping.length; entry++) {
					text = data[id][mapping[entry][1]];
					if(text == "" || (text != undefined && text.toUpperCase().match(term))) {
						match=true;
						break;
					}
				}
			}
			
			if(!match) {
				css.addClassToElement(item, "hide");
				css.removeClassFromElement(item, "selected");
			}			
		}
	
}

Table.prototype.createTable = function() {
	this.table = document.createElement("table");
	this.header = document.createElement("thead");
	this.tablebody = document.createElement("tbody");
	
	var tr = document.createElement("tr");
	for(var entry = 0; entry < mapping.length; entry++) {
		var e = document.createElement("th");
		e.appendChild(document.createTextNode(mapping[entry][0]));
		e.className = mapping[entry][1];
		tr.appendChild(e);
		
	}		
	this.header.appendChild(tr);
	
	for(id in data) {
		var tr = document.createElement("tr");
		tr.id = this.prefix + id;
		
		for(var entry = 0; entry < mapping.length; entry++) {
		    var e = document.createElement("td");
			e.appendChild(document.createTextNode(data[id][mapping[entry][1]]));
			tr.appendChild(e);
			
			if(this.explicitInclude) {
				css.addClassToElement(tr, "hide");
			}
			
			tr.onclick=toggleSelection
			tr.table = this;
		}
		
		this.tablebody.appendChild(tr);
	}
	
	this.table.appendChild(this.header);
	this.table.className += " sortable";
	this.table.appendChild(this.tablebody);
	this.container.appendChild(this.table);
}

toggleSelection = function (event) {
	if(!event) event = window.event;
	var item = (event.srcElement? event.srcElement : event.target).parentNode;
	if(item == null) {
		return;
	}
	
	id = item.id.replace(item.table.prefix, "");
	item.table.setSelected(id, !item.table.isSelected(id));		
	
}

Table.prototype.setSelected = function (id, state) {
	var item = document.getElementById(this.prefix + id);
	if(item == null) {
		return;
	}
	
	if(state) {
		this.selected.push(id);
		css.addClassToElement(item, "selected");
	} else {
		this.selected = this.selected.cut(id);
		css.removeClassFromElement(item, "selected");
	}
}

Table.prototype.removeSelected = function () {
	for(var i = 0; i < this.selected.length; i++) {
		itemid = this.selected[i];
		this.exclude(itemid);
	}
	this.clearSelection();
}

Table.prototype.exclude = function (id) {
	if(this.explicitInclude) {
		this.included = this.included.cut(id);
		this.hide(id);
		return;
	}
	if(!this.excluded.contains(id)) {
		this.excluded.push(id);
		this.hide(id);
	}
}

Table.prototype.include = function (id) {
	if(!this.explicitInclude) {
		this.excluded = this.excluded.cut(id);
		this.show(id);
		return;
	}
	if(!this.included.contains(id)) {
		this.included.push(id);
		this.show(id);
	}
}

Table.prototype.clearSelection = function () {
	for(var i=0; i < this.selected.length; i++) {
		css.removeClassFromId(this.prefix + this.selected[i], "selected");
	}
	this.selected=Array();
}

Table.prototype.hide = function (id) {
	var item = document.getElementById(this.prefix + id);
	if(item != null) {
		css.addClassToElement(item, "hide");
	}	
}

Table.prototype.show = function (id) {
	var item = document.getElementById(this.prefix + id);
	if(item != null) {
		css.removeClassFromElement(item, "hide");
	}	
}

Table.prototype.getSelection = function () {
	return this.selected;
}

Table.prototype.isSelected = function (id) {
	return this.selected.contains(id);
}

Table.prototype.getIncluded = function() {
	return this.included;
}
