/**
*页面使用js代码
*
*/
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var is_safari = (userAgent.indexOf('webkit') != -1 || userAgent.indexOf('safari') != -1);
var note_step = 0;
var note_oldtitle = document.title;
var note_timer;

function g(id) {
	return document.getElementById(id);
}
//验证码
function seccode() {
	var img = 'my.php?mod=do&ac=seccode&rand='+Math.random();
	document.writeln('<img id="img_seccode" src="'+img+'" align="absmiddle">');
}

function updateseccode() {
	var img = 'my.php?mod=do&ac=seccode&rand='+Math.random();
	if(g('img_seccode')) {
		g('img_seccode').src = img;
	}
}

function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}
function trim(str) { 
	var re = /\s*(\S[^\0]*\S)\s*/; 
	re.exec(str); 
	return RegExp.$1; 
}

//选项卡
function setTab(m,n){

 var tli=document.getElementById("menu"+m).getElementsByTagName("li");
 var mli=document.getElementById("main"+m).getElementsByTagName("ul");
 for(i=0;i<tli.length;i++){
  tli[i].className=i==n?"hover":"";
  mli[i].style.display=i==n?"block":"none";
 }
}

function switchTab(d, g, f, j, a) {
    try {
        for (i = 0; i < f; i++) {
            var l = document.getElementById("Tab_" + d + "_" + i);
            var b = document.getElementById("List_" + d + "_" + i);
            if (i != g) {
                l.className = a;
                //b.style.display = "none"
                b.className = '';
            }
        }
        try {
            for (ind = 0; ind < CachePic[d][g].length; ind++) {
                var k = document.getElementById(d + "_pic_" + g + "_" + ind);
                k.src = CachePic[d][g][ind];
                k.original = CachePic[d][g][ind];
            }
        } catch(h) {}
        document.getElementById("Tab_" + d + "_" + g).className = j;
        //document.getElementById("List_" + d + "_" + g).style.display = ""
        document.getElementById("List_" + d + "_" + g).className = 'block';
    } catch(c) {}
}

function getCookie(a) {
    var b = a + "=";
    a = document.cookie.indexOf(b);
    if (a != -1) {
        a += b.length;
        b = document.cookie.indexOf(";", a);
        if (b == -1) b = document.cookie.length;
        return unescape(document.cookie.substring(a, b))
    } else return ""
}
function _getCookie(a) {
    return document.cookie.match(RegExp("(^" + a + "| " + a + ")=([^;]*)")) == null ? "": decodeURIComponent(RegExp.$2)
}

function setCookie(a, b, c) {
    c = new Date((new Date).getTime() + c * 36E5);
    document.cookie = a + "=" + escape(b) + "; path=/; domain=.a67.com; expires=" + c.toGMTString()
}

function delCookie(a) {
    var b = new Date((new Date).getTime());
    document.cookie = a + "= ; path=/; domain=.a67.com; expires=" + b.toGMTString()
}

function LTrim(a) {
    var b;
    for (b = 0; b < a.length; b++) if (a.charAt(b) != " " && a.charAt(b) != " ") break;
    return a = a.substring(b, a.length)
}
//填写生日
function showbirthday(){
	document.getElementById('birthday').length=0;
	for(var i=0;i<28;i++){
		document.getElementById('birthday').options.add(new Option(i+1, i+1));
	}
	if(document.getElementById('birthmonth').value!="2"){
		document.getElementById('birthday').options.add(new Option(29, 29));
		document.getElementById('birthday').options.add(new Option(30, 30));
		switch(document.getElementById('birthmonth').value){
			case "1":
			case "3":
			case "5":
			case "7":
			case "8":
			case "10":
			case "12":{
				document.getElementById('birthday').options.add(new Option(31, 31));
			}
		}
	} else if(document.getElementById('birthyear').value!="") {
		var nbirthyear=document.getElementById('birthyear').value;
		if(nbirthyear%400==0 || nbirthyear%4==0 && nbirthyear%100!=0) document.getElementById('birthday').options.add(new Option(29, 29));
	}
}

//****************************************************************  
//* 名　　称：DataLength    
//* 功    能：计算数据的长度   
//* 入口参数：fData：需要计算的数据   
//* 出口参数：返回fData的长度(Unicode长度为2，非Unicode长度为1)  
//*****************************************************************  
function DataLength(fData)  
{  
    var intLength=0  
    for (var i=0;i<fData.length;i++)  
    {  
        if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255))  
            intLength=intLength+1  
        else  
            intLength=intLength+1      
    }  
    return intLength;
}

