var _HCP = window._HCP || {};
_HCP.Cookie = { 
    get: function(key) {
       tmp =  document.cookie.match((new RegExp(key +'=[a-zA-Z0-9.()=|%/]+($|;)','g')));
       if(!tmp || !tmp[0]) return null;
       else return unescape(tmp[0].substring(key.length+1,tmp[0].length).replace(';','')) || null;
    },  
    set: function(key, value, ttl, path, domain, secure) {
       cookie = [key+'='+    escape(value),
                'path='+    ((!path   || path=='')  ? '/' : path),
                'domain='+  ((!domain || domain=='')?  window.location.hostname : domain)];
       if (ttl)         cookie.push('expires='+_HCP.Cookie.hoursToExpireDate(ttl));
       if (secure)      cookie.push('secure');
       return document.cookie = cookie.join('; ');
    },  
        
    unset: function(key, path, domain) {
       path   = (!path   || typeof path   != 'string') ? '' : path;
        domain = (!domain || typeof domain != 'string') ? '' : domain;
       if (_HCP.Cookie.get(key)) _HCP.Cookie.set(key, '', 'Thu, 01-Jan-70 00:00:01 GMT', path, domain);
    },  
    hoursToExpireDate: function(ttl) {
       if (parseInt(ttl) == 'NaN' ) return ''; 
       else {
           now = new Date();
           now.setTime(now.getTime() + (parseInt(ttl) * 60 * 60 * 1000));
           return now.toGMTString();              
       }   
    },  
	test: function() {
       _HCP.Cookie.set('b49f729efde9b2578ea9f00563d06e57', 'true');
       if (_HCP.Cookie.get('b49f729efde9b2578ea9f00563d06e57') == 'true') {
           _HCP.Cookie.unset('b49f729efde9b2578ea9f00563d06e57');
           return true;
       }
       return false;
    },
    dump: function() {
       if (typeof console != 'undefined') {
           console.log(document.cookie.split(';'));
       }
    }
};
_HCP.inout = {
    showLoginHtml: function(){
        login_uid = _HCP.Cookie.get("uchome_uid");
        login_username = _HCP.Cookie.get("uchome_username");
        if(login_username == null || login_username == "null" || !login_username){
            login_username = "";
        }
        if(login_uid == "" || login_uid == null)
        {
            login_HTML = '<a href="#" onmouseover="this.href=\'http://passport.kuxun.cn/login\'" target="_self">登录</a><span class="jiange">|</span><a href="#" onmouseover="this.href=\'http://passport.kuxun.cn/register\'" target="_self">注册</a><span class="jiange">|</span><a href="#" onmouseover="this.href=\'http://home.kuxun.cn/helpcenter/\'" target="_blank">帮助</a>';
        }
        else
        {
            login_HTML = '<span id="huanying"><a href="#" onmouseover="this.href=\'http://passport.kuxun.cn\'" target="_self">你好&nbsp;&nbsp;'+decodeURI(login_username)+'</a><span class="jiange">|</span><a href="#nogo" target="_self" onclick="_HCP.inout.logout();">退出</a>'+'<span class="jiange">|</span><a href="#" onmouseover="this.href=\'http://home.kuxun.cn/helpcenter/\'" target="_blank">帮助</a></span>';

        }
	    $(".login .line20").html(login_HTML);
    },
    logout: function(){
        var uid = _HCP.Cookie.get("uchome_uid");
        var logout_HTML = '<a href="#" onmouseover="this.href=\'http://passport.kuxun.cn/login\'" target="_self">登录</a><span class="jiange">&nbsp;|&nbsp;</span><a href="#" onmouseover="this.href=\'http://passport.kuxun.cn/register\'" target="_self">注册</a><span class="jiange">|</span><a href="#" onmouseover="this.href=\'http://home.kuxun.cn/helpcenter/\'" target="_blank">帮助</a>';
        _HCP.Cookie.unset("uchome_uid","/",".kuxun.cn");
        _HCP.Cookie.unset("uchome_username","/",".kuxun.cn");
        _HCP.Cookie.unset("uchome_email","/",".kuxun.cn");
        //_HCP.Cookie.unset("user_flag","/",".kuxun.cn");
		window.location.href="http://passport.kuxun.cn/admin?act=logout";
    }
};
$(document).ready(function(){
	_HCP.inout.showLoginHtml();
});
