$().ready(function() {
    selected_site_ids = new Array();
    // datasource is a url
    $("#txt_sites").autocomplete(base_url + "autocomplete/get_sites", {
        scrollHeight:260,
        selectFirst: false,
        cacheLength:0,

        formatItem: function(item) {
            var tempTable = "";
            tempTable += "<table cellspacing='0' cellpadding='0' width='100%'>";
            tempTable += "<tr>";

            tempTable += "<td width='1%' align='left'>&nbsp;</td>";
            tempTable += "<td width='10%' align='left'>";
            if (item[2] != "") {
                tempTable += item[2];
            } else {
                tempTable +="&nbsp;";
            }
            tempTable += "</td>";
            tempTable += "<td width='89%' align='left'>";
            tempTable += item[0];
            tempTable += "</td>";
            tempTable += "</tr>";
            tempTable += "</table>";
            return tempTable;
        }
    }).result(function(event, item) {
        if (item[0] == "We don't have that site yet; do you think we should add it?") {
            location.href = item[1];
            return;
        }
        add_site(item[3]);
    });


    $("#txt_sites").result(function(event, data, formatted) {
        if (data) {
            $(this).parent().next().find("input").val(data[1]);
        }
    });
});
