function checkChannel(){
  clearStat();
  var useChecker = document.getElementsByName("useChecker");
  if(useChecker[0].checked){
    $.getJSONP("http://api.ustream.tv/json/channel/sibuyaking10/getValueOf/status?&callback={callback}", function(i) {if(i == "live"){document.stat_king.src="/img/live.gif";}});
    $.getJSON("http://api.justin.tv/api/stream/list.json?channel=sibuyaking100&jsonp=?",  function(l){if(l[0].stream_type == 'live'){document.stat_king.src="/img/live.gif";}});
    $.getJSON("http://api.justin.tv/api/stream/list.json?channel=tks_paneeeee&jsonp=?",  function(l){if(l[0].stream_type == 'live'){document.stat_tks.src="/img/live.gif";}});
    //$.getJSON("http://api.justin.tv/api/stream/list.json?channel=kansai_knight&jsonp=?",  function(l){if(l[0].stream_type == 'live'){document.stat_knight.src="/img/live.gif";}});
    $.getJSONP("http://api.ustream.tv/json/channel/kansaiknight/getValueOf/status?&callback={callback}", function(i) {if(i == "live"){document.stat_knight.src="/img/live.gif";}});
	$.getJSON("http://api.justin.tv/api/stream/list.json?channel=hatoyatv&jsonp=?",      function(l){if(l[0].stream_type == 'live'){document.stat_hato.src="/img/live.gif";}});
    //$.getJSON("http://api.justin.tv/api/stream/list.json?channel=syossy&jsonp=?",        function(l){if(l[0].stream_type == 'live'){document.stat_syossy.src="/img/live.gif";}});
    //$.getJSON("http://api.justin.tv/api/stream/list.json?channel=sibuya_piero&jsonp=?",        function(l){if(l[0].stream_type == 'live'){document.stat_piero.src="/img/live.gif";}});
    //$.getJSON("http://api.justin.tv/api/stream/list.json?channel=sibuya_ceo&jsonp=?",        function(l){if(l[0].stream_type == 'live'){document.stat_ceo.src="/img/live.gif";}});
    $.getJSONP("http://api.ustream.tv/json/channel/sibuyaceos/getValueOf/status?&callback={callback}", function(i) {if(i == "live"){document.stat_ceo.src="/img/live.gif";}});
    $.getJSON("http://api.justin.tv/api/stream/list.json?channel=jtv_tein&jsonp=?",        function(l){if(l[0].stream_type == 'live'){document.stat_ebikun.src="/img/live.gif";}});
    $.getJSON("http://api.justin.tv/api/stream/list.json?channel=hayasu&jsonp=?",        function(l){if(l[0].stream_type == 'live'){document.stat_hayasu.src="/img/live.gif";}});
    $.getJSON("http://api.justin.tv/api/stream/list.json?channel=hisyamoto01&jsonp=?",        function(l){if(l[0].stream_type == 'live'){document.stat_hisya.src="/img/live.gif";}});
    $.getJSON("http://api.justin.tv/api/stream/list.json?channel=begomasamune&jsonp=?",        function(l){if(l[0].stream_type == 'live'){document.stat_mimizu.src="/img/live.gif";}});
    //$.getJSON("http://api.justin.tv/api/stream/list.json?channel=korekorekkusu&jsonp=?",        function(l){if(l[0].stream_type == 'live'){document.stat_korekore.src="/img/live.gif";}});
    //$.getJSONP("http://api.ustream.tv/json/channel/korekorekkusu/getValueOf/status?&callback={callback}", function(i) {if(i == "live"){document.stat_korekore.src="/img/live.gif";}});
    $.getJSONP("http://api.ustream.tv/json/channel/korekorebantan/getValueOf/status?&callback={callback}", function(i) {if(i == "live"){document.stat_korekore.src="/img/live.gif";}});
    $.getJSON("http://api.justin.tv/api/stream/list.json?channel=pandagahaku&jsonp=?",        function(l){if(l[0].stream_type == 'live'){document.stat_gahaku.src="/img/live.gif";}});
    $.getJSONP("http://api.ustream.tv/json/channel/suepeko/getValueOf/status?&callback={callback}", function(i) {if(i == "live"){document.stat_suepeko.src="/img/live.gif";}});

    getUpdateTime();
  }
}

