﻿(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();
                $j('#VipUserDiv').hide();
                $j('#div_login2').hide();
                $(s.loginDiv).hide();

            }
        });
        //window.location.href="/";
    }
    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();
        $j('#div_login2').show();
        VipUserInfo();

    }
})(jQuery)

function getimg(obj)
    {
        var img ="";
            switch (obj)
            {
                case "Junior":
                    img = "<img src='../images/vip1.gif' alt=''>";
                    break;
                case "Medium":
                    img = "<img src='../images/vip2.gif' alt=''>";
                    break;
                case "Senior":
                    img = "<img src='../images/vip3.gif' alt=''>";
                    break;
                case "Gold":
                    img = "<img src='../images/vip4.gif' alt=''>";
                    break;
                case "Platinum":
                    img = "<img src='../images/vip5.gif' alt=''>";
                    break;
                case "Diamond":
                    img = "<img src='../images/vip6.gif' alt=''>";
                    break;
                default:
                    img = "<img src='../images/vip0.gif' alt=''>";
                    break;
            }
            return img;
    }


function VipUserInfo(){

	
var email=$j('#login_txtUserName').val();
$j('#VipUserDiv').show();
    $j.ajax({
        url:'../ajax.aspx?rd='+Math.random(),
        type:'get',
        data:'operate=SelectVipUserInfo&email='+email,
        dataType:'json',
        success:function(data){
            $j('#VipUserEmail').html($j('#login_spUserName').html());
            $j('#VipUserRank').html(data.VipUserRank);
            $j('#VipUserCredits').html(data.VipUserCredits);
            $j('#VipUserPoint').html(data.VipUserPoint);
            $j('#LoginUserImg').html(getimg(data.VipUserRank));
            $j('#NextPointChaLabel').html(data.PointsCha);
            $j('#NextCouponLabel').html(data.VipUserNextCredits);
            $j('#NextPointChaLabel1').html(data.PointsCha);
            $j('#NextCouponLabel1').html(data.VipUserNextCredits);
        }
    });
}

var $j=jQuery.noConflict();
$j(document).ready(function () {
	// set style to input="text"
	$j('#login_txtUserName').filter('.inpit_login').setLoginStyle('LoginEmail');
	$j('#login_txtUserPwd').filter('.inpit_login').setLoginStyle('Password');
	// init login control
	$j('#login_btnLogin').isLogin(setData('get','operate=IsLogin'));
	// userLogin
	$j('#login_btnLogin').click(function () {
		$j(this).login(setData('post','operate=Login&userName='+$j('#login_txtUserName').val()+'&userPwd='+$j('#login_txtUserPwd').val()));
	});
	// login Out
	$j('#login_btnLoginOut').click(function () {
		$j(this).loginOut(setData('get','operate=LoginOut'));
	});
	$j('#login_LiLoginOut').click(function(){
	    $j(this).loginOut(setData('get','operate=LoginOut'));
	});
	$j('#VipUserDiv').hide();
});
function OutLogin(){
	   $j(this).loginOut(setData('get','operate=LoginOut'));
	}
function setData(type,data) {
	return {
		url:'../ajax.aspx?rd='+Math.random(),
		type:type,
		data:data,
		domWait:document.getElementById('login_imgWait'),
		unLoginDiv:document.getElementById('div_unLogin'),
		loginDiv:document.getElementById('div_login'),
		domShowName:document.getElementById('login_spUserName')
	};
}
function ShowDiv(i)
{
if(document .getElementById (i).style.display=='none')
document .getElementById (i).style.display='block';
else 
document .getElementById (i).style.display='none';

}

function displayToolTip(id)
{
    $j('#'+id).show();
}

function hideToolTip(id)
{
    $j('#'+id).hide();
}

