//影视列表页收藏
var movieid=0;
var movie_type = 'movie';
var movie_title = '';
movie_col = new movie_cols();
function __init__(){}
//type表示的是影视还是明星
function movie_cols(type,id){
	this.type = type;
	if(type=='movie'){
		this.title = '电影';
	}
	else if(type=='teleplay'){
		this.title = '剧集';
	}
	else if(type == 'tv'){
		this.title = '综艺';
	}
	
	this.statuss = ['我想看这部'+this.title,'我在看这部'+this.title,'我看过这部'+this.title,'我对这部'+this.title+'没兴趣'];
	this.status_en = ['want','ing','ed','dl'];//分别为想看 在看 看过 不喜欢
	this.status ='';
	this.oldstatus='';//老的状态
	this.newstatus='';//新的状态
	this.id = id;
	this.url = 'http://movie.xunlei.com/favorite_v2.php';
	this.username = getCookie('usrname');
	this.divid = 'movie_col_box_id';
	this.soupingdiv = 'movie_suoping';
	this.grade_img0_src = 'http://images.movie.xunlei.com/movie_v2/images/star02.png';
	this.grade_img1_src = 'http://images.movie.xunlei.com/movie_v2/images/star01.png';
	this.grade_img2_src = 'http://images.movie.xunlei.com/movie_v2/images/star03.png';
	
	this.old_grade_selected_value = 0;//老的分数 初始化的
	this.jump_grade = 0;
	this.new_grade = 0;//新的分数
	this.is_on_grade_selected = false;
	this.msg = '';
	this.is_open = 1;
	this.edit = false;//是否为修改状态
	this.tg_flag = true;//高级选项的开关
	
	this.reg = /^[\u4E00-\u9FA5|0-9|A-Z|a-z|·|\-]+$/;

	this.FAVORITES_MAX_NUM = 10;  //最多可以输入10个标签
	this.FAVORITE_LEN_MAX   = 20;   //标签名长度的最大值
	this.FAVORITE_LEN_MIN   = 3;   //标签名长度的最大值
	this.MSG_MAX_LEN = 70;   //简评长度限制（字符）
	this.callback = false;
	this.flag = true;//防止重复点击
	this.point_one = false;
	this.cookie_param = {
				expires: 2,//期限（小时）
				path: '/', //路径
				domain: 'xunlei.com', //域名
				 secure: false//我也不知道
			};
	this.gaoji = true;//是否默认为高级选项
}
movie_cols.prototype.init = function(){
	type = this.type;
	if(type=='movie'){
		this.title = '电影';
	}
	else if(type=='teleplay'){
		this.title = '剧集';
	}
	else if(type == 'tv'){
		this.title = '综艺';
	}
	this.statuss = ['我想看这部'+this.title,'我在看这部'+this.title,'我看过这部'+this.title,'我对这部'+this.title+'没兴趣'];
}
movie_cols.prototype._init_cookie_name = function(){
	this.username = getCookie('usrname');
}
//初始化评分
movie_cols.prototype.grade_init = function(){
//	if(this.grade_change_click) return false;
	grade_img1_src = this.grade_img1_src;
	grade_img0_src = this.grade_img0_src;
	id = fav_init_point;
	for(var i=1; i<=10; i++){
		if(i<=id){
			$obj('game_favorite_grade_show_'+i).src = grade_img1_src;
		}
		else{
			$obj('game_favorite_grade_show_'+i).src = grade_img0_src;
		}
	}
	$obj('grade').value = id;
}
//前台收藏框 参数flag为修改或者增加的标记 1为增加 0为删除
movie_cols.prototype.movie_col_box = function(){
	var status=0;
	var point =0;
	if(arguments.length>=1){
		status = arguments[0];
	}
	else if(fav_movie_people_data && typeof(fav_movie_people_data.type)!="undefined"){
		status = fav_movie_people_data.type;
	}
	if(arguments.length>=2){
		point = arguments[1];
	}
	
	fav_movie_col_box(status,point);
	return true;
}


//初始化标签
movie_cols.prototype.init_favorite = function(){
	var time = new Date();
	var param = "action=getfavorite&username="+this.username+'&key='+this.id+'&type='+this.type+'&adfsdf='+time+Math.random();;
	var ajax = new xlAjax(this.url,param,this.init_favorite_back);
	ajax.get();
}
//初始化标签返回
movie_cols.prototype.init_favorite_back = function(request){
	var r = eval("(" + request.responseText + ")");
	if(r.tid==1){
		$obj('movie_favorites').value = r.data;
	}
}

