$(document).ready(function(){
    var loginForm = new PDialog('#login-form-wrapper','#login-form');
    //var confirmForm = new PDialog('#confirm-login-form-wrapper','#confirm-login-form');
    var infoForm = new PDialog('#info-form-wrapper','#info-form');
    var lostPassForm = new PDialog('#lost-pass-form-wrapper','#lost-pass-form');
    var changePassForm = new PDialog('#change-pass-form-wrapper','#change-pass-form');
    window.loginForm=loginForm;
    //window.confirmForm=confirmForm;
    window.lostPassForm=lostPassForm;
    window.infoForm=infoForm;
    window.changePassForm=changePassForm;
    loginForm.init();
    infoForm.init();
    //confirmForm.init();
    changePassForm.init();
    //------------------------------------------------------------------
   /* confirmForm.triggerClick('.confirm-login-btn',function(){});

    confirmForm.cancelClick('.confirm-login-button',function(obj){
        var isTimeOut=false;
        if(confirmForm.isLock==true) return false;
        confirmForm.lock();
        var data={
            username:$("#confirm-login-form-wrapper input[name='username']").val(),
            pass:$("#confirm-login-form-wrapper input[name='pass']").val()
            //type:$("#confirm-login-form-wrapper input[name='l_type_id']").val()
        };
        var message=PMessageGlobal.getMessage("MS021");
        confirmForm.MessageDisplay(message.type,message.messageText,true);
        var timer=setTimeout(function(){
            isTimeOut = true;
        }, 2000);
        $.ajax({
            url:'account/confirm_login',
            data:data,
            type:'post',
            dataType:'json',
            success:function(rdata){
                var type=rdata.type.toLowerCase();
                if(isTimeOut==false){
                    window.clearTimeout(timer);
                    var timer=setTimeout(function(){
                        confirmForm.MessageDisplay(type,rdata.message,false);
                        if(typeof(rdata.time)!=="undefined") {
                            confirmForm.RunTimer(rdata.time, rdata.url, function () {
                                confirmForm.unLock();
                                confirmForm.off(true, 0);
                                window.location=rdata.url;
                            });
                        }else confirmForm.unLock();
                        window.clearTimeout(timer);
                        if(rdata.error==false)
                            confirmForm.Clear();
                    }, 500);
                }
                else {
                    window.clearTimeout(timer);
                    confirmForm.MessageDisplay(type,rdata.message,false);
                    if(typeof(rdata.time)!=="undefined") {
                        loginForm.RunTimer(rdata.time, rdata.url, function () {
                            confirmForm.unLock();
                            confirmForm.off(true, 0);
                            window.location=rdata.url;
                        });
                    }
                    else confirmForm.unLock();
                    if(rdata.error==false)
                        confirmForm.Clear();
                }
            }
        });
    },false,false);*/

    loginForm.cancelClick('.login-button',function(obj){
        var isTimeOut=false;
        if(loginForm.isLock==true) return false;
        loginForm.lock();
        var data={
            username:$("#login-form-wrapper input[name='username']").val(),
            pass:$("#login-form-wrapper input[name='pass']").val()
        };
        var message=PMessageGlobal.getMessage("MS021");
        loginForm.MessageDisplay(message.type,message.messageText,true);
        var timer=setTimeout(function(){
            isTimeOut = true;
        }, 2000);
        $.ajax({
            url:'account/login',
            data:data,
            type:'post',
            dataType:'json',
            success:function(rdata){
                console.log(rdata);
                var type=rdata.type.toLowerCase();
                if(isTimeOut===false){
                    window.clearTimeout(timer);
                    var timer=setTimeout(function(){
                        loginForm.MessageDisplay(type,rdata.message,false);
                        if(typeof(rdata.time)!=="undefined") {
                            loginForm.RunTimer(rdata.time, rdata.url, function () {
                                loginForm.unLock();
                                loginForm.off(true, 0);
                                window.location=rdata.url;
                            });
                        }else loginForm.unLock();
                        window.clearTimeout(timer);
                        if(rdata.error===false)
                            loginForm.Clear();
                    }, 500);
                }
                else {
                    window.clearTimeout(timer);
                    loginForm.MessageDisplay(type,rdata.message,false);
                    if(typeof(rdata.time)!=="undefined") {
                        loginForm.RunTimer(rdata.time, rdata.url, function () {
                            loginForm.unLock();
                            loginForm.off(true, 0);
                            window.location=rdata.url;
                        });
                    }
                    else loginForm.unLock();
                    if(rdata.error===false)
                        loginForm.Clear();
                }
            }
        });
    },false,false);

    lostPassForm.cancelClick('.send-button',function(obj){
        var isTimeOut=false;
        lostPassForm.lock();
        var data={
            email:$("#lost-pass-form-wrapper input[name='email']").val()
        };
        var message=PMessageGlobal.getMessage("MS021");
        lostPassForm.MessageDisplay(message.type,message.messageText,true);
        var timer=setTimeout(function(){
            isTimeOut = true;
        }, 2000);
        $.ajax({
            url:'account/lost_pass',
            data:data,
            type:'post',
            dataType:'json',
            success:function(rdata){
                var type= rdata.type.toLowerCase();
                if(isTimeOut==false){
                    window.clearTimeout(timer);
                    setTimeout(function(){
                        lostPassForm.MessageDisplay(type,rdata.message,false);
                        if(rdata.error==false)
                            lostPassForm.Clear();
                        lostPassForm.unLock();
                    }, 500);
                }
                else {
                    window.clearTimeout(timer);
                    lostPassForm.MessageDisplay(type,rdata.message,false);
                    if(rdata.error==false)
                        lostPassForm.Clear();
                    lostPassForm.unLock();
                }
            }
        });
    },false,false);

    //USER INFORMATION FORM
    changePassForm.cancelClick('.change-pass-button',function(){
        changePassForm.lock();
        var isTimeOut=false;
        var data={
            old_pass:$("#change-pass-form-wrapper input[name='old_pass']").val(),
            new_pass:$("#change-pass-form-wrapper input[name='new_pass']").val(),
            confirm_pass:$("#change-pass-form-wrapper input[name='confirm_pass']").val()
        };
        var message=PMessageGlobal.getMessage("MS021");
        changePassForm.MessageDisplay(message.type,message.messageText,true);
        var timer=setTimeout(function(){
            isTimeOut = true;
        }, 10000);
        $.ajax({
            url:'account/change_pass',
            data:data,
            type:'post',
            dataType:'json',
            success:function(rdata){
                var type=rdata.type.toLowerCase();
                if(isTimeOut==false){
                    window.clearTimeout(timer);
                    var timer=setTimeout(function(){
                        changePassForm.MessageDisplay(type,rdata.message,false);
                        if(typeof(rdata.time)!=="undefined") {
                            changePassForm.RunTimer(rdata.time, rdata.url, function () {
                                changePassForm.unLock();
                                changePassForm.off(true, 0);
                            });
                        }
                        else changePassForm.unLock();
                        window.clearTimeout(timer);
                        if(rdata.error==false)
                            changePassForm.Clear();
                    }, 1000);
                }
                else {
                    window.clearTimeout(timer);
                    changePassForm.MessageDisplay(type,rdata.message,false);
                    if(typeof(rdata.time)!=="undefined") {
                        changePassForm.RunTimer(rdata.time, rdata.url, function () {
                            changePassForm.unLock();
                            changePassForm.off(true, 0);
                            changePassForm.Clear();
                        });
                    }
                    else changePassForm.unLock();
                }
            }
        });
    },false,false);

    lostPassForm.triggerClick('.lost-pass-link',function(){
        loginForm.off(false,0);
        changePassForm.off(false,0);
    });

    loginForm.triggerClick('.login-link',function(){
        lostPassForm.off(false,0);
        changePassForm.off(false,0);
    });

    loginForm.cancelClick('.register-link',function(){
        window.location="http://datnenmyphuoc.com.vn/account/register";
    },true);
    lostPassForm.cancelClick('.register-link',function(){
        window.location="http://datnenmyphuoc.com.vn/account/register";
    },true);

});