function a67_getCookie(a) {
    var b = a + "=";
    a = document.cookie.indexOf(b);
    if (a != -1) {
        a += b.length;
        b = document.cookie.indexOf(";", a);
        if (b == -1) b = document.cookie.length;
        return unescape(document.cookie.substring(a, b))
    } else return ""
}
function _getCookie(a) {
    return document.cookie.match(RegExp("(^" + a + "| " + a + ")=([^;]*)")) == null ? "": decodeURIComponent(RegExp.$2)
}

function a67_setCookie(a, b, c) {
	var   c   =   (typeof(c)   ==   "undefined "?'':c);

	if(c != ''){
		c = new Date((new Date()).getTime() + c * 60000);
		expire = "; expires=" + c.toGMTString()+";path=/";
	}

	document.cookie = a + "=" + escape(b) + "; path=/; domain=.a67.com; " +expire
}

function a67_delCookie(a) {
    var b = new Date((new Date).getTime());
    document.cookie = a + "= ; path=/; domain=.a67.com; expires=" + b.toGMTString()
}
function tcheck_p_login(){
	var login = a67_getCookie('papatata_ptid');
	var login2 = _getCookie('papatata_name');
	if(login == ''){
		
		return false;
	}else{
		return true;
	}
}
function check_p_login(){
	var login = a67_getCookie('papatata_ptid');
	var login2 = _getCookie('papatata_name');
	if(login == ''){
		
		$("#p_login_d").html('<ul><li><span>手机号：</span><p><input id="tel" name="tel" class="input" type="text" maxlength="17" value="" /></p></li><li><span>密码：</span><p><input id="password" name="password" class="input" type="password" maxlength="20" value=""/></p></li><li><span>验证码：</span><p><input type="text" class="yz" name="seccode" id="seccode" size=4 /> <img src="http://www.papatata.com/action/verifycode.php" title="看不清?点击更换图片!" onclick="this.src=\'http://www.papatata.com/action/verifycode.php\';" style="cursor:pointer"/></p></li><li><input type="button" class="btn_submit" value="登录" onclick="to_p_login();" />&nbsp;&nbsp;<a href="http://yy.a67.com/page/register.php" target="_blank"><font color="ff6600">注册会员</font></a></li></ul>');
	}else{
		msg(login2?login2:login);
	}
}
function p_logout(){
	a67_delCookie('papatata_phone');
	a67_delCookie('papatata_name');
	a67_delCookie('papatata_ptid');
	a67_delCookie('a67_auth');
	a67_delCookie('a67_loginuser');
	var login = a67_getCookie('papatata_ptid');
	if(login == ''){
		
		$("#p_login_d").html('<ul><li><span>手机号：</span><p><input id="tel" name="tel" class="input" type="text" maxlength="17" value="" /></p></li><li><span>密码：</span><p><input id="password" name="password" class="input" type="password" maxlength="20" value=""/></p></li><li><span>验证码：</span><p><input type="text" class="yz" name="seccode" id="seccode" size=4 /> <img src="http://www.papatata.com/action/verifycode.php" title="看不清?点击更换图片!" onclick="this.src=\'http://www.papatata.com/action/verifycode.php\';" style="cursor:pointer"/></p></li><li><input type="button" class="btn_submit" value="登录" onclick="to_p_login();" />&nbsp;&nbsp;<a href="http://yy.a67.com/page/register.php" target="_blank"><font color="ff6600">注册会员</font></a></li></ul>');
	}
}
function p_logout_r(){
	a67_delCookie('papatata_phone');
	a67_delCookie('papatata_name');
	a67_delCookie('papatata_ptid');
	a67_delCookie('a67_auth');
	a67_delCookie('a67_loginuser');
	var login = a67_getCookie('papatata_ptid');
	window.location.reload();
	
}

function ismobile(v){  
	var a = /^((\+86)|(86))?1(3|5|8)\d{9}$/ ;  
	if( v.length!=11||!v.match(a) ){  
		return false;  
	}else{  
		return true;  
	}  
}  
function msg(name){
	
	var ss = '<span>您好：'+name+'，&nbsp;&nbsp;<a href="http://t.a67.com/" target="_blank">我的美图</a>&nbsp;&nbsp;<a href="http://yy.a67.com/" target="_blank">我的好友</a>&nbsp;&nbsp;<a href="http://yy.a67.com/" target="_blank">我的主页</a>&nbsp;&nbsp;<a href="javascript:p_logout();">退出登录</a></span>';
	$("#p_login_d").html(ss);
}