//初始化我常用的标签和迅雷用户常用的标签
movie_cols.prototype.init_allfavorite = function(){
	var time = new Date();
	var param = "action=getallfavorite&username="+this.username+'&key='+this.id+'&type='+this.type+'&adfsdf='+time+Math.random();;
	var ajax = new xlAjax(this.url,param,this.init_allfavorite_back);
	ajax.get();
}
//初始化我常用的标签和迅雷用户常用的标签 返回
movie_cols.prototype.init_allfavorite_back = function(request){
	var r = eval("(" + request.responseText + ")");
	if(r.tid == 1){
		var my = r.data.my;
		if(my){
			var obj = $obj('my_often_favorites');
			obj.innerHTML = '';
			if(obj){
				for(var i=0 ;i<my.length;i++){
					var text = "<a href='javascript:void(0)' style='background-position:0 -569px;' onclick='movie_col.input_favorite(\""+my[i]+"\");'><span style='background-position:right -569px;'>"+my[i]+"</span></a>";
					obj.innerHTML = obj.innerHTML+text;
				}
			}
		}
		var movie = r.data.movie;
		if(movie){
			var obj = $obj('often_favorites');
			obj.innerHTML = '';
			if(obj){
				for(var i=0;i<movie.length;i++){
	//				var text = movie[i];
					var text = "<a href='javascript:void(0)' style='background-position:0 -569px;' onclick='movie_col.input_favorite(\""+movie[i]+"\");'><span style='background-position:right -569px;'>"+movie[i]+"</span></a>";
					obj.innerHTML = obj.innerHTML + text;
				}
			}
		}
	}
}

//输入标签
movie_cols.prototype.input_favorite = function(favorite){
	var f = $obj('movie_favorites').value + ' ' + favorite;
	if(this.favorite_check(f)){
		$obj('movie_favorites').value = f.trim();
	}
}

//检查标签是否输入有误
movie_cols.prototype.favorite_check = function(){

	if(arguments.length==1){
		 var fs = arguments[0];
	}
	else{
		fs = $obj('movie_favorites').value;
	}
	fs = fs.trim();
	
	var fa = fs.split(' ');  //原数组
	var len = fa.length;
	var max_str_len = 0;
	var min_str_len = 20;
	var max_str_val = '';
	var min_str_val = '';

	//非法标签
	var illege_favorite = '';
	var is_illege = false;

	for(var i=len-1; i>=0; i--){
		fa[i] = fa[i].trim();
		if(fa[i]==''){
			fa.splice(i, 1);
		}else{
			if(fa[i].strlen()>max_str_len){
				max_str_len = fa[i].strlen();
				max_str_val = fa[i];
			}
			if(fa[i].strlen()<min_str_len){
				min_str_len = fa[i].strlen();
				min_str_val = fa[i];
			}
			if(!this.reg.test(fa[i])){
				illege_favorite = fa[i];
				is_illege = true;
			}
		}
	}
	len = fa.length;

	if(len>this.FAVORITES_MAX_NUM){
		this.favorite_msg('最多可以输入'+this.FAVORITES_MAX_NUM+'个');
		return false;
	}

	if(fa.hasRepeatElementLowerCase()){
		this.favorite_msg('输入的标签有重复(不区分大小写)');
		return false;
	}

	if(max_str_len>this.FAVORITE_LEN_MAX){
		this.favorite_msg('标签长度不能超过'+this.FAVORITE_LEN_MAX+'个字符');
		return false;
	}
	
	if(min_str_len<this.FAVORITE_LEN_MIN && min_str_len>0){
		this.favorite_msg('标签长度不能少于'+this.FAVORITE_LEN_MIN+'个字符');
		return false;
	}

	//您只能使用中文和英文字符以及数字作为标签
	if(is_illege){
		this.favorite_msg('您只能使用中文和英文字符以及数字 -和·作为标签');
		return false;
	}

	this.favorite_msg('');
//	setTimeout("set_often_favorites_status()", 50);
	return true;
}

movie_cols.prototype.favorite_msg = function(msg){
	if(msg.trim()==''){
		$obj('movie_favorites').style.backgroundColor = '#fff';
	}else{
		$obj('movie_favorites').style.backgroundColor = '#f2f9fe';
	}
	try{
		$obj('movie_favorites').focus();
		$obj('favorites_input_alert').style.color = 'red';
		$obj('favorites_input_alert').innerHTML = msg;
	}
	catch(e){}
}


movie_cols.prototype.grade_change_show = function(id,flag){
//	this.is_on_grade_selected = true;
	grade_img2_src = this.grade_img2_src;
	grade_img0_src = this.grade_img0_src;
	grade_img1_src = this.grade_img1_src;
	var old_grade_selected_value = 0;
	if(fav_movie_people_data){
		old_grade_selected_value = fav_movie_people_data.grade;
	}
//	this.grade_change_click = false;
	for(var i=1;i<=10;i++){
		if(i<=old_grade_selected_value){
			if(i<=id){
				$obj('game_favorite_grade_show_'+i).src = grade_img2_src;
			}
			else{
				$obj('game_favorite_grade_show_'+i).src = grade_img1_src;
			}
		}
		else if(i<=id){
			$obj('game_favorite_grade_show_'+i).src = grade_img2_src;
		}
		else{
			$obj('game_favorite_grade_show_'+i).src = grade_img0_src;
		}
	}
	return false;
}
//打分
movie_cols.prototype.grade_change = function(id){
	fav_init_point = id;
	movie_col.grade_init();
}

