/*
	Author: z.c
	  Date: 19.04.2007
	******************
	USAGE:
		<script>
			var mouseX = 0;
			var mouseY = 0;
			function getMousePos(e) {
				if (!e) var e = window.event||window.Event;
				if("undefined" != typeof e.pageX) {
					mouseX = e.pageX;
					mouseY = e.pageY;
				} else {
					mouseX = e.clientX + ((document.body.scrollLeft == 0) ? document.documentElement.scrollLeft : document.body.scrollLeft);
					mouseY = e.clientY + ((document.body.scrollTop == 0) ? document.documentElement.scrollTop : document.body.scrollTop);
				}
			}
			if(window.Event && document.captureEvents) document.captureEvents(Event.MOUSEMOVE);
			document.onmousemove = getMousePos;
		
			var fieldSw = -1;
			var calInst = -1;
			function dateOutput(tmpDate) {
				alert(tmpDate);
			}
			function calHide(e) {
				calInst.hideCal(e);
			}
			function displayCal() {
				calInst.showCal(mouseY+"px",mouseX+"px");
			}
			calInst = new zcCalObj("calInst",dateOutput,calHide);
		</script>
*/
var zcCalObj = function (tmpInstanceName, tmpOutputFunc, tmpHideFunc) {
	this.instanceName = tmpInstanceName;
	this.outputFunc = tmpOutputFunc;
	this.hideFunc = tmpHideFunc;
	
	this.calDiv = document.createElement("DIV");
	this.calDiv.id = "zcCal-"+tmpInstanceName;
	this.calDiv.style.position = "absolute";
	this.calDiv.style.display = "none";
	this.calDiv.style.top = "0px";
	this.calDiv.style.left = "0px";
	this.calDiv.style.width = "150px";
	this.calDiv.style.cursor="default";
	this.calDiv.style.border="1px solid #000000";
	this.calDiv.style.backgroundColor = "#FFFFFF";
	document.body.appendChild(this.calDiv);
	
	var tmpDate = new Date();
	this.curMonth = tmpDate.getMonth();
	this.curYear = tmpDate.getFullYear();
	
	this.prevMonth = function () {
		var tmpDate = new Date(this.curYear, this.curMonth, 1, 0, 0, 0);
		tmpDate.setMonth(tmpDate.getMonth()-1);
		this.curMonth = tmpDate.getMonth();
		this.curYear = tmpDate.getFullYear();
		this.createContent();
	}
	this.nextMonth = function () {
		var tmpDate = new Date(this.curYear, this.curMonth, 1, 0, 0, 0);
		tmpDate.setMonth(tmpDate.getMonth()+1);
		this.curMonth = tmpDate.getMonth();
		this.curYear = tmpDate.getFullYear();
		this.createContent();
	}
	this.prevYear = function () {
		var tmpDate = new Date(this.curYear, this.curMonth, 1, 0, 0, 0);
		tmpDate.setYear(tmpDate.getFullYear()-1);
		this.curMonth = tmpDate.getMonth();
		this.curYear = tmpDate.getFullYear();
		this.createContent();
	}
	this.nextYear = function () {
		var tmpDate = new Date(this.curYear, this.curMonth, 1, 0, 0, 0);
		tmpDate.setYear(tmpDate.getFullYear()+1);
		this.curMonth = tmpDate.getMonth();
		this.curYear = tmpDate.getFullYear();
		this.createContent();
	}
	
	this.prepareOutput = function (tmpD) {
		var tmpOutput = tmpD.title.split(".");
		this.outputFunc(new Date(tmpOutput[2], (tmpOutput[1]-1), tmpOutput[0], 0, 0, 0));
		this.hideCal();
	}
	
	this.hideCal = function (e) {
		e = e || window.event;
	    var target = e.srcElement || e.target;
		if (target.id == "zcCalEL") return;
		this.calDiv.style.display = "none";
		document.onmouseup = function() {};
	}
	
	this.showCal = function (tmpTop, tmpLeft) {
		this.calDiv.style.top = tmpTop;
		this.calDiv.style.left = tmpLeft;
		this.calDiv.style.display = "block";
		document.onmouseup = this.hideFunc;
	}
	
	this.createContent = function() {
		var tmpContent = "";
		tmpContent+= "<table cellpadding=0 cellspacing=0 border=0 width=100%>";
			tmpContent+= "<tr>";
				tmpContent+= "<td id='zcCalEL' align=center style='width:20px; cursor:pointer;' onClick='"+this.instanceName+".prevMonth();' onMouseOver='this.style.backgroundColor=\"#CCCCCC\"' onMouseOut='this.style.backgroundColor=\"#FFFFFF\"'>&lt;</td>";
				tmpContent+= "<td align=center width=30><b>"+(this.curMonth+1)+"</b></td>";
				tmpContent+= "<td id='zcCalEL' align=center style='width:20px; cursor:pointer;' onClick='"+this.instanceName+".nextMonth();' onMouseOver='this.style.backgroundColor=\"#CCCCCC\"' onMouseOut='this.style.backgroundColor=\"#FFFFFF\"'>&gt;</td>";
				tmpContent+= "<td id='zcCalEL' align=center style='width:20px; cursor:pointer;' onClick='"+this.instanceName+".prevYear();' onMouseOver='this.style.backgroundColor=\"#CCCCCC\"' onMouseOut='this.style.backgroundColor=\"#FFFFFF\"'>&lt;</td>";
				tmpContent+= "<td align=center><b>"+this.curYear+"</b></td>";
				tmpContent+= "<td id='zcCalEL' align=center style='width:20px; cursor:pointer;' onClick='"+this.instanceName+".nextYear();' onMouseOver='this.style.backgroundColor=\"#CCCCCC\"' onMouseOut='this.style.backgroundColor=\"#FFFFFF\"'>&gt;</td>";
			tmpContent+= "</tr>";
		tmpContent+= "</table>";
		tmpContent+= "<table cellpadding=0 cellspacing=0 border=0 width=100% style='border-top:1px solid #000000;'>";
			tmpContent+= "<tr>";
				tmpContent+= "<td align=center><b>SO</b></td>";
				tmpContent+= "<td align=center><b>MO</b></td>";
				tmpContent+= "<td align=center><b>DI</b></td>";
				tmpContent+= "<td align=center><b>MI</b></td>";
				tmpContent+= "<td align=center><b>DO</b></td>";
				tmpContent+= "<td align=center><b>FR</b></td>";
				tmpContent+= "<td align=center><b>SA</b></td>";
			tmpContent+= "</tr>";
			
			tmpContent+= "<tr>";
			var tmpDate = new Date(this.curYear, this.curMonth, 1, 0, 0, 0);
			var tmpDate2 = new Date(tmpDate);
			tmpDate2.setDate(tmpDate2.getDate()-tmpDate.getDay());
			for (var x=0;x<tmpDate.getDay();x++) {
				tmpContent+= "<td align=center style='color:#CCCCCC;'>"+tmpDate2.getDate()+"</td>";
				tmpDate2.setDate(tmpDate2.getDate()+1);
			}
			while (tmpDate.getMonth() == this.curMonth) {
				if (tmpDate.getDay() == 0) tmpContent+= "</tr><tr>";  
				tmpContent+= "<td align=center style='cursor:pointer;' onClick='"+this.instanceName+".prepareOutput(this);' title='"+tmpDate.getDate()+"."+(this.curMonth+1)+"."+(this.curYear)+"' onMouseOver='this.style.backgroundColor=\"#CCCCCC\"' onMouseOut='this.style.backgroundColor=\"#FFFFFF\"'>"+tmpDate.getDate()+"</td>";
				tmpDate.setDate(tmpDate.getDate()+1);
			}
			while (tmpDate.getDay()>0) {
				tmpContent+= "<td align=center style='color:#CCCCCC;'>"+tmpDate.getDate()+"</td>";
				tmpDate.setDate(tmpDate.getDate()+1);
			}
			tmpContent+= "</tr>";
		tmpContent+= "</table>";
		this.calDiv.innerHTML = tmpContent;
	}
	this.createContent();
}