/*
 * module: dsbox_user_search 
 * 
 */
YUI.add('dsbox_user_search', function(Y) {
    Y.namespace('dsbox');

	var GUIStateReady = 'ready', 
		GUIStateWaitingToLoad = 'waiting',
		GUIStateLoading = 'loading';
    
    Y.dsbox.UserSearch = function(config) {
    	this._state = GUIStateReady;
    	this._config = config;
    	this._data = {}
    	this._load_pending = false;
    };

    Y.dsbox.UserSearch.prototype.start = function () {
    	this._init();
    	this._config.cb_init_done();
    };

    Y.dsbox.UserSearch.prototype.set_focus = function () {
        var focus_node = Y.one("#"+this._config.gui_uid +'_form_search');
    	if(focus_node) {
    		focus_node.focus();
    	}
    };

    Y.dsbox.UserSearch.prototype._clear_data = function () {
    };
    
    Y.dsbox.UserSearch.prototype._init = function () {
    	var table_data_node_id = this._config.gui_uid +'_div_data';
    	var form_search_node_id = this._config.gui_uid +'_form_search';
    	var button_search_node_id = this._config.gui_uid +'_do_search';
    	var owner_form_search_node_id = this._config.gui_uid +'_form_search_owner';

    	var html = '';
    	html += '<div class="dsbox_form_field" id="'+owner_form_search_node_id+'">';
		html += '  <div class="dsbox_form_field_input_box">';
    	html += '    <input id="'+form_search_node_id+'" class="dsbox_form_field_input_text" type="text" name="form_search"></input>';
		html += '  </div>';
    	html += '</div>';
    	html += '<div class="dsbox_table_no_select" id="'+table_data_node_id+'">';
    	html += '</div>';
    	Y.one("#"+this._config.gui_node_id).setContent(html);

    	var form_search_node = Y.one('#'+form_search_node_id);
    	form_search_node.plug(Y.Plugin.AutoComplete, {
	    });
    	form_search_node.ac.after('query', Y.bind(this._load_data_sleep_start, this));
    };

    Y.dsbox.UserSearch.prototype._load_data_sleep_start = function (e) {
    	if (this._state == GUIStateReady) {
    		this._load_data_sleep(e);
    	}
    	else if (this._state == GUIStateWaitingToLoad) {
    		clearTimeout(this._timer_id); 
    		this._load_data_sleep(e);
    	}
    	else if (this._state == GUIStateLoading) {
    		this._load_pending = true;
    	}
    };

    Y.dsbox.UserSearch.prototype._load_data_sleep = function (e) {
		this._state = GUIStateWaitingToLoad;
    	this._timer_id = setTimeout(Y.bind(this._load_data_start, this), 300);
    };
    
    Y.dsbox.UserSearch.prototype._load_data_start = function () {
    	var form_search_node_id = this._config.gui_uid +'_form_search';
		var form_search = Y.one('#'+form_search_node_id).get('value');
		this._state = GUIStateLoading;
		this._load_pending = false;
		this._load_data(form_search);
    };
    
    Y.dsbox.UserSearch.prototype._load_data = function (form_search) {
    	var _item_search_datasource = new Y.DataSource.Get({source:"/start/ajax_request_friend_search?"});
    	var _item_search_datasource_cfg = {
    			success: Y.bind(this._load_data_done, this),
    			failure: Y.bind(this._load_data_done, this)
    	};
    	_item_search_datasource.plug(Y.Plugin.DataSourceJSONSchema, {
    		schema: {
            	resultListLocator: "users",
            	resultFields: ["uid", "name"]
        	}
    	});
    	var request = '';
    	request += 'form_search='+form_search
    	
    	_item_search_datasource.sendRequest({
       			callback : _item_search_datasource_cfg,
       			request  : request
    	});
    	var form_search_node_id = this._config.gui_uid +'_form_search';
	};
	
    Y.dsbox.UserSearch.prototype._load_data_done = function (e) {
    	var data = e.response.results;
    	var table_data_node_id = this._config.gui_uid +'_div_data';
    	var scrollview_container_node_id = this._config.gui_uid +'_scrollview-container';
    	var scrollview_content_node_id = this._config.gui_uid +'_scrollview-content';
    	var scrollview_id = this._config.gui_uid +'_scrollview';
    	Y.all("#"+scrollview_container_node_id).remove();
    	
    	if(data.length == 0) {
        	var table_data_node_id = this._config.gui_uid +'_div_data';
        	html = '';
        	html += '<div id="'+scrollview_container_node_id+'" class="dsbox_form_field">';
    		html += '  <div class="dsbox_form_field_label">'+this._config.dictionary.no_data_found+'</div>';
        	html += '</div>';
    		Y.one('#'+table_data_node_id).setContent(html);
    	}
    	else {
	    	html = '';
	    	html += '<div id="'+scrollview_container_node_id+'" class="yui3-skin-sam scrollview_container">';
	    	html += '  <div id="'+scrollview_content_node_id+'" class="scrollview_content">';
	    	html += '    <ul>'; 
	    	html += this._config.cb_generate_html(this, data);
	    	html += '    </ul>'; 
	    	html += '  </div>';
	    	html += '</div>';
			Y.one('#'+table_data_node_id).setContent(html);

	    	html += this._config.cb_generate_html_post(this, data);

	    	this._scrollView = new Y.ScrollView({
	            id: scrollview_id,
	            srcNode: '#'+scrollview_content_node_id,
	            height: 400,
	            flick: {
	                minDistance:10,
	                minVelocity:0.3,
	                axis: "y"
	            }
	        });
			this._scrollView.render();
			this._scrollView.scrollbars.flash();
    	}
		this._state = GUIStateReady;
		if(this._load_pending) {
    		this._load_data_sleep_start('');
		}
    };
    
}, '1.0.0', { requires: ['base', 'base-base', "datasource-jsonschema", "datasource-xmlschema", "datasource-polling", "datasource-function", "scrollview"] });