movie_cols.prototype.tg = function(){
	var a = ['tagTitle','movie_dl','tagValue'];
	if(false){
		for(i in a){
			id = a[i];
			obj = $obj(id);
			obj.style.display='none';
		}
	}
	else{
		var obj;
		for(i in a){
			id = a[i];
			obj = $obj(id);
			if(obj){
				if(this.tg_flag){
					obj.style.display='none';
				}
				else{
					obj.style.display = '';
				}
			}
		}
		if(this.tg_flag){
			$obj('movie_qh').innerHTML = '↓高级选项';
		}
		else{
			$obj('movie_qh').innerHTML = '↑简化';
		}
		this.tg_flag = !this.tg_flag;
	}
}
movie_cols.prototype.msg_check=function(){
	try{
		var obj = $obj('movie_msg');
		if (obj) {
			var v=obj.value;
			if(v && v.length>this.MSG_MAX_LEN){
				$obj('movie_msg_alert').innerHTML = '评价不能超过'+this.MSG_MAX_LEN+'个字符';
				$obj('movie_msg_alert').style.color = 'red';
				return false;
			}
			$obj('movie_msg_alert').innerHTML = '';
		}
		return true;
	}
	catch(e){}
	return true;
	
}
//提交按钮
movie_cols.prototype.submit_button_click = function(){
	if(this.favorite_check() && this.msg_check()){
		var f = $obj('game_favorite_box_frm');
		f.action = this.url + '?action=shoucan&username='+this.username+'&key='+this.id+'&type='+this.type;
//		f.target="iframe_proxy";
		f.submit();
	}
	else{
		document.getElementById('shoucan_button_submit').disabled = false;
	}
}

movie_cols.prototype.submit_button_click_bak = function(back){
	var f = $obj('game_favorite_box_frm');
	f.action = '_blank';
	f.method = 'get';
	f.submit();
	fav_submit_back(back);
	return true;
}


//清除遮罩层
movie_cols.prototype.game_favorite_box_close = function(){
	fav_close_div();
	return true;
}
//锁屏
movie_cols.prototype._movie_suoping = function (){
	create_global_opacity_div(this.suopingdiv);
}
//解除锁屏
movie_cols.prototype._cancel_suoping = function (){
	cancel_div(this.suopingdiv);
}

var fav_status = 0 ;
var fav_point = 0;
//第一个参数为状态 第二个参数为分数
var fav_url = "http://movie.xunlei.com/favorite_v2.php";
var fav_movie_people_data = "";
var fav_username = getCookie('usrname');
var fav_div_flag = true;//是否允许弹出层
var fav_init_point = 0;//初始化的分数
//重新初始化COOKIE
function fav_init_username(){
	fav_username = getCookie('usrname');
}

var fav_init_movie_people_data_callback = false;
//初始化用户和电影的数据
function fav_init_movie_people_data(){
	var time = new Date();
	var param = "action=getstatus&type="+movie_type+"&username="+fav_username+"&key="+movieid+'&asdfasdf='+time+Math.random();;
	var obj = xlAjax(fav_url,param,fav_init_movie_people_data_back);
	if(arguments.length == 1){
		fav_init_movie_people_data_callback= arguments[0];
	}
	else{
		fav_init_movie_people_data_callback = false;
	}
	obj.get();
}

function fav_init_movie_people_data_back(request){
	var r = eval("("+request.responseText+")");
	if(r.tid==1){
		fav_movie_people_data = r.data;
	}
	else{
		fav_movie_people_data = 0;
	}
	
	if(fav_init_movie_people_data_callback){
		if(typeof(fav_init_movie_people_data_callback)=="string"){
			eval(fav_init_movie_people_data_callback);
		}
		else if(typeof(fav_init_movie_people_data_callback)=='function'){
			var t = fav_init_movie_people_data_callback;
			t();
		}
		fav_init_movie_people_data_callback = false;
	}
}

