
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options);
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        //var path = options.path ? '; path=' + (options.path) : '';
        var path = '; path=/';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

//$(document).ready(function() {
//$('#wCookies').click(function(){
//  $.cookie('name', 'test',{expires: 7});				  
//});
//$('#rCookies').click(function(){
//  var test = $.cookie('name');
//  alert (test);
//});
//$('#dCookies').click(function(){
//	$.cookie('name', null);							  
//});
//});

//var overlays_login;
var overlays_register;
var overlays_qa;
var userloginstatus=false;
var username='';
var password='';
var userpoint=0;
function u_check(){
    username= $.cookie('username');
    password= $.cookie('password');
    userpoint= $.cookie('userpoint');
    if(username!=''&&username!=null)userloginstatus=true;
    u_checkShow();
    }
    
function u_checkShow(){
    if(userloginstatus){
        $("#iUserInfo").html("欢迎您，<b>"+username+"</b>，积分 "+userpoint+" 点！");
        $("#h3_c3").show();
        $("#h3_c2").fadeTo("slow", 0,function(){$("#iUserTitle").html("");$("#iUserName").val("");$("#iPassword").val("");});
        $("#h3_c2").hide();
        }
        else{
        $("#h3_c2").show();
        $("#h3_c2").fadeTo("slow", 1);
        $("#h3_c3").hide();
        }
    }
function u_doLogout(){
    $.cookie('username', '',{expires: 7});
    username='';
    userloginstatus=false;
    $("#iUserTitle").html("欢迎您登录");
    u_checkShow();
    }
function u_doLogin(){
        if($("#iUserName").val()=="" || $("#iUserName").val()=="请输入用户名"){
			$("#iUserTitle").html("<span class=\"ico icoerror\"></span>请输入用户！");return false;}
        else if($("#iPassword").val()=="" || $("#iPassword").val()==null){
			$("#iUserTitle").html("<span class=\"ico icoerror\"></span>请输入密码！");return false;}
        var u,p;
        u=$("#iUserName").val();
        p=$("#iPassword").val();
        var url='/e/logincheck.asp?u='+escape(u)+'&p='+p+'&r='+Math.random();
        //window.open(url);
        $.getJSON(url,function(data)
        {
            if(data.ReturnValue==1){$("#iUserTitle").html(data.Error);}
            else{
            userpoint=data.point;
            $("#iUserTitle").html("登录验证成功!");
            $.cookie('username', $("#iUserName").val(),{expires: 7});
            $.cookie('password', $("#iPassword").val(),{expires: 7});
            $.cookie('userpoint', data.point,{expires: 7});
            userloginstatus=true;
            username= $("#iUserName").val();
            //overlays_login.eq(0).overlay().close();
            u_checkShow();
            }
        });
        
}
$(document).ready(function(){

    $("#h3_c2").hide();
    $("#h3_c3").hide();
	u_check();
	
     $("#iLogin").click(function(){
     u_doLogin();
     });
     $("#iPassword").keyup(function(){
     if(event.which==13)u_doLogin();
     });
//    $("#h3_c2").html("<ul><li><a id=\"aUserRegister\" rel=\"#overlay\" href=\"/s/register.html\">用户注册</a></li><li><a id=\"aUserLogin\" rel=\"#overlay\" href=\"/s/login.html\">登录</a></li><li><a href=\"#\" id=\"a2\">第三方登录</a></li></ul>");
//    $("#h3_c3").html("<ul><li id=\"iUserInfo\"></li><li>欢迎光临，</li><li>个人中心</li><li><a id=\"aUserLoginOut\" href=\"#\">安全退出</a></li></ul>");

	$("#iLogout").click(function(){
        $.cookie('username', '',{expires: 7});
        username='';
        userloginstatus=false;
        $("#iUserTitle").html("欢迎您登录");
        u_checkShow();
        });
    overlays_login = $("#aUserLogin").overlay({
    	mask: {
	    	color: '#ebecff',
		    loadSpeed: 200,
		    opacity: 0.9
	        },
        onBeforeLoad: function() {
			    $("#loginwindowcontent").load("/h/login.html?"+Math.random());
		    },
		    
	    closeOnClick: false
	    });
    overlays_register = $("#aUserRegister").overlay({
    	mask: {
	    	color: '#ebecff',
		    loadSpeed: 200,
		    opacity: 0.9
	        },
        onBeforeLoad: function() {
			    $("#registerwindowcontent").load("/h/register.html?"+Math.random());
		    },
		    
	    closeOnClick: false
	    });
	/*
	$("#aUserLogin").click(function(){
	    $("#cwin").hide();
        $("#cwin_content").load("/s/login.html");
	    $("#cwin").show();
	    center($("#cwin"));
        });
    */
    overlays_qa = $("#aqa").overlay({
    	mask: {
	    	color: '#ebecff',
		    loadSpeed: 200,
		    opacity: 0.9
	        },
        onBeforeLoad: function() {
			    $("#qawindowcontent").load("/h/qa.html?"+Math.random());
		    },
		    
	    closeOnClick: false
    });

    $("#iQQConnect").click(function(){
        if(userloginstatus)system_showmessage("请先注销");
        else{
            var A=window.open("/m/qq/redirect.asp","TencentLogin", "width=450,height=320,menubar=0,scrollbars=1,resizable=1,status=1,titlebar=0,toolbar=0,location=1");
        }
    });
});
