jQuery(document).ready( function(){
    var extraParams = {
            options : function() { return getAutocompleteOption() }
    };

    // Autocomplete
    input_width = jQuery("#simple_search").width();
    var default_url='/ezjscore/call/comexposiumcatalog::autocomplete::'+jQuery('#catalog_id').val();
    autoComplete( jQuery(".autocomplete_search_homepage"), input_width+'px', default_url, extraParams);
});

function getAutocompleteOption()
{
    var type='';
    
    if (jQuery('#company_ch').is(':checked'))
    {
        type='companyName';
    }
    if (jQuery('#brand_ch').is(':checked'))
    {
        type=type+'_brand';
    }
    if (jQuery('#product_ch').is(':checked'))
    {
        type=type+'_product';
    }
    
    if (type=='')
    {
    	type='all';
    }
    
    return type;
}
function autoComplete( input, width, url, extraParams)
{
    extraParams = extraParams || {};

    input.autocomplete( url, {
        dataType: 'json',
        parse: function(data)
        {
            var rows = new Array();
            var results = new Array();
            if (data)
	    { 
	     results = data.content.results;
             values = data.content.values;
             for( var i=0; i<results.length; i++ )
             {
                rows[i] = { data:results[i], value:results[i], result:values[i] };
             }
            }
            return rows;
        },
        formatItem: function( row, i, n )
        {
            return row;
        },
        width: width,
        cacheLength:0,
        scroll: false,
        highlight: false,
        matchContains: 1,
        minChars: 4,
        max: 5,
        selectFirst: false,
        extraParams: extraParams
    } );
}