function to_p_login(){
	var tel=$('#tel').val();
	var password=$('#password').val();
	var seccode=$('#seccode').val();
	if(!ismobile(tel)){
		alert('请输入正确的手机号');
		$('#tel').focus();
		return false;
	}
	if(password==''){
		alert('密码不能为空吧？！');
		$('#password').focus();
		return false;
	}
	if(seccode==''){
		alert('输入您看到的验证码');
		$('#seccode').focus();
		return false;
	}

	$.getJSON("http://yy.a67.com/action/login.php?act=in&action=a67&state="+encodeURIComponent('+86')+"&password="+encodeURIComponent(password)+"&tel="+tel+"&verifycode="+seccode+"&jsoncallback=?",function(data){
		if(data.flag=='success'){
			msg(data.name?data.name:data.tel);
		}else{
			if(data.flag=='verifyerror'){
				alert('验证码错误');
			}else if(data.flag=='pwderror'){
				alert('密码不正确');
			}else if(data.flag=='teleneedreg'){
				alert('请先注册');
			}
		}
    });

}

function get_uid(){
	var login = a67_getCookie('a_auc');
	var strs= new Array();
	strs=login.split(",");
	if(!strs[0]){
		return false;
	}else{
		return strs[0];
	}
}
function get_vipdegree(){
	var login = a67_getCookie('a_auc');
	var strs= new Array();
	strs=login.split(",");
	if(!strs[1]){
		return false;
	}else{
		return strs[1];
	}
}
jQuery(function($) {
	
	$(".bt_bao").click(function(){//下载包
		var btid = '#'+$(this).attr('id');
		var c = $(this).attr('name').split('_')[2];
		var d = $(this).attr('name').split('_')[3];
		$.dialog({
			id: 'bt_bao',
			drag: false,
			resize: false,
			follow: btid,
			lock:true,
			titel:'加入下载包',
			content: '请根据您手机支持的格式选择视频文件类型',
			width: '30em',
			height: 75,
			button: [
			{
				name: '3gp',
				callback: function () {
					var r = add_pkg(c,d,'3gp');
					if(r == 1 || r==-1){
						this.content('添加成功,赶快去下载包看看吧!').button({
							name: '3gp',
							disabled: true
						}).button({
							name: '去下载包',
							callback: function () {location.href='http://www3.a67.com/downpackage.php';},
							focus: true
						});
					}else if(r == -2){
						this.content('请先登录');
					}else{
						this.content('系统错误，信息：'+r);
					}

					this.follow(btid);
					this.lock();
					return false;

				}
			},
			{
				name: 'mp4',
				callback: function () {
					var r = add_pkg(c,d,'mp4');
					if(r == 1 || r==-1){
						this.content('添加成功,赶快去下载包看看吧!').button({
							name: 'mp4',
							disabled: true
						}).button({
							name: '去下载包',
							callback: function () {location.href='http://www3.a67.com/downpackage.php';},
							focus: true
						});
					}else if(r == -2){
						this.content('请先登录');
					}else{
						this.content('系统错误，信息：'+r);
					}

					this.follow(btid);
					this.lock();
					return false;
				}
			},
			{
				name: '高清',
				callback: function () {
					var r = add_pkg(c,d,'hmp4');
					if(r == 1 || r==-1){
						this.content('添加成功,赶快去下载包看看吧!').button({
							name: '高清',
							disabled: true
						}).button({
							name: '去下载包',
							callback: function () {location.href='http://www3.a67.com/downpackage.php';},
							focus: true
						});
					}else if(r == -2){
						this.content('请先登录');
					}else{
						this.content('系统错误，信息：'+r);
					}

					this.follow(btid);
					this.lock();
					return false;
				}
			}
			]

		});
	});

	$(".bt_login").click(function(){//登录
		var u = get_uid();
		if(!u){
			var list = $.dialog.list;
			for (var i in list) {
				list[i].close();
			};
			var dialog = $.dialog({id: 'bt_login'});
			$.ajax({
				url: '/public/login.html',
				success: function (data) {
					dialog.content(data);
					dialog.lock();
				},
				cache: false
			});
		}
	});

	$(".need_login").click(function(){//登录
		var u = get_uid();
		if(!u){
			var list = $.dialog.list;
			for (var i in list) {
				list[i].close();
			};
			var dialog = $.dialog({id: 'bt_login'});
			$.ajax({
				url: '/public/needlogin.html',
				success: function (data) {
					dialog.content(data);
					dialog.lock();
				},
				cache: false
			});
		}
	});
	
	//====================订阅start=======================
	$(".bt_subs").click(function(){
		var c = $(this).attr('name').split('_')[2];
		var d = $(this).attr('name').split('_')[3];
		var uid = get_uid();
		if(!uid){
			pup_need_login();
		}
		if(uid > 0){
			$.getJSON("http://my1.a67.com/api/acapi.php?mod=mylike&ac=addmark&json=1&uid="+uid+"&did="+d+"&channelid="+c+"&jsoncallback=?",function(data){
			if(data.state){
				alert('订阅成功');//成功
			}else{
				alert('订阅失败');//失败
			}
			});
		}
	});
	
	//===================订阅end========================
	
});
var baocount = 0;
function pup_login_win(){
	var list = $.dialog.list;
	for (var i in list) {
		list[i].close();
	};
	var dialog = $.dialog({id: 'bt_login'});
	$.ajax({
		url: '/public/needlogin.html',
		success: function (data) {
			dialog.content(data);
			dialog.lock();
		},
		cache: false
	});
}
//弹出不刷新的登录框
function pup_need_login(){
	var list = $.dialog.list;
	for (var i in list) {
		list[i].close();
	};
	var dialog = $.dialog({id: 'bt_login'});
	$.ajax({
		url: '/public/needlogin.html',
		success: function (data) {
			dialog.content(data);
			dialog.lock();
		},
		cache: false
	});
}

