var lists = new Array();
var item = new Array();
var langCode = 1;

var topics

var fileName = '';
var categoryString;
var detailMax = 10;
var listMax = 25;
var isSortedByKana = false;
var midashiHTML = '';
var step = 5;

//navi btns setting
var prsntColor = '#f0f0f0';
var defColor = '#fafafa'
var prsntObj = null;

var bgcolor = new Array(2);
bgcolor[0] = 'w';
bgcolor[1] = 'g';

/*
var iconNewHTML = new Array(2);
iconNewHTML[0] = '';
iconNewHTML[1] = '<img src="../images/icon_new.gif" width="23" height="14" alt="new">';
*/


//コールバック関数 ( 受信時に実行されます )
  function on_loadedXML(oj)
  {
    //responseXMLでレスポンスを取得
    var xdoc  =  oj.responseXML
	
	//XMLを配列に変換します
	//XML内の"name"という名前のタグ(要素)の配列を作る
    lists = xdoc.getElementsByTagName("lists");
  
    //XML内の"name"という名前のタグ(要素)の配列を作る
    item = xdoc.getElementsByTagName("item");
	


	//カタログのカテゴリ
	//categoryString = lists[0].attributes[0].value;
	//if(categoryString=='ドラマ') {
	//	midashiHTML = '<img src="../images/midashi_dorama.gif" width="165" height="36" alt="ドラマ">';
	//}
	//else{
	//	midashiHTML = '<p>'+categoryString+'</p>';
	//}
	
	//kanaでソート
	//sortnameKana(sortedIndex);
	

    //詳細リストを出力
    //putoutDetail(0);
	//putoutList(0);
	
	//ニュースを出力
	putoutNews(0);
  }

  //送信用関数
  function loadFile(url)
  {
    sendRequest(on_loadedXML,'','GET',url,true,true) 
	fileName = url;
  }
  

  //ニュースを出力
  function putoutNews(n)
  {
	
	//スレッドの最大値
	var nfirst = n*step;
	var itemMax = nfirst+step;
	
	if(itemMax> item.length){
		itemMax = item.length;
	}
	
    //サマリーを生成
	
	var summaryHTML ='';
	
	summaryHTML += '<table id="summaryTable" cellpadding="0" cellspacing="0">';
	
	var html='';
	var nhtml = '';
	//"item"タグが出てきた順に処理します
    for( i = nfirst ; i < itemMax ; i++ )
    {
        //各要素内のfirstChildのデータだけを取り出す
		html += '<tr class="'+bgcolor[i%2]+'"><td class="title"><a href="#'+i+'">'+item[i].getElementsByTagName("date").item(0).firstChild.nodeValue +
				'</a></td><td class="summary"><a href="#'+i+'">'+item[i].getElementsByTagName("title").item(0).firstChild.nodeValue +
				'</a></td></tr>';
				
		nhtml+= '<a name="'+i+'"></a>'+
				'<div class="news_detail_'+item[i].getElementsByTagName("style").item(0).firstChild.nodeValue +'">'+
				'<div class="header"><h2><span class="date">'+item[i].getElementsByTagName("date").item(0).firstChild.nodeValue +'</span><br>'+
				item[i].getElementsByTagName("title").item(0).firstChild.nodeValue +'</h2></div>'+
				'<div class="body"><p>'+parseBrake(item[i].getElementsByTagName("text").item(0).firstChild.nodeValue) +'</p>'+
				'<div class="newsPhoto">';
				
		if(item[i].getElementsByTagName("photo1").item(0).childNodes.length>0){
		nhtml+= '<img src="images/'+item[i].getElementsByTagName("photo1").item(0).firstChild.nodeValue+'">';
		}
		if(item[i].getElementsByTagName("photo2").item(0).childNodes.length>0){
		nhtml+= '<img src="images/'+item[i].getElementsByTagName("photo2").item(0).firstChild.nodeValue+'">';
		}
		nhtml+= '</div>'+
				'</div>'+
				'<div class="clear"></div>'+
				'</div>';
    }
	
	summaryHTML += html;
	summaryHTML +='</table>';
	
	//出力
	document.getElementById("banner_summary").innerHTML=summaryHTML;
	document.getElementById("itemList").innerHTML=nhtml;
		if(n>0){
			document.getElementById("categoryName").innerHTML = '<p>過去のトピック</p>';
			document.getElementById("pageCtrl").innerHTML ='';
			if(n>1){
				if(itemMax!=item.length){
					document.getElementById("pageCtrl").innerHTML ='<a onclick="javascript:putoutNews('+eval(n-1)+');" href="javascript:;">前の7件</a>&nbsp;|&nbsp;<a onclick="javascript:putoutNews('+eval(n+1)+');" href="javascript:;">次の7件</a>';
				}else{
					document.getElementById("pageCtrl").innerHTML ='<a onclick="javascript:putoutNews('+eval(n-1)+');" href="javascript:;">前の7件</a>&nbsp;|&nbsp;次の7件';
				}
			}else{
				if(itemMax!=item.length){
					document.getElementById("pageCtrl").innerHTML ='前の7件&nbsp;|&nbsp;<a onclick="javascript:putoutNews('+eval(n+1)+');" href="javascript:;">次の7件</a>';
				}else{
					document.getElementById("pageCtrl").innerHTML ='';
				}
			}
		}
		else{
			document.getElementById("categoryName").innerHTML = '<p>トピックス</p>';
			document.getElementById("pageCtrl").innerHTML ='';
		}
		var menuHtml='';
		if(n==0){
			menuHtml = '<li class="category prsnt"><a class="abox" onclick="javascript:putoutNews(0);" href="javascript:;"><span class="str">トピックス</span></a></li>';
			menuHtml +='<li class="category"><a class="abox" onclick="javascript:putoutNews(1);" href="javascript:;"><span class="str">過去のトピック</span></a></li>';
		}else{
			menuHtml = '<li class="category"><a class="abox" onclick="javascript:putoutNews(0);" href="javascript:;"><span class="str">トピックス</span></a></li>';
			menuHtml +='<li class="category prsnt"><a class="abox" onclick="javascript:putoutNews(1);" href="javascript:;"><span class="str">過去のトピック</span></a></li>';
		}
	 	document.getElementById("menu").innerHTML=menuHtml;

  }  
  function changeBtnState(x){
	if(prsntObj != null){
		prsntObj.parentNode.parentNode.childNodes[0].style.backgroundColor= defColor;
	}
	else{
		document.getElementById("menu").childNodes[0].style.backgroundColor= defColor;
	}
	 x.parentNode.parentNode.childNodes[0].style.backgroundColor=prsntColor;
	 prsntObj = x;
  }




  //詳細を出力
  function putoutDetail(n)
  {
	
	//スレッドの最大値
	var itemMax = (n+1)*detailMax;
	if(itemMax > item.length) itemMax = item.length;
	
    //リストヘッダを生成
	
	//リンクテキスト
	var linkHTML = '<div class="pageLink">';
	if(n!=0)		{linkHTML += '<a class="btn_l" onClick="javascript:putoutDetail('+eval(n-1)+')" href="javascript:;">前のページへ</a> ';}
	else			{linkHTML += '';}
					linkHTML +='<div class="pageIndex">'
	for(i =0; i<(item.length/detailMax); i++){
		if(i==n)	{linkHTML += '<span class="number">'+eval(i+1)+'</span>';}
		else		{linkHTML += '<a class="number" onClick="javascript:putoutDetail('+i+')" href="javascript:;">'+eval(i+1)+'</a>';}
	}
					linkHTML +='</div>'
	if((n+1)!=Math.floor(item.length/detailMax)) 
					{linkHTML += '<a class="btn_r" onClick="javascript:putoutDetail('+eval(n+1)+')" href="javascript:;">次のページへ</a>';}
	else			{linkHTML += '';}
					linkHTML += '</div>';
	
    html = '<div id="listTop">';
	html +='<h2 id="categoryName">'+midashiHTML+'</h2>';
	html +='<div id="controlTop">'+linkHTML+'</div>';
	html +='<div class="clear"></div>';
	html +='</div>';
	html +='<div id="download"><img src="images/btn_download.gif" alt="download" width="323" height="18"></div>';
	
	//alert(item[0].getElementsByTagName("sample").item(0).firstChild.nodeValue != '');
    
    //"item"タグが出てきた順に処理します
    for( i = n*detailMax ; i < itemMax ; i++ )
    {
		 var sampleView = '';
		if(item[i].getElementsByTagName("sample").item(0).childNodes.length>0){
			
			var timeLength = '0';
			if(item[i].getElementsByTagName("sampleTime").item(0).childNodes.length>0){
				timeLength = item[i].getElementsByTagName("sampleTime").item(0).firstChild.nodeValue;
			}
			
			sampleView = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="272" height="178">'
						+'<param name="movie" value="player_hd.swf">'
						+'<param name="quality" value="high">'
						+'<PARAM name="flashvars" value="movieFile=flvs/'+ item[i].getElementsByTagName("sample").item(0).firstChild.nodeValue 
						+'&amp;photoFile=images/'+ item[i].getElementsByTagName("images").item(0).firstChild.nodeValue
						+'&amp;totalTime='+ timeLength +'">'
						+'<embed src="player_hd.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="272" height="178"'
						+'FLASHVARS="movieFile=flvs/'+ item[i].getElementsByTagName("sample").item(0).firstChild.nodeValue 
						+'&amp;photoFile=images/'+ item[i].getElementsByTagName("images").item(0).firstChild.nodeValue
						+'&amp;totalTime='+ timeLength +'"></embed>'
						+'</object>'
		}else{
			sampleView = '<img src="images/'+ item[i].getElementsByTagName("images").item(0).firstChild.nodeValue +'" width="272" height="153" alt="'+item[i].getElementsByTagName("name").item(0).firstChild.nodeValue+'">'
		}
		
        //各要素内のfirstChildのデータだけを取り出す
		html += '<!-- item ['+i+'] -->';
		html += '<div class="item_detail">';
		html += '<div class="info">';
        //html += '<h3 class="title">'+'['+i+']'+ item[i].getElementsByTagName("name").item(0).firstChild.nodeValue + '</h3>';
		 html += '<h3 class="title">'+item[i].getElementsByTagName("name").item(0).firstChild.nodeValue + iconNewHTML[item[i].getElementsByTagName("new").item(0).firstChild.nodeValue]+ '</h3>';

		//html += '<p class="data">'
		//		+ item[i].getElementsByTagName("length").item(0).firstChild.nodeValue +'/'
		//		+ item[i].getElementsByTagName("code").item(0).firstChild.nodeValue +'/'
		//		+ item[i].getElementsByTagName("lang").item(0).firstChild.nodeValue + '</p>';
		html += '<p class="descript">'+parseBrake(item[i].getElementsByTagName("info").item(0).firstChild.nodeValue) + '</p>';
        html += '</div>'
		html += '<div class="sampleView">'+sampleView+'</div>'
        html += '<div class="clear"></div>'
		html += '</div>'
    }
    
	//リンクテキスト
	html +='<div id="controlBottom">'+linkHTML+'</div>';
	html +='<div class="clear"></div>';
	html +='</div>';
	
	//出力
	document.getElementById("itemList").innerHTML=html
  }
  
  //リストを出力
  function putoutList(n)
  {
	
	//スレッドの最大値
	var itemMax = (n+1)*listMax;
	if(itemMax > item.length) itemMax = item.length;
	
    //リストヘッダを生成
	
	//リンクテキスト
	var linkHTML = '<div class="pageLink">';
	if(n!=0)		{linkHTML += '<a class="btn_l" onClick="javascript:putoutList('+eval(n-1)+')" href="javascript:;">前のページへ</a> ';}
	else			{linkHTML += '';}
					linkHTML +='<div class="pageIndex">'
	for(i =0; i<(item.length/listMax); i++){
		if(i==n)	{linkHTML += '<span class="number">'+eval(i+1)+'</span>';}
		else		{linkHTML += '<a class="number" onClick="javascript:putoutList('+i+')" href="javascript:;">'+eval(i+1)+'</a>';}
	}
					linkHTML +='</div>'
	if(n!=Math.floor(item.length/listMax)) 
					{linkHTML += '<a class="btn_r" onClick="javascript:putoutList('+eval(n+1)+')" href="javascript:;">次のページへ</a>';}
	else			{linkHTML += '';}
					linkHTML += '</div>';
	
    html = '<div id="listTop">';
	html +='<h2 id="categoryName">'+midashiHTML+'</h2>';
	html +='<div id="controlTop"><a class="btn_viewChange_detail" onClick="javascript:putoutDetail(0);" href="javascript:;">詳細表示</a>'+'<div class="btn_viewChange_sort_selected">五十音順表示</div>'+linkHTML+'</div>';
	html +='<div class="clear"></div>';
	html +='</div>';
	
	html +='<table id="listTable" cellpadding="0" cellspacing="0">'
	html +='<tr class="head">'+
			'<td class="title"><span class="tableHead">タイトル</span></td>'+
			'<td class="length"><span class="tableHead">時間</span></td>'+
			'<td class="code"><span class="tableHead">コード</span></td>'+
			'<td class="lang"><span class="tableHead">言語</span></td>'+
			'<td class="play_btn" colspan="2"><span class="tableHead">詳細</span></td></tr>'
    
    //"item"タグが出てきた順に処理します
	var m =0;
	var backgroundClass = 'g';
	
    for( i = n*listMax ; i < itemMax ; i++ )
    {
		if((m%2)==1)	backgroundClass = 'w';
		else			backgroundClass = 'g';
		m++;
		
		 var sampleView = '';
		 var playIconHTML = ' &nbsp;';
		if(sortedIndex[i].getElementsByTagName("sample").item(0).childNodes.length>0){
			//playIconHTML = '<a class="btnPlay" onClick="javascript:btnAction(this,'+i+')" href="javascript:;">再生</a>';
			playIconHTML = '<img src="../images/icon_play.gif" width="14px" height="24px">';
			var timeLength = '0';
			if(sortedIndex[i].getElementsByTagName("sampleTime").item(0).childNodes.length>0){
				timeLength = sortedIndex[i].getElementsByTagName("sampleTime").item(0).firstChild.nodeValue;
			}
			
			sampleView = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="240" height="204" class="sample" style="display:block">'
						+'<param name="movie" value="player.swf">'
						+'<param name="quality" value="high">'
						+'<PARAM name="flashvars" value="movieFile=flvs/'+ sortedIndex[i].getElementsByTagName("sample").item(0).firstChild.nodeValue 
						+'&amp;photoFile=images/'+ sortedIndex[i].getElementsByTagName("images").item(0).firstChild.nodeValue
						+'&amp;totalTime='+ timeLength +'">'
						+'<embed src="player.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="240" height="204"'
						+'FLASHVARS="movieFile=flvs/'+ sortedIndex[i].getElementsByTagName("sample").item(0).firstChild.nodeValue 
						+'&amp;photoFile=images/'+ sortedIndex[i].getElementsByTagName("images").item(0).firstChild.nodeValue
						+'&amp;totalTime='+ timeLength +'"></embed>'
						+'</object>'
		}else{
			sampleView = '<img src="images/'+ sortedIndex[i].getElementsByTagName("images").item(0).firstChild.nodeValue +'" width="240" height="180" alt="'+sortedIndex[i].getElementsByTagName("name").item(0).firstChild.nodeValue+'" class="sample">'
		}
		
        //各要素内のfirstChildのデータだけを取り出す
		//html += '<tr class="'+backgroundClass+'"><td class="title">'+backgroundClass+'['+i+']'+ sortedIndex[i].getElementsByTagName("name").item(0).firstChild.nodeValue +
		html += '<tr class="'+backgroundClass+'"><td class="title">'+sortedIndex[i].getElementsByTagName("name").item(0).firstChild.nodeValue + iconNewHTML[sortedIndex[i].getElementsByTagName("new").item(0).firstChild.nodeValue]+
				'</td>'+
				'<td class="length">'+ sortedIndex[i].getElementsByTagName("length").item(0).firstChild.nodeValue +'</td>'+
				'<td class="code">'+ sortedIndex[i].getElementsByTagName("code").item(0).firstChild.nodeValue +'</td>'+
				'<td class="lang">'+ sortedIndex[i].getElementsByTagName("lang").item(0).firstChild.nodeValue +'</td>'+
				'<td class="play">'+playIconHTML+'</td>'+
				'<td class="btn"><a class="openDetail" onClick="javascript:btnAction(this,'+i+')" href="javascript:;">[詳細]</a></td></tr>';
		html += '<tr class="'+backgroundClass+'_des"><td colspan="6"><div class="description">'+sampleView+
				parseBrake(sortedIndex[i].getElementsByTagName("info").item(0).firstChild.nodeValue) +'</div></td></tr>'
				
    }
    
	html += '</table>'
	
	//リンクテキスト
	html +='<div id="controlBottom"><a class="btn_viewChange_detail" onClick="javascript:putoutDetail(0);" href="javascript:;">詳細表示</a>'+'<div class="btn_viewChange_sort_selected">五十音順表示</div>'+linkHTML+'</div>';
	html +='<div class="clear"></div>';
	html +='</div>';
	
	//出力
	document.getElementById("itemList").innerHTML=html;
	
  }
  
  
  //よみがなでソート
  function sortnameKana(target){
	  s = new Array();
	  for (q=target.length ; q >= 0; q--){ 
		for (p=0 ; p < q-1; p++ ) { 
			if(target[p].getElementsByTagName("kana").item(0).firstChild.nodeValue > target[p+1].getElementsByTagName("kana").item(0).firstChild.nodeValue){ 
				s[0] = target[p+1]; 
				target[p+1] = target[p]; 
				target[p] = s[0];
			} 
		} 
	} 
	isSortedByKana = true;
  }
  
  //ボタンアクション
  function btnAction(x,n){
	  if(x.className=='openDetail' || x.className=='btnPlay') {
		//trace(x.parentNode.parentNode.nextSibling.firstChild.firstChild.firstChild.childNodes[3].nodeName);
		x.parentNode.parentNode.nextSibling.firstChild.firstChild.style.display = 'block';
		
		var sampleView = '';
		if(sortedIndex[n].getElementsByTagName("sample").item(0).childNodes.length>0){
			
			var timeLength = '0';
			if(sortedIndex[n].getElementsByTagName("sampleTime").item(0).childNodes.length>0){
				timeLength = sortedIndex[n].getElementsByTagName("sampleTime").item(0).firstChild.nodeValue;
			}
			
			playNow = 0;
			if(x.className=='btnPlay') playNow = 1;
			sampleView = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="240" height="204" class="sample">'
						+'<param name="movie" value="player.swf">'
						+'<param name="quality" value="high">'
						+'<PARAM name="flashvars" value="movieFile=flvs/'+ sortedIndex[n].getElementsByTagName("sample").item(0).firstChild.nodeValue 
						+'&amp;photoFile=images/'+ sortedIndex[n].getElementsByTagName("images").item(0).firstChild.nodeValue
						+'&amp;totalTime='+ timeLength +'&amp;playNow='+ playNow +'">'
						+'<embed src="player.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="240" height="204"'
						+'FLASHVARS="movieFile=flvs/'+ sortedIndex[n].getElementsByTagName("sample").item(0).firstChild.nodeValue 
						+'&amp;photoFile=images/'+ sortedIndex[n].getElementsByTagName("images").item(0).firstChild.nodeValue
						+'&amp;totalTime='+ timeLength +'&amp;playNow='+ playNow +'"></embed>'
						+'</object>'
			
		}else{
			sampleView = '<img src="images/'+ sortedIndex[n].getElementsByTagName("images").item(0).firstChild.nodeValue +'" width="240" height="180" alt="'+sortedIndex[n].getElementsByTagName("name").item(0).firstChild.nodeValue+'" class="sample">'
		}
		dumpImg(sampleView);
		dumpClear();		
		
		var HTML = sampleView+parseBrake(sortedIndex[n].getElementsByTagName("info").item(0).firstChild.nodeValue);
		
		


		x.parentNode.parentNode.nextSibling.firstChild.firstChild.innerHTML = HTML;
		//x.parentNode.parentNode.nextSibling.firstChild.firstChild.firstChild.childNodes[3].style.height = '250';
		
		x.parentNode.parentNode.childNodes[5].innerHTML = '<a class="closeDetail" onClick="javascript:btnAction(this,'+n+')" href="javascript:;">[閉る]</a>' ; 
		//trace(x.parentNode.parentNode.childNodes[5].nodeName);
		//if(x.className=='btnPlay'){
		//	x.parentNode.parentNode.childNodes[1].innerHTML = '<a class="btnStop" onClick="javascript:btnAction(this,'+n+')" href="javascript:;">停止</a>' ; 
		//}
		
	  }
	  else if(x.className=='closeDetail') {
		x.parentNode.parentNode.nextSibling.firstChild.firstChild.style.display = 'none';
		if(sortedIndex[n].getElementsByTagName("sample").item(0).childNodes.length>0){
		//x.parentNode.parentNode.childNodes[1].innerHTML = '<a class="btnPlay" onClick="javascript:btnAction(this,'+n+')" href="javascript:;">再生</a>' ; 
		}
		x.parentNode.parentNode.childNodes[5].innerHTML = '<a class="openDetail" onClick="javascript:btnAction(this,'+n+')" href="javascript:;">[詳細]</a>' ; 
	  }
	  else if(x.className=='btnStop' ) {
		x.parentNode.parentNode.nextSibling.firstChild.firstChild.style.display = 'none';
		x.parentNode.parentNode.childNodes[5].innerHTML = '<a class="openDetail" onClick="javascript:btnAction(this,'+n+')" href="javascript:;">[詳細]</a>' ; 
		//x.parentNode.parentNode.childNodes[1].innerHTML = '<a class="btnPlay" onClick="javascript:btnAction(this,'+n+')" href="javascript:;">再生</a>' ; 
//		trace(x.parentNode.parentNode.childNodes[1].innerHTML);
	  }
		  
  }
  
  function trace(x){
	  document.getElementById("consol").innerHTML +='<br>'+x;
  }
  
  function dumpImg(x){
	  document.getElementById("dump").innerHTML +=x;
  }
  function dumpClear(){
	  document.getElementById("dump").innerHTML ="";
  }
  
  function printState(x){
	  document.getElementById("status").innerHTML =x;
  }
  
  function changeBtnState(x){
	if(prsntObj != null){
		prsntObj.parentNode.parentNode.childNodes[0].style.backgroundColor= defColor;
	}
	else{
		document.getElementById("menu").childNodes[0].style.backgroundColor= defColor;
	}
	 x.parentNode.parentNode.childNodes[0].style.backgroundColor=prsntColor;
	 prsntObj = x;
  }

