Index: /main/trunk/greenstone3/web/interfaces/default/js/hierarchy.js
===================================================================
--- /main/trunk/greenstone3/web/interfaces/default/js/hierarchy.js	(revision 31062)
+++ /main/trunk/greenstone3/web/interfaces/default/js/hierarchy.js	(revision 31063)
@@ -9,5 +9,5 @@
 }
 // Function to set Id as TEXTAREA value
-function setHierarchyId(a)
+function chooseHierarchyOption(a)
 {
 	
@@ -23,12 +23,9 @@
 		$(tr.getElementsByTagName("TEXTAREA")).val(metaValue);
 		//	Set button name
-		removeSuggestionsMenu(tr, metaTitle);
+		setHierarchyButtonText(tr, metaTitle);
+		removeSuggestionsMenu(tr);
 		//Hide menu after click
 		$(tr).find(".metaDataHierarchyMenu").find("ul li ul li").hide();
-		//If we left TEXTAREA, hide all menus
-		//if (document.activeElement.tagName != "TEXTAREA")
-		//{
-		//	$(tr).find(".metaDataHierarchyMenu").hide();
-		//}
+		//Set focus on textarea
 		tr.getElementsByTagName("TEXTAREA")[0].focus();
 		
@@ -39,9 +36,7 @@
 function openHierarchyMenuLevel(menuItem)
 {
-        //console.log(menuItem);
-			
-			var tr = findAncestorByTagName(menuItem,"TR");
-			//get current MetaDataName
-			var metaName = $(tr.getElementsByClassName("metaTableCellName")[0]).text();
+		   var tr = findAncestorByTagName(menuItem,"TR");
+		   //get current MetaDataName
+		   var metaName = getMetaName(tr);
 		   //Get current hierarchy from storages
 		   var hierarchyData = hierarchyStorage[metaName];
@@ -58,7 +53,5 @@
            		 {
            		 	id = menuItem.attr('id').match(getIdExp);
-					 }
-                //console.log("ID " + id);
-                //id.match(getIdExp);
+			     }
                 if (id == null)
                 {
@@ -73,10 +66,8 @@
                 {
    					if(childExpr.test(key)){
-   						levelItems[key]='<li id="'+key+'" ><button onclick="setHierarchyId(this)"  metavalue='+ hierarchyData[key][0] +' metatitle='+ hierarchyData[key][1] +'>' + hierarchyData[key][1] + '</button></li>';
-      					//console.log(levelItems[key]);
-      					
+   						levelItems[key]='<li class="hierarchyOption" id="'+key+'" ><button onclick="chooseHierarchyOption(this)"  metavalue="'+ hierarchyData[key][0] +'" metatitle="'+ hierarchyData[key][1] +'">' + hierarchyData[key][1] + '</button></li>';
    					}
    				 }
-              //If no elements in hierarchy level
+                 //If no elements in hierarchy level
    				 if (jQuery.isEmptyObject(levelItems))
    				 {
@@ -84,5 +75,4 @@
    					menuItem.append("<ul></ul>");
    				 	
-   				 //	console.log("no elements in hierarchy level");
    				 }
    				 else {
@@ -104,7 +94,4 @@
 						//menuItem.find('ul');
 						menuItem.children('ul').slideDown();
-                  
-									 
-   				 	//console.log("debug line 5");
    				 }
    				 
@@ -135,23 +122,20 @@
 		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
 		  {
-			  var hierarchyFile = xmlhttp.responseText;
-			  var StringData = [];
-				var hierarchyData = {};
-				//var expr = /^([0-9]+(?:\.[0-9]+)*)\ ([0-9]+(?:\.[0-9]+)*)\ (.*)/m;
-				var expr = /^(\S*|\"[^\"]*\")\ +([0-9]+(?:\.[0-9]+)*)\ +(.*)/m;
-				StringData = hierarchyFile.split('\n');
-				for (var i = 0; i < StringData.length; i++) 
-					{
-					var result = StringData[i].match(expr);
-					// If result not null
-					if (result != null && result.length == 4) 
-						{
-						// populate hierarchy object
-						hierarchyData[result[2]] = [result[1], result[3]];
-						}
-
-					}
-				addHierarchyToStorage(metaName, hierarchyData);
-				setHierarchyEventsWrappers(metaName);
+			var hierarchyFile = xmlhttp.responseText;
+			var StringData = [];
+			var hierarchyData = {};
+			var expr = /^(\S*|\"[^\"]*\")\ +([0-9]+(?:\.[0-9]+)*)\ +(.*)/m;
+			StringData = hierarchyFile.split('\n');
+			for (var i = 0; i < StringData.length; i++) {
+				var result = StringData[i].match(expr);
+				// If result not null
+				if (result != null && result.length == 4) {
+					// populate hierarchy object
+					hierarchyData[result[2]] = [ result[1].replace(/^\"|\"$/g, ''), result[3].replace(/^\"|\"$/g, '') ];
+				}
+
+			}
+			addHierarchyToStorage(metaName, hierarchyData);
+			setHierarchyEventsWrappers(metaName);
 		  }
 	  }
@@ -174,7 +158,7 @@
 {
 		//get current MetaDataName
-		var metaName = $(row.getElementsByClassName("metaTableCellName")[0]).text();
-		
-		var hierarchyMenuName = 'Menu';
+		var metaName = getMetaName(row);
+		defaultHierarchyButtonText = 'Top level menu';
+		var hierarchyButtonText = defaultHierarchyButtonText;
 		// Check if textarea already contain right menu key
 		var textAreaValue = $(row).find('TEXTAREA').val();
@@ -182,13 +166,18 @@
 		//Get current hierarchy from storages
 		var hierarchyData = hierarchyStorage[metaName];
-		
-		if (hierarchyData[textAreaValue] && (hierarchyData[textAreaValue] != null)) 
-		{				
-			hierarchyMenuName = hierarchyData[textAreaValue][1];
-			
+		//TODO Modificate
+		
+		
+		for(var key in hierarchyData)
+		{
+			if (hierarchyData[key][0] == textAreaValue)
+				{
+					hierarchyButtonText = hierarchyData[key][1];
+					break;
+				}
 		}
 		
 		//Menu element
-		var mainmenu = '<td class="metaDataHierarchyMenu" style="display: none;"><ul><li id="hierarchyLevel"><button class="hierarchyMenuButton"  title="Menu">' + hierarchyMenuName + '</button></li></ul></td>'
+		var mainmenu = '<td class="metaDataHierarchyMenu" style="display: none;"><ul><li id="hierarchyLevel"><button class="hierarchyMenuButton"  title="Menu">' + hierarchyButtonText + '</button></li></ul></td>'
 		//Insert hierarchy menu
 		$(row).find('.metaTableCellRemove').after(mainmenu);
@@ -199,4 +188,5 @@
 		{
 			var input = $(this).val();
+			var hierarchyButtonText;
 			var row = this.parentElement.parentElement;
 			//RegExp to test a valid key in input 
@@ -205,17 +195,18 @@
 			var KeyStartExp = /^(?:[0-9]+(?:\.[0-9]+)*)?\.$/;
 			//if input valid and key found 
-			if ( KeyExp.test(input) && hierarchyData[input]) 
+			removeSuggestionsMenu(row);
+			createSuggestionsMenu(row);
+			/*if ( KeyExp.test(input) && hierarchyData[input] || KeyStartExp.test(input)) 
 			{
-				removeSuggestionsMenu(row,hierarchyData[input]);
 				createSuggestionsMenu(row);
 			}
-			else if (KeyStartExp.test(input)) 
-			{
-				removeSuggestionsMenu(row,hierarchyMenuButton);
-				createSuggestionsMenu(row);
+			*/
+			if (hierarchyData[input]){
+				hierarchyButtonText = hierarchyData[input][1];
+			} else {
+				hierarchyButtonText = defaultHierarchyButtonText;
 			}
-			else {
-				removeSuggestionsMenu(row,hierarchyMenuButton);	
-			} 
+			setHierarchyButtonText(row, hierarchyButtonText);
+			
 		});
 		//Show created menu
@@ -226,30 +217,38 @@
 {
 	//get current MetaDataName
-	var metaName = $(row.getElementsByClassName("metaTableCellName")[0]).text();
-	//Get current hierarchy from storages
+	var metaName = getMetaName(row); 
+	//Get current hierarchy from storage
 	var hierarchyData = hierarchyStorage[metaName];
-	
-	var input = $(row.getElementsByClassName("metaTableCellArea")[0]).val();
-	
-	//RegExp to get SuggestionsMenu
-	var SuggestionsMenuExp = new RegExp("^0*" + input.replace(/\./g, '\\.0*') + "\\.?[0-9]+$")
 	//Hierarchy suggestions menu
 	var SuggestionsMenu = "";
-	for(var key in hierarchyData)
+	
+	var input = $(row.getElementsByClassName("metaTableCellArea")[0]).val();
+	
+	if (input.replace(/[0-9\.\s]/g, '') === "")
 	{
-	var SuggestionsMenuItems = {};
-		
-		if (SuggestionsMenuExp.test(key)) 
+		//RegExp to get SuggestionsMenu
+		var SuggestionsMenuExp = new RegExp("^0*" + input.replace(/\./g, '\\.0*') + "\\.?[0-9]+$")
+		
+			for(var key in hierarchyData)
 		{
-			SuggestionsMenuItems[key]='<li class="hierarchySuggestionsMenu" id="'+key+'" ><button metavalue='+ hierarchyData[key][0] +' metatitle='+ hierarchyData[key][1] +' onclick="setHierarchyId(this)" >' + key.substring(String(input).length) + " " + hierarchyData[key][1] + '</button></li>';
+			if (SuggestionsMenuExp.test(key)) 
+			{
+				SuggestionsMenu +='<li class="hierarchySuggestionsMenu hierarchyOption" id="'+key+'" ><button metavalue="'+ hierarchyData[key][0] +'" metatitle="'+ hierarchyData[key][1] +'" onclick="chooseHierarchyOption(this)" >' + key.substring(String(input).length) + " " + hierarchyData[key][1] + '</button></li>';
+			}
 		}
-		
-		for(var key in SuggestionsMenuItems)
+	} else {
+		//RegExp to get SuggestionsMenu
+		var SuggestionsMenuExp = new RegExp(".*" + input + ".*","i")
+		
+			for(var key in hierarchyData)
 		{
-			//Fill menu with items
-			SuggestionsMenu += SuggestionsMenuItems[key];
+			if (SuggestionsMenuExp.test(hierarchyData[key][1]) && input !== hierarchyData[key][0]) 
+			{
+				SuggestionsMenu +='<li class="hierarchySuggestionsMenu hierarchyOption" id="'+key+'" ><button metavalue="'+ hierarchyData[key][0] +'" metatitle="'+ hierarchyData[key][1] +'" onclick="chooseHierarchyOption(this)" >' + hierarchyData[key][1] + '</button></li>';
+			}
 		}
-		
 	}
+	
+	
 	
 	//Append new SuggestionsMenu
@@ -258,26 +257,21 @@
 	$(row).each(function(){setHierarchyHoverEvent($(this),".hierarchySuggestionsMenu")});
 }
-function removeSuggestionsMenu(row,menuNewText)
-{
-	//Remove old SuggestionsMenu
+//function removeSuggestionsMenu(row,menuNewText)
+function removeSuggestionsMenu(row)
+{
 	$(row).find(".hierarchySuggestionsMenu").remove();
-	//Replace text on Hierarchy menu to default
-	$(row).find(".hierarchyMenuButton").text(menuNewText);
+}
+function setHierarchyButtonText(row, title){
+	$(row).find(".hierarchyMenuButton").text(title);
 }
 
 function setHierarchyEventsWrappers(metaName)
 {
-	
 	//Loop through every metaTableCell
 	$(".metaTableCellName").each(function() {
 		//Check if it is a hierarchy row
-		//TODO implement real check
-	    //if($(this).text()=="rubricator")
 		var currentMetaName = $(this).text();
-		//console.log(metaName)
-		//console.log(metaDataName)
 		if (currentMetaName in hierarchyStorage && currentMetaName == metaName)
 	    {
-			//console.log('testXX')
 	    	var row = this.parentElement;
 	    	var textArea = row.getElementsByClassName("metaTableCellArea")[0];
@@ -369,15 +363,14 @@
 	    		       {
   	    		    	    var row = this.parentElement.parentElement;
-  	    		    	    //Test if there are open submenu
+  	    		    	    
   	    		    	    var found = $(row).find('.metaDataHierarchyMenu ul li ul').filter(":visible")[0];
-  	    		    	    
-  	    		    	    //Hide hierarchy menu if there are no open submenus
-	    		       		if ( found === undefined) 
+  	    		    	    //Test if there are open submenu and cursor left tr element
+	    		       		if ( found === undefined && !$(row).is(':hover')) 
 	    		       		{
-	    		       			//Hide menu
+	    		       			//Hide hierarchy menu if there are no open submenus
 	    		       			$(row).find('.metaDataHierarchyMenu').hide();
-	    		       			//console.log(this);
-	    		       			//console.log(row);
-	    		       			//console.log(found);
+	    		       			
+	    		       			//Set metadata value if textarea contains hierarchy path
+	    		       			substituteHierarchyMetaValue(row);
 	    		       		}
 	    		       		
@@ -388,11 +381,24 @@
 	  });
 }
+
+function substituteHierarchyMetaValue(row){
+	var text = $(row).find('TEXTAREA').val();
+	var metaName = getMetaName(row);
+	var hierarchyData = hierarchyStorage[metaName];
+	if (hierarchyData[text]){
+		var metaValue = hierarchyData[text][0];
+		$(row).find('TEXTAREA').val(metaValue);
+		
+	}
+
+}
+function getMetaName(row){
+	return $(row.getElementsByClassName("metaTableCellName")[0]).text();
+}
 var hierarchyStorage = {};
 function addHierarchyToStorage(metaDataName,processedHierarchy)
 {
 	hierarchyStorage[metaDataName] = processedHierarchy;
-	//console.log( hierarchyStorage)
-	//console.log( metaDataName)
-	
-}
-
+	
+}
+
