

var inventoryItemCreate = {
		
		defaults : {
			products: [],
			selector_type : '#inventoryitemform-type',
			selector_name : '#inventoryitemform-name',
			selector_id : '#inventoryitemform-id_product',
		},
		product: null,
		init: function(o){
			this.defaults = $.extend( this.defaults, o );
			console.info(this.defaults.products);
			this.initAutocomplete();
			
		},
		
		initAutocomplete :	function (){
			var self = this;
			
			var $input = $(self.defaults.selector_name);
			$input.typeahead({source : self.defaults.products, 
			            autoSelect: true,
			            items: 20,
			            minLength: 3,
			}); 
			$input.change(function() {
			    var current = $input.typeahead("getActive");
			    $("#filter_text").val('');
			    if (current) {
			        // Some item from your model is active!
			        if (current.name == $input.val()) {
			        	self.product = current;
			        } else {
			        	self.product = null;
			        }
			    } else {
			    	self.product = null;
			    }
			    self.productChange();
			});
		},
		productChange: function( ){
			this.clearProductData();
			this.setProductData();
		},
		clearProductData: function(){
			$(this.defaults.selector_type).val('');
			$(this.defaults.selector_id).val('');
		},
		setProductData: function(){
			if ( this.product ){
				$(this.defaults.selector_type).val( this.product.type);
				$(this.defaults.selector_id).val( this.product.id);
			}
			
		}
}