﻿function ProgScheduleCalendar(initObj){
	this.id=initObj.id;
	this.title=initObj.title;
	this.startDate=initObj.startDate;
	this.endDate=initObj.endDate;
	this.validStart=initObj.validStart;
	this.validEnd=initObj.validEnd;
	this.curDate=initObj.curDate;
	this.lang=initObj.lang;
	this.schedules=initObj.schedules;
	this.tag=new TagObj();
	this.dayArr=["S", "M", "T", "W", "T", "F", "S"];
	this.createCalendar=function(){
		document.write(this.getCalendarHTMLString());
		this.setValidCalendar();
		//this.setCurrentDate();
		this.setSchedulesDay();
	}
	this.getCalendarHTMLString=function(){
		this.tag.beginTag("table", "class", "calendar", "id", this.id, "cellspacing", 0, "cellpadding", 0, "align", "center");
		this.createCalendarHeading(this.tag, this.title);
		this.createCalendarWeekDayRow();
		this.createCalendarDateCell(this.tag, this.id, this.startDate, this.endDate);
		this.createScheduleDisplayCell();
		this.tag.closeTag("table");
		return this.tag.getTagString();
	};
	this.createCalendarHeading=function(){
		if(this.title==null || this.title.length==0) return ;
		this.tag.beginTag("tr", "class", "head");
		this.tag.beginTag("td", "colspan", 7);
		this.tag.writeString(this.title);
		this.tag.closeTag("tr");
	}
	this.createCalendarWeekDayRow=function(){
		this.tag.beginTag("tr", "class", "wkday");
		for(i=0;i<this.dayArr.length;i++){
			if(i==0 || i==this.dayArr.length-1){
				this.tag.beginTag("td", "class", "rest");
			}else{
				this.tag.beginTag("td", "class", "day");
			}
			this.tag.writeString(this.dayArr[i]);
			this.tag.closeTag("td");
		}
		this.tag.closeTag("tr");
	}
	this.createCalendarDateCell=function(){
		var t=new Date(this.startDate.getTime());
		var openTr=true;
		this.tag.beginTag("tr");
		if(this.startDate.getDay()>0) {
			this.tag.beginTag("td", "colspan", this.startDate.getDay());
		}
		while(t<this.endDate){
			if(t.getDay()==0  && ! openTr) {
				openTr=true;
				this.tag.beginTag("tr");
			}
			if(t.getDay()==0 || t.getDay()==6){
				this.tag.beginTag("td", "id", this.id+"_"+this.getDateCellId(t), "class", "rest");
			}else{
				this.tag.beginTag("td", "id", this.id+"_"+this.getDateCellId(t));
			}
			this.tag.writeString(t.getDate());
			this.tag.closeTag("td");
			if(t.getDay()==6) {
				this.tag.closeTag("tr");	
				openTr=false;
			}
			t.setDate(t.getDate()+1);
		}
		if(openTr) {
			openTr=false;
			this.tag.closeTag("tr");
		}
	}
	this.createScheduleDisplayCell=function(){
		this.tag.beginTag("tr");
		this.tag.beginTag("td", "colspan", 7, "id", this.id+"_schDisplay", "class", "sch_display");
		
		this.tag.closeTag("tr");
	}
	this.getDateString=function(d){
		return d.getFullYear()+"-"+(d.getMonth()+1+"").padl(2, "0")+"-"+(d.getDate()+"").padl(2, "0");
	}
	this.getDateCellId=function(dt){
		return dt.getFullYear()+(dt.getMonth()+1+"").padl(2, "0")+(dt.getDate()+"").padl(2, "0");
	}
	this.getTimeString=function(d){
		return (""+d.getHours()).padl(2, "0")+":"+(""+d.getMinutes()).padl(2, "0");
	}
	this.getDateLink=function (t){
		
		if(this.lang=="1")
			return "/gh_tc/ff/ff-schedule.aspx?dt="+this.getDateString(t);
		else
			return "/gh_tc/ff/ff-e-schedule.aspx?dt="+this.getDateString(t);
	}
	this.setValidCalendar=function(){
		var t=new Date(this.validStart.getTime());
		while(t<=this.validEnd){
			var id=this.id+"_"+this.getDateCellId(t);
			var aid=this.id+"_a_"+this.getDateCellId(t);
			MM_findObj(id).innerHTML="<a href='"+this.getDateLink(t)+"' id='"+aid+"' >"+t.getDate()+"</a>";
			MM_findObj(id).className += " valid ";
			t.setDate(t.getDate()+1);
		}
	}
	this.showDaySchedule=function(dt){
		var obj=MM_findObj(this.id+"_schDisplay");
		if(obj!=null){
			obj.innerHTML=this.composeDisplay(dt);
		}
	}
	this.composeDisplay=function (dt){
		var tag=new TagObj();
		tag.beginTag("div", "class","sch_date");
		var dtStr=this.getDateCellId(dt);
		tag.writeString(this.getDateString(dt));
		tag.closeTag("div");
		for(var i=0;i<this.schDisplay[dtStr].length;i++){
			var o=this.schDisplay[dtStr][i];
			tag.beginTag("div", "class", "theater");
			tag.beginTag("span", "class", "sch_time");
			tag.writeString(this.getTimeString(o.dt));
			tag.closeTag();
			tag.writeString(" "+o.theaterName+" "+o.roomName);
			tag.closeTag();
		}
		return tag.getTagString();
	}
	this.setSchedulesDay=function(){
		var sch=this.schedules;
		var dis=this.schDisplay=new Object();
		var ve=new Date(this.validEnd.getTime());
		ve.setDate(ve.getDate()+1);		
		if(sch.length>0){
			for(var i=0;i<sch.length;i++){
				if(sch[i].dt<this.validStart || sch[i].dt>ve) continue;
				var dtStr=this.getDateCellId(sch[i].dt);
				if(dis[dtStr]==null) dis[dtStr]=new Array();
				dis[dtStr].push(sch[i]);
				MM_findObj(this.id+"_"+dtStr).className+=" on ";
				MM_findObj(this.id+"_a_"+dtStr).onmouseover=getMouseOver(this.id, sch[i].dt);				
			}
		}
	}
}
function getMouseOver(k,dt){
	return function(){ 
		window[k].showDaySchedule(dt); 
	}
}