﻿/*
        
    */
    (function($){
        $.fn.login = function(data){userLogin(data);}
        $.fn.isLogin = function(data){isLogin(data);}
        $.fn.loginOut = function(data){userLoginOut(data);}
        $.fn.setLoginStyle = function(defaultValue){setStyle($(this), defaultValue);}
        
        setStyle = function(txtInput, defaultValue){
            $(txtInput).focus(function(){
                if($(txtInput).val() == defaultValue){
                    $(txtInput).val('');
                }
            });
            
            $(txtInput).blur(function(){
                if($(txtInput).val() == ''){
                    $(txtInput).val(defaultValue);
                }
            });
        }
        
        userLogin = function(s){
            $(s.domWait).show();
            $.ajax({
                url : s.url,
                type : s.type,
                data : s.data,
                dataType : 'json',
                success : function(data){
                    $(s.domWait).hide();
                    initLoginControl(s, data, true);
                }
            });
        }
        
        isLogin = function(s){
            $(s.domWait).show();
            $.ajax({
                url : s.url,
                type : s.type,
                data : s.data,
                dataType : 'json',
                success : function(data){
                    $(s.domWait).hide();
                    initLoginControl(s, data, false);
                }
            });
        }
        
        userLoginOut = function(s){
            $(s.domWait).show();
            $.ajax({
                url : s.url,
                type : s.type,
                data : s.data,
                dataType : 'html',
                success : function(data){
                    $(s.domWait).hide();
                    $(s.unLoginDiv).show();
                    $(s.loginDiv).hide();
                }
            });
        }
        
        initLoginControl = function(s, user, isUserLogin){
            if(user == null){
                if(isUserLogin){
                    alert('Login Failure !');
                }
                return;
            }
            $(s.domShowName).html(user.Fullname);
            $(s.unLoginDiv).hide();
            $(s.loginDiv).show();
        }
    })(jQuery)