// ｩ cod3.net 2008

function $_(elemId)
{
	return document.getElementById(elemId)
}

function Control(className,innerHTML,mouseOver,mouseOut){
	var ctl=document.createElement("a");
	ctl.className=className;ctl.innerHTML=innerHTML;
	ctl.onmouseover=function(){mouseOver()};
	ctl.onmouseout=function(){mouseOut()};
	return ctl
}

function Scroll(div,direction){
	if(direction=="left"){
		this.startLeftScroll(div)
	}
	if(direction=="right"){
		this.startRightScroll(div)
	}
}

Scroll.prototype={
	startLeftScroll:function(div){
		var t=this;
		this.scrollInterval=window.setInterval(function(){
			t.scroll(div,'left')
		}
		,12)
	}
	,startRightScroll:function(div){
		var t=this;
		this.scrollInterval=window.setInterval(function(){
			t.scroll(div,'right')
		}
		,12)
	}
	,stop:function(){
		window.clearInterval(this.scrollInterval)
	}
	,scroll:function(div,direction){
		if((direction=="left")||(direction="right")){
			$_(div).scrollLeft+=direction=="left"?-6:6
		}
	}
};

function Controls(divToControl){
	var left=new Control(
		"left",
		"&lt;",
		function(){this.sLeft=new Scroll(divToControl,'left')},
		function(){this.sLeft.stop()}
	);
	var right=new Control(
		"right",
		"&gt;",
		function(){this.sRight=new Scroll(divToControl,'right')},
		function(){this.sRight.stop()}
	);
	var ctlDiv=document.createElement("div");
	ctlDiv.className="controls";
	ctlDiv.appendChild(left);
	ctlDiv.appendChild(right);
	return ctlDiv
}

function put0b4(number){
	if(number<10){
		number='0'+number
	}
	return number
}

function Holder(childClass){
	this.parentId=childClass+"Holder";
	this.childClass=childClass
}

function Child(){}
Child.prototype={
	callBack:null,className:null,contents:null,left:null,length:1,getDiv:function(){
		var div=document.createElement("div");
			div.className=this.className;
			div.innerHTML=this.contents;
			if(this.className=="event"){
				div.style.margin="0px 0px 0px "+this.left+"px";
				div.style.width=((this.length*120)+((this.length-1)*8))+"px";
				if(typeof this.callBack!="undefined"){
					var thisCallBack=this.callBack;
					div.onclick=function(){
						thisCallBack()
					}
				}
			}
		return div
	}
};

Holder.prototype={
	parentId:null,childClass:null,lastLeft:0,addChild:function(child){
		child.className=this.childClass;
		var thisChildLeft=parseFloat(child.left)+parseFloat(child.length);
		child.left=(child.left-this.lastLeft)*128;
		this.lastLeft=thisChildLeft;
		var childDiv=child.getDiv();
		this.div.appendChild(childDiv)
	},
	toDiv:function(){
		var holderDiv=document.createElement("div");
		holderDiv.className=this.parentId;
		this.div=holderDiv;
		return holderDiv
	}
};

function Timetable(parentDiv){
	this.parentDiv=parentDiv;
	$_(parentDiv).className="timetable";
	var hourHolder=new Holder("hour");
	$_(this.parentDiv).appendChild(hourHolder.toDiv());
	var eventHolder=new Holder("event");
	var eHdiv=eventHolder.toDiv();
	eHdiv.appendChild(new Controls(parentDiv));
	$_(this.parentDiv).appendChild(eHdiv);
	for(var x=1;x<24;x++){
		if(x==1){
			youbi = "(日)";	
		}
		else if(youbi =="(日)"){
				youbi = "(月)";
		}
		else if(youbi =="(月)"){
				youbi = "(火)";
		}
		else if(youbi =="(火)"){
				youbi = "(水)";
		}
		else if(youbi =="(水)"){
				youbi = "(木)";
		}
		else if(youbi =="(木)"){
				youbi = "(金)";
		}
		else if(youbi =="(金)"){
				youbi = "(土)";
		}
		else if(youbi =="(土)"){
				youbi = "(日)";
		}
		else if(youbi =="(日)"){
				youbi = "(月)";
		}
		var hour=new Child();
		hour.contents="2/" + put0b4(x) + youbi;
		hourHolder.addChild(hour);
		if(x==23){
				break;
		}
	}
	this.hourHolder=hourHolder;
	this.eventHolder=eventHolder;
	this.goToNow()
}


function base60(base100){
	base100=base100*1;
	base100=base100.toFixed(2);
	var mn=Math.floor(base100.substring(base100.length-2,base100.length)/0.6);
	return Math.floor(base100)+'.'+mn
}


Timetable.prototype={
	addEvent:function(desc,start,length,callBack){
		var evt=new Child();
		evt.contents=desc;
		evt.left=base60(start);
		evt.length=base60(length);
		evt.callBack=callBack;
		this.eventHolder.addChild(evt)
	},
//20090114 fix -start-
//	goTo:function(hour){
//		$_(this.parentDiv).scrollLeft=base60(hour)*128
	goTo:function(ymd){
		var y=ymd.getFullYear();
		var m=ymd.getMonth()+1;
		var d=ymd.getDate();
		ymd = y + "" + put0b4(m) + "" + put0b4(d);
//		alert(ymd);
		if(ymd>=20090201 && ymd<=20090228){
			$_(this.parentDiv).scrollLeft=(d-4.55)*128;
		}else if(ymd>=20090301){
			$_(this.parentDiv).scrollLeft=28*128;
		};
//20090114 fix -end-
	},
	goToNow:function(){
		var theDate=new Date();
//20090114 fix -start-
//		var theHours=put0b4(theDate.getHours());
//		var theMinutes=put0b4(theDate.getMinutes());
//		var gotoString=theHours+'.'+theMinutes;
//		this.goTo(gotoString)
		this.goTo(theDate)
//20090114 fix -end-
	}
};