
function jobPositionComponent(placeHolder, label, dataSet) {
	var self = this;
	this.selections = [];
	
	this.updateSelection = function() {
		this.divSelection.innerHTML = '';
		this.divSelection.style.display = (this.selections.length>0) ? "block" : "none";
		this.selections.each(function(item){
			var table = document.createElement("table");
			table.style.padding = "0";
			table.setAttribute("cellspacing", 0);
			table.style.width = "100%";
			table.style.borderBottom = "1px solid #cbd2e7";
			table.style.borderRight = "1px solid #cbd2e7";
			table.style.backgroundColor = "#EFF5FF";
			var tbody = document.createElement("tbody");
			var tr = document.createElement("tr");
			
			var td1 = document.createElement("td");
			td1.style.padding = "4px 3px 4px 6px";
			
			if (item.lIdJobPositionParent==0) {
				td1.innerHTML = "<b>"+item.sLabel+"</b>";
			} else {
				td1.innerHTML = "<b>"+item.sLabelJobPositionParent+"</b> / "+item.sLabel;
			}
				
			tr.appendChild(td1);
			
			var td2 = document.createElement("td");
			td2.style.width = "16px";
			td2.style.padding = "2px 2px 0 0";
			var imgDelete = document.createElement("img");
			imgDelete.src = rootPath+"include/images/icons/delete.png";	
			imgDelete.style.cursor = "pointer";
			imgDelete.onclick = function() {
			
				dataSet.each(function(itemMain){
					try {
						itemMain.subItems.each(function(itemChild){
							if (item.lId==itemChild.lId) {
								itemChild.treeItem.checkbox.checked = false;
								throw $break;
							}
						});
					} catch(e){}
				});
				
				try {item.treeItem.checkbox.checked = false;} catch(e){}
				
				if (item.lIdJobPositionParent==0) {
					if (item.treeItem) {
						onSelectMainJC(item);
					} else {
						removeItemSelection(item);
					}
				} else {
					if (item.treeItem) {
						onSelectChildJC(item);
					} else {
						removeItemSelection(item);
					}
				}
				
				self.updateSelection();
			}
			td2.appendChild(imgDelete);
			tr.appendChild(td2);
			
			tbody.appendChild(tr);
			table.appendChild(tbody);
			self.divSelection.appendChild(table);
		});
	}
	
	
	function onSelectMainJC(item) {
		if (item.treeItem.subDom.hasChildNodes()) {
			item.subItems.each(function(subItem){
				subItem.treeItem.checkbox.checked = item.treeItem.checkbox.checked;
			});
		}
		
		if (item.treeItem.checkbox.checked) {
			try {
				item.subItems.each(function(subItem){removeItemSelection(subItem);});
			} catch(e){}
			self.selections.push(item);
		} else {
			removeItemSelection(item);
		}
	}
	
	function onSelectChildJC(item) {
		if (item.treeItem.checkbox.checked) {
			/*if (isFullSelection(item.parent)) {
				item.parent.treeItem.checkbox.checked = true;
				item.parent.subItems.each(function(subItem){removeItemSelection(subItem);});
				self.selections.push(item.parent);
			} else {*/
			
				self.selections.push(item);
			//}
		} else {
			item.parent.subItems.each(function(subItem){
				removeItemSelection(subItem);
				if (subItem.treeItem.checkbox.checked) {
					self.selections.push(subItem);
				}
			});
			removeItemSelection(item.parent);
			item.parent.treeItem.checkbox.checked = false;
		}
	}
	
	function isFullSelection(item) {	
		var b = true;
		item.subItems.each(function(item){
			if (!item.treeItem.checkbox.checked) {
				b = false;throw $break;
			}
		});
		return b;
	}
	
	function removeItemSelection(item) {
		self.selections.each(function(sel, index){
			if (sel.type==item.type && sel.lId==item.lId) {
				self.selections.splice(index, 1);
				throw $break;
			}
		});
	}
	
	
	function isItemOnSelection(item) {
		var b = false;
		self.selections.each(function(sel){
			if (sel.type==item.type && sel.lId==item.lId) {
				b = true;
				throw $break;
			}
		});
		return b;
	}
	
	function loadChilds(itemJC) {
		JobPosition.getJSONArrayFromParent(itemJC.lId, lang, function(sJSON){
			itemJC.subItems = sJSON.evalJSON();
			itemJC.treeItem.subDom.innerHTML = (itemJC.subItems==0) ? 'vide' : '';
			itemJC.subItems.each(function(item){
				item.type = "childJC";
				item.parent = itemJC;
				item.treeItem = new TreeItem(item);
				if (isItemOnSelection(item)) {
					item.treeItem.checkbox.checked = true;
				} else {
					item.treeItem.checkbox.checked = itemJC.treeItem.checkbox.checked;					
				}
				itemJC.treeItem.subDom.appendChild(item.treeItem.domNode);
			});
		})
	}
	
	function TreeItem(item) {
		var selfItem = this;
	
		this.domNode = document.createElement("div");
		mt.dom.disableSelection(this.domNode);
		this.domNode.className = "item";
		
		var divLabel = document.createElement("div");
		
		var imgIco = document.createElement("img");
		imgIco.src = rootPath+"include/images/closeTreeItem.gif";
		imgIco.className = "middle";

		this.checkbox = document.createElement("input");
		this.checkbox.type = "checkbox";
		this.checkbox.style.overflow = "hidden";
		this.checkbox.style.width = "13px";
		this.checkbox.style.height = "13px";
		this.checkbox.onclick = function() {
			if (item.type=="mainJC") {
				onSelectMainJC(item);
			} else if (item.type=="childJC") {
				onSelectChildJC(item);
			}
			self.updateSelection();
		}
		this.checkbox.className = "middle";
		
		var label;
		if (item.type=="mainJC") {
			label = document.createElement("a");
			label.href = "javascript:void(0)";
			label.onclick = function() {
				if (selfItem.subDom.style.display=="none") {
					if (!selfItem.subDom.hasChildNodes()) {
						selfItem.subDom.innerHTML = "chargement...";
						loadChilds(item);
					}
					imgIco.src = rootPath+"include/images/openTreeItem.gif";
					selfItem.subDom.style.display="block";
				} else {
					imgIco.src = rootPath+"include/images/closeTreeItem.gif";
					selfItem.subDom.style.display="none";
				}
			}
		} else {
			label = document.createElement("span");
		}
		label.className = "middle";
		label.innerHTML = item.sLabel;
		
		this.subDom = document.createElement("div");
		this.subDom.style.display = "none";

		this.subDom.style.marginLeft = "30px";
		
		if (item.type=="mainJC") divLabel.appendChild(imgIco);
		label.style.paddingLeft = "3px";
		if (item.type=="childJC") divLabel.appendChild(this.checkbox);
		divLabel.appendChild(label);
		
		this.domNode.appendChild(divLabel);
		this.domNode.appendChild(this.subDom);
	}
	
	this.build = function(placeHolder, divSelection) {
		this.divSelection = divSelection;		
		
		var div = document.createElement("div");
		div.style.backgroundColor = "#FFF";
		div.style.padding = "4px";
		div.style.border = "1px solid #333";
		div.style.borderTop = "1px solid #888";
		div.style.borderLeft = "1px solid #888";
		div.style.width = "320px";
		div.style.height = "200px";
		div.style.overflow = "auto";
				
		Event.observe(div, 'click', function(e){
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
		})
		
		placeHolder.appendChild(div);
		
		dataSet.each(function(item){
			item.type = "mainJC";
			item.treeItem = new TreeItem(item);
			div.appendChild(item.treeItem.domNode);
		});
	}
	
	this.getSelections = function() {
		var dataset = [];
		this.selections.each(function(item){dataset.push(item.lId)});
		return dataset;
	}
	
	this.populate = function(dataSet) {
		this.selections = dataSet;
		this.updateSelection();
	}
	
	this.buildCombo = function(){
		
		var container = document.createElement("div");
		container.className = "positionComponent";
		mt.dom.disableSelection(container);
		
		container.onmouseover = function(){
			divImg.style.backgroundPosition = "0 -17px";
		}
		container.onmouseout = function(){
			divImg.style.backgroundPosition = "0 0";
		}
		
		Event.observe(container, 'click', function(e){
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
			$$('.comboComponentBox').each(function(item){Element.hide(item);});
			Element.show(divTree);
		});
		
		Event.observe(document, 'click', function(e){
			Element.hide(divTree);
		});
		
		var divImg = document.createElement("div");
		divImg.className = "comboImg";
		
		var divLabel = document.createElement("div");
		divLabel.style.padding = "2px 0 2px 4px";
		divLabel.innerHTML = label;
		
		container.appendChild(divImg);
		container.appendChild(divLabel);
		
		var divTree = document.createElement("div");
		divTree.className = "comboComponentBox";
		divTree.style.display = "none";
		divTree.style.position = "absolute";
		divTree.style.zIndex = 99;
		
		
		var divSelection = document.createElement("div");
		divSelection.style.padding = "2px 0 4px";
		divSelection.style.display = "none";
		
		self.build(divTree, divSelection);
		
		var p = $(placeHolder);
		p.style.paddingBottom = "2px";
		p.appendChild(container);
		p.appendChild(divTree);
		p.appendChild(divSelection);
		
	}
	
	this.buildCombo();

}

