/*
    * @Name: Auto Complete Avatar Finder
    * @Author: Dustin Diaz
    * @Date: 2007-02-14
    * @Desc: An API that allows you to match avatar names and
             return various kinds of information
             
             
    * @namespace: IMVU.catalog
    * @requires:
        * YAHOO
            * YAHOO.util.Dom
            * YAHOO.util.Event
            * YAHOO.util.Connect
        * IMVU
*/

IMVU.namespace('catalog');
(function() {
    
    var Dom = YAHOO.util.Dom;
    var Event = YAHOO.util.Event;
    var Connect = YAHOO.util.Connect;
    var connection_in_progress = false;
    var conn_obj = {};
    var timer = {};
    
    IMVU.catalog.connection_uri = '/catalog/includes/api/xhr_avatar_finder.php';
    
    var clean_value = function(val) {
        return val;
        var s = val.toString().replace(/\s/g, '');
        return s;
    };
    
    var invalidKeyPressed = function( e ) {        
        var code = Event.getCharCode( e );
        if(( code >= 32 && code < 36 ) || ( code >= 41 && code < 48 ) || ( code > 57 && code < 65 ) || ( code > 90 && code < 97 )) {
            Event.stopEvent( e );
            return true;
        }
    };
    
    /*
        * @param el {HTMLInputElement, String}: The HTML input element you're 
            assigning the functionality to
        * @param config {Object}: The configuration object that allows you to 
            decide what kind of results you want back. This will also determine
            how your callback is delivered to you
        * @param callback {Function}: When a request has completed, your callback
            method will be fired with the appropriate data
    */
    IMVU.catalog.AvatarFinder = function(el, config, callback) {
        this.init(el, config, callback);
    };
    IMVU.catalog.AvatarFinder.prototype = {
        init : function(el, config, callback) {
            this.el = Dom.get(el);
            this.matching = config['matching'];
            this.callbackFn = callback;
            this.timeout = config['timeout'];
            this.onSuccess = new YAHOO.util.CustomEvent(
                'avatar request success', 
                this, 
                true,
                YAHOO.util.CustomEvent.FLAT
            );
            
            this.beginRequestEvent = new YAHOO.util.CustomEvent("begin");
            this.requestSuccessEvent = new YAHOO.util.CustomEvent("success");
            
            Event.on(this.el, 'keyup', this.bridge_to_request, this, true);
            Event.on(this.el, 'keypress', invalidKeyPressed );
        },
        check_name_availability_onload : function( el ) {
        	this.el = Dom.get( el );
        	this.request.call( this, this.el.value );
        },
        bridge_to_request : function(e) {            
            if( invalidKeyPressed( e ) ) {
                return;
            }
            this.request.call(this, this.el.value);
        },
        request : function(avatar_value) {
            while ( IMVU.catalog.connection && Connect.isCallInProgress(IMVU.catalog.connection) ) {
                Connect.abort(IMVU.catalog.connection);
            }
            
            var that = this;
            var avatar_value = clean_value(avatar_value);
            var uri = IMVU.catalog.connection_uri+'?avatar='+avatar_value+'&matching='+this.matching;
            //connection_in_progress = true;
            IMVU.catalog.connection = Connect.asyncRequest('GET', uri, {
                success : function(o) {
                    that.requestSuccessEvent.fire(o);
                    var response = o.responseText;
                    that.callbackFn(response);
                    //connection_in_progress = false;
                },
                failure : function(o) {
                    //connection_in_progress = false;
                },
                timeout : this.timeout
            });
            that.beginRequestEvent.fire();
        }
    };
    
})();