	$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
	  while(x<c.length){var m=r.exec(c.substr(x));
	    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
	    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
	    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
	URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
	  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
	  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
	});

		
	$(function() {
		function split(val) {
			return val.split(/,\s*/);
		}
			
		function extractLast(term) {
			return split(term).pop();
		}
		$("#autore").autocomplete({
			source: function(request, response) {
				var el = request.name;
				$.getJSON("ricerca.php", {
					val: request.term,
					par: 'autore',
					action: 'getworld'
				}, response);
			},

			minLength: 2,
			select: function(event, ui) {
				//log(ui.item ? ("Selected: " + ui.item.label) : "Nothing selected, input was " + this.value);
			},
			open: function() {
				$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
			},
			close: function() {
				$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
			}
		});


		
		$("#anno").autocomplete({
			source: function(request, response) {
				var el = request.name;
				$.getJSON("ricerca.php", {
					val: request.term,
					par: 'anno',
					action: 'getworld'
				}, response);
			},

			minLength: 2,
			select: function(event, ui) {
				//log(ui.item ? ("Selected: " + ui.item.label) : "Nothing selected, input was " + this.value);
			},
			open: function() {
				$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
			},
			close: function() {
				$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
			}
		});

		$('#autore, #titolo, #anno, #argomento').focus(function (){
			if ($(this).val()==$(this).attr('orig')) $(this).val('');
		});
		$('#autore, #titolo, #anno, #argomento').blur(function (){
			if ($(this).val()=='') $(this).val($(this).attr('orig'));
		});
		$('#schedalb').dialog({
			autoOpen: false,
			modal: true,

			width: 500
		});
		

		$('#ricercabtn').click(function (){
			$('#searchResult').fadeOut('fast', function (){
				$('#searchResult').fadeIn('fast');
				$('#searchResult').html('<br /><div style="text-align: center"><img src="img/load.gif" align="center" /><br /> Ricerca in corso<br /><br />Attendere...<br /><br /><br /></div>');
				var filtro = "act=ricerca";
				if ($('#autore').val() != 'Autore') filtro += '&autore='+$.URLEncode($('#autore').val());
				if ($('#titolo').val() != 'Titolo') filtro += '&titolo='+$.URLEncode($('#titolo').val());
				if ($('#argomento').val() != 'Argomento') filtro += '&argomento='+$.URLEncode($('#argomento').val());
				if ($('#anno').val() != 'Anno') filtro += '&anno='+$.URLEncode($('#anno').val());
				$.ajax({
					   type: "POST",
					   url: "ricerca.php?action=search",
					   data: filtro,
					   success: function(msg){
							$('#searchResult').fadeOut('fast', function (){
								if ($.trim(msg)=='0'){
									$('#schedalb').dialog( "option", "title", '' );
									$('#schedalb').dialog('open');
									$('#schedalb').html('<br /><br /><div style="text-align: center; font-weight: normal; font-size: 15px">La ricerca non ha portato a nessun risultato.</div>');
									$('#searchResult').html('');
								}else{
									$('#schedalb').dialog( "option", "title", 'Scheda dettagli' );
									$('#searchResult').html(msg);
									$('#searchResult').fadeIn('slow');
									$('.result tr.riga').mouseover(function (){
										$(this).children('td').addClass('hover');
									});
									$('.result tr.riga').mouseout(function (){
										$(this).children('td').removeClass('hover');
									});
									$('.result tr.riga').click(function (){
										var idl = $(this).attr('idl');
										$('#schedalb').html('<br /><div style="text-align: center"><img src="img/load2.gif" align="center" /><br /> Caricamento in corso<br /><br />Attendere..</div>');
										$('#schedalb').dialog('open');
										$.ajax({
											   type: "POST",
											   url: "ricerca.php?action=getscheda",
											   data: 'idl='+idl,
											   success: function(msg){
												   $('#schedalb').html(msg);
											   }
										});
									});
								}
							});
					   }
				});
			});
		});
	});
