

(function( $ ){

    var methods = {
        init : function( options ) {

            return this.each(function(){

                // If the plugin hasn't been initialized yet
                if ( ! $(this).data('cityAutocomplete') ) {
                    $(this).data('cityAutocomplete', {
                        iataCode : null
                    });
                }

                $(this).autocomplete({
                    source: "/IATACityAutocomplete.view",
                    dataType: "json",
                    minLength: 3,
                    open: function(event, ui) {
                        $(this).data('cityAutocomplete').iataCode = null;
                    },
                    select: function( event, ui ) {
                        $(this).data('cityAutocomplete').iataCode = ui.item.id;
                    }
                });

            });
        },
        destroy : function( ) {

            return this.each(function(){

                // Namespacing FTW
                $(window).unbind('.cityAutocomplete');
                $(this).data('cityAutocomplete').remove();
                $(this).removeData('cityAutocomplete');

            })

        },
        getIataCode : function( ) {
            return $(this).data('cityAutocomplete').iataCode;
        }
    };

    $.fn.cityAutocomplete = 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' );
        }

        return $(this);
    };
})( jQuery );



