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(book_id, segment_id){
	var recent = readCookie('__recentb');
	var time = getTimestamp();

	if(recent != ''){
	
		book_arr = new Array();
		book_arr = recent.split(";;");
		
		j = 0;
		
		new_book_arr = new Array();
		for(i=0; i<book_arr.length; i++){ //;分割
			tmp = new Array();
			if(book_arr[i].indexOf(",,")>0){
				tmp = book_arr[i].split(",,") //tmp 每组里面分割;	
				if(tmp[0] != book_id){ //最近一个不相同
					new_book_arr[j] = tmp; //去除已收藏相同书的
					j++;
				}else{
		
				}	
			}
		}
		var str = '';
		if(new_book_arr.length>=record_num){
			
			for(i=record_num-1; i >0; i--){
				str = str + ';;' +new_book_arr[i][0] +',,'+ new_book_arr[i][1] +',,'+ new_book_arr[i][2];
			}
			
			str = book_id+',,'+segment_id+',,'+time+str;
	
		}else{
			str = book_id+',,'+segment_id+',,'+time;
			for(i=0;i<new_book_arr.length;i++){
				str +=';;'+new_book_arr[i][0]+',,'+new_book_arr[i][1]+',,'+new_book_arr[i][2];
			}
		}
		
		
	}else{
		str = book_id+',,'+segment_id+',,'+time;
	}
	writeCookieBook('__recentb', str, 24*30*6);
}


function readRecord(bookid){
	var recent = readCookie('__recentb');
	var now_time = parseInt(getTimestamp()/1000);
	str = '';
	var past_str='';
	var date_str='';
	var book_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++){
			book_str+=book_arr[j][0]+',';
			past_str+=book_arr[j][1]+',';
			//date_str+=book_arr[j][2]+',';
			date_str+=parseInt(book_arr[j][2]/1000)+',';
			//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;
		var ajax_url='http://anime.xunlei.com/interface/getBookHistory/'+past_str+'/'+date_str+'/'+now_time+'/';
		Ajax(ajax_url,'debug=false&rd='+rand(100000),'get',document.getElementById('recentPlayed'),1);
		var ajax_url2='http://anime.xunlei.com/interface/getBookHistoryTips/'+bookid+'/'+book_str+'/'+past_str+'/'+date_str+'/';
		Ajax(ajax_url2,'debug=false&rd='+rand(100000),'get',document.getElementById('bookHistoryTips'),1);
	}else{
		document.getElementById('bookHistoryTips').innerHTML='<div class="tip"><strong>温馨提示：</strong><span>您暂时没有看过此漫画。</span></div>';
	}
}

function readRecordBox(){
	var recent = readCookie('__recentb');
	var now_time = parseInt(getTimestamp()/1000);
	str = '';
	var past_str='';
	var date_str='';
	var book_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++){
			book_str+=book_arr[j][0]+',';
			past_str+=book_arr[j][1]+',';
//			date_str+=book_arr[j][2]+',';
			date_str+=parseInt(book_arr[j][2]/1000)+',';
			//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;';
		}
		
		var ajax_url='http://anime.xunlei.com/interface/getBookHistory/'+past_str+'/'+date_str+'/'+now_time+'/';
		Ajax(ajax_url,'debug=false&rd='+rand(100000),'get',document.getElementById('recentPlayed'),1);

	}
}

function clearRecord(){
	writeCookieBook('__recentb', '', 0);	
	location.reload();
	location.href = location.href;
	//document.getElementById('recentPlayed').innerHTML='';
}

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;
}
