calendar = {
	place:null,
	mode:"d",
	colorTypes:{
		"hoverDay":"#EDF0F3",
		"deHoverDay":"#FFFFFF",
		"hoverToday":"#DFE6EB",
		"deHoverToday":"#DAE2E8"		
	},
	dayColors:[],
	showNextMonth:true,
	selDay:{d:-1,m:-1,y:-1},
	getDay:function(d, m, y){},
	cleanDay:function(){
		this.getDay(-1, -1, -1);
	},
	init:function(el, day, mode){
		if(!mode)mode = "d";
		mode = mode.replace(/(this|next|prev)/, "");
		this.mode = mode;
		this.place = el;
		this.selDay = day;
		this.getMonth(day.m, day.y);
	},
	getMonth:function(m, y) {
		this.place.innerHTML = this.buildCal(m, y);
		return false;
	},
	setDayColor:function(x, type) {
		var lim = (this.mode == "w") ? 7:1;
		for(var i=0;i<lim;i++){
			var xday = ge('day' + (x+i));
			if(xday)xday.style.backgroundColor = this.colorTypes[this.dayColors[x+i][type]];
		}
	},
	buildCal:function(m, y) {
		var mn = ebrowse_mn;
		var dim = [31,0,31,30,31,30,31,31,30,31,30,31];

		var oD = new Date(y, m-1, 1);
		oD.od = oD.getDay();
		if (oD.od == 0) {
			oD.od = 7;
		}

		var dontDoLine = 0;
		var leftStyle = '';
		var todayDate = new Date();
		
		dim[1] = ( ((oD.getFullYear() % 100 != 0) && (oD.getFullYear() %4 == 0)) || (oD.getFullYear() % 400 == 0) ) ? 29 : 28;

		var t = [];
		switch(this.mode){
		case 'm':
			var selDay = (y==this.selDay.y) ? this.selDay.m : 0;
			nextYear = y + 1;
			lastYear = y - 1;
			t.push('<div><table class="main" cols="7" cellpadding="0" border="0" cellspacing="0"><tr align="center">');
			t.push('<td class="monthArr"><a class="monthArrow" href="" onclick="return calendar.getMonth(1,'+lastYear+')">&larr;</a></td>');
			t.push('<td colspan="5" align="center" class="month">'+y+'</td>');
			t.push('<td class="monthArr"><a class="monthArrow" href="" onclick="return calendar.getMonth(1,'+nextYear+')">&rarr;</a></td></tr><tr align="center">');
			t.push('</tr></table><table class="main" cols="2" cellpadding="0" border="0" cellspacing="0">');
			for(var i=1;i<=12;i++) {
				leftStyle = "";
				if(i%2==1){
					if(i>1)t.push('</tr><tr align="center">');
					leftStyle = ' dayLeft';
				}
				this.dayColors[i] = (i==selDay) ? {'over':'hoverToday','out':'deHoverToday'} : {'over':'hoverDay','out':'deHoverDay'};
				clDay = (i==selDay)?'today':'day';
				curDate = new Date(y, i-1, 1);
				if(i!=selDay && curDate < todayDate)clDay += " pastDay";
				t.push('<td class="'+clDay+leftStyle+'" style="width:50%" onclick="calendar.getDay(1,'+i+','+y+',\'m\')" onmouseover="calendar.setDayColor('+i+',\'over\')" onmouseout="calendar.setDayColor('+i+',\'out\')" id="day'+i+'">'+mn[i-1]+'</td>');
			}
			
			t.push('</tr></table></div>');
			return t.join("");
			break;

		default:
			var selDay = (y==this.selDay.y && m==this.selDay.m) ? this.selDay.d : 0;
			if (m == 12) {
				nextMonth = 1; nextYear = y + 1;
			} else {
				nextMonth = m + 1; nextYear = y;
			}

			if (m == 1) {
				lastMonth = 12; lastYear = y - 1;
			} else {
				lastMonth = m - 1; lastYear = y;
			}
			t.push('<div><table class="main" cols="7" cellpadding="0" border="0" cellspacing="0"><tr align="center">');
			t.push('<td class="monthArr"><a class="monthArrow" href="" onclick="return calendar.getMonth('+lastMonth+','+lastYear+',\''+this.mode+'\')">&larr;</a></td>');
			t.push('<td colspan="5" align="center" class="month">'+mn[m-1]+' '+y+'</td>');
			t.push('<td class="monthArr"><a class="monthArrow" href="" onclick="return calendar.getMonth('+nextMonth+','+nextYear+',\''+this.mode+'\')">&rarr;</a></td></tr><tr align="center">');
			for (var s=0;s<7;s++) {
				t.push('<td class="daysofweek">'+ ebrowse_days.substr(s*ebrowse_dayname_length,ebrowse_dayname_length)+'</td>');
			}
			t.push('</tr><tr align="center">');

			for(var i=1;i<=42;i++) {
				var leftStyle = (i % 7 == 1) ? ' dayLeft' : '';
				var x = ((i-oD.od>=0) && (i-oD.od<dim[m-1])) ? i-oD.od+1 : 0;
				var curDate = new Date(y, m - 1, i-oD.od+1);
				var x1 = x;
				var lim = 1;
				if(this.mode=="w"){
					var x1 = i - oD.od - i % 7 + 2;
					if( i % 7 == 0) x1 -= 7;
					if(selDay){
						var lim = 8 - (selDay + oD.od - 1) % 7;
						if(lim == 8) lim = 1;
					}
				}
				this.dayColors[x] = (x>=selDay && x<selDay + lim) ? {'over':'hoverToday','out':'deHoverToday'} : {'over':'hoverDay','out':'deHoverDay'};
				if(x>=selDay && x<selDay + lim){
					clDay = 'today'+leftStyle;
				}else{
					clDay = 'day'+leftStyle;
					if(curDate - todayDate + 86400000 < 0) clDay += " pastDay";
				}
				
				var onMouse = 'onclick="calendar.getDay('+x1+','+m+','+y+',\''+this.mode+'\')" onmouseover="calendar.setDayColor('+x1+',\'over\')" onmouseout="calendar.setDayColor('+x1+',\'out\')"';
				if (x > 0) {
					t.push('<td '+onMouse+' id="day'+x+'" class="'+clDay+'">'+x+'</td>');
				} else {
					if (i != 36) {
						if (!dontDoLine) {
							if(this.mode != "w")onMouse = "";
							date = (i>7 && this.showNextMonth)?curDate.getDate():"&nbsp";
							t.push('<td '+onMouse+' class="day noMonthDay'+leftStyle+'">'+date+'</td>');
						}
					} else {
						dontDoLine = 1;
					}
				}
				if ((i % 7 == 0) && (i<36)) {
					t.push('</tr><tr align="center">');
				}
			}
			t.push('</tr></table></div>');
			return t.join("");
			break;
		}
	}
}