function setLoop(){
  setInterval('checkChannel()',60000);
}

function clearStat(){
  document.stat_king.src = "/img/offline.gif";
  document.stat_tks.src = "/img/offline.gif";
  document.stat_knight.src = "/img/offline.gif";
  document.stat_hato.src = "/img/offline.gif";
  //document.stat_syossy.src = "/img/offline.gif";
  //document.stat_piero.src = "/img/offline.gif";
  document.stat_ceo.src = "/img/offline.gif";
  document.stat_ebikun.src = "/img/offline.gif";
  document.stat_hayasu.src = "/img/offline.gif";
  document.stat_hisya.src = "/img/offline.gif";
  document.stat_mimizu.src = "/img/offline.gif";
  document.stat_korekore.src = "/img/offline.gif";
  document.stat_gahaku.src = "/img/offline.gif";
  document.stat_suepeko.src = "/img/offline.gif";
  $('#update').html('ステータス更新日時：--/--(---) --:--:--');
}

function getUpdateTime(){
Week = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
ND= new Date();
month = ND.getMonth() + 1;
day  = ND.getDate();
hour = ND.getHours();
min  = ND.getMinutes();
sec  = ND.getSeconds();
if(month < 10) month = '0' + month;
if(day < 10) day = '0' + day;
if(min < 10) min = '0' + min;
if(sec < 10) sec = '0' + sec;
$('#update').html('ステータス更新日時：' + month + '/' + day + '(' + Week[ND.getDay()] + ') ' + hour + ':' + min + ':' + sec);
}

function resizeStream(w,h){
	$('#wStream').width(w).height(h);
	$('#wStream').contents().find('object').width(w).height(h);
	obj = document.getElementById('mChat');
	obj.style.left = w + 'px';
	obj = document.getElementById('main');
	obj.style.width = $('#wChat').width() + $('#wStream').width() + 'px';
	obj = document.getElementById('mFloor');
	obj.style.width = $('#wChat').width() + $('#wStream').width() + 'px';
	obj = document.getElementById('mStream2');
	obj.style.top = h + 30 + 'px';
}

function resizeChat(w,h){
	$('#wChat').width(w).height(h);
	$('#wChat').contents().find('embed').width(w).height(h);
	obj = document.getElementById('main');
	obj.style.width = $('#wChat').width() + $('#wStream').width() + 'px';
	obj = document.getElementById('mFloor');
	obj.style.width = $('#wChat').width() + $('#wStream').width() + 'px';
}

function setStream(){
	var curStreamId = document.formNextStream.streamId.value;
	var curStreamType;
	var curChatId = document.formCurStream.curChatId.value;
	var curChatType = document.formCurStream.curChatType.value;
	var curUrl = "http://yellowbusters.com/";
	
	document.formCurStream.curStreamId.value = curStreamId;
    var nextStreamType = document.getElementsByName("streamType");
    if(nextStreamType[1].checked){
    	document.formCurStream.curStreamType.value = "u";
    	curStreamType = "u";
    }else{
    	document.formCurStream.curStreamType.value = "j";
    	curStreamType = "j";
    }
    
    if(curStreamId || curChatId){
    	curUrl += "v/" + curStreamId + "/" + curChatId + "/";
    }
    if(curStreamType == "u"){
    	curUrl += "u";
    }
     if(curChatType == "j"){
    	curUrl += "/j";
    }
       
    document.formUrl.url.value = curUrl;
    
    viewStream();
}

function viewStream(){
    var streamType = document.formCurStream.curStreamType.value;
	var ch = document.formCurStream.curStreamId.value;
    if(streamType == "u"){
    	ch += '/u';
    	resizeStream(640,390);
    }else{
    	resizeStream(640,360);
    }
	var streamURL = 'http://yellowbusters.com/p/stream/' + ch;
	//document.wStream.location.href = streamURL;
	document.getElementById("wStream").src = streamURL;
}



window.onload = function() {
  checkChannel();
  setLoop();
};

