﻿jQuery.fn.extend({
    pressEnter: function(fun){
     this.keypress(function(e){
      if(e.which=="13"){
             fun();
        }
      });
   }
});


$(document).ready(function() {

    $(function() {
        $("#PassWord").pressEnter(function() {
            $("#LoginButton").click();
        });
    })

    $("#LoginButton").click(function() {
        if ($("#UserName").val() == '' || $("#PassWord").val() == '') {
            alert("用户名或密码不能为空，请确认。");
        }
        else {

            $("#divUserLoginLoading").show();
            $.post("/Ajax/Ajax.aspx?action=Login&d=" + Math.random(), { Action: "post", UserName: $("#UserName").val(), PassWord: $("#PassWord").val() },
		        function(data, textStatus) {
		            if (data.uid == "-1" || data.uid == "-2") {
		                alert("用户名或密码不对，请重试。");
		                $.CUCUserLogin();
		            }
		            else {
		                //		                $.cookie('SNSUserID',  data.uid, { expires: 7, path: '/', domain: '56star.cn',secure:false});
		                //		                $.cookie('SNSUserName',  data.username, { expires: 7, path: '/', domain: '56star.cn',secure:false });
		                //		                $("#head").html(data.loginHome);
		                //		                $.CUCUserLogin();

		                location.href = "/ajax/UserCenter.aspx?action=Login";
		            }
		        }, "json");
        }
    });

    $("#AOut").click(function() {
        $.cookie('SNSUserID', null, { expires: 1, path: '/', domain: '56star.cn', secure: false });
        $.cookie('SNSUserName', null, { expires: 1, path: '/', domain: '56star.cn', secure: false });
        $.CUCUserLogin();
    }
    );

    $("#ACencet").click(function() {
        $("#divUserLoginLoading").hide();
    });
})

jQuery.CUCUserLogin = function() {
    if ($.cookie('SNSUserID') != null && $.cookie('SNSUserID') != '') {
        $("#divUserLoginLoading").hide();
        $("#divUserLoginInfo").html($("#divUserLoginInfo").html().replace("script:username", $.cookie('SNSUserName')));
        $("#divUserLoginInfo").show();

        $("#AOut").click(function() {
        	$.cookie('SNSUserID',  null, { expires: 1, path: '/', domain: '56star.cn',secure:false});
        	$.cookie('SNSUserName',  null, { expires: 1, path: '/', domain: '56star.cn',secure:false });
            $.CUCUserLogin();
        }
       );
    }
    else {
        $("#divUserLoginInfo").hide();
        $("#divUserLoginLoading").hide();
		
    }
}