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

	var GUIStateCreating = 'creating', 
		GUIStateReady = 'ready';
    
    Y.dsbox.ItemCreate = function(config) {
    	this._state = GUIStateCreating;
    	this._config = config;
    };

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

    Y.dsbox.ItemCreate.prototype._init = function () {
    	var form_id = this._config.gui_uid +'_form_create_item';
    	var add_id = this._config.gui_uid +'_add';

    	var html = '';
		html += '<form id="'+form_id+'" onSubmit="return false;">';
		html += '  <div class="dsbox_form_field">';
		html += '    <div class="dsbox_form_field_label">'+this._config.dictionary.name_title+'</div>';
		html += '    <div class="dsbox_form_field_input_box">';
		html += '      <input type="text" name="form_item_name" id="form_item_name"></input>';
		html += '    </div>';
		html += '  </div>';
		html += '  <div class="dsbox_form_field">';
		html += '    <div class="dsbox_form_field_label">'+this._config.dictionary.shop_group_title+'</div>';
		html += '    <div class="dsbox_form_field_input_box">';
		html += '      <select name="form_shop_group">';
		html +=          this._config.cb_generate_shop_group_options_html();
		html += '      </select>';
		html += '    </div>';
		html += '  </div>';
		html += '  <div class="dsbox_form_field">';
		html += '    <div class="dsbox_form_field_label">'+this._config.dictionary.keywords_title+'</div>';
		html += '    <div class="dsbox_form_field_input_box">';
		html += '      <textarea  name="form_keywords"></textarea>';
		html += '    </div>';
		html += '  </div>';
		html += '  <div class="dsbox_button_box">';
		html += '    <input class="dsbox_button" type="button" value="'+this._config.dictionary.button_title_add+'" id="'+add_id+'"/></input>';
		html += '  </div>';
		html += '<br/>';
    	Y.one("#"+this._config.gui_node_id).append(html);
		Y.on("click", Y.bind(this._click_add, this), "#"+add_id);
    };
    
    Y.dsbox.ItemCreate.prototype._click_add = function (e) {
    	this._add_item();
    };

    Y.dsbox.ItemCreate.prototype._add_item = function () {
    	var url = "/start/do_create_new_item/"+this._config.list_uid+"?item_type="+this._config.item_type+"&";
    	var cfg = {
    		method: "POST",
    		form: {
    			id: Y.Node.one('#'+this._config.gui_uid +'_form_create_item'),
    			useDisabled: true
    		},
    		on: {
    			success: Y.bind(this._add_item_done, this)
    		}
    	};
    	var request = Y.io(url, cfg);
    };
	
    Y.dsbox.ItemCreate.prototype._add_item_done = function (e, result) {
    	if(result.status == 200) {
    		this._config.cp_add_item_done(this, parseInt(result.responseText));
    	}
    };

    Y.dsbox.ItemCreate.prototype.set_focus = function () {
        var focus_node = Y.one("#form_item_name");
    	if(focus_node) {
    		focus_node.focus();
    	}
    };

    
}, '1.0.0', { requires: ['base', 'base-base', "datasource-jsonschema", "datasource-xmlschema", "datasource-polling", "datasource-function"] });

