$package('difc.loginbar',
    $class('LoginBarAction').$implements(ejs.events.IEventDispatcher).$define(
        $private({
            httper:null,
            userid:null,
            password:null,
            h_signin:null,
            h_signout:null,
            beforeDivision:null,
            afterDivision:null,
            progressDivision:null,
            state:false,
            userInfo:{
                name:''
            },
            stateTune:function(){
                if(this._$state){
                    (this._$beforeDivision || { style:{ display:'' }}).style.display = 'none';
                    (this._$afterDivision || { style:{ display:'' }}).style.display = '';
                    (this._$progressDivision || { style:{ display:'' }}).style.display = 'none';
                    (ejs.$(['pn_loginbar_username_', this.id].join('')) || {innerHTML:''}).innerHTML = this._$userInfo.name;
                }
                else{
                    (this._$beforeDivision || { style:{ display:'' }}).style.display = '';
                    (this._$afterDivision || { style:{ display:'' }}).style.display = 'none';
                    (this._$progressDivision || { style:{ display:'' }}).style.display = 'none';
                }
            },
            progressing:function(){
                (this._$beforeDivision || { style:{ display:'' }}).style.display = 'none';
                (this._$afterDivision || { style:{ display:'' }}).style.display = 'none';
                (this._$progressDivision || { style:{ display:'' }}).style.display = '';
            },
            onFaultHandler:function(event){
                alert(['[error-code:',event.errorCode,']'].join(''));
            },
            onSigninHandler:function(){
                var evt = arguments[0] || window.event;
                var _target = evt.target || evt.srcElement;
                if(_target.owner._$userid.value && _target.owner._$password.value){
                    _target.owner._$httper.removeEventListener(ejs.net.ResultEvent.COMPLETE, _target.owner._$onValidateHandler);
                    _target.owner._$httper.removeEventListener(ejs.net.ResultEvent.COMPLETE, _target.owner._$onSigninCompleteHandler);
                    _target.owner._$httper.removeEventListener(ejs.net.ResultEvent.COMPLETE, _target.owner._$onSignoutCompleteHandler);
                    _target.owner._$httper.addEventListener(ejs.net.ResultEvent.COMPLETE, _target.owner._$onSigninCompleteHandler);
                    _target.owner._$httper.send(['req=', encodeURIComponent('signin'), '&uid=', encodeURIComponent(_target.owner._$userid.value), '&pwd=', encodeURIComponent(hex_md5(_target.owner._$password.value))].join(''));
                    _target.owner._$progressing();
                }
            },
            onSignoutHandler:function(){
                var evt = arguments[0] || window.event;
                var _target = evt.target || evt.srcElement;
                _target.owner._$httper.removeEventListener(ejs.net.ResultEvent.COMPLETE, _target.owner._$onValidateHandler);
                _target.owner._$httper.removeEventListener(ejs.net.ResultEvent.COMPLETE, _target.owner._$onSigninCompleteHandler);
                _target.owner._$httper.removeEventListener(ejs.net.ResultEvent.COMPLETE, _target.owner._$onSignoutCompleteHandler);
                _target.owner._$httper.addEventListener(ejs.net.ResultEvent.COMPLETE, _target.owner._$onSignoutCompleteHandler);
                _target.owner._$httper.send(['req=',encodeURIComponent('signout')].join(''));
                _target.owner._$progressing();
            },
            onValidateHandler : function(event){
                if(event.result != 'failure'){
                    this._$arrangeInfoHandler(event.result);
                    this._$state = true;
                }
                else{
                    this._$state = false;
                }
                this._$stateTune();
            },
            onSignoutCompleteHandler : function(event){
                if(event.result == 'success'){
                    this._$state = false;
                    this._$userid.value = '';
                    this._$password.value = '';
                    location.href = location.href;
                }
                else{
                    this._$state = true;
                }
                this._$stateTune();
            },
            onSigninCompleteHandler : function(event){
                this._$onValidateHandler(event);
                if(event.result != 'failure'){
                    location.href = location.href;
                }
            },
            arrangeInfoHandler : function(data){
                eval(['var _info = {', data, '};'].join('')); 
                this._$userInfo = _info;
            }
        }),
        $public({
            id:null,
            construct:function(config){
                this.id = config.id;
                this._$h_signin = ejs.$(['pn_loginbar_signin_', this.id].join(''));
                this._$h_signout = ejs.$(['pn_loginbar_signout_', this.id].join(''));
                this._$beforeDivision = ejs.$(['pn_loginbar_before_', this.id].join(''));
                this._$afterDivision = ejs.$(['pn_loginbar_after_', this.id].join(''));
                this._$progressDivision = ejs.$(['pn_loginbar_progress_', this.id].join(''));
                this._$userid = ejs.$(['pn_loginbar_userid_', this.id].join(''));
                this._$password = ejs.$(['pn_loginbar_password_', this.id].join(''));

                this._$httper = new ejs.net.HttpService();
                this._$httper.eventHandle = this;
                this._$httper.url = '/ws/archangel/php_service/handleapp/login.action.class.php';
                this._$httper.resultType = ejs.net.ResultType.text;
                this._$httper.method = ejs.net.HttpMethod.post;
                this._$httper.addEventListener(ejs.net.FaultEvent, this._$onFaultHandler);

                this._$h_signin.owner = this;
                this._$h_signout.owner = this;
                ejs.addEventListener(this._$h_signin, 'click', this._$onSigninHandler);
                ejs.addEventListener(this._$h_signout, 'click', this._$onSignoutHandler);
            },
            validate:function(){
                this._$httper.removeEventListener(ejs.net.ResultEvent.COMPLETE, this._$onSigninCompleteHandler);
                this._$httper.removeEventListener(ejs.net.ResultEvent.COMPLETE, this._$onSignoutCompleteHandler);
                this._$httper.removeEventListener(ejs.net.ResultEvent.COMPLETE, this._$onValidateHandler);
                this._$httper.addEventListener(ejs.net.ResultEvent.COMPLETE, this._$onValidateHandler);
                this._$httper.send(['req=',encodeURIComponent('validate')].join(''));
            }
        })    
    )
);

