/*
 * box quick search
 */
 
function showHideList(elm) {
	var id = elm;
	var elm = document.getElementById(id);
	
	if (elm.className == 'expand')
	{
		elm.className = 'collapse';
		elm.id = 'collapse';
		elm.title = 'nascondi la lista';
		elm.innerHTML = '<span>-</span>';

		document.getElementById('search_list').style.display='block';
	}
	else 
	{
		elm.className = 'expand';	
		elm.id = 'expand';
		elm.innerHTML = '<span>+</span>';

		document.getElementById('search_list').style.display='none';
	}
}

function deleteSearch(elm_id,search_id)
{
	var callback = {
			
	    // Successful XHR response handler
	    success : function (o) {
			var res = Utilities.jsonParse(o.responseText);
			
			if(res['result'] == 'OK'){
				var toRemove = document.getElementById(elm_id);
				
				//rinomino il titolo delle ricerche
				var nodes = YAHOO.util.Dom.getChildren('search_list');				
				if(nodes[0].id  == toRemove.id && nodes.length > 1){
					var title = document.getElementById("last_search_title");
					var temp = "link_";
					temp += nodes[1].id.replace("l","");
					var newTitle = document.getElementById(temp);
					title.innerHTML = newTitle.innerHTML;
					title.setAttribute("href","http://" + location.hostname + "/search/type/" + nodes[1].id.replace("l",""));				
				}
				
				document.getElementById('search_list').removeChild(toRemove);
				
				
				
				// aggiornamento numero ricerche ancora presenti
				
				if((document.getElementById('searchesNum').value - 1) == 0){
					document.getElementById('last_search').innerHTML = '<a href="http://' + location.hostname + '/search">Crea la tua ricerca</a>';
					document.getElementById('collapse').style.display = 'none';
				}
				else{
					document.getElementById('searchesNum').value -= 1;
				}
				
			}
			else{
				OverlayConfiguration.genericMessage.showPlain(res['header'],res['body'], 'rosso');
			}
	    },
	   
	    failure : function (o) {
	    } 
	};
	
	YAHOO.util.Connect.asyncRequest("GET", 'http://' + location.hostname + '/gui_manager/delete_search/' + search_id, callback);	
}

function ReplaceChild(parentElm, newElm, oldElm) {
	parentElm.replaceChild(newElm, oldElm);
}

function renameSearch(toChange,elmid,searchId,newTitle) 
{
	var input = document.getElementById(elmid);
	var inputValue = input.value;
	var newLink = document.createElement('a');
	// newLink.setAttribute('href','/theclub/fwtest/index_v2.php?page=search/risultati');
	newLink.setAttribute('href','http://' + location.hostname + '/search/type/' + searchId);
	newLink.setAttribute('id','link_' + searchId);
	newLink.innerHTML = inputValue;
	
	var lastSearch = document.getElementById(toChange);
	
	ReplaceChild(lastSearch,newLink,input);
	//document.getElementById('rinomina').style.display='block';
	
	//se l'elemento modificato è il primo della lista modifico anche il titolo
	var nodes = YAHOO.util.Dom.getChildren('search_list');
	if(nodes[0].id + "_item" == toChange){
		var title = document.getElementById("last_search_title");
		title.innerHTML =  newTitle;
	}
}

function createInput(toChange, searchId)
{
	var lastSearch = document.getElementById(toChange);
	var link = lastSearch.firstChild;
	var startName = link.innerHTML;
	var validKey = false;
	
	var callback = {
			
	    // Successful XHR response handler
	    success : function (o) {
			var res = Utilities.jsonParse(o.responseText);
			
			if(res['result'] == 'OK'){
			}
			else{
				OverlayConfiguration.genericMessage.showPlain(res['header'],res['body'], 'rosso');
			}
	    },
	   
	    failure : function (o) {
	    } 
	};

	
	var searchInput = document.createElement("input");
	searchInput.setAttribute('value',startName);
	searchInput.setAttribute('type','text');
	searchInput.setAttribute('id','rename');
	searchInput.setAttribute('maxlength',40);
	/*searchInput.setAttribute('onBlur','javascript:renameSearch("' + toChange + '","' + searchInput.id + '")');*/
	document.getElementById('rinomina' + searchId).style.display='none';
	ReplaceChild(lastSearch, searchInput, link);		
	searchInput.focus();
	searchInput.select();	
	
	searchInput.onkeypress = function (evt) {
		var code;
		if (window.event) {
			code = window.event.keyCode;
		}
		else if (evt) {
			code = evt.keyCode;
		}
		
		if (code == 13 ) {
			renameSearch(''+toChange+'', ''+searchInput.id+'',''+searchId+'', ''+searchInput.value+'');
			document.getElementById('rinomina' + searchId).style.display='block';
			YAHOO.util.Connect.asyncRequest("POST", 'http://' + location.hostname + '/gui_manager/edit_search', callback, 'search_id=' + searchId + '&search_name=' + searchInput.value);	
		}
	}
	
	searchInput.onblur = function () {
		renameSearch(''+toChange+'', ''+searchInput.id+'',''+searchId+'', ''+searchInput.value+'');
		document.getElementById('rinomina' + searchId).style.display='block';
		YAHOO.util.Connect.asyncRequest("POST", 'http://' + location.hostname + '/gui_manager/edit_search', callback, 'search_id=' + searchId + '&search_name=' + searchInput.value);
	}	
	
	
}

