// JavaScript by giuseppe@epidemialab.it

function ondeggia(elemento) {
    var dato = $(elemento).css('marginTop').split('p');
    if (dato[0] == 596) {
        newy = 587;
        newx = 0;
    } else {
        newy = 596;
        newx = 10;
    }
    $(elemento).animate({
        marginTop: newy
    }, 1800, 'linear', function () {
        ondeggia(this);
    });
}

function go_pesce(elemento) {
    var dato = $(elemento).css('marginTop').split('p');
    if (dato[0] == 300) {
        newy = 587;
    } else {
        newy = 300;
    }
    $(elemento).animate({
        marginTop: newy
    }, 1800, 'linear', function () {
        setTimeout(function () {
            go_pesce(this);
        }, 1000);
    });
}


function check_h(div,h_min,h_max){
	
if ($(div).length>0){
  var h = $(div).css('height').split('p');	
  if(h[0]<=h_min){
  	 $(div).css('height',h_max);
  }
 }
}


function lattina(){
    var dato = $('#lattina_tonno').css('marginTop').split('p');
 
	if(dato[0] == 10) {
		newy = 16;
		//newx = 0;
	} else {
		newy = 10;
	//	newx = 10;
	}
	$('#lattina_tonno').animate({
		marginTop : newy
	}, 1050, 'linear', function() {
		lattina();
	});

	
	
}


$(document).ready(function () {
    ondeggia(".onda_gs");
    check_h(".box_testo",550,550);

    function mostra_nascondi_tendina(classe) {
        $(classe).slideToggle(300);
    }
    $("#select_ordina").click(function () {
        mostra_nascondi_tendina('.box_drop');
    });
    $(".box_drop").mouseleave(function () {
        mostra_nascondi_tendina('.box_drop');
    });
	
	$('#ricetta_campo_cerca, #campo_cerca').click(function () {
	 $(this).val('');		 					 
	});
	
	$('#ricetta_campo_cerca, #campo_cerca').mouseleave(function () {
		if(	  $(this).val()=='' ){									   
	         $(this).val( $(this).attr('title'));
		}
	});

});



// Funzioni carrello 

function scrollPage(anc, pixel) {
    target = $(anc);
    $('html, body').animate({
        scrollTop: target.offset().top - (pixel * -1)
    }, 1000);
}


function add_cart(idProdotto) {
    // Check if is select a misure
    if (parseInt($('#qnt').val()) > 0) {
        //effettuo la chiamata Ajax
        $.ajax({
            type: "POST",
            url: "php/add_cart.php",
            data: ({
                idProdottoPost: idProdotto,
                idMisuraPost: parseInt($('#misura').val()),
                qnt: parseInt($('#qnt').val())
            }),
            success: function (data) {
                var dato = data.split('||');
                $('#nr_item_cart').html(dato[0]);
                $('#total_cart').html(dato[1]);
                $('#box_drop_carrello').html(dato[2]);
                $('.box_drop_carrello').slideToggle(300);
                if (parseInt(dato[3]) > 0) {
                    alert("Solo " + dato[3] + " unita' disponibili in magazzino. \n\nOnly " + dato[3] + " units avaiable");
                }
                scrollPage('#cont_drop', 0);
            }
        });
    }
} // function add_cart(idProdotto)

function cancella_elemento_carrello(idao, idprodotto) {
    $.ajax({
        type: "POST",
        url: "php/delete_item.php",
        data: ({
            idProdottoPost: idprodotto,
            idAoPost: idao,
            idspese: $('#spedizioni').val()
        }),
        success: function (data) {
            var dato = data.split('||');
            $('#row_carrello').html(dato[0]);
            $('#sub_totale').html(dato[1]);
            $('#totale_carrello').html(dato[1]);
            $('#nr_item_cart').html(dato[2]);
            $('#total_cart').html(dato[1]);
            $('#spedizioni').html('');
            $('#spedizioni').html(dato[4]);
            $('#ss_price').html(dato[5]);
            $('#totale_carrello').html(dato[6]);
            $('#box_drop_carrello').html(dato[3]);
        }
    });
}

