var SEARCH_lastInput = microtime();
var SEARCH_fulltext = "";
var SEARCH_timeout;
$(document).ready(function(){
	$('#search form input[type=text]').keyup(function(){
		SEARCH_fulltext = $(this).val();
		if((microtime() - SEARCH_lastInput) < 0.3){ SEARCH_lastInput = microtime(); clearTimeout(SEARCH_timeout);}
		
		SEARCH_lastInput = microtime();
		SEARCH_timeout = setTimeout("ajaxSearch()", 300);
	});
	$('#login-box-hidden').center();
	$('a#login').click(function(){
		showShadow();
		$('#login-box-hidden').center();
		$('#login-box-hidden').fadeIn();
		return false;
	});
	
    $('#search .arrow-down, #search span a, #choose-categories .top-bar a').click(function(){
        $('#search .arrow-down').toggleClass('arrow-up');
        $('#main-categories').slideToggle();
    });

    $('#login').click(function(e){
        var position = $(this).position();
        $('#login-box').css("left", (position.left - 200) + "px");
        $('#login-box').css("top", (position.top + 18) + "px");
        $('#login-box').toggle();
    });
    // Chiusura messaggi (error, success,...)
    $('.border .internal a.close').click(function(){
        $(this).parent().parent().parent().slideUp();
    });
    
    var mouse_inside_categories;
    $('#main-categories, div#search a, div#search span').hover(function(){ 
    	mouse_inside_categories=true; 
    }, function(){ 
    	mouse_inside_categories=false; 
    });
    $("body").mouseup(function(){ 
        if(! mouse_inside_categories) $('#main-categories').hide();
    });
    $('#main-categories ul li a').click(function(){
    	var idCat = $(this).attr("idCat");
    	$('#search form #id-category').val(idCat);
    	$('#search form span a').html($(this).text());
    	$('#main-categories').hide();
    	if($('#search .arrow-down').hasClass('arrow-up')){
    		$('#search .arrow-down').removeClass('arrow-up');
    	}
    	ajaxSearch();
    });
});
function showShadow(){
	$('#shadow').show();
}
function startLoading(){
    $('#loader').fadeIn();
}
function stopLoading(){
    $('#loader').fadeOut();
}
function loadingError(text){
	stopLoading();
	$('#loader-error').html(text);
	$('#loader-error').slideDown();
	setTimeout( function() {
		$('#loader-error').slideUp();
	}, 3000 );
	
}
/* Center the element on the middle of the page */
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}
jQuery.fn.centerHome = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop()-150 + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}
function closeLogin(){
	$('#login-box-hidden').fadeOut();
	$('#shadow').hide();
}
function printResults(found, total){
	$('.result-n-bar').fadeIn();
	var percentage = 100/total*found;
	$('.result-n-bar .quantity-bar').css("width", percentage+"%");
	$('#x-results').html(found+' risultati di '+total);
}
function ajaxSearch(){
	if(SEARCH_fulltext == ""){
		//printResults(0, 0);
		//return;
	}
	var urlc = server_home+'search/ad/0/'+SEARCH_fulltext;
	if($('#id-category').val() > 0){
		urlc = server_home+'search/ad/'+$('#id-category').val()+'/'+SEARCH_fulltext;
	}
	
	$.ajax({
		url: urlc,
		dataType: 'json',
		data: {
			fulltext: SEARCH_fulltext,
			json: true
		},
		type: 'POST',
		success: function(data){
		    if(data.error == false){
		    	printResults(data.found, data.total);
		    }
		    else {
		    	loadingError(data.error);
		    }
		}
	});
}
function selectMainCategory(id){
	$('#main-categories ul li a').click();
}
function microtime () {
    var now = new Date().getTime() / 1000;
    return now;
}
