// JavaScript Document
var serverTime = new Date();
var eachList = 16;
// JavaScript Document
function hideThis(divId){
	document.getElementById("columnText"+divId).style.display = "none";
	document.getElementById("columnHide"+divId).style.display = "block";
}

function showThis(divId){
	for(var i =0 ; i <3 ; i ++){
		if(divId == i){
			document.getElementById("columnText"+i).style.display = "block";
			document.getElementById("columnHide"+i).style.display = "none";
		}else{
			hideThis(i);
		}
	}
}

var marqueeContent=new Array();

var marqueeInterval=new Array();
var marqueeId=0;
var marqueeDelay=2000;
var marqueeHeight=18;
function initMarquee() {
    var str=marqueeContent[0];
	if(marqueeContent.length > 1 ){
		document.write('<div id="marqueeBox" style="overflow:hidden;width:400px;height:'+marqueeHeight+'px" onmouseover="clearInterval(marqueeInterval[0])" onmouseout="marqueeInterval[0]=setInterval(\'startMarquee()\',marqueeDelay)"><div>'+str+'</div></div>');
		marqueeId++;
		marqueeInterval[0]=setInterval("startMarquee()",marqueeDelay);
	}else if(marqueeContent.length == 1){
		document.write('<div id="marqueeBox" style="overflow:hidden;width:400px;height:'+marqueeHeight+'px" onmouseover="clearInterval(marqueeInterval[0])" onmouseout="marqueeInterval[0]=setInterval(\'startMarquee()\',marqueeDelay)"><div>'+str+'</div></div>');
	}
}
function startMarquee() {
    var str=marqueeContent[marqueeId];
    marqueeId++;
    if(marqueeId>=marqueeContent.length) marqueeId=0;
    if(document.getElementById("marqueeBox").childNodes.length==1) {
    var nextLine=document.createElement('DIV');
    nextLine.innerHTML=str;
    document.getElementById("marqueeBox").appendChild(nextLine);
    }
    else {
        document.getElementById("marqueeBox").childNodes[0].innerHTML=str;
        document.getElementById("marqueeBox").appendChild(document.getElementById("marqueeBox").childNodes[0]);
        document.getElementById("marqueeBox").scrollTop=0;
    }
    clearInterval(marqueeInterval[1]);
    marqueeInterval[1]=setInterval("scrollMarquee()",20);
}
function scrollMarquee() {
    document.getElementById("marqueeBox").scrollTop++;
    if(document.getElementById("marqueeBox").scrollTop%marqueeHeight==(marqueeHeight-1)){
    clearInterval(marqueeInterval[1]);
    }
}

var rpTmp = "<div class=\"list\"><table width=\"190\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">"
		  + "  <tr title=\"#3#\">"
		  + "    <td width=\"15\"><img src=\"images/icon1.gif\" width=\"10\" height=\"10\" /></td>"
		  + "    <td width=\"155\" align=\"left\">#0#</td>"
		  + "    <td width=\"20\" align=\"left\"><a href=\"vod_detail.html?s=#1#&t=#2#\" title=\"#3#\"><img src=\"images/icon_vod.gif\" width=\"17\" height=\"17\" border=\"0\" /></a></td>"
		  + "  </tr>"
		  + "</table>"
		  + "</div>";

function initRP(tid){

	$.ajax({
		   url: 'xml/'+tid+'.xml',
		   dataType: 'xml',
		   cache: false,
		   success: function(xml){
			   var appendTxt = "";
			   var n = 0;
			   $(xml).find("item").each(function(){
					if(n==0){
						$("#HiPlayer").attr("src" , "http://hichannel.hinet.net/player/iPlayer_vod.jsp?id="+$(this).find("mediaID").text()+"&channel=2009wg");
					}
					if(n<7){
						var media_id = $(this).find("mediaID").text();
						var media_title = $(this).find("title").text();
						appendTxt+= messageFormat(rpTmp , new Array(media_title , media_id , "1" , media_title));
						n++;
					}else{
						return false;
					}
				});
			   	$('#rpList').append(appendTxt);
			   }
		   });
}

function initVodPlay(mid , tid){
	var thisPage = 1;
	var tagNum = 1;
	for(var z in vodTheme){
		if(vodTheme[z] == tid){
			tagNum = parseInt(z , 10) + 1;
		}
	}
	$.ajax({
		   url: 'xml/'+tid+'.xml',
		   dataType: 'xml',
		   cache: false,
		   success: function(xml){
			   var getMedia = false;
			   $(xml).find("item").each(function(i){
					var media_id = $(this).find("mediaID").text();
					if(media_id == mid){
						var media_title = $(this).find("title").text();
						var media_eng_title = $(this).find("eng_title").text();
						var media_content = $(this).find("content").text();
						$("#HiPlayer").attr("src" , "http://hichannel.hinet.net/player/iPlayer_vod.jsp?id="+media_id+"&channel=2009wg");
						$("#media_title").text(media_title);
						$("#eng_title").text(media_eng_title);
						$("#media_content").text(media_content);
						getMedia = true;
						thisPage = Math.ceil(i/eachList);
						return false;
					}
				});
			   
			   if(!getMedia){
			   	location.href="/";
			   }
			   }
		   });
	$('#cell'+tagNum).attr('class' , 'active');
	initFullVod(tid , thisPage);
}

