/*
 *
 *   Visualizza e Nasconde la textbox della ricerca della parte pubblica utilizzando jquery autocomple
 *
 *   richiede jquery.autocomplete.js
 *
 *   @author: Vito Santiate - http://www.sanitate.it
 *   @date: Venerdi 28 Maggio 2010
 *
 */
$(document).ready(function() {

    $(".searchBox").autocomplete('/autocompletesearch.ashx', {
		resultsClass: "ac_results searchBox",
        minChars: 2,
        max: 10,
        autoFill: true,
        mustMatch: true,
        matchContains: false,
        multiple: true,
        multipleSeparator: " | ",
        scrollHeight: 220
    });

    $(".search input").bind("keydown", function(event) {
        // track enter key
        var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        //Esc
        if (keycode == 27) {
            $(".search a").show();
            $(".search .deleteicon").hide();
        }
        if (keycode == 13) { // keycode for enter key
            var value = $(this).val();
            // force the 'Enter Key' to implicitly click the Update button
            window.location.href = "/Photos.aspx?search=" + value;
            return false;
        }
        else {
            return true;
        }
    }); // end of function

    $("li.searchtag .close").click(function() {
        $("li.searchtag .deleteicon").hide();
        $("li.searchtag a").show();
    });

    $(".search .close").click(function() {
        $(".search .deleteicon").hide();
        $(".search a").show();
    });

    $("li.searchtag a").click(function() {
        $(this).hide();
        $("li.searchtag .deleteicon").show();
        $("li.searchtag input").focus();
        return false;
    });

    $(".search a").click(function() {
        $(this).hide();
        $(".search .deleteicon").show();
        $(".search input").focus();
        return false;
    });

    $('.searchBoxTag').result(function(event, data, formatted) {
        if (formatted != undefined) {
            // force the 'Enter Key' to implicitly click the Update button
            var oldTags = jQuery.url.param("tags");
            var oldCity = jQuery.url.param("city");
            var oldPow = jQuery.url.param("pow");
            var oldFavourites = jQuery.url.param("favorites");

            var tagsParam = oldTags != undefined ? "&tags=" + oldTags : "";
            var cityParam = oldCity != undefined ? "&city=" + oldCity : "";
            var pow = oldPow != undefined ? "&pow=" + oldPow : "";
            var favorites = oldFavourites != undefined ? "&favorites=" + oldFavourites : "";

            window.location.href = "/Photos.aspx?search=" + formatted + tagsParam + cityParam + favorites + pow;
        }
    });

    $("li.searchtag input").bind("keydown", function(event) {
        // track enter key
        var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        //Esc
        if (keycode == 27) {
            $("li.searchtag a").show();
            $("li.searchtag .deleteicon").hide();
        }
    }); // end of function
    $(".searchBoxTag").autocomplete('autocompletesearch.ashx?tag=true', {
		resultsClass: "ac_results searchBoxTag",
        minChars: 2,
        max: 10,
        autoFill: true,
        mustMatch: true,
        matchContains: false,
        multiple: false,
        scrollHeight: 220
    });
});