//弹出需要刷新的登录框
function pup_login(){
	var list = $.dialog.list;
	for (var i in list) {
		list[i].close();
	};
	var dialog = $.dialog({id: 'bt_login'});
	$.ajax({
		url: '/public/login.html',
		success: function (data) {
			dialog.content(data);
			dialog.lock();
		},
		cache: false
	});
}

var vipuserdegree= new Array(
new Array(500,3,'lv1'),
new Array(500,3,'lv1'),
new Array(5000,50,'lv2'),
new Array(10000,100,'lv3'),
new Array(50000,500,'lv4'),
new Array(100000,1000,'lv5'),
new Array(200000,3000,'lv6')
);

var channellist =  new Array(
new Array(0,'',1),
new Array(1,'电影',1),
new Array(2,'电视剧',5),
new Array(3,'动漫',10),
new Array(4,'综艺',2),
new Array(5,'音乐',1),
new Array(6,'短片',1),
new Array(7,'小说',1),
new Array(8,'IPAD高清',1)
);
function checkpkglimit(dcidlist,type){
	if(dcidlist == '')return -2;
	var super_vipdegree = get_vipdegree();
	if(!super_vipdegree) return -1;
	var arr=dcidlist.split(",");
	var totallimit = vipuserdegree[super_vipdegree][1];
	var totalfile=0;
	var r= new Array();

	var info = new Array();
	for (var i in arr) {
		r[i]= arr[i].split("|");
		var k = (r[i][0]+''+r[i][1])*1;
		eval('var g'+k+' = new Array();');

	}

	var r= new Array();
	for (var i in arr) {
		r[i]= arr[i].split("|");
		var k = (r[i][0]+''+r[i][1])*1;
		eval('g'+k+'.push(r[i][2]);');
		eval('info[k] = new Array(r[i][0],r[i][1],g'+k+');');
	}
	var r= new Array();

	for (var i in info) {
		if(info[i][0] == '' || info[i][1] =='') continue;
		var t = info[i][0]*1;
		totalfile = totalfile*1 + channellist[t][2]*1;
	}
	if(type == 'bef'){
		if(totalfile>=totallimit){
			return -3;
		}
	}else{
		if(totalfile>totallimit){
			return -3;
		}
	}
	return 1;
}

function add_pkg(c,d,s){
	var u = get_uid();
	if(u && c && d){
		var o = a67_getCookie('ug_pkg');
		var r = checkpkglimit(o,'bef');
		if(r >=1){
			if(o.indexOf(c+'|'+d+'|'+s+',') == -1){
				o = c+'|'+d+'|'+s+','+o;
				a67_setCookie('ug_pkg',o,24*60);
			}else{
				return -999;
			}
			return 1;
		}else{
			if(r == -3){
				return '视频个数已达到上限';
			}else{
				return r;
			}
		}

	}else if(!u){
		return -2;
	}else{
		alert(c+''+d);
		return -3;
	}
}

//得到下载包列表
function get_downbao(){
	var u = get_uid();
	if(!u){
		return '';
	}else{
		var bao = a67_getCookie('ug_pkg');
		return bao.slice(0,-1);
	}
}
//解析下载包为数组
function downbaotoArray(bao){
	if(!bao) return new Array();
	var strs= new Array();
	var r= new Array();
	return strs=bao.split(",");
}

