//构造函数：用指定的名字和可选的性质为指定的文档创建一个cookie对象
//参数：
//	document:	保存cookie的Document对象。必需的。
//	name:		指定cookie名的字符串。必需的。
//	hourse:		一个可选的数字。指定从现在起到cookie过期的小时数。
//	path:		一个可选的字符串。指定了cookie的路径性质。
//	domain:		一个可选的字符串。指定了cookie的域性质。
//	secure：		一个可选的布尔值。如果为true。需要一个安全的cookie.
//
function Cookie(document, name, hours, path, domain, secure) {
	//该对象所有预定义的属性都以'$'开头,
	//这是为了与存储在cookie中的属性值区别开。
	this.$document = document;
	this.$name = name;
	if(hours){
		this.$expiration = new Date((new Date()).getTime() + hours * 3600000);
	} else {
		this.$expiration =  null;
	}
	if( path ) {
		this.$path = path;
	} else { 
		this.$path = null;
	}
	if(secure)
		this.$secure = true;
	else
		this.$secure = false;
	if(domain)
		this.$domain = domain;
	else
		this.$domain = null;
}
// 该函数是cookie对象的store()方法。
Cookie.prototype.store = function(){
	//首先遍历cookie对象的属性，并且将cookie值连起来。
	//由于cookie将等号和分号作为分隔符，
	//所以我们使用权用冒号和&来分隔存储在单个cookie值中的状态变量。
	//注意，我们对每个状态变量的值进行了转义，以防它含有标点符号或其他非法字符。
	var cookieval = "";
	for(var prop in this){
		//忽略所有名字以$开头的属性和所有方法。
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
			continue;
		if (cookieval != "")
			cookieval += '&';
		cookieval += prop + ':' + escape(this[prop]);
	}
	//既然我们已经有了cookie值，就可以连接完整的cookie串，
	//其中包括名字和创建cookie对象时指定的各种性质。
	var cookie = this.$name + '=' +cookieval;
	if (this.$expiration)
		cookie += '; expires=' + this.$expiration.toGMTString();
	if(this.$path)
		cookie += '; path=' + this.$path;
	if(this.$domain)
		cookie += '; domain' + this.$domain;
	if(this.$secure)
		cookie += '; secure';
	//下面设置Document.cookie属性来保存cookie.
	this.$document.cookie = cookie;
}

//该函数是cookie对象的load()方法
Cookie.prototype.load = function(){
	//首先得到属于该文档的所有cookie的列表
	//通过读Document.cookie属性可以实现这一点。
	var allcookies = this.$document.cookie;
	if (allcookies == "") return false;
	//下面从该列表中提取已命名的cookie.
	var start = allcookies.indexOf(this.$name + '=');
	if(start == -1)return false;	//该页未定义cookie
	start += this.$name.length + 1; //跳过名字或符号。
	var end = allcookies.indexOf(';',start);
	if(end == -1) end = allcookies.length;
	var cookieval = allcookies.substring(start,end);
	
	//既然我们已经提取了已命名的cookie的值，
	//就可以把它分割存储到状态变量名或值中。
	//名字/值对由&分隔，名字和值之间则由冒号分隔。
	//我们使用split()方法解析所有数据。
	var a = cookieval.split('&');		//分割成名字/值对。
	for(var i=0;i<a.length;i++)			//把每对值存入数组。
		a[i] = a[i].split(':');
		
	// Now that we've parsed the cookie value, set all the names and values
	// of the state variables in this Cookie object. Note that we unescape()
	// the property value, because we called secape() when we storted it.
	for(var i=0;i<a.length;i++){
		this[a[i][0]] = unescape(a[i][1]);
	}
	//We're done, so return the success code
	return true;
}

//This function is the remove() method of the Cookie object
Cookie.prototype.remove = function(){
	var cookie;
	cookie = this.$name + '=';
	if(this.$path) cookie += '; path=' + this.$path;
	if(this.$domain) cookie += '; domain' + this.$domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
	this.$document.cookie = cookie;
	
}

//学校模型
//	id:		学校编号
//	url:	学校访问的链接
//	name:	学校名称
function School(id,url,name){
	this.$name = name;
	this.$id = id;
	this.$url = url;
	if(this.$url.substring(0,1)!= "/" )
		this.$url="/"+this.$url
}
School.prototype.toString = function(){
	return this.$id+'|'+this.$url+'|'+this.$name;
}
//last view
function SysViewHistory(){
	this.cookie = new Cookie(document,"sys_cernet_SchoolHistory",24,"/",".eol.cn");
	this.hasCookie = this.cookie.load();
	this.schools = this.read();
}
//	添加历史记录
SysViewHistory.prototype.add = function(school){
	if(!this.findById(school.$id)){
		var str = this.toString();
		if(str != "")
			str = school.toString() + ';' + str;
		else
			str = school.toString();
		
		this.schools = this.parse(str);
		this.store();
	}
	this.render();
}
//	存储数据
SysViewHistory.prototype.store = function(){
	this.cookie.history = this.toString();
	this.cookie.store();
}
// 转化成String： id|url|name;id|url|name
SysViewHistory.prototype.toString = function(){
	if(this.schools != null && this.schools != ""){
		var str = "";
		var end  = this.schools.length>9?9:this.schools.length;
		for(var i=0;i<end;i++){
			if(str!="")
				str += ";";
			str += this.schools[i][0] + '|' + this.schools[i][1] + '|' + this.schools[i][2];
		}
		return str;
	}
	return "";
}
//	解析字符串，变成数组
SysViewHistory.prototype.parse = function(str){
	var s = str;
	if(s && s != null && s != ""){
		s = s.split(";");
		for(var i=0;i<s.length;i++){
			s[i] = s[i].split("|");
		}
		return s;
	}
	return null;
}
//	读出cookie中的history信息
SysViewHistory.prototype.read = function(){
	if(this.hasCookie){
		return this.parse(this.cookie.history);
	}
	return null;
}

SysViewHistory.prototype.get = function(){
	if(this.hasCookie)
		return this.cookie.history;
	else
		return null;
}
//根据ID进行查找
SysViewHistory.prototype.findById = function(id){
	if(this.schools != null){
		for(var i=0; i<this.schools.length;i++)
			if(this.schools[i][0] == id)
				return true;
		return false;
	}
	return false;
}
SysViewHistory.prototype.isTop = function(id){
	if(this.schools != null){
		if(this.schools[0][0] == id)
			return true;
	}
	return false;
}
// 在页面中呈现
SysViewHistory.prototype.render = function(){
		var str = '<ul >';
		if(this.schools && this.schools != null && this.schools!=""){
			for(var i = 0;i<this.schools.length;i++){
				str += 	'\t\t\n<li><a target="_blank" href="'+this.schools[i][1]+'">'+this.schools[i][2]+'</a></li>';
			}
		}
		str += '\t\n</ul>';
		document.write(str);	
}


//---执行测试

var sysv = new SysViewHistory();
//v.add(new School('13','index.html','大京大学'));
