/*
 * module: dsbox_gui_show 
 * 
 */
YUI.add('dsbox_gui_show', function(Y) {

	Y.namespace('dsbox');
 
	var GUIStateReady = 'ready', 
		GUIStateWait = 'wait',
	    GUIStateLoading = 'loading';
    
    Y.dsbox.GUIShow = function(config) {
    	this._state = GUIStateReady;
    	this._config = config;
    	this._page_id = 1;
    	this._timer_id = null;
    };

    Y.dsbox.GUIShow.prototype.start = function () {
    	Y.all("#"+this._config.node_id).on("mouseover", Y.bind(this._pause_show, this));
    	Y.all("#"+this._config.node_id).on("mouseout", Y.bind(this._restart_show, this));
    	this.start_show_load_start();
    };
    
    Y.dsbox.GUIShow.prototype.start_show_load_success = function (rc, data) {
    	if(this._state == GUIStateLoading) {
          Y.one("#"+this._config.node_id).set("innerHTML", data.responseText);
          this._state = GUIStateWait;
          this._timer_id = setTimeout(Y.bind(this.start_show_shift_page, this), 17000);
    	}
    };
    
    Y.dsbox.GUIShow.prototype.start_show_load_start = function() {
    	if(this._state == GUIStateReady) {
      	  this._state = GUIStateLoading;
      	  this._load_cfg = {
      			method: "GET",
    			on: {
    				success: Y.bind(this.start_show_load_success, this)
    			}
    		};
      	  this._load_request = Y.io("/welcome/ajax_show_page/"+this._config.lang+"/"+this._page_id, this._load_cfg);
    	}
    };

    Y.dsbox.GUIShow.prototype.start_show_shift_page = function() {
    	if(this._state == GUIStateWait) {
    		this._page_id = this._page_id + 1;
    		this._state = GUIStateReady;
        	this.start_show_load_start();
    	}
    };

    Y.dsbox.GUIShow.prototype._pause_show = function() {
    	if(this._state == GUIStateWait) {
    		this._state = GUIStateReady;
    		clearTimeout(this._timer_id);
    	}
    };
    
    Y.dsbox.GUIShow.prototype._restart_show = function() {
    	if(this._state == GUIStateReady) {
	        this._state = GUIStateWait;
	        this._timer_id = setTimeout(Y.bind(this.start_show_shift_page, this), 1000);
    	}
    };
    
}, '1.0.0', { requires: ['base', 'base-base', "anim-base", "io"] });