var nlTmp = '<li><a href="http://sports.hinet.net/2009wg/news_detail.jsp?s=#0#" class="title_black12" title="#2#" >#1#</a></li>';
function initRNewsList(){

	var nl = $$newList.items;
	var appendTxt = "";
	for(var n=0; n < nl.length; n++){
		appendTxt+= messageFormat(nlTmp , new Array(nl[n].sn, nl[n].title.cutTo(20) , nl[n].title));
	}
	$('#newsList').append(appendTxt);
}

var vodTmp  = '<li>'
			+'    	<a href="vod_detail.html?t=#0#&s=#1#" title="#4#"><img src="#2#" width="140" height="76" border="0" align="absmiddle" class="pic1" /></a>'
			+'        <div><span class="title_lightB12">片名：</span><a href="vod_detail.html?t=#0#&s=#1#" title="#4#" class="title_black12">#3#</a></div>'
			+'    </li>';
			
function initFullVod(tid , startPage){
	$.ajax({
		   url: 'xml/'+tid+'.xml',
		   dataType: 'xml',
		   cache: false,
		   success: function(xml){
			   $('#vodList').html('<img src="images/loading.gif" />');
			   var appendTxt = "";
			   var startNum = (startPage-1)*eachList
			   var n = startNum + eachList -1;
			   var cid = 1;
			   if(tid == 17800){
			   	cid=2;
			   }
			   $(xml).find("item").each(function(i){
					if(i>=startNum && i <= n){
						var media_id = $(this).find("mediaID").text();
						var media_title = $(this).find("title").text();
						var media_img = $(this).find("images_2").text();
						appendTxt+= messageFormat(vodTmp , new Array( cid , media_id , media_img , media_title.cutTo(28) , media_title));
					}
				});
			    if(appendTxt != ""){
					$('#vodList').html('');
				}
			   	$('#vodList').append(appendTxt);
				if($(xml).find("item").length > eachList){
					var totalPage = Math.ceil($(xml).find("item").length/eachList);
					var pageAppend = '';
					if(startPage == 1){
						pageAppend += '<img src="images/arr_redl.gif" width="11" height="13" border="0" />上一頁 | ';
					}else{
						pageAppend += '<a href="javascript:void(0);" onclick="initFullVod(' + tid + ' ,' + (startPage-1) + ')"><img src="images/arr_redl.gif" width="11" height="13" border="0" />上一頁</a> |';
					}
					for(var n=1; n<= totalPage; n++){
						if(n != startPage){
							pageAppend += ' <a href="javascript:void(0);" onclick="initFullVod(' + tid + ' ,' + n + ')">' + n +'</a> ';
						}else{
							pageAppend += ' <strong class="red15">' + n + '</strong> ';
						}
					}
					
					if(startPage == totalPage){
						pageAppend += '| 下一頁<img src="images/arr_redr.gif" width="11" height="13" border="0" />';
					}else{
						pageAppend += '| <a href="javascript:void(0);" onclick="initFullVod(' + tid + ' ,' + (startPage+1) + ')">下一頁<img src="images/arr_redr.gif" width="11" height="13" border="0" /></a>';
					}
					$("#vodPageOpt").html(pageAppend);
				}else{
					$("#vodPageOpt").html('<strong class="red15">1</strong>');
				}
				
			   },
			   error: function(){$('#vodList').html('<img src="images/loading.gif" />');}
		   });
}

function initRightArena(){
	var rightArena = $$rightArea.items;
	$('#rightArena').append(rightArena[0].right_area);
}

var liveTmp = '<ul #5#>'
            + ' 	<li id="wiH40">#0#</li>'
            + '     <li id="wiH40">#1#</li>'
            + '     <li id="wiH190">#2#<div>#3#</div></li>'
            + '     <li id="wiH20">#4#</li>'
			+ '</ul>';
var liveIcon = '<a href="#1#?c=#0#"><img src="images/icon_live.gif" width="19" height="16" border="0" /></a>';
function initLiveBar(){
	var lives = $$liveList.items;
	if(lives[0].live=='T'){
		var item = lives[0].schedule;
		$(item).each(function(i){
			var showIcon = ""
			if(this.signal != ''){
				if(parseInt(this.signal , 10)>31){
					showIcon = messageFormat(liveIcon , new Array((parseInt(this.signal,10) -31) , 'live.html'));
				}else{
					showIcon = messageFormat(liveIcon , new Array(this.signal+"&t="+this.type_sn , 'liveChannel.html'));
				}		
			}
			$('#liveList').append(messageFormat( liveTmp , new Array(this.date , this.stime , this.type , this.stype , showIcon , i%2==1?'class="bgGray"':'') ));
		});
		if(item.length >0){
			$('#liveBar').show();
		}
	}
}

