$(document).ready(function() {
	
	$("#resources_author").autocomplete('/ajax/get-authors-by-resource', {
        width: 194,
        multiple: false,
        minChars: 3,
        mustMatch: false,
        scroll: true,
        selectFirst: true,
        parse: function(data){
            return $.map(eval(data), function(row){
                return {
                    data: row,
                    value: row,
                    result: row
                }
            });
        },
        formatItem: function(item){
            return (item);
        }
    }).result(function(event, data, formatted){
        if (data) {
            $('#resources_volume').val('');
        }
    });
	
	$("#resources_volume").autocomplete('/ajax/get-albums-by-author', {
        multiple: false,
        minChars: 3,
        mustMatch: false,
        scroll: true,
        selectFirst: true,
        cacheLength: 0,
        extraParams: {
            author: function(){
                return $("#resources_author").val();
            }
        },
        parse: function(data){
            return $.map(eval(data), function(row){
                return {
                    data: row,
                    value: row,
                    result: row
                }
            });
        },
        formatItem: function(item){
            return (item);
        }
    }).result(function(event, data, formatted){
        if (data) {
            
        }
    });
	
	$("#resources_username").autocomplete('/ajax/get-username-for-autocomplete', {
        width: 200,
        minChars: 3,
        multiple: false,
        matchContains: true,
        selectFirst: false,
        mustMatch: true
    });

	$("#search_text").autocomplete('/ajax/search-on-the-fly', {
        width: 500,
        scrollHeight: 300,
        multiple: false,
        minChars: 3,
        mustMatch: false,
        scroll: true,
        delay: 0,
        selectFirst: false,
        cacheLength: 0,
        extraParams: {
            search_in: function(){
                return $("#search_in").val();
            },
            search_by: function(){
                return $("#search_by").val();
            }
        },
        parse: function(data){
            return $.map(eval(data), function(row){
            	var input_text = stripHTML(row['text']);
                return {
                    data: row,
                    value: row,
                    result: input_text
                }
            });
        },
        formatItem: function(item){
            return item.text;
        }
    }).result(function(event, item) {
    	  location.href = item.url;
    });

});

function stripHTML(text){
	return text.replace(/<\/?[^>]+>/gi, "");
}