﻿
$(document).ready(function() {

    var textBoxSearch = $("#TdHeaderSearch input[title!='']");
    //var oldTextValue = textBoxSearch.val();
    textBoxSearch.hint();
    //textBoxSearch.restoreHint();
    //if (oldTextValue.length > 0) textBoxSearch.val(oldTextValue);

    $("#TdHeaderSearch input[title!='']").tagSuggest({
        url: "/tag-suggestion.aspx",
        tagContainer: "div",
        tagWrap: "div",
        elContainer: $("#DivSuggestContainer"),
        takeFirstOnEnter: false,
        getAdditionalParameter: function() {
            var rbMoto = $("#TdHeaderSearch input[value=RbMoto]").attr("checked");
            var rbScooter = $("#TdHeaderSearch input[value=RbScooter]").attr("checked");
            var rbNuovo = $("#TdHeaderSearch input[value=RbNuovo]").attr("checked");
            var rbUsato = $("#TdHeaderSearch input[value=RbUsato]").attr("checked");
            var param = {
                moto: rbMoto,
                scooter: rbScooter,
                nuovo: rbNuovo,
                usato: rbUsato
            };
            return param;


        },
        overridePressEnter: function() {
            var goButton = $(GlbJqueryFilterGoButton);
            window.location.href = goButton.attr("href");
            goButton.click();
        }
    });
    //disabling autocompletition of firefox:
    textBoxSearch.attr('autocomplete', 'off');
    //al click del pulsante di ricerca testuale nascondo le suggestions...
    $(GlbJqueryFilterGoButton).click(function(event) {
        $.TagSuggester("#TdHeaderSearch input[title!='']").hideSuggestions();
    });
    //SetPromo();
    timerPromoId = setInterval(SetPromo, 10000)

});

$(document).unload(function() { clearInterval(timerPromoId) });

function SetPromo() {
    //moto:
    var link = $("#BoxPromoMoto a:eq(0)");
    var imgLink = link.find("img").filter("eq(0)");
    var linkText = link.find("span").filter("eq(0)");
    //var json = { items: [ {id: '', Modello: '', urlFoto: ''}
    //alert(GlbCurrentOfferteMotoIndex + "_" + (GlbOfferteMoto.items.length -1));
    if (GlbCurrentOfferteMotoIndex > GlbOfferteMoto.items.length - 1) GlbCurrentOfferteMotoIndex = -1;
    var justDoIt = false;
    $.each(GlbOfferteMoto.items, function(i, item) {
        if (i > GlbCurrentOfferteMotoIndex && !justDoIt) {
            imgLink.attr("alt", item.Modello);
            imgLink.attr("src", item.urlFoto);
            linkText.text(item.Modello);
            link.attr("url", "/moto/detailsL.aspx?marca=" + item.Marca + "&modello=" + item.Modello);
            GlbCurrentOfferteMotoIndex++;
            justDoIt = true;
        }
    });
    //scooter:
    var link = $("#BoxPromoScooter a:eq(0)");
    var imgLink = link.find("img").filter("eq(0)");
    var linkText = link.find("span").filter("eq(0)");
    //var json = { items: [ {id: '', Modello: '', urlFoto: ''}
    //alert(GlbCurrentOfferteMotoIndex + "_" + (GlbOfferteMoto.items.length -1));
    if (GlbCurrentOfferteScooterIndex > GlbOfferteScooter.items.length - 1) GlbCurrentOfferteScooterIndex = -1;
    justDoIt = false;
    $.each(GlbOfferteScooter.items, function(i, item) {
        if (i > GlbCurrentOfferteScooterIndex && !justDoIt) {
            imgLink.attr("alt", item.Modello);
            imgLink.attr("src", item.urlFoto);
            linkText.text(item.Modello);
            link.attr("url", "/scooter/detailsL.aspx?marca=" + item.Marca + "&modello=" + item.Modello);
            GlbCurrentOfferteScooterIndex++;
            justDoIt = true;
        }
    });
}