var CookieUtil = {
	"creatCookie": function(content,expiresDate){
		if(expiresDate != null){
			document.cookie = content+'expires=' + expiresDate.toGMTString()+'; path=/; domain=quotes.fund.163.com';
		}else{
			//永不过期
			var nnn = new Date();
			nnn.setYear(2088);
			document.cookie = content+'expires=' + nnn.toGMTString()+'; path=/; domain=quotes.fund.163.com';
		}
	},
	"getValue": function(key){
		var cookieStr = document.cookie;
	    if (cookieStr == ""){
	        return null; //没有取到 cookie 字符串，返回默认值
	    }
	    var cookieSet = cookieStr.split("; ");
	    var startPos = -1;
	    var endPos = -1;
	    for (var i=0; i<cookieSet.length; i++){
	        startPos = cookieSet[i].indexOf(key);
	        if (startPos != 0){
	            continue; //当前 cookie 不是名称为 varName 的 cookie，判断下一个 cookie
	        }
	        startPos += key.length + 1; //当前 cookie 就是名称为key的 cookie，由于有等号，所以 +1
	        endPos = cookieSet[i].length;
	        return unescape(cookieSet[i].substring(startPos, endPos));
	    }
	    return null;
	},
	"logout": function(){
		var nnn = new Date();
		nnn.setYear(1978);
		document.cookie ='Fund_163_Inf=; MyAttetionalFundList=; expires='+ nnn.toGMTString() +'; path=/; domain=quotes.fund.163.com';
		document.location.href = 'http://money.163.com/fund/';
		//window.open('http://money.163.com/fund/','_self');
		//location.reload();
	},
	"setCookie": function(name,value) {
		var exp = new Date();
		exp.setYear(2088);
		document.cookie = name + "="+ escape (value) + "; expires=" + exp.toGMTString() + "; path=/; domain=163.com";		
	},
	"getCookie": function(name) {
		 var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    	 if(arr != null)
  			return unescape(arr[2]); 
    	return null;
	},
	"delCookie": function(name) {
		var exp = new Date();  //当前时间
    	exp.setTime(exp.getTime() - 1);
    	var cval=CookieUtil.getCookie(name);
    	if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString() + "; path=/; domain=163.com";
	}
	
}

var UserCookie = {
	"init": function(){
		UserID = '';
		dataSet = new Array(2);
		//0:登陆信息,'Fund_163_Inf'
		//1:自选股列表,'MyAttetionalFundList'
		//初始化
		dataSet[0]=UserCookie.getVisitedInf(2)+'|'+(new Date().toLocaleString());
		dataSet[1]=CookieUtil.getValue('MyAttetionalFundList');
		
		UserCookie.initPanel();
	},
	"initI": function(){
		UserID = '';
		dataSet = new Array(2);
		//0:登陆信息,'Fund_163_Inf'
		//1:自选股列表,'MyAttetionalFundList'
		//初始化
		dataSet[0]=UserCookie.getVisitedInf(2)+'|'+(new Date().toLocaleString());
		dataSet[1]=CookieUtil.getValue('MyAttetionalFundList');
	},
	"getVisitedInf": function(MsgType){
		var re = CookieUtil.getValue('Fund_163_Inf');
		if(re == null || re == '' || re == 'undefined'){//需要重新登陆
			return '';
		}else{
			var arr = re.split('|');
			UserID = arr[0];
			switch(MsgType){
				case 0:
					var msg = '欢迎:'+arr[0];
					msg+= '\n上次登陆时间:';
					msg+= arr[1];
					msg+= '\n';
					return msg;
				case 1:
					return arr;
				case 2:
					return UserID;
			}
		}
	},
	"processDataSet": function(){
		var cv = '';
		for(var i=0;i<dataSet.length;i++){
			switch(i){
				case 0:
					cv+= 'Fund_163_Inf=';
					cv+= escape(dataSet[0]);
					cv+= '; ';
					break;
				case 1:
					cv+= 'MyAttetionalFundList=';
					cv+= escape(dataSet[1]);
					cv+= '; ';
					break;
				default:
			}
		}
		CookieUtil.creatCookie(cv,null);
	},
	"setVisitedInf": function(userID){
		UserID = userID;
		dataSet[0] = userID+'|'+(new Date().toLocaleString());
		UserCookie.processDataSet();
		CookieUtil.creatCookie('',null);
	},
	"setMyAttetionalFund": function(fundList){
		if(UserID==null || UserID== ''){
			alert('你尚未登陆,请登陆.');
			return;
		}
		dataSet[1] = fundList;
		UserCookie.processDataSet();
	},
	"getMyAttetionalFund": function(){
		return CookieUtil.getValue('MyAttetionalFundList');
	},
	"setMyAttentionalFundCodeList": function(fundCodeList) {
		if(fundCodeList!=null && fundCodeList !='') {
			CookieUtil.setCookie("MyAttentionalFundCodeList",fundCodeList);
		}
	},
	"getMyAttentionalFundCodeList": function() {
		//竖线分隔的基金代码列表
		return CookieUtil.getCookie("MyAttentionalFundCodeList");
	},
	"initPanel" : function(){
		var msg = UserCookie.getVisitedInf(0);
		if(msg == null || msg == ''){
			$('userContrlPanel').innerHTML = '<a href="login.html">登陆我的基金</a> | <a href="http://reg.163.com/reg0.shtml">注册通行证</a>';
		}else{
			$('userContrlPanel').innerHTML = '欢迎: '+UserID +'&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" target="_self" onclick=CookieUtil.logout()>退出</a>';
		}
	}
}