//
// Create autocomplete field.
//
// Display resoults
// Autogenerate div for data display



var autocomplete = {}

autocomplete.results = new Array();
autocomplete.askDb = false;


autocomplete.setData = function(type,displayDiv){
    autocomplete.aType = type;
    autocomplete.displayDiv = displayDiv;
}

autocomplete.search = function(text){
    if (this.askDb) {
        var results = this.getDbData(text);
        if (results !== false){
            this.prepareResults(results);
            return this.compareString(text);
        }
        return false;
    } else {
        return this.compareString(text);
    }
}

autocomplete.prepareResults = function(data){
    var i = data.length;
    while (i--){
        this.results[data[i].title.replace(/\s+/g," ")] = data[i].title;
    }
}

autocomplete.display = function(text){

}

autocomplete.compareString = function(text){
    for (var key in this.results){

    }
}

autocomplete.checkString = function(){

}

autocomplete.getDbData = function(text){
    $.ajax({
        type: "GET",
        async : false,
        url: "page/autocomplete.php",
        data : ({string : text}),
        dataType : "json",
        success: function(data){
            if (data.length > 0){
                return data;
            } else {
                return false;
            }
        }
    })
}