var RightLiveListTmp = "<div class=\"list\"><table width=\"190\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">"
		  + "  <tr>"
		  + "    <td width=\"15\"><img src=\"images/icon1.gif\" width=\"10\" height=\"10\" /></td>"
		  + "    <td align=\"left\">#0#</td>"
		  + "  </tr>"
		  + "  <tr>"
		  + "    <td width=\"15\"></td>"
		  + "    <td align=\"left\" class=\"txt_orch\">#1#</td>"
		  + "  </tr>"
		  + "</table>"
		  + "</div>";
		  
function initRightLiveList(cid){
	var liveList = $$dataList.items;
	
	if(liveList.length > 0){
		
		var item = liveList[cid-1].schedule;
		if(item.length > 0){
			$(item).each(function(i){
				$('#liveList').append(messageFormat( RightLiveListTmp , new Array(this.type +  (this.stype.trim() != ""?" - " : "") + this.stype ,  "2009/" + this.date  + " " + this.stime )));
			});
		}else{
			$('#liveList').append("<font color=\"white\">敬請期待</font>");
		}	
	}
	
	if(cid==2){
		$("#HiPlayer").attr("src" , "http://hichannel.hinet.net/player/iPlayer_live.jsp?id=606&channel=Event");
	}else{
		$("#HiPlayer").attr("src" , "http://hichannel.hinet.net/player/iPlayer_live.jsp?id=605&channel=Event");
	}
}

var liveListTmp = '<tr>'
			    + '  <th width="36%">#0#</th>'
                + '  <td width="37%" height="24">#1#</td>'
                + '  <td width="16%"><span class="gray15">#2#</span></td>'
                + '  <td width="11%" height="24" class="txt_black15">#3#</td>'
                + '</tr>';

var liveIcon1 = '<a href="live.html?c=#0#"><img src="images/icon_tv#0#.gif" width="38" height="19" border="0" /></a>';

var liveIcon2 = '<table width="53" border="0" cellpadding="0" cellspacing="0">'
              + '  <tr>'
              + '    <td><a href="liveChannel.html?c=#0#&t=#1#"><img src="images/icon_play1.gif" width="36" height="14" border="0"></a></td>'
              + '    <td width="17" align="center" background="images/icon_playBg.gif"><a href="liveChannel.html?c=#0#&t=#1#" class="txt_b11">#0#</a></td>'
              + '  </tr>'
              + '</table>';
var liveIcon3 = '<img src="images/icon_end.gif" width="53" height="14" border="0" />';

var liveIcon4 = '<a href="vod_detail.html?s=#0#&t=2"><img src="images/icon_full.gif" width="53" height="14" border="0" /></a>';

function initLiveSchedule(){
	var scheduleList = $$scheduleList.items;
	if(scheduleList.length == 2){
		for(var n in scheduleList){
			var item = scheduleList[n].schedule;
			var strNum = parseInt(n) + 1 ;
			$('#cell' + strNum).html(scheduleList[n].date);
			if(item.length > 0){
				$(item).each(function(i){
					var icon = "";
					if(this.showIcon == 'T'){
						if(this.vod.trim() != ''){
							icon = messageFormat(liveIcon4 , new Array(this.vod.trim()));
						}else if(this.finish.trim() == 'T'){
							icon = liveIcon3;
						}else if(this.signal < 31 && this.signal != 0){
							icon = messageFormat(liveIcon2 , new Array(this.signal , this.typeSn));
						}else if(this.signal == 33){
							icon = messageFormat(liveIcon1 , new Array("2"));
						}else{
							icon = messageFormat(liveIcon1 , new Array("1"));
						}

					}
					$('#listTable' + n).append(messageFormat(liveListTmp , new Array(this.type , this.sType , this.stime + " - " + this.etime , icon)));
				});
			}else{
				$('#listTable' + n).append("<tr><td><font color=\"white\">敬請期待</font></td></tr>");
			}
			
			if(scheduleList[n].selected == 'T'){
				$('#cell'+strNum).attr("class" , 'active');
				if(strNum ==2){
					$('#d1').hide();
					$('#d'+strNum).show();
				}
			}
		}
	}
}

function initChannelInfo(cid , tid){
	var infos = $$typeList.items;
	var getDatas = false;
	var strCh = "";
	if(cid<10){
		strCh = '0'+cid;
	}else{
		strCh = cid;
	}
	$(infos).each(function(i){
						   if(this.type_sn == tid && this.status != 'D'){
						   	$('#channelType').append(this.title);
							$('#channelContent').append(this.content);
							$('#channelContent').append('<br/><font color="red">訊號來源：高雄世運會</font>');
							getDatas = true;
							$('#HiPlayer').attr('src' , 'http://hichannel.hinet.net/player/iPlayer_live.jsp?id=4'+strCh+'&channel=Event ');
							return false;
						   }
						   });
	if(!getDatas){
		//location.href='/';
	}
}

function initLivePath(liveid){
		var defTxt = "影音直播-精選一台";
		if(liveid == 2){
			defTxt="影音直播-精選二台"; 
		}
		$('#path div span').html(defTxt);
}

function initChannelPath(liveid){
		var defTxt = "影音直播-" + liveid + "台";
		$('#path div span').html(defTxt);
}