// JScript File
/*
 * Dove si butta -  jQuery interface
 * Copyright (c) 2009  Oliver Astrologo
 * Dual licensed under MIT and GPL.
 * @author  Oliver Astrologo
 * @version 1.1
 */
var $carousel;
function showRequest(XMLHttpRequest) {    
    jQuery('#butta-bene-form').addClass("ac_loading");
}
function showError(XMLHttpRequest, textStatus, errorThrown) {   
    jQuery('#butta-bene-form').removeClass("ac_loading");
    alert('Spiacenti ma al momento non è possibile visualizzare i risultati, riprova fra poco.');
	jQuery.ajax({		
			type: "POST",
			dataType: 'json',   
			cache: false,
			beforeSend: null,   
			error: null,
			url: "/data-sources/error.php",
			data: {
				error_dump : errorThrown + ' ' + textStatus				
			},      
			success: null
		}); 	
}
function processFormData() {
	jQuery('.custom_note').fadeOut().html('');
    var key = jQuery('#butta-bene-form').val();
    if (key != '' && key != jQuery('#butta-bene-form-label').html()) {        
        jQuery.ajax({
            type: "GET",
            dataType: 'json',
            cache: false,
            beforeSend: showRequest,
            error: showError,
            url: dataSrc,
            data: {
                action: "process",
                q: key
            },
            success: function (data) {
                searchResponse(data, key);
            }
        });
    }
}
function searchResponse(data, key) {
    jQuery('#butta-bene-form').removeClass("ac_loading");
    var responseStatus = data.responseStatus;
    jQuery('.carousel-title').html(key);
    if (responseStatus == 'OK') {       
        if (data.responseData.verde == 1) $carousel.scroll(2);
        if (data.responseData.bianco == 1) $carousel.scroll(3);
        if (data.responseData.blu == 1) $carousel.scroll(4);
        if (data.responseData.giallo == 1) $carousel.scroll(5); 
		if (data.responseData.isole == 1) $carousel.scroll(6);
		if (data.responseData.centri == 1) $carousel.scroll(6);
		if (data.responseData.punti_mobili == 1) $carousel.scroll(6);
		if (data.responseData.pile == 1) $carousel.scroll(7);
		if (data.responseData.medicinali == 1) $carousel.scroll(8);
		if (data.responseData.bidbianco == 1) $carousel.scroll(3);
        if (data.responseData.bidblu == 1) $carousel.scroll(4);
        if (data.responseData.bidverde == 1) $carousel.scroll(2);
        if (data.responseData.amianto == 1) $carousel.scroll(10);
     
	 
	    if (data.responseData.riciclacasa == 1) {
            jQuery("#riciclacasa").show();
        }
        else {
            jQuery("#riciclacasa").hide();
        }
        if (data.responseData.punti_mobili == 1) {
            jQuery("#punti_mobili").show();
        }
        else {
            jQuery("#punti_mobili").hide();
        }
        if (data.responseData.centri == 1 || data.responseData.isole == 1) {
            jQuery("#centri").show();
        }
        else {
            jQuery("#centri").hide();
        }
			
		if(data.responseData.note  != '')  jQuery('.custom_note').html(data.responseData.note).show();
		      
    } else { 
        $carousel.scroll(11);
    }
};

function mycarousel_initCallback(carousel) {
    $carousel = carousel;
}
jQuery(function () {
    jQuery("#butta-bene").submit(function () {
        processFormData();
        return false;
    });
    //jQuery(".dove-si-butta-item").hide();
    jQuery('#responses-carousel').jcarousel({
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    processFormData();
	
});