function get_downbaoinfo(){
	if(!baocount) $("#downtools").hide();
	var arr = downbaotoArray(get_downbao());
	if(arr!= ''){
		var r= new Array();
		var info = new Array();
		for (var i in arr) {
			r[i]= arr[i].split("|");
			var k = (r[i][0]+''+r[i][1])*1;
			eval('var g'+k+' = new Array();');

		}
		var r= new Array();
		for (var i in arr) {
			r[i]= arr[i].split("|");
			var k = (r[i][0]+''+r[i][1])*1;
			eval('g'+k+'.push(r[i][2]);');
			eval('info[k] = new Array(r[i][0],r[i][1],g'+k+');');
		}
		//alert(info[k]);
		for(var i in info){
			baocount++;
			ajax_getmovinfo(info[i][0],info[i][1],info[i][2]);
		}
	}else{
		alert('下载包为空');
		$("#downlist").html('<dl ><dd><ul><li class="nm" style="*margin-top:8px;">您的下载包为空，请选择要下载的文件，然后点击"<b>加入下载包</b>"。</li></ul></dd></dl>');
	}
}
//获得电影信息
function ajax_getmovinfo(c,d,a){
	if(c && d){
		$.getJSON("/datecahe/bao/movinfo/"+encodeURIComponent(c)+"/"+encodeURIComponent(d),function(data){
			if(data.ztitle){
				baocount--;
				if(!baocount) {
					$("#loding").hide();
					$("#downtools").show();
				}
				if(data.fileurl){
					//u['3gp'] = data.fileurl['3gp'];
					$("#downlist").append('<dd><ul><li class="nm" style="*margin-top:8px;"><input name="did[]" id="did_'+data.did+'" type="checkbox"  checked="checked" class="chk_sel" onclick="clear_downid('+data.did+');" /></li><a href="" title="'+data.ztitle+'" >'+data.ztitle+'</a>['+data.channelname+']['+a.toString()+']</ul></dd>');
					for(var i in a){
						//alert(data.fileurl[a[i]].toString());
						if(data.fileurl[a[i]] != ''){
							var s = data.fileurl[a[i]];
							for(var j in s){
								var dd = j*1+1;
								$("#downlist").append('<dd style="display: none"><ul><li class="xunlei2"><input name="downid[]" id="downid_'+data.did+'" type="checkbox"  checked="checked" class="chk_sel" /><a href="'+s[j]+'" title="" thunderPid="03821" thunderType="" thunderResTitle="" onClick="return OnDownloadClick_Simple(this,2,4)" oncontextmenu="ThunderNetwork_SetHref(this)">'+a[i]+'格式'+dd+'</a></li></ul></dd>');
							}
						}
					}
				}

			}
		});
	}
}

function clear_downid(id){
	jQuery("input[id='downid_"+id+"']:checkbox").each(function () {
		if ($(this).attr("checked")) {
			$(this).attr("checked",'');
		}else{
			$(this).attr("checked",'checked');
		}
	})
}

function do_downbao(){
	var u = get_uid();
	if(u){
		var bao = a67_getCookie('ug_pkg');
		var r = checkpkglimit(bao,'aft');
		if(r >=1){
			if(bao){
				$.getJSON("http://my1.a67.com/api/acapi.php?mod=mylike&ac=addtall&uid="+encodeURIComponent(u)+"&dcidlist="+encodeURIComponent(bao)+"&jsoncallback=?",function(data){
					if(data.state>0){
						xunleidownbao();
					}else{
					}

				});
			}
		}else{
			alert('视频个数已达到上限');
		}
	}else{
		pup_login_win();
	}
}

function xunleidownbao(){
	var b = '';
	var c = 0;
	jQuery("input[name='downid[]']:checkbox").each(function () {
		if ($(this).attr("checked")) {
			var h = $(this).parent().parent().find(".xunlei2 a").attr('href');
			var t = $(this).parent().parent().find(".xunlei2 a").attr('title');
			b += "BatchTasker.AddTask('"+h+"','"+t+"');\n";
			c++;
		}
	})
	if(c == 0){
		alert('下载任务为空,请添加下载任务！');
	}else{
		//do_downbao();
		var s = '';
		s += 'BatchTasker.BeginBatch('+c+',03821);';
		s += b;
		s += 'BatchTasker.EndBatch(03821);';
		eval(s);
		//a67_delCookie('ug_pkg');
	}

}
