$(document).ready(function(){
	
	appiccica("GMWhere/elements/generic_selector.php","1");
	
	$('#rapid-src li').bind('click',function(){
		$("#rapid-src input[name='dove']").val("");
		$("#rapid-src-kind").val($(this).children('span').attr('class'));
		$('#rapid-src li').removeClass();
		$('#rapid-src li').not(this).addClass('unselect clearfix');
		$(this).addClass('rbutton clearfix');
		var a=$(this).children('span').attr('class');
		$("#rapid-src input[name='dove']").autocomplete("destroy");
		switch(a){
			case "m":
				$("#src_field").html("<input type=\"text\" name=\"dove\" class=\"ui-corner-all\"></input><span id=\"find\" class=\"ui-corner-all\">Cerca</span>");
				appiccica("GMWhere/elements/generic_selector.php","1");
			break;
			case "c ":
				$("#src_field").html("<input type=\"text\" name=\"dove\" class=\"ui-corner-all\"></input><span id=\"find\" class=\"ui-corner-all\">Cerca</span>");
				appiccica("GMWhere/elements/generic_selector.php","2");
			break;
			case "d ":
				$("#src_field").html("<input type=\"text\" name=\"dove\" class=\"ui-corner-all\"></input><span id=\"find\" class=\"ui-corner-all\">Cerca</span>");	
				appiccica("GMWhere/elements/generic_selector.php","3");
			break;
			case "u ":
				$("#src_field").html("<input type=\"text\" name=\"dove\" class=\"ui-corner-all\"></input><span id=\"find\" class=\"ui-corner-all\">Cerca</span>");
				appiccica("GMWhere/elements/user_selector_graph.php","4");
			break;
			case "g ":
				$("#src_field").html("<form action=\"http://www.gustamodena.it/google.php\" id=\"searchbox_010884927804378312363:um6p6sykgvi\">  <input type=\"hidden\" name=\"cx\" value=\"010884927804378312363:um6p6sykgvi\" />  <input type=\"hidden\" name=\"cof\" value=\"FORID:11\" />  <input type=\"text\" name=\"q\" size=\"22\" class=\"ui-corner-all\"/>  <input type=\"submit\" name=\"sa\" class=\"ui-corner-all\" style=\"background-color: #FF0000; color: #FFFFFF; cursor: pointer; display: inline-block; font-family: Georgia; font-size: 2em; padding:0 !important; border:none !important; font-style: italic; height: 35px; text-align: center; width: 100px;\" value=\"Cerca\" /> </form><script type=\"text/javascript\" src=\"http://www.google.com/coop/cse/brand?form=searchbox_010884927804378312363%3Aum6p6sykgvi&lang=it\"></script>");
			break;
			default:
				appiccica("GMWhere/elements/generic_selector.php","1");
			break;
		}
	});
	
	$("#find").click(function(){
		go();
	});
	
});

function go(){
		var a=$("#rapid-src-kind").val();
		var cod=$("#rapid-src-cod").val();
		var txt=$("#rapid-src-text").val();
		var extra=$("#rapid-src-extra").val();
		var way=$("#rapid-src-way").val();
		if(way==""){
			switch(a){
				case "m ":
					window.open("dove-mangiare.php?t="+cod+"&tag="+txt,'_self');
				break;
				case "c ":
					window.open("dove-comprare.php?t="+cod+"&tag="+txt,'_self');
				break;
				case "d ":
					window.open("dove-dormire.php?t="+cod+"&tag="+txt,'_self');
				break;
				case "u ":
					var name=$("input[name='dove']").val();
					window.open("profilo.php?term="+encodeURIComponent(name),'_self');
				break;
				default:
					window.open("dove-mangiare.php?t="+cod+"&tag="+txt,'_self');
				break;
			}
		}else{
			if(a=="u "){
				window.open("profilo.php?usr="+cod,'_self');
			}else
			window.open("locale.php?cod="+cod+"",'_self');
		}
}

function goNow(item){
		var a=$("#rapid-src-kind").val();
		var cod=item.text;
		var txt=item.value;
		var extra=item.extra;
		var way=item.way;
		if(way==undefined){
			switch(a){
				case "m ":
					window.open("dove-mangiare.php?t="+cod+"&tag="+txt,'_self');
				break;
				case "c ":
					window.open("dove-comprare.php?t="+cod+"&tag="+txt,'_self');
				break;
				case "d ":
					window.open("dove-dormire.php?t="+cod+"&tag="+txt,'_self');
				break;
				case "u ":
					window.open("profilo.php?usr="+cod,'_self');
				break;
				default:
					window.open("dove-mangiare.php?t="+cod+"&tag="+txt,'_self');
				break;
			}
		}else{
			if(a=="u "){
				window.open("profilo.php?usr="+cod,'_self');
			}else
				window.open("locale.php?cod="+cod+"",'_self');
		}
}

function appiccica(url,kind){
	$("#rapid-src input[name='dove']").autocomplete({
		source: function(request, response) {
				$.ajax({
					url: url,
					dataType: "json",
					data: "term="+encodeURIComponent(request.term)+"&how=20"+"&m="+kind,
					success: function(data) {
						response($.map(data, function(item) {
							var add="";
							if(kind=="4")
								return {
								label: ""+item.extra+"<span class=\"ui-corner-all\"style=\"display: inline-block; margin-left:5px; background-color:#fff; border:1px solid #ffa800; height:47px; padding:3px; width:240px; font-size:1.5em; font-family:georgia; font-style:italic;\"> "+item.value+"</span> ",
								text: item.cod,
								value: item.value,
								extra: item.extra,
								way: item.way
								}
							else
								return {
									label: item.value+" "+item.extra+" ",
									text: item.cod,
									value: item.value,
									extra: item.extra,
									way: item.way
								}
						}))
					}
				});
			},
		html: true,
		select: function(event,ui){
			event.preventDefault();
			$("#rapid-src input[name='dove']").val(ui.item.value);
			/*$("#rapid-src-cod").val(ui.item.text);
			$("#rapid-src-text").val(ui.item.value);
			$("#rapid-src-extra").val(ui.item.extra);
			$("#rapid-src-way").val(ui.item.way);*/
			goNow(ui.item);
		},
		open: function(event,ui){
			$("ul[class='ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all']").css({'z-index':'3','padding-right': '1px','overflow-y':'auto','max-height': '400px'});
			$("#rapid-src-cod").val("");
			$("#rapid-src-text").val("");
			$("#rapid-src-extra").val("");
		},
		focus: function(event,ui){
		//	event.preventDefault();
		},
		change: function(event,ui){	
			event.preventDefault();
		//	$("#rapid-src input[name='dove']").val("");
			/*$("#rapid-src-cod").value(ui.item.value);
			$("#rapid-src-text").value(ui.item.text);
			$("#rapid-src-extra").value(ui.item.extra);*/
		},
		close: function(event,ui){	
		//	event.preventDefault();
			$("#rapid-src input[name='dove']").val(ui.item.value);
			if(ui.item){
				$("#rapid-src-cod").val(ui.item.text);
				$("#rapid-src-text").val(ui.item.value);
				$("#rapid-src-extra").val(ui.item.extra);
			}
		},
		minLength: 2
	});
}

