﻿var xmlHttp;
//解析XML文件
var toDay;
//当天日期
var toDay_str;
//当天日期的字符串表现形式'2008.5.26'
var curTime;
//当前的时间的小时和分钟
var array_channel;
//频道数组
var array_playdate;
//单个频道下按日期分组的数组
var array_gbsplaydate;
//卫视频道下按日期分组的数组
var array_gbsplaybill;
//卫视频道下单个日期下的播放列表数组
var array_playbill;
//单个日期下的播放列表数组
var array_showplaybill;
//显示在页面上的当前播放节目数组
var channelName;
//频道名称
var str;
var weekstr; 
//本周每天播放的数据
var shownum = 4;
//每次显示N个节目
var curIndex = 0;
//当前显示器的节目编号

function createXMLHttpRequest(){
 if (window.ActiveXObject){
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 }else if (window.XMLHttpRequest){
  xmlHttp = new XMLHttpRequest("Msxml2.XMLHTTP.3.0");
 }
}

function doSearch(url){
 array_showplaybill = new Array();
 toDay = new Date();
 
 url = url + '?t=' + Math.random();
 createXMLHttpRequest();
 xmlHttp.onreadystatechange = handleStateChange;
 xmlHttp.open("GET",url,"true");
 xmlHttp.send(null);
}

function handleStateChange(){
 if (xmlHttp.readyState == 4){
  if (xmlHttp.status == 200){
   parseResults();
  }
 }
}

function parseResults() {
	toDay_str = toDay.getFullYear().toString() + '.' + (toDay.getMonth() + 1).toString() + '.' + toDay.getDate().toString();
	curTime = (toDay.getMinutes() < 10) ?toDay.getHours().toString() + 0 +  toDay.getMinutes(): toDay.getHours().toString() + toDay.getMinutes();
	//curHour = curHour < 10 ? '0' + curHour.toString() : curHour;
	getPlaybill();
}

function getPlaybill() {
	array_channel = xmlHttp.responseXML.getElementsByTagName("channel");

	for (i = 0; i < array_channel.length; i ++)
	//循环每一个频道
	{
		if (array_channel[i].getAttribute("name") == "卫视") 
		{
			array_gbsplaydate = array_channel[i].getElementsByTagName("playdate");
			
			//开始取得本周的日期数组
			var weekArray = new Array();
			var newWeekOf = (toDay.getDay() == 0) ? 7 : toDay.getDay();
			var dateDiff;
			for (b = 1; b <= 7; b++)
			{		
				weekArray.push(toDay.DateAdd(b - newWeekOf).toString());
			}
			//结束取得本周的日期数据
			
			for (a = 0; a < array_gbsplaydate.length; a++ )
			{
				//根据XML取得日期数据在本周日期数组中循环判断
				for (c = 0; c < weekArray.length; ++c)
				{
					if (array_gbsplaydate[a].getAttribute("date") == weekArray[c])
					{
						weekstr = "<ul>";
						array_gbsplaybill = array_gbsplaydate[a].getElementsByTagName("playbill");
						for (d = 0; d < array_gbsplaybill.length; d++)
						{
							weekstr = weekstr + '<li><span class="time">' + array_gbsplaybill[d].firstChild.data.Trim().substr(0,5) +'</span><span class="showbiz">' + array_gbsplaybill[d].firstChild.data.Trim().substr(5,15) + '</span>';
							if (array_gbsplaybill[d].getAttribute('album') != "")
							{
								weekstr = weekstr + '<a href="'+ array_gbsplaybill[d].getAttribute('album') +'">[专题]</a>';
							}
							weekstr = weekstr + '</li>';
						}
						weekstr = weekstr + "</ul>";
						document.getElementById("weektab_sub_" + c).innerHTML = weekstr; 
					}
				}
			}
            document.getElementById("weektab_" + (newWeekOf - 1)).className = 'tabon';
		}
		//上面代码主要是取 湖南卫视 数据
		
		array_playdate = array_channel[i].getElementsByTagName("playdate");
		channelName = array_channel[i].getAttribute("name");
		for (j = 0; j < array_playdate.length; j++)
		//循环频道下的每天的节目
		{
			if (array_playdate[j].getAttribute("date").Trim() == toDay_str)
			//返回今天的节目
			{
				array_playbill = array_playdate[j].getElementsByTagName("playbill");
				var curPlayIndex = (array_playbill.length > 0) ? 0 : -1;

				for (k = 1; k < array_playbill.length; k++ )
				{
					tempTime = array_playbill[k].firstChild.data.Trim().substr(0,5).replace(':','');
					tempTime = tempTime * 1;
					curTime = curTime * 1;
					
					if (curTime >= tempTime)
					{
						curPlayIndex = k;
					} else {
						break;
					}
				}
				if (curPlayIndex > -1)
				{
					array_showplaybill.push('<li><a href="' + array_playbill[curPlayIndex].getAttribute("direct") + '">[' + channelName + "]  " + array_playbill[curPlayIndex].firstChild.data + "</a></li>");
				}
			}
		}
	} 
	if (array_showplaybill.length <= shownum) 
	{
		str = '<ul>';
		for (i = 0; i < array_showplaybill.length; i++)
		{
			str = str + array_showplaybill[i];
		}
		str = str + "</ul>";
	    document.getElementById("todayplaybill").innerHTML = str;
	} else {
		autoShowPlayBill();
		setInterval('autoShowPlayBill()', 4000);
	}
}

function autoShowPlayBill()
{
	str = '<ul>';
	tempIndex = curIndex;
	for (i = 0; i < shownum; ++i)
	{
		str += array_showplaybill[curIndex];
		++curIndex;
		curIndex = (curIndex >= array_showplaybill.length) ? 0 : curIndex;
	}
	curIndex = tempIndex;
	++curIndex;
	curIndex = (curIndex >= array_showplaybill.length) ? 0 : curIndex;
	str += '</url>';
	document.getElementById("todayplaybill").innerHTML = str;
}

String.prototype.Trim = function() {
  return this.replace(/(^\s*)|(\s*$)/g,'');
}

Date.prototype.DateAdd = function(Number) {
	var dtTmp = this
	return new Date(Date.parse(dtTmp.toGMTString()) + (86400000 * Number));
}

Date.prototype.toString = function(showWeek) 
{ 
　return  this.getFullYear().toString() + '.' + (this.getMonth() + 1).toString() + '.' + this.getDate().toString();
}
