var t_usrname = '';



function change_show_hide(div_name) {

	if (document.getElementById(div_name).style.display=="block") {		

		document.getElementById(div_name).style.display="none" ;

	} else {

		document.getElementById(div_name).style.display="block" ;

	}

}



function showtab(this_tab,div_name,total_tab) {

	for (var i=1; i<=total_tab; i++) {

		var this_btn_name = div_name + "_btn" + i;

		var this_sub_name = div_name + "_sub" + i;

		

		if (i == this_tab) {

			document.getElementById(this_sub_name).style.display="block" ;

			document.getElementById(this_btn_name).className="select" ;

			/*if (url) {

				showtab_url(this_tab,div_name)			

			}*/

		} else {

			document.getElementById(this_sub_name).style.display="none" ;

			document.getElementById(this_btn_name).className="" ;

		}

	}

}



function selectoption(this_tab,div_name,total_tab) {

	for (var i=1; i<=total_tab; i++) {

		var this_sub_name = div_name + "_sub" + i;

		if (i == this_tab) {

			document.getElementById(this_sub_name).style.display="block" ;

		} else {

			document.getElementById(this_sub_name).style.display="none" ;

		}

	}

}



function showtab_url(this_tab,div_name) {

	/*

	switch (div_name) {

		case 'juzhao':

			var index = 1;

			break;

		case 'ygp':

			var index = 2;

			break;

	}

	*/

	var this_more_name = div_name + "_more";

	var this_url = document.getElementById(div_name + "_url" + this_tab).value;

	//var this_url = "movie/" + movieid + "/" + tab_type[index][this_tab] + "/list";

	document.getElementById(this_more_name).href=this_url ;

}



function change_menu_select(num) {

	var select = "menu_select_" + num;

	document.getElementById(select).setAttribute("className", "current");

}









function show_teleplay_content(response) {

	if(response.responseText == '') {

		show_teleplay_fail();

	} else {

		document.getElementById("select_teleplay_content_show").innerHTML = response.responseText;

	}    

}

function show_teleplay_fail() {

    document.getElementById("select_teleplay_content_show").innerHTML = "对不起，没有找到剧集资料。";

}



function get_page_list(page, total_page, url){

	var return_str = '';

	var p_first = '&laquo; ...';

	var p_previous = '&laquo;上一页';

	var p_next = '下一页&raquo;';

	var p_end = '... &raquo;';

	var previous = '';

	var next = '';



	page = parseInt(page);

	total_page = parseInt(total_page);



	if (page<1) {

		page = 1;

	}

	if (page>total_page) {

		page = total_page;

	}

	var start_page = page-4;

	var end_page = page+4;

	if (start_page<1) {

		start_page = 1;

		end_page = start_page+7;

	}

	if (end_page>total_page) {

		end_page = total_page;

		start_page = end_page-7;

	}

	if (start_page<1) {

		start_page = 1;

	}



	for(var i=start_page; i<=end_page; i++) {

		if (i == page) {

			return_str += "<span>"+i+"</span> ";

		} else {

			return_str += "<a href='"+url+"?page="+i+"'>"+i+"</a> ";

		}

	}



	if (page>2)

	{

		previous += "<a href='"+url+"?page=1'>"+p_first+"</a> ";

	}

	if (page>1)

	{

		var select_page = page-1;

		previous += "<a href='"+url+"?page="+select_page+"'>"+p_previous+"</a> ";

	}



	if (page<total_page)

	{

		var select_page = page+1;

		next += "<a href='"+url+"?page="+select_page+"'>"+p_next+"</a> ";

	}

	if (page<total_page-1)

	{

		next += "<a href='"+url+"?page="+total_page+"'>"+p_end+"</a>";

	}







	return "<p>"+previous+return_str+next+"</p>";

}



function is_int(num) {

	var patrn=/^[0-9]{1,}$/;   

	if (patrn.exec(num)) {

		return true;

	}

	return false;

}



function trim(s) {

	return s.replace(/(^\s*)|(\s*$)/g, "");

}



