
var history_power = setInterval('checkHistory()', 300);
function checkHistory(){
	if($('.history').hasClass('history_on')){
		setTimeout(function(){readHistory();},0);
	}
}

// 播放历史开关
function switchHistory(){
	if(!$('.history').hasClass('history_on')){
		$('.clean_history').hide();
		$('.history').addClass('history_on');
		$('.ico_history').addClass('ico_history_on');
		$('.head').addClass('head_on');
		$('.nav').fadeOut(0);
		
		setTimeout(function(){
			var tmp_data = readHistory();
			if(tmp_data != '' && tmp_data != null && tmp_data != undefined){
				$('.no_history').hide();
				$('.clean_history').show();
			}else{
				$('.no_history').show();
			}
		},0);
	}else{
		$('.history').removeClass('history_on');
		$('.ico_history').removeClass('ico_history_on');
		$('.head').removeClass('head_on');
		$('.nav').fadeIn(0);
	}
	
	$('.head').click(function(){
		$('.history').removeClass('history_on');
		$('.ico_history').removeClass('ico_history_on');
		$('.head').removeClass('head_on');
		$('.nav').fadeIn(0);
	});
	$('.history_on').click(function(){
		$('.history').removeClass('history_on');
		$('.ico_history').removeClass('ico_history_on');
		$('.head').removeClass('head_on');
		$('.nav').fadeIn(0);
	});
}

$(document).ready(function(){
	$('.clean_history').click(function(){
		$('#history_content').empty();
		$('.clean_history').hide();
		$('.no_history').show();
		if(use_cookie){
			setTimeout(function(){setCookie('view_history', '', 0);},0);
		}else{
			localStorage.removeItem('view_history');
		}
	});
	$('.ico_history').click(function(event){
		event.stopPropagation();
	});
	$('.history_in').click(function(event){
		event.stopPropagation();
	});
});

function readHistory(){
	this.itemChar="@@";
	this.groupChar="|";
	if(use_cookie){
		var tmp_data = getCookie('view_history');
	}else{
		var tmp_data = this.ioReader('view_history');
	}
	var results = "";
	if(tmp_data != "" && tmp_data != null){
		tmp_data = decodeURI(tmp_data);
		var group = this.string2Array(tmp_data,this.groupChar);
		for(var i = 0; i < group.length; i++){
			var movieid = this.string2Array(group[i],this.itemChar)[10];
			var subid = this.string2Array(group[i],this.itemChar)[6];
			var urlid = Math.floor(movieid / 1000);
			var pos = this.string2Array(group[i],this.itemChar)[13];
			var url;
			if(this.string2Array(group[i],this.itemChar)[11] == 'video'){
				url = 'http://m.kankan.com/vod/' + urlid + '/' + movieid + '.shtml';
			}
			else if(this.string2Array(group[i],this.itemChar)[11] == 'music'){
				url = 'http://m.kankan.com/yinyue/' + urlid + '/' + movieid + '.shtml';
			}
			else{
				url = 'http://m.kankan.com/v/' + urlid + '/' + movieid + '/' + subid + '.shtml';
			}
			results += '<li><a href="' + url + '?new=1&pos=' + pos + '"><strong>' 
			    + this.string2Array(group[i],this.itemChar)[2] + '  ' 
				+ this.string2Array(group[i],this.itemChar)[7] + '</strong><em>' 
				+ ((this.string2Array(group[i],this.itemChar)[13]/60000) < 1 ? '观看少于一分钟': '观看至' + parseInt((this.string2Array(group[i],this.itemChar)[13]/60000)) + '分钟')
				+ '</em><span aria-hidden="true" data-icon="p"></span></a></li>';
		}
		$('#history_content')
		.empty()
		.append(results);
		$('.title').show();
		$('.clean_history').show();
		$('.no_history').hide();
	}
	return tmp_data;
}

function ioReader(key){
	return localStorage[key];
}
			
function string2Array(str,splitChar){
	
	return str.split(splitChar);
}		