//弹出收藏层
function fav_movie_col_box(){
	if(fav_div_flag && !document.getElementById(movie_col.divid)){
		movie_col._init_cookie_name();
		fav_init_username();
//		old_status_html = document.getElementById('my_score_status').innerHTML;
//		document.getElementById('my_score_status').innerHTML = "<img src='http://images.movie.xunlei.com/movie_v2/images/spinner.gif' />";
		var status=0;
		var point=0;
		if(arguments.length>=1){
			status=arguments[0];
		}
		if(arguments.length>=2){
			point=arguments[1];
		}
		if(arguments.length>=3){
			movieid = arguments[2];
			movie_col.id=movieid;
		}
		else{
			return false;
		}
		if(arguments.length>=4){
			movie_type=arguments[3];
			movie_col.type=movie_type;
		}
		if(arguments.length>=5){
			movie_title=arguments[4];
		}
		movie_col.init();
//		fav_init_movie_people_data(function(){fav_movie_col_box_back(status,point);});
//		if(typeof(fav_movie_people_data)!='undefined'){
//			fav_movie_col_box_back(status,point);
//		}
//		else{
			fav_init_movie_people_data(function(){fav_movie_col_box_back(status,point);});
//		}
//		this._init_('movie_col.movie_col_box_back()');
	}
}

function fav_movie_col_box_back(status,point){
	var type = status;
	if(!fav_movie_people_data){//增加
		var title = movie_col.statuss[type];
	}
	else{
		type = fav_movie_people_data.type;
		var title = '修改';
	}
	title +=": "+movie_title;
	fav_movie_col_box_html(type,title,point);
}
//取得HTML
function fav_movie_col_box_html(type,title,point){
	var tpl = new template('movie_shoucan',fav_movie_col_box_html_back);
	tpl.setparam('title',title);
	tpl.setparam('id',movieid);
	if(fav_movie_people_data){
		tpl.setparam('msg',fav_movie_people_data.msg);
		tpl.setparam('edit',true);
	}
	else{
		tpl.setparam('msg','');
		tpl.setparam('edit',false);
	}
	tpl.setparam('type',type);
	tpl.setparam('movie_type',movie_type);
	tpl.get();
	if(point==0 && fav_movie_people_data && typeof(fav_movie_people_data.grade)!='undefined'){
		fav_init_point = fav_movie_people_data.grade;
	}
	else{
		fav_init_point = point;
	}
	return true;
}
//取得HTML的回调
function fav_movie_col_box_html_back(html){
	movie_tpl.div(movie_col.divid,html.responseText);//弹出DIV
	movie_col.grade_init();//初始积分
	
	if(!fav_movie_people_data || parseInt(fav_movie_people_data.is_open)){

	}
	else{
		document.getElementById('movie_is_open').checked = 'checked';
	}
	
	if(fav_movie_people_data){//编辑状态 需要初始化用户的标签
//		movie_col.init_favorite();
		fav_init_favorite();
	}
	movie_col.init_allfavorite();//初始化我常用的标签和迅雷用户常用的标签
	if(movie_col.gaoji==false){
		var a = ['tagTitle','movie_dl','tagValue'];
		for(i in a){
			id = a[i];
			obj = $obj(id);
			if(obj) { obj.style.display='none';}
		}
		movie_col.tg_flag = false;
		$obj('movie_qh').innerHTML = '↓高级选项';
		movie_col.gaoji=true;
	}
	fav_div_flag = false;
}
//关闭遮罩
function fav_close_div(){
//	document.getElementById('my_score_status').innerHTML = old_status_html;
	movie_tpl.cancel_div(movie_col.divid);
	fav_div_flag = true;
}
//用户提交的回调
function fav_submit_back(back){

	set_movie_xlcookie_id(movieid);//增加操作记录
	if(!fav_movie_people_data){//增加收藏次数
		jump_click(movieid, 'jump_favorite');
	}
	if((!fav_movie_people_data || fav_movie_people_data.grade==0) && parseInt($obj('grade').value)>0){
		jump_click(movieid, 'jump_rating');//增加评分次数
	}
	fav_movie_people_data = new Array();
	if(!back.grade){
		back.grade = 0;
	}
	back.type = back.status;
	fav_movie_people_data = '';
	fav_close_div();//关闭遮罩
}

//初始化弹出层的标签
function fav_init_favorite(){
	if(fav_movie_people_data){
		if(typeof(fav_movie_people_data.favorites)!='undefined'){
			$obj('movie_favorites').value = fav_movie_people_data.favorites;
		}
		else{
			fav_init_favorite_ajax();
		}
	}
}
//异步初始化弹出层标签
function fav_init_favorite_ajax(){
	var time = new Date();
	var param = "action=getfavorite&username="+fav_username+'&key='+movieid+'&type='+movie_type+'&adfsdf='+time+Math.random();;
	var ajax = new xlAjax(fav_url,param,fav_init_favorite_ajax_back);
	ajax.get();
}
//初始化标签返回
function fav_init_favorite_ajax_back(request){
	var r = eval("(" + request.responseText + ")");
	if(r.tid==1){
		fav_movie_people_data.favorites = r.data;
		$obj('movie_favorites').value = r.data;
	}
}