function getCookie(name) {
	var search = name + "=";
	var offset = document.cookie.indexOf(search);
	if (offset != -1) {
		offset += search.length;
		var end = document.cookie.indexOf(";", offset);
		if (end == -1)
			end = document.cookie.length;
		var r = unescape(document.cookie.substring(offset, end));
		if(!r && name=='usrname'){
			return getCookie('usernewno');
		}
		else{
			return r;
		}
	}
	else if(name=='usrname'){
		return getCookie('usernewno');
	}
	else{
		return "";
	}
	
}



function setCookie(name, value, hours){

	var expireDate=new Date(new Date().getTime()+hours*3600000);

	document.cookie = name + "=" + escape(value) + "; path=/; domain=/; expires=" + expireDate.toGMTString() ;

}



function delCookie(name) {

	var expireDate=new Date(new Date().getTime());

	document.cookie = name + "= ; path=/; domain=/; expires=" + expireDate.toGMTString() ;

}



function rand(num){

	return Math.floor(Math.random()*num)+1; 

}



function set_rating_record(type, id, usrname, rating) {

	var tmp = new Image();

	tmp.src='http://rating.movie.xunlei.com/set_rating.php?type='+type+'&id='+id+'&usrname='+usrname+'&rating='+rating+'&cache='+rand(9999999);	

}



function set_collect_record(type, id, usrname) {

	var tmp = new Image();	

	tmp.src='http://rating.movie.xunlei.com/set_collect.php?type='+type+'&id='+id+'&usrname='+usrname+'&cache='+rand(9999999);	

}



function set_tag_record(type, id, sid, content) {

	var tmp = new Image();			

	var str = 'type='+type+'&id='+id+'&sid='+sid+'&content='+content;

	tmp.src='http://rating.movie.xunlei.com/set_tag.php?'+encodeURI(str)+'&cache='+rand(9999999);	

}



function set_tracker_record(type, id, col) {

	var tmp = new Image();		

	if (type==1) {

		var adftrack_ref=escape(document.referrer);

		tmp.src="http://tracker.movie.xunlei.com/movie.php?movieid="+id+"&cache="+rand(9999999)+"&pre=" + adftrack_ref;

	} else {

		tmp.src='http://tracker.movie.xunlei.com/set_movie_stat.php?t='+type+'&id='+id+'&c='+col+'&rand='+rand(9999999);	

	}

}



function set_pic_record(id) {

	var tmp = new Image();

	var adftrack_ref=escape(document.referrer);

	tmp.src="http://tracker.movie.xunlei.com/still.php?movieid="+id+"&cache="+rand(9999999)+"&pre=" + adftrack_ref;	

}



function add_list_collect(id, type) {

	if(check_is_login()) {

		set_collect_record(type, id, t_usrname);

		set_tracker_record(type, id, 'collect');

		alert("已添加到您的电影收藏！");

	}

}



function check_is_login() {

	return movie_check_login();

	/*

	t_usrname = getCookie('usrname');

	if (t_usrname == "") {

		if (confirm("您还没有登录，点击\"确定\"登录")) {

			setTimeout("window.location.href='http://i.xunlei.com/login.html?u1="+location.href+"'", 500);		

			//location.href="http://i.xunlei.com/login.html?u1="+location.href;

		}

		return false;

	} 

	return true;*/

}



function check_is_login_url(url, target) {

	return movie_check_login();

	/*t_usrname = getCookie('usrname');

	if (t_usrname == "") {

		if (confirm("您还没有登录，点击\"确定\"登录")) {

			if (target == '_blank' && false) {

				setTimeout("window.open('http://i.xunlei.com/login.html?u1="+url+"')", 500);

			} else {

				setTimeout("window.location.href='http://i.xunlei.com/login.html?u1="+url+"'", 500);

			}

		}

		return false;

	} 

	return true;*/

}



function jump_click(movieid, kind){

	var tmp = new Image();			

	tmp.src = "http://tracker.movie.xunlei.com/movie_jump.php?kind="+ kind +"&movieid="+movieid+"&rand="+rand(9999999);

}



