var bangumi={'e':'','h':''};
function escapeHTML(str){str+='';return str.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;");}
function getPrograms(){
	$.ajax({
		url: 'http://bangumi.skyperfectv.co.jp/api/version:2/search/on_air:1/category:'+encodeURI("映画")+'?api_key=a8d163bd6b',
		data: {},
		dataType: 'jsonp',
		success: function(data){
			var h,p,pf,rec,recs=data['records'],pfs=['h','e'],prog={'e':[],'h':[]};
			var hd2bs={"h625":"e200","h626":"e201","h627":"e202"},chnm={"e200":"BS200【BS10】スター・チャンネル１","e201":"BS201 スター・チャンネル２","e202":"BS202 スター・チャンネル３","e221":"CS221 東映チャンネル","e222":"CS222 衛星劇場","e223":"CS223 チャンネルNECO","e224":"CS224 洋画★シネフィル・イマジカ","e228":"CS228 ザ・シネマ","e239":"CS239 日本映画専門チャンネルＨＤ","e240":"CS240 ムービープラスHD","s318":"Ch.318 ＦＯＸムービー","s319":"Ch.319 Ｖ☆パラダイス","s709":"Ch.709 エキサイティング・グランプリ","h623":"Ch.623 ＷＯＷＯＷシネマ","h625":"Ch.625 スター・チャンネル１","h626":"Ch.626 スター・チャンネル２","h627":"Ch.627 スター・チャンネル３","h628":"Ch.628 衛星劇場ＨＤ","h629":"Ch.629 東映チャンネルHD","h630":"Ch.630 洋画★シネフィル・イマジカHD","h631":"Ch.631 ザ・シネマHD","h632":"Ch.632 ムービープラスＨＤ","h633":"Ch.633 チャンネルNECO-HD","h634":"Ch.634 日本映画専門チャンネルＨＤ"},arService = {'s':'<img src="common/img/icon_channel_sd.gif" alt="[SD]" />','h':'<img src="common/img/icon_channel_hd.gif" alt="[HD]" />','e':'<img src="common/img/icon_channel_e2.gif" alt="[e2]" />'};
			while(rec=recs.shift()){
				pf='s';if(rec['service']=='スカパー！ｅ２')pf='e';else if(rec['service']=='スカパー！ＨＤ')pf='h';p=pf=='s'?'h':pf,pc=pf+rec['channel'];if(!chnm[pc])continue;
				prog[p].unshift({"title":rec['title'],"start":rec['start'],"end":rec['end'],"ch":pc,"bp":p});
				if(pf=='h'&&hd2bs[pc])prog['e'].unshift({"title":rec['title'],"start":rec['start'],"end":rec['end'],"ch":hd2bs[pc],"bp":'h'});
			}
			while(pf=pfs.shift()){
				h='<ul>';
				while(rec=prog[pf].shift()){
					var title=rec.title,ch=rec['ch'];
					if(title&&title!='インフォメーション'&&title.indexOf('番組情報')<0&&title.indexOf('番組案内')<0&&title.indexOf('番組紹介')<0&&title.indexOf('競馬実況')<0&&title.indexOf('スター・チャンネル ダイジェスト')<0&&!(ch=='s709'&&title.indexOf('ガイド')==0)){
						title=title.replace(/\[無料?\]/,"").replace("[映]","").replace("[HV]","").replace("[SS]","").replace("[HD]","").replace("[W]","");
						var sd=new Date(),ed=new Date();
						sd.setTime(rec['start']*1000);var sh=sd.getHours().toString(),sm=sd.getMinutes().toString();if(sm.length<=1)sm='0'+sm;
						ed.setTime(rec['end']*1000);var eh=ed.getHours().toString(),em=ed.getMinutes().toString();if(em.length<=1)em='0'+em;
						var pfx1=sh<4?'深':(sh<12?'前':'後'),pfx2=eh<4?'深':(eh<12?'前':'後');
						h+="<li>"+pfx1+(sh%12)+':'+sm+'～';if(pfx1!=pfx2)h+=pfx2;
						h+=(eh%12)+':'+em+'<br /><a style="color:#090;" href="http://bangumi.skyperfectv.co.jp/' +(rec['bp']=='e'?'e2':'HD')+'/query:'+encodeURI(title)+'/" target="_blank">'+escapeHTML(title)+'</a><br /><a href="'+(pf=='h'?"http://www.skyperfectv.co.jp/prog_navi/s":"http://www.e2sptv.jp/prog_navi/e")+ch.substring(1)+'.html" target="_blank">'+arService[ch.substring(0,1)]+' '+chnm[ch]+'</a><br /></li>';
					}
				}
				h+='</ul>';bangumi[pf]=h;
			}
			$('#tab_onair_e2').html(bangumi['e']);
			$('#tab_onair_hd').html(bangumi['h']);
		}
	});
}
function makeCal(){
	var sv=$(".cal_ch:checked").val();
	$("#cal").html("");
	$("#cal").calendar({
		navi:{ja:['','']},
		caption:{ja:'%M/%D-%nM/%nD'},
		hideOther:true,
		todayLink:{ja:''},
		addDay: function(td){
			var url='http://bangumi.skyperfectv.co.jp/'+sv+'/category:'+encodeURI('映画')+'/date:';
			var a=td.attr("id").split("-");
			td.html("<a href=\""+url+a[1].substr(2,2)+a[2]+a[3]+"\" target=\"_blank\">"+this.day+"</a>");
		}
	});
	$("#btnCalendar a").attr("href","http://bangumi.skyperfectv.co.jp/"+sv+"/query:"+encodeURI('映画')+"/");
}
function setKeywords(){
	var url="http://bangumi.skyperfectv.co.jp/"+$(".prog_ch:checked").val(),cat="";
	$(".keyword").each(function(){
		if($(this).attr("title")!="")cat="category:"+encodeURI($(this).attr("title"))+"/";
		var arg=url+"/"+cat+"query:"+encodeURI($(this).text())+"/";
		$(this).attr("href",arg);
	});
}
function buildList(data){
	var cnt=data['count'],max=cnt>5?5:cnt,html='<ul class="pickupList">',pick=new Array(),proc=0,rnd;
	while (proc<max){
		rnd=parseInt(Math.random()*cnt);
		if (!pick[rnd]){var o=data['data'][rnd];html+=o;pick[rnd]=1;proc++;}
	}
	html+="</ul>";
	return html;
}

$(function() {
	getPrograms();
	setKeywords();
	$(".prog_ch").click(function(){$("#prgSearchForm").attr("action","http://bangumi.skyperfectv.co.jp/"+$(".prog_ch:checked").val()+"/");setKeywords()});

	$.ajax({
		url: 'special/movies.js',
		dataType: 'json',
		success: function(data) {
			var cnt=data['count'],max=cnt>3?3:cnt;
			var html='',pick=new Array(),proc=0,rnd;
			while (proc<max){
				rnd=parseInt(Math.random()*cnt);
				if (!pick[rnd]){
					var o=data['data'][rnd];pick[rnd]=1;proc++;
					html+='<div class="ichioshi_box"><p class="ichioshi_img"><a href="special/#'+o['id']+'">'+o['photo']+'</a></p><div class="ichioshi_boxInner"><p class="ichioshi_txt">'+o['catch']+'</p><p class="ichioshi_ttl"><a href="special/#'+o['id']+'">'+o['title']+'</a></p><p class="ichioshi_style">'+(o['hd'] ? '<img src="common/img/ico_hd.gif" width="26" height="11" alt="" />' : '')+'</p><p class="ichioshi_copy">'+o['credit']+'</p></div></div>';
				}
			}
			$('#ichioshi_area').html(html);
		}
	});

	$.ajax({
		url: 'xml/genre_e2.js',
		dataType: 'json',
		success: function(data){$('#keyTab1List1').html(buildList(data));}
	});

	$.ajax({
		url: 'xml/genre_hd.js',
		dataType: 'json',
		success: function(data){$('#keyTab1List4').html(buildList(data));}
	});


});

