Jquery best practins .data (tracking vars)
<p>Considering the code below and the best practicing for jquery development plugins I have one problem. I have tried to keep track of the options, but in this case $(this).data("options" { example:'1' }); are not working, so I used</p>

<p>$.fn.autocomplete.options = new Array;</p>

<p>In the last line of the code, so my plugin is working but in a global var scope and I wouldn't like to do it. I have been digging for more information but no glue... any suggestions?</p>

<p>(function( $ ){</p>

<pre><code>var methods = {

    init : function( options ) { 

        var defaults = {};

        var options =  $.extend(defaults, options);

        var main = '#' + $(this).attr("id");

        $.fn.autocomplete.options[main] = options;

        // Monta o fieldset

        var autocomplete_results = document.createElement('div');

        $(autocomplete_results).attr("id", $(this).attr("id") + "_results");
        $(autocomplete_results).attr("class", "jquery_autocomplete_results");
        $(autocomplete_results).attr("style", "position:absolute;display:none;");
        $(autocomplete_results).css("width", $(this).css("width"));

        var autocomplete_options = document.createElement('div');

        $(autocomplete_options).attr("id", $(this).attr("id") + "_options");
        $(autocomplete_options).attr("class", "jquery_autocomplete_options");

        if (options.type == 'single-regular')
        {
            $(this).attr("onkeyup", "$('#" + $(this).attr("id") + "').autocomplete('search', this);");
            $(this).attr("onblur", "$('#" + $(this).attr("id") + "_results').fadeOut('fast', null)");
        }
        else
        {
            var autocomplete_input = document.createElement('input');

            if ($(this).attr("style")) $(autocomplete_input).attr("style", $(this).attr("style"));

            $(autocomplete_input).attr("onkeyup", "$('#" + $(this).attr("id") + "').autocomplete('search', this);");
            $(autocomplete_input).attr("onblur", "$('#" + $(this).attr("id") + "_results').fadeOut('fast', null)");

            $(this).css("display", "none");
            $(this).before($(autocomplete_input));
        }

        $(this).after($(autocomplete_options));
        $(this).after($(autocomplete_results));
    },
    search : function( obj ) {


        var main = "#" + $(this).attr("id");

        var content = "#" + $(this).attr("id") + "_results";

        var options = $.fn.autocomplete.options[main];

        if ((options.url) &amp;&amp; (obj.value))
        {
            $.getJSON(options.url + '&amp;q=' + obj.value, function(data) {

                if (data)
                {
                    $(content).html("");

                    var limit = 0;

                    $.each(data, function(k, v) {

                        if (limit &lt; 10)
                        {
                            $(content).append('&lt;li id="' + v.id + '" class="autocomplete_content_record" onclick="$(\'' + main + '\').autocomplete(\'add\', {id: this.id, name: this.innerHTML})"&gt;' + v.name + '&lt;/li&gt;');

                            limit++;
                        }
                    });

                    $(content).css("display", "block");
                }
            });
        }
    },
    add : function( data ) {

        var main = "#" + $(this).attr("id");

        var content = "#" + $(this).attr("id") + "_options";

        var options = $.fn.autocomplete.options[main];

        if (options.type == 'single-regular')
        {
            $(main).attr("value", data.id);
        }
        else
        {
            var autocomplete_record = document.createElement('div');

            $(autocomplete_record).html('&lt;input type="checkbox" value="' + data.id + '" checked="checked" onclick="$(parentNode).remove(); $(\'' + main + '\').autocomplete(\'update\');"&gt; ' + data.name + '&lt;br&gt;');

            $(content).append(autocomplete_record);

            $(this).autocomplete('update');
        }
    },
    update : function( content ) {

        var main = "#" + $(this).attr("id");

        var content = "#" + $(this).attr("id") + "_options";

        var items = $(content).find("input");

        var items_selected = '';

        for (var i = 0; i &lt; items.length; i++)
        {
            if (items_selected != '') items_selected += ',';

            items_selected += items[i].value;
        }

        $(main).val(items_selected);
    }
};

$.fn.autocomplete = function( method ) {

    if ( methods[method] ) {
        return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
        return methods.init.apply( this, arguments );
    } else {
        $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }  
};
</code></pre>

<p>})( jQuery );</p>

<p>$.fn.autocomplete.options = new Array;</p>
