/////らじる★らじる 共通JS/////

//基本制御
/*font size*/
var fontsizeA='12px';var fontsizeB='x-small';var fontsizeC='small';
	var vNum = navigator.appVersion.charAt(0);
	var bName = navigator.appName.charAt(0);
	document.writeln('<STYLE TYPE="text/css"><!--');
	if(navigator.appVersion.indexOf('Mac') > -1){basefontsize = fontsizeA;}
	else if(navigator.userAgent.indexOf('MSIE 5')>-1){basefontsize = fontsizeB;}
	else if(navigator.userAgent.indexOf('MSIE 6')>-1){basefontsize = fontsizeB;}
	else if(navigator.userAgent.indexOf('MSIE 7')>-1){basefontsize = fontsizeC;}
	else if(navigator.userAgent.indexOf('MSIE 8')>-1){basefontsize = fontsizeA;}
	else if(navigator.userAgent.indexOf('MSIE 9')>-1){basefontsize = fontsizeA;}
	else if(navigator.userAgent.indexOf('MSIE 10')>-1){basefontsize = fontsizeA;}
	else{basefontsize = bName == "M" ? fontsizeB : fontsizeA ;}
	document.writeln('body,th,td{font-size:'+basefontsize+';}');
	document.writeln('--></STYLE>');

//プレイヤーからOPENER
function openRADIRU(n){
	if(n==7)openerurl='../faq.html#q7';
		else if(n==3)openerurl='../hensei.html';
		else openerurl='../index.html';
	tgtwindow=navigator.userAgent.indexOf('Chrome')!=-1?'':'RR';
	var parentwin=window.open(openerurl,tgtwindow);
	parentwin.focus();
	}

