AutoCPL = Class.create({
    element: null,
    parent: null,
	container: null,
    state: null,
    IEquirk: false,
    holdFocus: false,
    
	initialize: function(parent) {
        this.parent = $(parent);

        this.element = new Element('div',{
			id: this.parent.identify() + '_autocomplete'
        });
        //Modification. pscott 2009-6-15  previous class assignment not working in IE8
        this.element.addClassName("AutoCPL_Container");
		this.container = new Element('div',{
            'id': 'AutoCPL'
		});
		//Modification. pscott 2009-6-15  previous class assignment not working in IE8
		this.container.addClassName("AutoCPL");
		this.container.hide();
		this.container.appendChild(this.element);
		this.parent.parentNode.appendChild(this.container);
		
		//Addition. pscott 3-24-11 : set the holdFocus property to fix safari scroll bug
		this.container.observe('mouseover', function(evt){this.holdFocus = true;}.bind(this));
		this.container.observe('mouseout', function(evt){this.holdFocus = false;}.bind(this));
		
        this.parent.observe('keyup', this.keypress.bind(this));
        this.parent.observe('focus', this.focus.bind(this));
        this.parent.observe('blur', this.lostFocus.bind(this));
	},
	
	//Modification. pscott 3-24-11 : more cross browser compliant implementation
    lostFocus: function(event) {
		if(this.holdFocus){
			this.parent.focus();
		}
		else{
			this.parent.value = 'Enter Style Number';
			this.container.hide();
		}
    },
    focus: function(){
        if(this.IEquirk){
            this.IEquirk = false;
            return;
        }
        if(this.parent && $F(this.parent.identify()) == 'Enter Style Number'){
            this.parent.value = '';
            this.updateList();
        }else if(!this.container.visible()){
            this.updateList();
        }
    },
    keypress: function(event){
        var KEY_UP = 40;
        var KEY_DOWN = 38;

        if(!(event.keyCode == KEY_UP || event.keyCode == KEY_DOWN)){ // && $F(AutoComplete.parent) !=''
            this.updateList();
        }else{
            if($F(this.parent.identify()) !=''){
                if(event.keyCode == KEY_UP)
                    this.keyup();
                if(event.keyCode == KEY_DOWN)
                    this.keydown();
            }else{
                this.container.hide()
                this.element.update();
            }
        }
    },
    keyup: function(){
        
    },
    keydown: function(){
        
    },
    updateList: function(){
        new Ajax.Request(
            '/serviceGateway.cfm', {
                method: 'get',
                parameters: {
                    component: 'product',
                    method: 'getProducts',
                    productNumber: $F(this.parent.identify())
                },
                onSuccess: function(transport) {
                    this.element.update();
                    var results = transport.responseText.evalJSON();
                    results.each( function(row){
                            var newRow = new Element('div',{ 'class': 'autosuggest_item'}).update(row.productnumber);
                            newRow.observe('click', this.clicked.bind(this));
                            this.element.appendChild(newRow);
                            newRow = null;
                    }.bind(this));
                    if(this.element.innerHTML != ''){
                        AutoComplete.container.show();
                    }else{
                        AutoComplete.container.hide();
                    }
                }.bind(this)
            }
        );
    },
    clicked: function(event){
        if(navigator.appName.indexOf("Microsoft") != -1)
            this.IEquirk=true;
        this.parent.value = Event.element(event).innerHTML;
        this.parent.focus();
        this.container.hide();
        if($('express'))
            $('express').submit();
    }
});