function update_cart(segno, idao, idmisura, idprodotto) {
    if ($('#isole').attr('checked') != true) {
        ck = 'off';
    } else {
        ck = 'on';
    }
    $.ajax({
        type: "POST",
        url: "php/update_item.php",
        data: ({
            segnoPost: segno,
            idProdottoPost: idprodotto,
            idMisuraPost: idmisura,
            idAoPost: idao,
            idspese: $('#spedizioni:checked').val(),
            isole: ck
        }),
        success: function (data) {
            var dato = data.split('||');
            $('#row_carrello').append("<div class='loader'><img src='img/loader.gif' /></div>");
            setTimeout(function () {
                $('.loader').remove();
                $('#row_carrello').html(dato[0]);
                $('#sub_totale').html(dato[1]);
                $('#totale_carrello').html(dato[6]);
                $('#nr_item_cart').html(dato[2]);
                $('#total_cart').html(dato[1]);
                $('#box_drop_carrello').html('');
                $('#box_drop_carrello').html(dato[7]);
                if (parseInt(dato[3]) > 0) {
                    alert("Solo " + dato[3] + " unita' disponibili in magazzino. \n\nOnly " + dato[3] + " units avaiable");
                }
                $('#spedizioni').html('');
                $('#spedizioni_box').html(dato[4]);
                $('#ss_price').html(dato[5]);
            }, 2000);
        }
    });
}

function quantityUpDown(segno) {
    attualeValore = parseInt($('#qnt').val());
    if (segno == '+') {
        attualeValore += 1;
    } else {
        attualeValore -= 1;
    }
    if (attualeValore >= 1) { // Update text
        $('#qnt').val(attualeValore);
    }
}

function trim(txt) {
    return txt.replace(/\s+$|^\s+/g, " ");
}

function check_indirizzo_alternativo(indirizzo, numero_civico, citta_provincia) {
    var val = $('#alternativo').val();
    if (val == 'No') {
/*  var  html_code='<label class="cart_form">'+indirizzo+' *</label><input type="text" name="indirizzoa" id="indirizzoa" required="required"  /><label class="cart_form">'+numero_civico+' *</label><input type="text" name="numero_civicoa" id="numero_civicoa" class="civico" required="required" /><input name="capa" type="text" class="cap" id="capa" value="" required="required" /><label class="cart_form">'+citta_provincia+' *</label><input type="text" name="cittaa" id="cittaa" class="citta" required="required" /><input type="text" name="provinciaa" id="provinciaa" class="prov" required="required" /><label class="cart_form"> </label>';
	  
	$('#box_alternativo').html(html_code);*/
        $('#box_alternativo').css('display', 'block');
        $('input[rel*=alt]').attr('required', "required");
    } else {
        $('#box_alternativo').css('display', 'none');
        $('input[rel*=alt]').attr('required', "");
    }
}

function change_price(idProdotto) {
    // Check if is select a misure
    if (parseInt($('#misura').val()) > 0) {
        //effettuo la chiamata Ajax
        $.ajax({
            type: "POST",
            url: "php/update_price.php",
            data: ({
                idProdottoPost: idProdotto,
                idMisuraPost: parseInt($('#misura').val())
            }),
            success: function (data) {
                var dato = data.split('||');
                $('#box_prezzo_dettaglio').html("");
                $('#box_prezzo_dettaglio').append("<div class='loader_price'><img src='img/loader_price.gif' /></div>");
                setTimeout(function () {
                    $('.loader_price').remove();
                    $('#box_prezzo_dettaglio').html(dato[1]);
                }, 2000);
            }
        });
    } else {
        //$("#alert").click();
        //	mostra_finestra('Errore','Selezionare la misura');
    }
} // function change_price(idProdotto)

function svuota(oggetto) {
    $('#' + oggetto).attr('value', '');
}

function aggiorna_ss(val) {
/* var dato=val.split('###');
 var old_value_ss=$('#ss_price').html(); 
 var app=parseFloat($('#totale_carrello').html()) -  parseFloat(old_value_ss);
 
 
 $('#ss_price').html(dato[1]);

$('#totale_carrello').html( app + parseFloat(dato[1]) );*/
    //effettuo la chiamata Ajax
    $.ajax({
        type: "POST",
        url: "php/update_ss.php",
        data: ({
            idspese: val
        }),
        success: function (data) {
            var dato = data.split('||');
            $('#ss_price').html(dato[0]);
            $('#totale_carrello').html(dato[2]);
        }
    });
}

function aggiorna_ss_isole(val) {
    //effettuo la chiamata Ajax
    $.ajax({
        type: "POST",
        url: "php/update_ss_isole.php",
        data: ({
            ceccato: val
        }),
        success: function (data) {
            var dato = data.split('||');
            $('#ss_price').html(dato[0]);
            $('#totale_carrello').html(dato[2]);
        }
    });
}