//プレイヤー小画面open
function RRopen(ch){
	var RRwin=window.open('./player/index.html?'+ch,'rrwin','width=265,height=640,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	RRwin.focus();
	}

//onload関数
if(window.addEventListener){window.addEventListener("load",onloadFuncs,false);}
	else{window.attachEvent("onload",onloadFuncs);}
function onloadFuncs(){
	//TOPページ「お知らせ」
	if(document.getElementById('RRBTN3')){ //=index
		if($('#oshirase li').length)return false; //for firefox cache
		window.name='RR';
		$.ajax({
			url: "./files/include/oshirase.txt?"+Math.floor(Math.random()*1000),
			success: function(lis){
				if(lis.indexOf('</li>')>-1){
					$('#oshirase').append(lis);
					$('#oshiraseunit').prepend('<h2>お知らせ</h2>');
					$('#oshiraseunit').fadeIn(250);
					}
				else{$('#oshiraseunit').remove();}
				}
			});
		set3iframe();
		}
	//プレイヤー小画面
	else if(document.getElementById('playerUNIT')){ //=player
		//mac chromeだけ横幅操作
		if( ((navigator.userAgent.indexOf('Mac')>-1)&&(navigator.userAgent.indexOf('Chrome') != -1)) ||  ((navigator.userAgent.indexOf('Safari')>-1)&&(navigator.userAgent.indexOf('Chrome') == -1))  ){
			window.resizeTo(262,700);
			}
		}
	else if(document.getElementById('henseisrc'))return false;
	else if(document.getElementById('detailsrc'))return false;
	
	if(document.getElementById('ifTgt1'))setLUD();
	noanr();
	}

//ただいま放送中API
var exquery='';
function noanr(){
	//
	exquery='';
	if(navigator.userAgent.indexOf("MSIE")!=-1){}
	else if(navigator.userAgent.indexOf("Firefox")!=-1){}
	else if(navigator.userAgent.indexOf("Safari")!=-1&&navigator.userAgent.indexOf("Chrome")==-1){exquery='&t='+Math.floor(Math.random()*10000000000)}
	else if(navigator.userAgent.indexOf("Chrome")!=-1){exquery='&t='+Math.floor(Math.random()*10000000000)}
	$.ajaxSetup({cache:true});
	$.ajax({type:"GET", url:noanrpath+exquery, dataType:"script"});
	}
var noanrpath='http://cgi4.nhk.or.jp/tvnavi/api/noa-nr.cgi?mode=jsonp';

//ただいま放送中
var timerID;
var ignore1st=navigator.userAgent.indexOf('Firefox\/3')>-1 ? 0:1;
var trycount=3;
var items;
var starts_ends={};
function nowonair(json){
	items = json['items'];
	ima=new Date();
	//開始と終了時間リスト作成
	for(var i in items){
		tmp=items[i].starttime.split('-');
		tmp2=items[i].endtime.split('-');
		starts_ends[i]=new Array(new Date(tmp[0]+'/'+parseInt(tmp[1],10)+'/'+tmp[2]),new Date(tmp2[0]+'/'+parseInt(tmp2[1],10)+'/'+tmp2[2]));
		}
		
	//ヘッダ1行「ただいま放送中」
	//格納
	if(document.getElementById('NOA')){
		//表示順ランダム
		delaycontrol=new Array(0,250,500);randomdc=[];
		for(i=delaycontrol.length;i>0;i--){
			tmp=Math.floor(Math.random()*(delaycontrol.length));
			tmp=tmp==3?tmp=0:tmp;
			randomdc[i-1]=delaycontrol[tmp];delaycontrol.splice(tmp,1)
			}
		$('#noaR1').html(starts_ends[0][0]<ima?'<a href="javascript:RRopen(\'r1\');">'+cutText(items[0].title,30)+'</a>':'').delay(randomdc[0]).fadeIn(800);
		$('#noaR2').html(starts_ends[2][0]<ima?'<a href="javascript:RRopen(\'r2\');">'+cutText(items[2].title,30)+'</a>':'').delay(randomdc[1]).fadeIn(800);
		$('#noaFM').html(starts_ends[4][0]<ima?'<a href="javascript:RRopen(\'fm\');">'+cutText(items[4].title,30)+'</a>':'').delay(randomdc[2]).fadeIn(800);
		}
	//プレイヤー小画面
	else if(document.getElementById('playerUNIT')){
		if(ignore1st>0)setItems();
		ignore1st=1;
		}

	endtimes=new Array(starts_ends[0][1],starts_ends[2][1],starts_ends[4][1]);
	endtimes.sort();

	//直近放送終了が現在より前だったら60秒後にリトライ
	if(endtimes[0]-ima>0){
		offset=Math.floor(Math.random()*60)+1;
		targettime=endtimes[0]-ima+(offset*1000);
		trycount=3;
		}
	else{
		if(trycount>0){
			targettime=60000;
			trycount--;
			}
		else{
			clearInterval(timerID); return false;
			}
		}
	timerID=setInterval( function(){
		noanr();
		clearInterval(timerID);
		}, targettime)
	}
//小画面items格納
var chName=new Array('R1','R2','FM');
function setItems(){
	if(ch=='r2')chord=new Array(2,4,0);
	else if(ch=='fm')chord=new Array(4,0,2);
	else chord=new Array(0,2,4);
	//内容クリア
	$('#T1').html('');$('#H1').html('');$('#D1').html('');$('#L1').html('');
	$('#P1').html('');$('#T2').html('');$('#H2').html('');$('#T3').html('');$('#H3').html('');
	document.getElementById('C2').className='otherch';
	document.getElementById('C3').className='otherch';
	//格納
	if(starts_ends[chord[0]][0]<ima){
		$('#T1').html(timeToText(starts_ends[chord[0]][0])+'〜'+timeToText(starts_ends[chord[0]][1])+' ('+Math.floor((starts_ends[chord[0]][1]-starts_ends[chord[0]][0])/(60*1000))+'分)');
		$('#H1').html(items[chord[0]].title);

		tmp_subtitle=items[chord[0]].subtitle+'<br />';
		tmp_content=items[chord[0]].content!=''?items[chord[0]].content+'<br />':'';
		tmp_act=items[chord[0]].act!=''?items[chord[0]].act+'<br />':'';
		tmp_music=items[chord[0]].music!=''?items[chord[0]].music+'<br />':'';
		tmp_free=items[chord[0]].free;
		tmp_detailText=items[chord[0]].free!=''?items[chord[0]].free:tmp_subtitle+tmp_content+tmp_act+tmp_music;
		tmp_link=items[chord[0]].link;

		$('#D1').html('<pre>'+tmp_detailText+'</pre>');
		if(tmp_link!=''){
			$('#L1').html('<a href="'+tmp_link+'" target="_blank"><img src="../files/img/homepage.gif" width="178" height="27" alt="番組ホームページ" /></a>');
			}
			else{$('#L1').html('')}
		$('#P1').html('<a href="http://nhk.jp/'+ch+'/" target="_blank"><img src="../files/img/to_'+ch+'top.gif" width="178" height="27" alt="'+chName[chord[0]/2]+'トップページ" /></a>');
		}

	document.getElementById('C2').className=chName[chord[1]/2]+' otherch';
	if(starts_ends[chord[1]][0]<ima){
		$('#T2').html(timeToText(starts_ends[chord[1]][0])+'〜'+timeToText(starts_ends[chord[1]][1])+' ('+Math.floor((starts_ends[chord[1]][1]-starts_ends[chord[1]][0])/(60*1000))+'分)');
		$('#H2').html('<a href="./index.html?'+(chName[chord[1]/2].toLowerCase())+'">'+items[chord[1]].title+'</a>');
		}
	else{$('#H2').html('　');}
		document.getElementById('C3').className=chName[chord[2]/2]+' otherch';
	if(starts_ends[chord[2]][0]<ima){
		$('#T3').html(timeToText(starts_ends[chord[2]][0])+'〜'+timeToText(starts_ends[chord[2]][1])+' ('+Math.floor((starts_ends[chord[2]][1]-starts_ends[chord[2]][0])/(60*1000))+'分)');
		$('#H3').html('<a href="./index.html?'+(chName[chord[2]/2].toLowerCase())+'">'+items[chord[2]].title+'</a>');
		}
	else{$('#H3').html('　');}
	}

//文字数カット
function cutText(t,cnt){
	if(t.length>cnt){t=t.substr(0,cnt-1)+'…'}
	return t;
	}
//日付フォーマット変更（x時x分）
var week=new Array('日','月','火','水','木','金','土');
function timeToText(d){
	t1=d.getHours()<12?'午前'+d.getHours() : '午後'+(d.getHours()-12);
	t2=d.getMinutes()<10?'0'+d.getMinutes() : d.getMinutes();
	tt=t1+':'+t2+'';
	return tt;
	}

//番組表を更新
var day_flag=0;
var obi_flag=0;
function set3iframe(){
	for(i=0;i<3;i++){
		document.getElementById('ifTgt'+(i+1)).innerHTML='<iframe src="./hensei/'+(chName[i].toLowerCase())+'.html?d='+day_flag+'&o='+obi_flag+'" frameborder="0" marginwidth="0" marginheight="0" noresize></iframe>';
		}
	setLUD();
	}
function setLUD(){
	d=new Date();
	$('#lud').html('最終更新：'+(d.getMonth()+1)+'月'+d.getDate()+'日('+week[d.getDay()]+') '+timeToText(d)+'&nbsp;');
	}
var ima=new Date();
