/*
 * Ctrl Modifications - jQuery UI Autocomplete 1.8.2
 *
 *  $Id$
 */
(function( $ ) {
	
$.ui.autocomplete.prototype.options = {
    minLength: 1,
    delay: 300,
    queryArg: 'search',
    normalizeResponse: function(response) { return response; },
    normalizeRow: function(item) {
        if ( typeof item === "string" ) {
            return {
                label: item,
                value: item
            };
        }
        return $.extend({
            label: item.label || item.value,
            value: item.value || item.label
        }, item );
    },
    position:  {
		my: "left top",
		at: "left bottom",
		collision: "none"
	},
};
    
$.ui.autocomplete.prototype._search = function( value ) {
    this.term = this.element
        .addClass( "ui-autocomplete-loading" )
        // always save the actual value, not the one passed as an argument
        .val();

    /*
     * jason @ orchestra
     * ctrl modification
     * 
     * add queryArg option
     */
    var q = {};
    q[this.options.queryArg] = value;
    this.source( q, this.response );
};

$.ui.autocomplete.prototype._response = function( content ) {
    if ( content ) { // if ( content.length ) {
        content = this._normalize( content );
        this._suggest( content );
        this._trigger( "open" );
    } else {
        this.close();
    }

    this.element.removeClass( "ui-autocomplete-loading" );
};

$.ui.autocomplete.prototype._normalize = function( items ) {
    // assume all items have the right format when the first item is complete
    //if ( items.length && items[0].label && items[0].value ) {
    //    return items;
    //}
    items = this.options.normalizeResponse(items);
    
    return $.map( items, this.options.normalizeRow );
};



$.effects.fade = function(o) {
    return this.queue(function() {
        var elem = $(this),
            mode = $.effects.setMode(elem, o.options.mode || 'hide');

        elem.animate({ opacity: mode }, {
            queue: false,
            duration: o.duration,
            easing: o.options.easing,
            complete: function() {
                (o.callback && o.callback.apply(this, arguments));
                elem.dequeue();
            }
        });
    });
};
}( jQuery ));

