
function  weekOfYear(year,month,day){          
	var date1 = new Date(year, 0, 1);     
	var date2 = new Date(year, month-1, day, 1);     
	var dayMS = 24*60*60*1000;     
	var firstDay = (7-date1.getDay())*dayMS;     
	var weekMS = 7*dayMS;     
	date1 = date1.getTime();     
	date2 = date2.getTime();     
	return  Math.ceil((date2-date1-firstDay)/weekMS)+1;     
} 

function getTimestamp(){ var timestamp = Date.parse(new Date()); return timestamp; }


function timestampDiff(timestamp){
	
	
	if(timestamp == 'undefined' || timestamp == null || timestamp=='')return ' ';
	timestamp = parseInt(timestamp);
	if(timestamp>getTimestamp()) return ' ';
	var now = new Date();
	var nowYear = now.getYear();
	var nowMonth = now.getMonth();
	var nowDate = now.getDate();
	var nowWeek = weekOfYear(nowYear, nowMonth, nowDate);
	var nowHour = now.getHours();
	var nowMinute = now.getMinutes();
	var nowSecond = now.getSeconds();

	var target = new Date(timestamp);
	var targetYear = target.getYear();
	var targetMonth = target.getMonth();
	var targetDate = target.getDate();
	var targetWeek = weekOfYear(targetYear, targetMonth, targetDate);
	var targetHour = target.getHours();
	var targetMinute = target.getMinutes();
	var targetSecond = target.getSeconds();
	
	if(nowYear != targetYear){
		
		return targetYear+'/'+targetMonth+'/'+targetDate;	
		
	}else if(nowMonth != targetMonth){
		if(nowMonth - targetMonth == 1)	{
			return '上个月';	
		}else{
			return (nowMonth-targetMonth)+'个月前';	
		}
	}else if(nowWeek != targetWeek){
		if(nowWeek - targetWeek == 1){
			return '上星期';	
		}else{
			return (nowWeek - targetWeek)+'个星期前';		
		}
	}else if(nowDate != targetDate){
		if(nowDate - targetDate ==1){
			return '昨天';	
		}else if(nowDate - targetDate ==2){
			return '前天';			  
		}else{
			return (nowDate - targetDate)+'天前';	
		}
	}else if(nowHour != targetHour){
	
		return (nowHour-targetHour)+'小时前';
	
	}else if(nowMinute != targetMinute){
		
		return (nowMinute - targetMinute)+'分钟前';
		
	}else if(nowSecond != targetSecond){
		
		return (nowSecond - targetSecond)+'秒前';
	}else{
		return '正在浏览';	
	}
}


var record_num = 15;

function record(anime_id, chapter_id, servertime) {

	var recent = readCookie('__recent');
	var time = (getTimestamp()/1000) - 0*24*3600;
	time = servertime ? servertime : time;
	if(recent != '') {	
		chapter_arr = new Array();
		chapter_arr = recent.split(";");		
		new_chapter_arr = new Array();
		
		for(i=0; i<chapter_arr.length; i++){
			tmp = new Array();
			if(chapter_arr[i].indexOf(",")>0){
				tmp = chapter_arr[i].split(",");	
				if(tmp[0] != anime_id){
					new_chapter_arr.push(chapter_arr[i]);
				}	
			}
		}
		new_chapter_arr.push(anime_id+','+chapter_id+','+time);
		var str = '';
		
		if(new_chapter_arr.length>record_num){
			new_chapter_arr.shift();	
		}
		str = new_chapter_arr.join(';');
	} else {
		str = anime_id+','+chapter_id+','+time;
	}

	//writeCookie('__recent', str, 24*30*6);	
	writeCookieWithDomain('__recent', str, 24*30*6, 'anime.xunlei.com');	
}


function readRecord(){
	var recent = readCookie('__recent');

	str = '';
	
	if(recent != ''){
		
		book_arr = new Array();
		book_arr = recent.split(";;");

		for(i=0; i<book_arr.length; i++){
			book_arr[i] = book_arr[i].split(",,");
		}
		
		for(j=0; j<book_arr.length; j++){
			str += ' <a href="http://www.agm-mh.cn/Book/category/'+book_arr[j][0]+'" title='+book_arr[j][1]+'>'+book_arr[j][1]+book_arr[j][3]+'</a> - '+timestampDiff(book_arr[j][4])+' &nbsp;&nbsp;';
		}
		
		str += '<a href="javascript:void(0)" onclick="clearRecord()"><u><font class=green>清空所有记录</font></u></a>';
		
		document.getElementById('recent_div').style.display = 'block';
		document.getElementById('recent').innerHTML = str;
	}
}


function readCategoryRecord(book_id){
	
	var recent = readCookie('__recent');

	str = '';

	if(recent != ''){
		
		book_arr = new Array();
		new_book_arr = new Array();
		book_arr = recent.split(";;");

		for(i=0; i<book_arr.length; i++){
			new_book_arr[i] = book_arr[i].split(",,");
		}
		
		for(j=0; j<new_book_arr.length; j++){
			if(new_book_arr[j][0] == book_id){
				
				try{

					if(segment_next[new_book_arr[j][2]] != 'undefined' && segment_next[new_book_arr[j][2]]){

						str = '您在<b>'+timestampDiff(new_book_arr[j][4])+'</b>曾访问过 <a href="http://images.agm-mh.cn/segments/'+makeHtml(new_book_arr[j][2])+'" target="_blank">'+new_book_arr[j][3]+'</a> , 点击 <a href="http://images.agm-mh.cn/segments/'+makeHtml(segment_next[new_book_arr[j][2]])+'" target="_blank">'+segment_next_title[new_book_arr[j][2]]+'</a> 可直接访问下一章节';								
					}else{			
						str = '您在<b>'+timestampDiff(new_book_arr[j][4])+'</b>曾访问过 <a href="http://images.agm-mh.cn/segments/'+makeHtml(new_book_arr[j][2])+'" target="_blank">'+new_book_arr[j][3]+'</a> , 暂时未有下一章节, 请浏览一下其他漫画吧^_^！';								
					}
					
				}catch(e){
					
				}
				document.getElementById('recent_commbox').style.display = 'block';
				document.getElementById('recent_commbox_text').innerHTML = str;
			}
		}
	}
}


function clearRecord(){
	writeCookie('__recent', '', 0);
	$('vodHistory').innerHTML = '<div style="height:8px">&nbsp;</div><ul><li>暂无观看历史</li></ul>';
}

function clearRecordBox(){
	writeCookieBook('__recent', '', 0);
	location.reload();
	location.href = location.href;
}

function writeCookieBook(name, value, hours) {
	domain='anime.xunlei.com';
	var expire = "";
	if(hours != null) {
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString() + "; path=/";
	}
	document.cookie = name + "=" + escape(value)+';domain='+domain +';'+ expire;
}