/*
 Calendar Class
 create by: Yuanhui
 create date: 2006-7-4
 email: yuanhui9@gmail.com
 blog: http://www.donghegou.com/
*/
function Calendar() {
	var theCalendar = this;
	this.calendarObj = null;
	this.calendarPad = null;
	this.prevMonth = null;
	this.nextMonth = null;
	this.goToday = null;
	this.disDate = null; 
	this.body = null;
	this.today = [];
	this.currentDate = [];
	this.sltDate = "";
	this.target = null;
	this.source = null;
	this.weekNames = ["日", "一", "二", "三", "四", "五", "六"];
	
	this.calendarClassName = "calendar";
	this.shadowClassName = "shadow";
	this.padClassName = "pad";
	this.panelClassName = "panel";
	this.disDateClassName = "dis-date";
	this.weekClassName = "week";
	this.sunWeekClassName = "sun-week";
	this.bodyClassName = "cal-body";
	this.bodyCellClassName = "cal-body-cell";
	this.todayClassName = "today-cell";
	
	this.calendarWidth = 209;
	this.calendarHeight = 180;
	this.textSize = "12";
	this.shadowBg = "images/calendar_shadow.png";
	this.shadowAlpha = "20";
	this.panelBorderThick = "1";
	this.panelBorderStyle = "solid";
	this.panelBorderColor = "#919999";
	this.panelBgColor = "#FFF";
	this.disDateTextStyle = "font-weight:bold;";
	this.weekTextStyle = "font-weight:bold;";
	this.sunWeekTextStyle = "font-weight:bold; color:#F00;";
	this.dayTextStyle = "";
	this.todayTextStyle = "font-weight:bold; background-color:#CCC;";
	this.prevImg = "images/calendar_previous.gif";
	this.nextImg = "images/calendar_next.gif";
	this.closeText = "关闭";
	this.prevAltText = "上个月";
	this.nextAltText = "下个月";
	
	/************************************** Style function *********************************************/
	this.setSize = function(w, h) {
		if (w != undefined) this.calendarWidth = w;
		if (h != undefined) this.calendarHeight = h;
	}
	
	
	/************************************** Logic function *********************************************/
	this.addPad = function() {
		document.write("<div id=\"calendar\">");
		document.write("<iframe width='100%' height='100' frameborder='0'></iframe>");
		document.write("<div id=\"cal_shadow\"></div>");
		document.write("<div id=\"cal_pad\"></div>");
		document.write("</div>");
		this.calendarObj = document.getElementById("calendar");
		this.calendarObj.className = this.calendarClassName;
		var shadow = document.getElementById("cal_shadow");
		shadow.className = this.shadowClassName;
		this.calendarPad = document.getElementById("cal_pad");
		this.calendarPad.className = this.padClassName;
		this.calendarObj.style.cssText = "position:absolute; z-index: 9988; display:none; font-size: " + this.textSize + "px;";
		this.calendarObj.style.width = this.calendarWidth;
		this.calendarObj.style.height = this.calendarHeight;
		shadow.style.cssText = "position:relative; left:3; top:3; z-index:0; background:url(" + this.shadowBg + ") no-repeat 3px 3px; filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=" + this.shadowAlpha + ",finishOpacity=20);";
		shadow.style.height = this.calendarHeight;
		this.calendarPad.style.cssText = "position:absolute; left:0; top:0; z-index:1;";
	}
	
	this.addPanel = function() {
		var trRow = null, tdCell = null;
		var tablePanel = document.createElement("table");
		this.calendarPad.appendChild(tablePanel);
		tablePanel.className = this.panelClassName;
		tablePanel.style.cssText = "border-collapse:collapse;";
		tablePanel.style.borderWidth = this.panelBorderThick;
		tablePanel.style.borderStyle = this.panelBorderStyle;
		tablePanel.style.borderColor = this.panelBorderColor;
		tablePanel.style.backgroundColor = this.panelBgColor;
		tablePanel.style.width = this.calendarWidth - 4;
		tablePanel.style.height = this.calendarHeight - 4;
		
		trRow = tablePanel.insertRow(0);
		this.prevMonth = this.insertTbCell(trRow, 0, this.prevImg, 1, "left");
		this.prevMonth.alt = this.prevAltText;
		this.prevMonth.onclick = function() {
			theCalendar.currentDate[1]--;
			if (theCalendar.currentDate[1] <= 0) {
				theCalendar.currentDate[0]--;
				theCalendar.currentDate[1] = 12;
			}
			var defDate = theCalendar.currentDate[0] + "-" + theCalendar.currentDate[1] + "-" + theCalendar.currentDate[2];
			theCalendar.show(theCalendar.target, defDate, theCalendar.source);
		}
		
		tdCell = trRow.insertCell(1);
		tdCell.colSpan = 5;
		tdCell.className = this.disDateClassName;
		tdCell.style.cssText = "text-align:center;" + this.disDateTextStyle;
		this.disDate = tdCell;
		this.nextMonth = this.insertTbCell(trRow, 2, this.nextImg, 1, "right");
		this.nextMonth.alt = this.nextAltText;
		this.nextMonth.onclick = function() {
			theCalendar.currentDate[1]++;
			if (theCalendar.currentDate[1] > 12) {
				theCalendar.currentDate[0]++;
				theCalendar.currentDate[1] = 1;
			}
			var defDate = theCalendar.currentDate[0] + "-" + theCalendar.currentDate[1] + "-" + theCalendar.currentDate[2];
			theCalendar.show(theCalendar.target, defDate, theCalendar.source);
		}
		
		trRow = tablePanel.insertRow(1);
		for (var i = 0; i < 7; i++) {
			tdCell = trRow.insertCell(i);
			tdCell.className = this.weekClassName;
			tdCell.style.cssText = "text-align:center;" + this.weekTextStyle;
			tdCell.innerHTML = this.weekNames[i];
			if (i == 0) {
				tdCell.className = this.sunWeekClassName;
				tdCell.style.cssText = "text-align:center;" + this.sunWeekTextStyle;
			}
		}
		
		trRow = tablePanel.insertRow(2);
		tdCell = trRow.insertCell(0);
		tdCell.colSpan = 7;
		tdCell.vAlign = "top";
		var tdBody = document.createElement("table");
		tdBody.className = this.bodyClassName;
		tdCell.appendChild(tdBody);
		tdBody.style.cssText = "width:100%; border-collapse:collapse;";
		this.body = tdBody;
		
		trRow = tablePanel.insertRow(3);
		tdCell = trRow.insertCell(0);
		tdCell.colSpan = 7;
		tdCell.align = "center";
		tdCell.innerHTML = this.closeText;
		tdCell.style.cssText = "border: 1px solid #CCC; background-color: #F2F2F2; cursor: hand;";
		tdCell.onclick = function() {
			theCalendar.hide();
		}
	}
	
	this.insertTbCell = function (trRow, cellIndex, src, colSpan, align) {
		var tbCell = trRow.insertCell(cellIndex);
		if (colSpan != undefined) tbCell.colSpan = colSpan;
		tbCell.style.cssText = "text-align:" + align + ";padding-" + align + ": 5px; height:24px; cursor:hand;";
		var btnCell = document.createElement("img");
		tbCell.appendChild(btnCell);
		btnCell.src = src;
		return btnCell;
	}
	
	this.setDefaultDate = function() {
		var defDate = new Date();
		this.today[0] = defDate.getFullYear();
		this.today[1] = defDate.getMonth() + 1;
		this.today[2] = defDate.getDate();
	}
	
	this.show = function(targetObj, defDate, srcObj) {
		if (targetObj == undefined) {
			alert("Don't set target object! \n method: calendarInstance.show(obj, string, obj);");
			return false;
		} else {
			this.target = targetObj;
		}
		srcObj == undefined ? this.source = this.target : this.source = srcObj;
		
		var firstDay;
		var cells = new Array();
		var reg = /^\d{4}-\d{1,2}-\d{1,2}$/
		if (defDate == undefined || defDate == "" || !defDate.match(reg)) {
			var theDate = new Array();
			this.disDate.innerHTML = this.today[0] + "-" + this.today[1] + "-" + this.today[2];
			theDate[0] = this.today[0];
			theDate[1] = this.today[1];
			theDate[2] = this.today[2];
		} else {
			/*
			var reg = /^\d{4}-\d{1,2}-\d{1,2}$/
			if (!defDate.match(reg)) {
				alert("Default date format is error! \n\n ex.'yyyy-mm-dd'");
				return false;
			}
			*/
			var theDate = defDate.split("-");
			this.disDate.innerHTML = theDate[0] + "-" + theDate[1] + "-" + theDate[2];
		}
		this.currentDate[0] = theDate[0];
		this.currentDate[1] = theDate[1];
		this.currentDate[2] = theDate[2];
		var theFirstDay = this.getFirstDay(theDate[0], theDate[1]);
		var theMonthLen = this.getMonthLen(theDate[0], theDate[1]);
		
		this.calendarObj.style.display = "block";
		var theRows = Math.ceil((theMonthLen + theFirstDay) / 7);
		while (this.body.rows.length > 0) {
			this.body.deleteRow(0);
		}
		
		var n = 0, day = 0;
		for (var i = 0; i < theRows; i++) {
			var theRow = this.body.insertRow(i);
			for (var j = 0; j < 7; j++) {
				n++;
				if (n > theFirstDay && n <= (theMonthLen + theFirstDay)) {
					day = n - theFirstDay;
					this.insertBodyCell(theRow, j, day);
				} else {
					var theCell = theRow.insertCell(j);
				}
			}
		}
		var offsetPos = this.getAbsolutePos(this.source);
		if ((document.body.offsetHeight - (offsetPos.y + this.source.offsetHeight - document.body.scrollTop)) < this.calendarPad.style.pixelHeight) {
			var calTop = offsetPos.y - this.calendarPad.style.pixelHeight + this.source.offsetHeight;
		} else {
			var calTop = offsetPos.y + this.calendarPad.style.pixelHeight + this.source.offsetHeight;
		}
		if ((document.body.offsetWidth - (offsetPos.x + this.source.offsetWidth - document.body.scrollLeft)) > this.calendarPad.style.pixelWidth) {
			var calLeft = offsetPos.x;
		} else {
			var calLeft = this.source.offsetLeft + this.source.offsetWidth;
		}
		this.calendarObj.style.pixelLeft = calLeft;
		this.calendarObj.style.pixelTop = calTop;
		return true;
	}
	
	this.getAbsolutePos = function(el) {
		var r = {x: el.offsetLeft, y: el.offsetTop};
		if (el.offsetParent) {
			var tmp = this.getAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		return r;
	}
	
	this.getFirstDay = function(theYear, theMonth) {
		var firstDate = new Date(theYear, theMonth - 1, 1);
		return firstDate.getDay();
	}
	
	this.getMonthLen = function(theYear, theMonth) {
		theMonth--;
		var oneDay = 1000 * 60 * 60 * 24;
		var thisMonth = new Date(theYear, theMonth, 1);
		var nextMonth = new Date(theYear, theMonth + 1, 1);
		var len = Math.ceil((nextMonth.getTime() - thisMonth.getTime()) / oneDay);
		return len;
	}
	
	this.insertBodyCell = function(theRow, j, day) {
		var theCell = theRow.insertCell(j);
		theCell.className = this.bodyCellClassName;
		theCell.style.cssText = "text-align:center; cursor:hand;" + this.dayTextStyle;
		if (day == this.currentDate[2]) {
			theCell.className = this.todayClassName;
			theCell.style.cssText = "text-align:center; cursor:hand;" + this.todayTextStyle;
		}
		theCell.innerHTML = day;
		theCell.onclick = function() {
			theCalendar.sltDate = theCalendar.currentDate[0] + "-" + theCalendar.currentDate[1] + "-" + day;
			theCalendar.target.value = theCalendar.sltDate;
			theCalendar.hide();
		}
	}
	
	this.hide = function() {
		this.calendarObj.style.display = "none";
	}
	
	this.setup = function() {
		this.addPad();
		this.addPanel();
		this.setDefaultDate();
	}
}