/**************************************************
* * 赛事查询
* * 2008-9-18
* **************************************************
* * msn:danxinju@hotmail.com
* * author:淡新举
***************************************************/
i8.tourCalendar = function(location ,monthCommond ,monthView ,dayView ,prevCommond ,nextCommond ,tourLayer){
	this.currDate = new Date();	//当前日期
	this.monthCommond = monthCommond;	//月份按钮
	this.monthView = monthView;	//月份视图
	this.dayView = dayView;	//日期视图
	this.prevCommond = prevCommond;	//上翻按钮
	this.nextCommond = nextCommond;	//下翻按钮
	this.tourLayer = tourLayer;	//浮动层
	
	this.prevDay = null;	//上翻时间戳
	this.nextDay = null;	//下翻时间戳

	this.prevClickObj = null;	//单元格前置点击对象

	this.location = location;	//请求资源地址
};

//根据年份返回月份天数
i8.tourCalendar.prototype.getMonthDays = function(year){
	var feb = (year % 4 == 0)? 29:28; 
	return new Array(31, feb , 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
};

//程序初始化
i8.tourCalendar.prototype.init = function(){
	var _self = this;	//复制当前对象
	this.monthCommond.innerHTML = (this.currDate.getMonth() + 1) +"月";	//设置当前月
	
	this.currDate.setDate(1);
	this.setDay();	//加载默认天

	//设置月份单击
	this.monthCommond.onclick = function(e){
		e = e?e : event;
		if (e.stopPropagation)
		{
			e.stopPropagation();
		}
		else
		{
			e.cancelBubble = true;
		}
		
		_self.showMonthView(this);
	};
	
	var lis = this.monthView.getElementsByTagName("li");
	for (var i=0;i< lis.length; i++)
	{
		lis[i].onclick = function(){
			_self.hideMonthView();
			_self.monthCommond.innerHTML = this.innerHTML;

			var mm = parseInt(this.innerHTML.replace("月" ,"")) - 1;
			_self.currDate.setMonth(mm);
			_self.currDate.setDate(1);

			_self.setDay();
		};
	}

	//设置上翻事件
	this.prevCommond.onclick = function(){
		_self.prevWeek();
	};

	//设置下翻事件
	this.nextCommond.onclick = function(){
		_self.nextWeek();
	};

	//单击文档关闭
	document.onclick = function(){
		_self.hideMonthView();
		_self.hideDayView();
	};
};

//上翻星期
i8.tourCalendar.prototype.prevWeek = function(){
	if (this.prevDay <= 1)
	{
		return false;
	}
	//清空单元格变色
	this.clearColorForSpan();

	//将上翻时间戳赋值给下翻时间戳
	this.nextDay = this.prevDay + 1;
	var days = this.dayView.getElementsByTagName("span") ,temp = new Date();
	for (var i=days.length-1;i>-1 ;i-- )
	{
		if (this.prevDay > 0)
		{
			if (temp.getDate() == this.prevDay && temp.getMonth() == this.currDate.getMonth())
			{
				days[i].innerHTML = "<strong>"+ this.prevDay +"</strong>";
			}
			else
			{
				days[i].innerHTML = this.prevDay;
			}
			this.prevDay--;
		}
		else
		{
			days[i].innerHTML = "&nbsp;";
		}
	}

	//注册事件
	this.regEventForDayView(this);
};

//下翻星期
i8.tourCalendar.prototype.nextWeek = function(){
	var arrMonth = this.getMonthDays(this.currDate.getFullYear);
	
	if (this.nextDay > arrMonth[this.currDate.getMonth()])
	{
		return false;
	}
	//清空单元格变色
	this.clearColorForSpan();
	
	//将下翻时间戳赋值给上翻时间戳
	this.prevDay = this.nextDay-1;
	var days = this.dayView.getElementsByTagName("span") ,temp = new Date();
	for (var i=0;i<days.length ;i++ )
	{
		if (this.nextDay <= arrMonth[this.currDate.getMonth()])
		{
			if (temp.getDate() == this.nextDay && temp.getMonth() == this.currDate.getMonth())
			{
				days[i].innerHTML = "<strong>"+ this.nextDay +"</strong>";
			}
			else
			{
				days[i].innerHTML = this.nextDay;
			}
			this.nextDay++;
		}
		else
		{
			days[i].innerHTML = "&nbsp;";
		}
	}

	//注册事件
	this.regEventForDayView(this);
};

//根据月份改变日期
i8.tourCalendar.prototype.setDay = function(){
	//清空单元格变色
	this.clearColorForSpan();

	var week = this.currDate.getDay() ,j = 1;
	var days = this.dayView.getElementsByTagName("span");
	for (var i=0;i<days.length ;i++ )
	{
		if (i >= week)
		{
			days[i].innerHTML = j;
			j++;
		}
		else
		{
			days[i].innerHTML = "&nbsp;";
		}
	}
	
	//记录日期戳
	this.prevDay = 1;
	this.nextDay = j;

	//注册事件
	this.regEventForDayView(this);
};

//显示月份
i8.tourCalendar.prototype.showMonthView = function(element){
	var pos = this.getPositionByElement(element);
	this.monthView.style.top = (pos.top + 15) + "px";
	this.monthView.style.left = (pos.left - 3) + "px";
	this.monthView.style.display = "";
};

//隐藏月份
i8.tourCalendar.prototype.hideMonthView = function(){
	this.monthView.style.display = "none";
};

//注册日期单击事件
i8.tourCalendar.prototype.regEventForDayView = function(_self){
	var objs = this.dayView.getElementsByTagName("span");
	for (var i=0;i<objs.length ;i++ )
	{
		if (objs[i].innerHTML != "&nbsp;")
		{
			objs[i].onclick = function(e){

				e = e?e : event;
				if (e.stopPropagation)
				{
					e.stopPropagation();
				}
				else
				{
					e.cancelBubble = true;
				}

				_self.clearColorForSpan();
				_self.prevClickObj = this;
				this.style.background = "#E4C881";

				if(this.childNodes[0].nodeType == 1)
				{
					_self.currDate.setDate(parseInt(this.childNodes[0].innerHTML));
				}
				else
				{
					_self.currDate.setDate(parseInt(this.innerHTML));
				}
				_self.showDayView(this);
			};
		}
		else
		{
			objs[i].onclick = function(){
				return false;
			};
		}
	}
};

//清空单元格变色
i8.tourCalendar.prototype.clearColorForSpan = function(){
	if (this.prevClickObj)
	{
		this.prevClickObj.style.background = "#E0D9CB";
	}
};

//显示赛事浮动层
i8.tourCalendar.prototype.showDayView = function(element){
	var currDate = this.currDate;
	var pos = this.getPositionByElement(element);
	var tourLayer = this.tourLayer;
	var date = this.formatDateToSting8([currDate.getFullYear() ,currDate.getMonth()+1 ,currDate.getDate()]);

	ajax(this.location + date +".txt" ,function(text){
		
		try
		{
			eval("var json="+ text);
			var sb = new StringBuilder();
			sb.add("<h5>"+ currDate.getFullYear() +"年"+ (currDate.getMonth()+1) +"月"+ currDate.getDate() +"日赛程</h5><div><ul>");
			if (json.rows)
			{	
				for (var i=0;i<json.rows.length ; i++)
				{
					sb.add("<li title=\""+ json.rows[i].tourName +"\">"+ json.rows[i].tourTime.split(" ")[1].substr(0 ,5) +" "+ json.rows[i].tourName +"</li>")
				}
			}
			sb.add("</ul></div>");

			tourLayer.innerHTML = sb.toString();
			tourLayer.style.top = (pos.top + 15) + "px";
			tourLayer.style.left = (pos.left - 135) + "px";
			tourLayer.style.display = "";
		}
		catch (e)
		{
			tourLayer.style.display = "none";
		}
		

	} ,"get");

	
	
};

i8.tourCalendar.prototype.hideDayView = function(){
	this.tourLayer.style.display = "none";
};

//格式日期为8位字符串
i8.tourCalendar.prototype.formatDateToSting8 = function(arrDate){
	if (parseInt(arrDate[1]) < 10)
	{
		arrDate[1] = "0"+ arrDate[1];
	}
	if (parseInt(arrDate[2]) < 10)
	{
		arrDate[2] = "0"+ arrDate[2];
	}

	return arrDate.join("");
};

//传入对象，返回绝对位置
i8.tourCalendar.prototype.getPositionByElement = function(element){
	var offsetTop = element.offsetTop;
	var offsetLeft = element.offsetLeft;
	while(element = element.offsetParent) 
	{
		offsetTop += element.offsetTop;
		offsetLeft += element.offsetLeft;
	}
	
	return {top:offsetTop ,left:offsetLeft};
};

//设定默认日期
i8.tourCalendar.prototype.setDefaultDate = function(dd){
	this.currDate.setDate(dd);
	this.monthCommond.innerHTML = (this.currDate.getMonth() + 1) +"月";	//设置当前月
	
	var days = this.dayView.getElementsByTagName("span");
	var temp = 0;
	
	while (temp == 0)
	{
		for (var i=0;i<days.length ;i++ )
		{
			if (days[i].innerHTML == dd)
			{
				days[i].innerHTML = "<strong>"+ days[i].innerHTML +"</strong>";
				return false;
			}
			if (days[i].innerHTML.toLowerCase().indexOf("<strong>") != -1)
			{
				return false;
			}
		}
		this.nextWeek();
	}

};