/* --- Cal --- */

function Cal(id,y,m,d) {
	this.id = id;
	this.y = this.build_y = y;
	this.m = this.build_m = m;
	this.d = this.build_d = d;
	this.onclk = function() {};
}
Cal.prototype = {	
	
	GenCal : function(noHead) {
		var cal = '';
		var ab = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec');
		if (!noHead) {
			cal +=  '<div class="cal_wrapper"><div id="'+this.id+'" class="cal_square">';
		}
		cal += '	<div class="top">';
		if (!this.NoMoving) {
		cal +='		<a class="pY">&laquo;</a> <a class="pM">&laquo;</a>';
		}
		cal +='		'+ab[this.build_m-1]+' '+this.build_y;
		if (!this.NoMoving) {
		cal +='		<a class="nM">&raquo;</a> <a class="nY">&raquo;</a>';
		}
		cal +='	</div>';
		cal +='	<div class="row head">';
		cal +='		<a>Su</a><a>Mo</a><a>Tu</a><a>We</a><a>Th</a><a>Fr</a><a>Sa</a>';
		cal +='	</div>';
		cal +='	<div class="row">';
		
		cur = new Date();
		da = new Date(this.build_y,this.build_m-1,1);
		da.setDate( da.getDate() + (-1 - da.getUTCDay()) ) ;

		for(c=1;c<36;c++){
			cl = '';
			h = false;

			da.setDate( da.getDate() + 1 );
			
			if ( da.getMonth() == this.build_m-1 ) {
				if (da.getTime() > cur.getTime()) { 
					cl += ' no'; 
				}
				if (EventOnDate(da)) {
					cl += ' eventdate';
					h = l + '#d'+da2Stamp(da);
				}
				if (cl.length>0) { cl = ' class="'+cl+'"'; }
			}
			
			cal +='<a'+((h)?(' href="'+h+'"'):(''))+cl+'>' + ( (da.getMonth() != this.build_m-1) ? (''):(da.getUTCDate()) ) + '</a>';
			
			if(c%7==0 && c!=35) { cal +='</div><div class="row">'; }
		}
		cal +='</div>';
		if (!noHead) {
			cal +=  '</div></div>';
		}
		
		return cal;
	},
	
	output : function () {
		document.write( this.GenCal() );
		this.AddCalEvents(this);
	},
	
	replaceCal : function () {
		$i(this.id).innerHTML = this.GenCal(true);
		this.AddCalEvents(this);
	},
	
	AddCalEvents : function(CalObj) {				
		as = $i(this.id).getElementsByTagName('a');
		for(i=0; i<as.length; i++) {
			if (as[i].className.length > 0) {
				switch(as[i].className) {
					case('pY'): as[i].onclick = function() { CalObj.Calmove('y',-1); }; break;
					case('pM'): as[i].onclick = function() { CalObj.Calmove('m',-1);  }; break;
					case('nY'): as[i].onclick = function() { CalObj.Calmove('y',1);  }; break;
					case('nM'): as[i].onclick = function() { CalObj.Calmove('m',1);  }; break;
				}
			} else if (as[i].innerHTML * 1 > 0) {
				if (as[i].innerHTML * 1 > 20 && i < 14) { 
					tm = this.build_m * 1 - 1;
					this.ChkOnlyMonth(as[i]);
				} else if (as[i].innerHTML * 1 < 8 && i > 28) { 
					tm = this.build_m * 1 + 1;	
					this.ChkOnlyMonth(as[i]);	
				} else if (as[i].innerHTML == this.d && this.build_m == this.m) {
					tm = this.build_m;
					as[i].className = 'selected';
					as[i].parentNode.className += ' selected';
					this.selected = as[i];
				} else { tm = this.build_m; }
				if (as[i].className != 'no') {
					as[i].onclick = function() { CalObj.dateClicked(this); }
				}
				as[i].setAttribute('id', this.id + '_' + this.build_y + '-' + str_padL(tm,'0',2) + '-' + str_padL(as[i].innerHTML,'0',2));
			}
		}
	},
	
	ChkOnlyMonth : function(obj) {	
		if (this.NoMoving) { 
			obj.innerHTML = '';
			obj.onclick = null;
			obj.className = 'no';
		} 
	},
	
	Calmove : function(t, mov) { 
		da = new Date(this.build_y, this.build_m - 1 , 1);
		if (t=='m') {
			da.setMonth( da.getMonth() + mov ) ;
		} else if(t=='y') {
			da.setFullYear( da.getFullYear() + mov ) ;
		}
		this.build_m = da.getMonth() + 1;
		this.build_y = da.getFullYear();
		this.build_d = null;
		this.replaceCal();
	},
	
	highLightSelected : function () {
		/*if (this.selected) { 
			this.selected.className = '';
			if (this.selected.parentNode) {
				this.selected.parentNode.className = 'row';
			}
		}
		obj = $i( this.id + '_' + this.CalWhatDay() );
		this.selected = obj;
		obj.className = 'selected';
		obj.parentNode.className += ' selected';
		this.selected_d = this.d;
		this.onclk(this.y,this.m,this.d);*/
	},
	
	dateClicked : function (obj) {
		if (obj) {
			parts = obj.id.replace(this.id + '_','').split('-');
			y = parts[0];
			m = parts[1];
			d = parts[2];
			this.y = this.build_y = y;
			this.m = this.build_m = str_padL(m,'0',2);
			this.d = this.build_d = str_padL(d,'0',2);	
			this.highLightSelected();
		}
	},
	
	selectDateStamp : function(stamp) {
		parts = stamp.split('-');
		if (parts[0] != this.build_y || parts[1] != this.build_m) {
			afterjump = true;
		} else { afterjump = false; }
		this.y = this.build_y = parts[0];
		this.m = this.build_m = parts[1];
		this.d = this.build_d = parts[2];
		if (afterjump) { this.replaceCal(); }
		this.highLightSelected();
	},
	
	CalWhatDay: function() {
		return this.y + '-' + this.m + '-' + this.d;	
	}

}

function str_padL(str, padwith, howlong) {
	str = str.toString();
	if (str.length < howlong && str.length > 0) {
		while(str.length < howlong) {
			str = padwith+str;	
		}
	}
	return str;
}
function da2Stamp(da) { 
	return da.getFullYear() + '-' + str_padL((da.getMonth() + 1),'0',2) + '-' + str_padL(da.getDate(),'0',2);
}

var eventListIndex = new Array();
function EventOnDate(da) {
	
	if ( eventListIndex[ da2Stamp(da) ] ) {
		return true;
	}
}
function renderEventList() {
	for(i in eventList) {
		d = eventList[i].split('/');
		s = '20'+d[2]+'-'+d[0]+'-'+d[1];
		eventListIndex[s] = true;
	}
}