$(function(){
  $.getScript("https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js");
    var host = window.location.host;
    var domain = 'https://www.youxiake.com';
    if(host.indexOf('.test')>-1){
      domain = domain.replace('https','http').replace('com','test');
    }
    if(host.indexOf('.local') >-1){
        domain = domain.replace('https','http').replace('com','local');
    }
    $.getScript(domain +"/login/sso?callback=sso");
});
function sso(data){
    var str = '.'+window.location.host;

    // alert(data);
    // console.log(data);
    var domain = window.location.host.substring(window.location.host.indexOf('.'))
    if (parseInt(data.uid) > 0) {
        var auth = getCookie('yxk_auth');

        $(".userid").show();
        $(".fb").show();
        $(".login").hide();
        $("#userid").text(data.username);
        $("#myzone").attr('href', '/u/' + data.uid);
        $("#yxzone").attr('href', 'http://www.youxiake.com/u/' + data.uid);
        $("#userid").attr('href', '/u/' + data.uid);

        if (auth === undefined || auth != data.token) {
            document.cookie = "yxk_auth=" + data.token + ";path=/;domain=" + domain;
        }
    } else {
        document.cookie = "yxk_auth=" + null + ";path=/;domain=" + domain + ";expires=Thu, 01 Jan 1970 00:00:00 UTC;";
    }

}

function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
