var shortdays = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
function Calendar() {
	var month; 
	var year;
	var attachToElement;
	var useBlanksForMonths = false;
	var calno = 1;
	
	var d = new Date();
	var startCalWithDay = 1;
	
	function setMonth(month) { this.month = month; }
	function setAttachToElement(element) { this.attachToElement = element; }
	function setUseBlanksForMonths(boolval) { if(boolval) this.useBlanksForMonths = true; }
	function setYear(year) { this.year = year; }

	this.setMonth=setMonth;
	this.setYear = setYear;
	this.setAttachToElement=setAttachToElement;
	this.setUseBlanksForMonths=setUseBlanksForMonths;

	function generateHTML() {
		d.setDate(1);
		var curryear = (this.year == null || this.month == undefined) ? d.getFullYear() : this.year;
		d.setFullYear(curryear);
		var currmonth = (this.month == null || this.month == undefined) ? d.getMonth(): this.month;
		d.setMonth(currmonth);
		var firstday = d.getDay();
		var startfromdate = -(firstday - startCalWithDay);
		if (startfromdate == 1) { //if startCalWithDay = Monday(1) and firstday = Sunday (0)
			startfromdate = -6;
		}
		d.setDate(d.getDate() + startfromdate);

		var monthcontrol = $("<div />").addClass("tt_title").append(months[currmonth] + " " + d.getFullYear());
		var toc = "\n<table>";
		toc += "\n<tr class=\"alt\">";
		if (startCalWithDay == 1)
			for ( var i = 0; i < shortdays.length; i++)
				toc += "<td>" + shortdays[i].charAt(0) + "</td>";
		toc += "</tr>";
		toc += "\n<tr>";
		var dayboxes = 35;
		if(currmonth + 1 != 1) {
			var tempd = new Date();
			tempd.setMonth(5,0);
			if(startfromdate == -6 || startfromdate == -5 && tempd.getDate() == 31)
				dayboxes = 42;
			tempd = null;
		}
		
		for ( var i = 1; i <= dayboxes; i++) {
			toc += "<td>";
			if(currmonth != d.getMonth() && !this.useBlanksForMonths)
				toc += "<span class=\"greyout\">";
			else if(currmonth == d.getMonth())
				toc += "<span class=\"d"+d.getDate()+"\">";
			if(!this.useBlanksForMonths || currmonth == d.getMonth())
				toc += d.getDate();
			else  
				toc += "&nbsp;";
			if(!this.useBlanksForMonths || currmonth == d.getMonth())
				toc += "</span>";				
					
			toc += "</td>";
			 
			if (i % 7 == 0)
				toc += "</tr>\n<tr>";
			d.setDate(d.getDate() + 1);
		}
		toc += "</tr>\n</table>";
		var wrapper = $("<div />").attr("id", "gencal"+(calno++)).append(monthcontrol).append(toc);
		$(this.attachToElement).append(wrapper);
	}

	this.generateHTML=generateHTML;
}

