function Calendar(name,x,y,hSpace,vSpace) {
	this.base = DynamicObj;
	this.base(name);
		
	this.x = x;
	this.y = y;
	this.hSpace = hSpace;	// dependant on font size
	this.vSpace = vSpace;	// undependant on font size
	
	// range occuying first column
	this.current1stColumn = 0;
	// weekdays index of the 1-29 range
	this.index1st = 0;
	this.currentDaysInMonth = 31;

	this.daylist = new Array('Mo','Tu','We','Th','Fr','Sa','Su');
	
	this.spot = new Array();
	this.monthyear;

	this.w = 7*this.hSpace;
	this.h = 8*this.vSpace;
	
	this.isVisible = true;
	
	this.overContainer = false;
	
	this.build = function(){
		this.css = css(this.id,this.x,this.y,this.w,this.h,'#eee',this.isVisible);
		this.div = '<div id="'+this.id+'">\n';
		this.css += css(this.id+'MonthYear',this.hSpace,0,this.hSpace*5,this.vSpace);
		this.div += '<table height='+this.vSpace+' cellpadding=0 cellspacing=0 border=0><tr>'
		this.div+='<td class=monthselect align=center width='+this.hSpace+'><A href="#" onclick="calendarIncMonth(-1);return false;">&lt;</A></td>'
		this.div+='<td class=monthselect width='+(this.hSpace*5)+'><div id="'+this.id+'MonthYear'+'"></div></td>'
		this.div+='<td class=monthselect align=center width='+this.hSpace+'><A href="#" onclick="calendarIncMonth(1);return false;">&gt;</A></td>'
		this.div += '</tr></table>';
		this.div += '<table height='+this.vSpace+' cellpadding=0 cellspacing=0 border=0><tr>'
		for (var i=0;i<7;i++) {
			this.div+='<td class=datecells align=center width='+this.hSpace+'>'+this.daylist[i]+'</td>'
		}
		this.div += '</tr></table>';	
		for (var i=0;i<7;i++) {
			this.css+=css(this.id+'Cal'+i,this.hSpace*i,this.vSpace*2,this.hSpace,this.vSpace*5)
			this.div+='<div id="'+this.id+'Cal'+i+'">'
			for(var j = i+1;j < 32;j+=7){
				this.div+='<div id="'+this.id+'Day'+j+'" class=datecells style="width: '+this.hSpace+'px; height: '+this.vSpace+'px; text-align:center">&nbsp;<A href="#" onclick="calendarSetValue('+j+');return false;">'+j+'</A></div>'
			}
			this.div+='</div>\n'
		}
		this.div+='</div>'
	}
	this.activate = function(){	
		this.getRef();
		this.clipTo(0,this.w,this.h,0);
		this.monthyear = new DynamicObj(this.id+'MonthYear', this);		
		this.monthyear.x = this.hSpace;
		this.monthyear.y = 0;
		this.monthyear.w = this.hSpace*5;
		this.monthyear.h = this.vSpace;
		this.monthyear.getRef();
		this.monthyear.clipTo(0,this.w,this.h,0);
		for (var i=0;i<7;i++) {
			var range = new DynamicObj(this.id+'Cal'+i, this);
			range.x = this.hSpace*i;
			range.y = this.vSpace*2;
			range.w = this.hSpace;
			range.h = this.vSpace*5;
			range.getRef();
			// IE fix
			range.clipTo(0,this.hSpace,this.vSpace*5,0);			
			this.spot[i] = range;
		}
		dynEl = this;
		//e.g. if overflow, don't rely on mouse hightlight for overContainer
		this.addEventHandler("onmouseover", function(){
			//window.status = arguments[2] + '<' + dynEl.x + ' || ' + arguments[2] + '>(' + dynEl.x + ' + ' + dynEl.w + ') || ' + arguments[3] + '<' + dynEl.y + ' || ' + arguments[3] + '>(' + dynEl.y + ' + ' + dynEl.h + ')';
			dynEl.overContainer = true;
			dynEl.target.focus();
		});
		this.addEventHandler("onmouseout", function(){
			//each mouse highlight generates mouseout
			dynEl.overContainer = false;
			dynEl.target.focus();
		});
	}
	this.setDate = function(date,month,year)
	{
		this.month = month;
		this.year = year;
		var d = new Date();		
		d.setDate(1);
		//now set the date
		d.setFullYear(year);
		d.setMonth(month - 1);
		
		var pos = d.getDay() + 6
		//add position index of Sun (0) element in this.daylist
		if(pos > 6)
			pos = pos - 7;	
		this.setCDate(pos, getMonthLength(this.month,this.year));		
	}
	this.nextMonth = function()
	{
		if(this.month == 12) {
			this.month = 1;
			this.year += 1;
		}else
			this.month++;
		var pos = this.index1st + this.currentDaysInMonth;
		if(pos < 35)
			pos = pos - 28;
		else
			pos = pos - 35;
		this.setCDate(pos, getMonthLength(this.month,this.year));
	}
	this.lastMonth = function()
	{
		if(this.month == 1) {
			this.month = 12;
			this.year -= 1;
		}else
			this.month--;
		var monthLength = getMonthLength(this.month,this.year);		
		var pos = this.index1st - monthLength;
		if(pos > -29)
			pos = pos + 28;
		else
			pos = pos + 35;		
		this.setCDate(pos, monthLength);	
	}
	this.setCDate = function(firstWeekDay, daysInMonth)
	{	
		this.monthyear.setContents('<table cellpadding=0 cellspacing=0 border=0><tr><td class=datecells align=center width='+(this.hSpace*5)+' height='+this.vSpace+'>'+this.month+' '+this.year+'</td></tr></table>');
		if(firstWeekDay != this.index1st){
			var heightOffset,widthOffset;
			if(firstWeekDay < this.index1st){
				heightOffset = -this.vSpace;
				diff = this.index1st-firstWeekDay;
				widthOffset = (7-diff) * this.hSpace;
				to = diff;
			}else{
				heightOffset = 0;
				diff = firstWeekDay-this.index1st;
				widthOffset = diff * this.hSpace;
				to = 7-diff;
			}
			var what = this.spot[this.current1stColumn]		
			for(var i = 0;i < to;i++)
			{
				what.moveBy(widthOffset, heightOffset);
				if(this.current1stColumn == 6)
					this.current1stColumn = 0;
				else
					this.current1stColumn++;
				what = this.spot[this.current1stColumn]		
			}			
			widthOffset = widthOffset - this.w;
			heightOffset = heightOffset + this.vSpace;
			for(var j = this.current1stColumn;i < 7;i++)
			{
				what.moveBy(widthOffset, heightOffset);
				if(j == 6)
					j = 0;
				else
					j++;
				what = this.spot[j];
			}
			this.index1st = firstWeekDay;
		}		
		if(this.currentDaysInMonth != daysInMonth){			
			if(this.currentDaysInMonth < daysInMonth){
				inc = 1;
				i = this.currentDaysInMonth - 28;
				to = daysInMonth - 28;
				d = this.vSpace*5;
			}else{
				inc = -1;
				i = this.currentDaysInMonth - 29;
				to = daysInMonth - 29;
				d = this.vSpace*4;
			}
			for(;i != to;i+=inc)
			{
				this.spot[i].clipTo(0,this.hSpace,d,0);			
			}
			this.currentDaysInMonth = daysInMonth;
		}
	}
}
Calendar.prototype = new DynamicObj;