function process_searchs(theform) {

		if (theform.keyword.value == '') {

			alert("请先输入要搜索的关键字！");

			theform.keyword.focus();

			return false;

		}

		

		var keyword = encodeURIComponent(theform.keyword.value);

		if (theform.type.value=="search_movie_by_keyword") {			 

			var url = "http://movies.xunlei.com/search/" + keyword;

			window.location = url;

			return false;

		}

		if (theform.type.value=="search_movie_by_artist") {			 

			var url = "http://movies.xunlei.com/search_by_artist/" + keyword;

			window.location = url;

			return false;

		}

		if (theform.type.value=="search_artist_by_keyword") {

			var url = "http://movies.xunlei.com/person/search/" + keyword;

			window.location = url;

			return false;

		}			

}



//检查是否登陆

function movie_check_login(){

	try {

		var usrname = getCookie('usrname');

		if (usrname == "") {

			global_login_frm_show(this,true,window.location.href,0,"movie");
			//global_login_frm_show(this,true,window.location.href,ref_flag,"movie");

			return false;

		}

		else{

			return true;

		}

	} catch (e) {}

}

var login_success_callback_function = '';

//需要回调的登陆

function movie_check_login_callback(){

	var ref_flag = 1;

	if(arguments.length==1){

		login_success_callback_function = arguments[0];

		ref_flag = 0;

	}

	try {

		var usrname = getCookie('usrname');

		if (usrname == "") {

			

			global_login_frm_show(this,true,window.location.href,ref_flag,"movie");

			return false;

		}

		else{

			if(arguments.length>=1){

				login_success_eval_callback(1);

			}

			else{

				return true;

			}

		}

	} catch (e) {}

}



//执行登陆的回调的回调

function login_success_eval_callback(flag){

	if(typeof(login_success_callback_function)!='undefined'){

		if(typeof(login_success_callback_function)=='function'){

			login_success_callback_function(flag);

		}

		else if(typeof(login_success_callback_function)=='string'){

			try{

				eval(login_success_callback_function);

			}

			catch(e){}

		}

	}

	login_success_callback_function = '';

}



//登陆过后的回调

function login_success_callback(json){

	update_user_status();

	login_success_eval_callback(0);//执行回调的回调

}



//初始化想看人数 在看 看过人数

function _init_want_ing_ed(id){

	try{



		var num = 0;

		var cookiekey;

		var obj = document.getElementById('collect_count_show');

		obj.innerHTML = '';

		for(var i=0;i< collect_out_array.length;i++){

			num = parseInt(collect_out_array[i].num);

			if(collect_out_array[i].title == '人想看本片' || collect_out_array[i].title == 'want'){

				cookiekey = 'movie_want_'+id;

				var title = '人想看';

			}

			else if(collect_out_array[i].title == '人正在看本片' || collect_out_array[i].title == 'ing'){

				if(check_can_show_collect()){

					cookiekey = 'movie_ing_'+id;

					var title = '人正在看';

				}

				else{

					continue;

				}

			}

			else if(collect_out_array[i].title == '人已看过本片' || collect_out_array[i].title == 'ed'){

				if(check_can_show_collect()){

					cookiekey = 'movie_ed_'+id;

					var title = '人已看过';

				}

				else{

					continue;

				}

			}

			else if(collect_out_array[i].title == 'dl'){

				cookiekey = 'movie_dl_'+id;

				var title = '人没兴趣';

			}

			

			var tmp = xlcookie(cookiekey);

			if(tmp !== null){

				num = num + parseInt(tmp);

			}

			

			obj.innerHTML = obj.innerHTML + '<span style="color: rgb(252, 131, 1); font-size: 13px;float:none;"><strong id="'+cookiekey+'">'+num+'</strong></span>&nbsp;'+title+'&nbsp;&nbsp;';

		}

	} catch(e){}

}



