var refined = false;
var selValue = "";
$(document).ready(function() {
    //convert the json string to associative array
    $('#celebsearch').autocomplete(m_celebrities ,
    {
        width:170,
        max: 50,
        scrollHeight: 220,
        highlight: false,
        minChars: 2,
        matchContains: "word",
	autoFill: true,
        formatItem: function(data, i, n, value) {
            return "<span class=\"ac_name\">" +  data.name + "</span>";
        },
        formatMatch: function(row, i, max) {
            return row.name;
        },
        formatResult: function(row) {
            return row.profile;
        }
    }
    );

   $('#celebsearch').result(function(event, data, formatted) {
        refined = true;
        var url_str = window.location.href;
        var url_arr = url_str.split("/");
        if(url_arr.length > 4)
            window.location = '/'+url_arr[3]+'/profile/' + data.profile;
        else
            window.location = '/en/profile/' + data.profile;

    });

    //Bind to the keypress event to reset the hidden elements
    $("#celebsearch").bind(($.browser.opera ? "keypress" : "keydown"), function(event) {
        if (selValue.toLowerCase() != $("#celebsearch").val().toLowerCase())
        {
            refined = false;
            selValue = "";
        }
    });

    $('#celebsearchform').submit(function() {
        if (!refined || selValue.toLowerCase() != $("#celebsearch").val().toLowerCase())
            return false;
        return true;
    });

});
