/* autocomplete */
function findValue(li) {
	$("#t").val(li.extra[0]);
	var sValue = li.selectValue;
	$('#risultati').html("<img src='/res/css/img2/loader.gif'/>");
	$('#s').click();
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	if (row[1]=='l') t = "posto";
	if (row[1]=='c') t = "città";
	if (row[1]=='b') t = "artista";
	if (row[1]=='p') t = "città e provincia";
	return row[0] + " (" + t + ")";
}

function lookupAjax(){
	var oSuggest = $("#q")[0].autocompleter;
	oSuggest.findValue();
	return false;
}

function cerca() {
	$('#f').submit();
}

/* images loader */
var MAXFIND_IMAGES = 3;

var $sent = "";
var $ar = new Array();

function cresci(dis) {
	$(dis).parent().css("height",$(dis).parent().height());
	$(dis).animate({width:86,padding:0,margin:0},200, function () { });
}
function decresci(dis) {
	$(dis).animate({width:70,padding:3,margin:5},200, function () {  });
}

function getimg(d) {
	var nome = $(d).find("b").html();
	if (MAXFIND_IMAGES > 0) {
		if ($sent.indexOf(","+nome+",")==-1) {
			$ar[ nome ] = "";
			$sent = $sent + ($sent.indexOf(",")==$sent.length ? "" : ",") + nome +",";
			var $div = $(d);
			MAXFIND_IMAGES = MAXFIND_IMAGES - 1;
			$($div).find('div.imgs').html("<img class='load' src='/res/css/img2/loader.gif'/>");
			$.get('/res/inc/ajax.getpic.php', 
				{
				q: nome
				},
				function(data){
					if (data) {
						$ar[nome]=data;
						$($div).find('div.imgs').html("<img src='"+data+"'/>");
						$($div).find('div.imgs img').mouseenter(function() {cresci(this);});
						$($div).find('div.imgs img').mouseleave(function() {decresci(this);});
						$($div).find('div.imgs img').load(function() {
							cresci($($div).find('div.imgs img'));
							setTimeout(function() {decresci($($div).find('div.imgs img'));},320);
						});
					} else {
						$($div).find('div.imgs').html("<img src='/res/css/img2/nopic.gif'/>");
					}
				}
			);
		} else {
			if ($ar[nome]) { 
				$(d).find('div.imgs').html("<img src='"+$ar[nome]+"'/>");
				$(d).find('div.imgs img').mouseenter(function() { cresci(this); });
				$(d).find('div.imgs img').mouseleave(function() { decresci(this); });
				$(d).find('div.imgs img').load(function() {
					cresci($(d).find('div.imgs img'));
					setTimeout(function() {decresci($(d).find('div.imgs img'));},320);
				});
			} else {
				setTimeout(function() {getimg(d);},500);
			}
		}
	} else {
		$(d).find('div.imgs').html("<img src='/res/css/img2/nopic.gif'/>"); return;
	}
}

$(document).ready(function() {
	try {
		$("#q").autocomplete(
			"/res/inc/ajax.search.php",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:0,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:false,
				maxItemsToShow:15
			}
		);
		setTimeout( "$('#q').focus();", 500);
	}
	catch (e){
	}

	$('#segnala').click(
		function(event) {
			event.preventDefault();
			$('#segnala').hide();
			$('#segnalaok').show();
			$('#segnalaok').html(" un momento...");
			$.get('/res/inc/ajax.segnala.php', 
				{
				q: $('#myspace').val()
				},
				function(data){
					if (data && data=='ok') {

						$('#segnalaok').html("ok!");
						setTimeout(function() { $('#segnalaok').hide(); $('#segnala').show(); },3000);
					} else {
						$('#segnalaok').html(" no. nisba :(");
						setTimeout(function() { $('#segnalaok').hide(); $('#segnala').show(); },3000);
					}
				}
			);
		}
	);

	$('div.wrapper').append("<div class='a1'></div><div class='a2'></div><div class='a3'></div><div class='a4'></div>");

	$('#risultati div.o').each( function () {
		getimg(this)
	} );
	
	// funzione per gestire load more
	x = function(event) { 
		event.preventDefault();
		$.get('index.php', 
			{
			t: $t,
			q: $q,
			s: $('#loadmore a').attr("rel")
			},
			function(data){
				$('#loadmore').remove();
				$('div.capsula:last').append(data);
				$('#loadmore a').click(x);
				MAXFIND_IMAGES = 3;
				$('#risultati div.capsula:last div.o').each( function () {
					getimg(this)
				} );

			}
		);
		$('#loadmore a').html("<img class='load' src='/res/css/img2/loader.gif'/>");

	}

	$('#loadmore a').click(x);

	/* per funzionamento link interni su iphone */
	var siteURL = "http://" + top.location.host.toString();
	$("a[href^='"+siteURL+"'], a[href^='/'], a[href^='./'], a[href^='../'], a[href^='#']").click(function (e) {
		e.preventDefault();
		window.location=$(this).attr("href");
		return false;
	});

} );