function comment_vote(cid, f) {

	try	{

		if (movie_check_login()) {

			var usrname = getCookie('usrname');

			if (getCookie('comment_vote') == cid) {

				alert("您的意见已提交");

			} else {

				setCookie('comment_vote', cid, 1);

				var tmp = new Image();	

				tmp.src='http://rating.movie.xunlei.com/set_comment_vote.php?usrname='+usrname+'&cid='+cid+'&vote='+f+'&cache='+rand(9999999);

				if (f == 1)	{

					document.getElementById('vote_yes').innerHTML = parseInt(document.getElementById('vote_yes').innerHTML)+1;

				}				

				document.getElementById('vote_total').innerHTML = parseInt(document.getElementById('vote_total').innerHTML)+1;

				document.getElementById('vote_show').style.display = "inline-block";

				alert("谢谢您的参与，您的意见已提交");

			}

		}

	}catch (e){}

}



function get_my_date(y,m,d) {

	var get_date = new Date(y+"/"+m+"/"+d);

	var now = new Date();	

	if (now>get_date) {

		var nDiff=now-get_date; 

		if (nDiff>0 && nDiff<3600000*24) {

			return "今天";

		} else if (nDiff>3600000*24 && nDiff<3600000*48) {

			return "昨天";

		}

	}		

	return y+"-"+m+"-"+d;

}

function check_can_show_collect(){

	if(movie_type=='movie'){

		if(typeof(releasedate)=='undefined'){

			return true;

		}

		else{

			var cuo = new Date();

			var timestemp = cuo.getTime();

			timestemp = timestemp/1000;

			return releasedate<timestemp;

		}

	}

	else{

		if(typeof(teleplay_status)=='undefined'){

			return true;

		}

		else{

			return teleplay_status!=0;

		}

	}

}

get_city_callback = false;
function get_city(){
	var t = new Date();
	var url = 'http://movie.xunlei.com/getip.php';
	var a = xlAjax(url,'t='+t,get_city_back);
	if(arguments.length>0) get_city_callback = arguments[0];
	a.get();
}
function get_city_back(request){
	city = request.responseText;
	if(typeof(get_city_callback)=='function'){
		var t = get_city_callback;
		t(city);
	}
}
function CopyURL(){
	try{
		if(arguments.length==0){
	    	var obj = document.getElementById("urllocation");
		}
		else{
			var obj = $obj(arguments[0]);
		}
		if(!obj){
			return '';
		}
	    obj.value = "你好，我在迅雷上找到了一部很好看的影片《"+movie_title+"》，还可以在线播放哦！\n影片地址："+document.location.href;
	    obj.select();
	    js = obj.createTextRange();
	    js.execCommand("Copy");
	    alert("影片地址已复制到剪贴板，您可以粘贴到QQ、MSN、论坛或通过其他方式发送给朋友了。");
	}
	catch(e){
		alert("对不起，目前此功能只支持IE，请直接复制地址栏的地址！")
	}
}

/*获得过去了多久 之类5小时之前
*/
function get_time_pass(t){
	var s = new Date(t);
	var c = new Date();
	var fdate = '';
	if(c.getTime()-s.getTime()>60*60*24*1000){//大于一天的直接使用年月日的显示
		return parseInt((c.getTime()-s.getTime())/86400000)+'天前';
		//return s.getFullYear()+"年" + s.getMonth()+1+"月" + s.getDate()+"日";
	}
	var cTime = (c.getTime()-s.getTime())/1000; //相差多少秒
	if(cTime>3600){//大于1小时
		fdate += parseInt(cTime/3600)+"小时";
		/*cTime=cTime%3600;//还余下多少秒
		if(cTime==0){
			document.write(fdate+" 前");
			return 0;
		}
		fdate += parseInt(cTime/60)+"分钟";*/
		return fdate+" 前";
	}else{
		if(cTime>60){//大于一分钟
				fdate = parseInt(cTime/60)+"分钟";
				return fdate+"前";

		}else {
			return parseInt(cTime)+"秒前";
		}
	}
}
