﻿function setCookie(name,value)
{
  var Days = 3000; //此 cookie 将被保存 3000 天
  var exp  = new Date();    //new Date("December 31, 9998");
  exp.setTime(exp.getTime() + Days*24*60*60*1000);
  document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString()+";path=/Novel;domain=sky-fire.com";
}
function getCookie(name)
{
  var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
  if(arr != null) return unescape(arr[2]); return null;
}
function delCookie(name)
{
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval=getCookie(name);
  if(cval!=null) document.cookie=name +"="+cval+";expires="+exp.toGMTString()+";path=/Novel;domain=sky-fire.com";
}
function ChangeViewHistory(novelName,url,cover)
{
  //delCookie('NovelViewHistory');
  var novelStr = getCookie('NViewHistory');
  //alert(novelStr);
  var newSet = novelName+'##'+url+'##'+cover;
  if(novelStr!=null) //小说集合不为空
  {	var novelSet = novelStr.split('|');
	//alert(novelSet.length);
	if(!IsExist(novelName,novelStr)){   //如果当前小说还没加入浏览记录
	//alert('ss');
	if(novelSet.length<4)  
	{
		//alert(newSet);
		newSet += "|"+novelStr;
	}
	else
	{
		for(var i = 0;i<3;i++)
		{
			//alert(novelSet[i]);
			newSet += "|"+novelSet[i];
		}
	}
	//alert(newSet);
	setCookie('NViewHistory',newSet);
	}
  }
  else setCookie('NViewHistory',newSet);
}

function ShowViewHistory()
{
  var historyStr = getCookie('NViewHistory');
  if(historyStr!=null)
  {
	var historyInfo = historyStr.split('|');
	//alert(historyInfo.length);
	var html = '';
	if(historyInfo.length<=2) //数组小于或等于3
	{
		html += '<tr>';
		for(var i=0;i<historyInfo.length;i++) //循环数组
		{
			var singleInfo = historyInfo[i].split('##');
			html += '<td><table width="100%" border="0" cellspacing="0" cellpadding="5"><tr align="center"><td><a href="'+singleInfo[1]+'"><img src="'+singleInfo[2]+'" alt="'+singleInfo[0]+'" class="img_space4" border="0" width="80" height="100"/></a></td></tr><tr align="center"><td class="td_height_PX16"><a href="'+singleInfo[1]+'" class="Comic_list">'+singleInfo[0]+'</a></td></tr></table></td>';
		}
		html += '</tr>';
	}
	else
	{
		html += '<tr>';		
		for(var i=0;i<2;i++) //循环数组
		{
			var singleInfo = historyInfo[i].split('##');
			html += '<td><table width="100%" border="0" cellspacing="0" cellpadding="5"><tr align="center"><td><a href="'+singleInfo[1]+'"><img src="'+singleInfo[2]+'" alt="'+singleInfo[0]+'" class="img_space4" border="0" width="80" height="100"/></a></td></tr><tr align="center"><td class="td_height_PX16"><a href="'+singleInfo[1]+'" class="Comic_list">'+singleInfo[0]+'</a></td></tr></table></td>';
		}
		html += '</tr><tr>';
		for(var i=2;i<historyInfo.length;i++) //循环数组
		{
			var singleInfo = historyInfo[i].split('##');
			html += '<td><table width="100%" border="0" cellspacing="0" cellpadding="5"><tr align="center"><td><a href="'+singleInfo[1]+'"><img src="'+singleInfo[2]+'" alt="'+singleInfo[0]+'" class="img_space4" border="0" width="80" height="100"/></a></td></tr><tr align="center"><td class="td_height_PX16"><a href="'+singleInfo[1]+'" class="Comic_list">'+singleInfo[0]+'</a></td></tr></table></td>';
		}
		html += '</tr>';
	}
  }
  else html = '<tr><td align="center"><b>暂无您的浏览记录</b></td></tr>';
  //alert(html);
  document.write(html);
}

//是否小说已存在
function IsExist(novelName,str)
{
  var novelSet = str.split('|');
  //alert(novelSet);
  for(var i=0;i<novelSet.length;i++)
  {
	//alert(novelSet[i].split(';')[0]);
	if(novelName==novelSet[i].split('##')[0])
		return true;
  }
  return false;
}