$().ready(function() {

    function findValueCallback(event, data, formatted) {
        $("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
    }

    function formatItems(row) {
        return row[0] + " (<strong>id: " + row[1] + "</strong>)";
    }

    function formatItem(row) {  	return row[0] + " (id: " + row[1] + ")";  }

        function formatResult(row) {
            return row[0].replace(/(<.+?>)/gi, '');
        }

    //datasource is a url
    $("#addtag").autocomplete(base_url + "autocomplete/suggest_tags", {
            scrollHeight:260,
            selectFirst: false,
            cacheLength:0,

            formatItem: function(item) {
                //alert(item[0]);
                var tempTable="";
                tempTable+="<table cellspacing='0' cellpadding='0' width='100%'>";
                tempTable+="<tr>";

                tempTable+="<td width='1%' align='left'>&nbsp;</td>";
                tempTable+="<td width='99%' align='left'>";
                tempTable+=item[0];
                tempTable+="</td>";
                tempTable+="</tr>";
                tempTable+="</table>";
                return tempTable;
            }
    }).result(function(event, item) {
        handle_add_tag_submit();
    });


    $("#addtag").result(function(event, data, formatted) {
        if (data) {
            $(this).parent().next().find("input").val(data[1]);
        }
    });

});
