


calendar = function(id, d, highlight, adjusted)
{
	if (adjusted == undefined)
	{	
		var d = new Date(d * 1000);
	}
	this.id			= id;
	this.highlight	= highlight;
	this.date_obj	= d;
	this.write		= this.build_calendar;
	this.month		= d.getMonth();
	this.yearmonth	= d.getFullYear();
	this.yearmonth	+= (this.month+1).toString().length == 1 ? '0' + (this.month+1).toString() : (this.month+1).toString();
	this.date		= d.getDate();
	this.day		= d.getDay();
	this.year		= d.getFullYear();
	this.hours		= d.getHours();
	this.minutes	= d.getMinutes();
	this.seconds	= d.getSeconds();
	this.selected_date = highlight == false ? '' : this.year + '' + this.month + '' + this.date;
	d.setDate(1);
	this.firstDay = d.getDay();
	d.setDate(this.date);
}

calendar.prototype =
{
	build_calendar : function()
	{
		var month = this.month+1;
		var month_path = path +lang+'/eventi/' + this.year + '/';
		month_path += (month.toString().length<2) ? '0' : '';
		month_path += month + '.html';
		
		var str = '';
		//	Calendar Heading
		str = '<div id="cal' + this.id + '">';
		str += '<p id="calendar-nav"><a href="javascript:void(0)" onClick="' + this.id + '.change_month(-1)">&laquo;</a>';
		str += '<a href="javascript:void(0)" onClick="' + this.id + '.change_month(+1)">&raquo;</a></p>';
		str += '<h5 class="title"><a class="navMonth" href="'+ month_path +'">' + months[this.month] + ' ' + this.year + '</a></h5>';
		str += '<ul id="calendar">';
		
		days.each(function(day) { str += '<li class="days">' + day + '<\/li>' });
		
		//	Day Cells
		selDate = (last_date != '') ? last_date : this.date;
		
		for (j = 0; j < 42; j++)
		{
			var displayNum = (this.firstDay+7)%7 == 0 ? (j - this.firstDay - 5) : (j - this.firstDay + 2);
			
			//str += '<li class="day-number blank">'+displayNum+'</li>';
			
			if (displayNum <= 0) // leading empty cells
			{
				str += '<li class="day-number blank"></li>';
			}
			
			else if (j < this.firstDay - 1) // leading empty cells
			{
				str += '<li class="day-number blank">'+displayNum+'</li>';
			}
			
			else if (displayNum == selDate && this.highlight == true) // Selected date
			{
				str += '<li class="day-number selected" id="today">' + displayNum + '<\/li>';
			}
			else if (displayNum > this.total_days())
			{
				str += '<li class="day-number blank"></li>';
			}
			else  // Unselected days
			{
				str += '<li class="day-number">' + displayNum + '<\/li>';
			}
			
		}
		str += '<\/ul>';	
		str += '<div class="float-fixer"></div><\/div>';
		return str;
	},
	
	add_events : function()
	{
		new Ajax.Request(
				path + lang + "/ajax/getEventsMap/"+ this.yearmonth,
				{
					method		: 	'post', 
					onSuccess	: 	function(r)
					{
						var jsoned 	= 	r.responseText.evalJSON(true);
						$$('.day-number').each(function(li)
						{
							index 	= 	parseInt(li.innerHTML.stripTags());
							if (jsoned.eventsmap[index])
							{
								cellid 	= 	'e' + index;
								var 	tooltip 	= 	new Element('div', { 'class': 'tooltip' , 'id': cellid });
								var 	ttop 		= 	new Element('div', { 'class': 'tooltip-top' });
								var 	tbottom 	= 	new Element('div', { 'class': 'tooltip-bottom' });
								var 	tcorp 		= 	new Element('div', { 'class': 'tooltip-corp' });
								
								jsoned.eventsmap[index].each(function(ev){
									var 	a 	= 	new Element('a', { 'href' : path+ev.url}).update(ev.titolo.escapeHTML());
									var 	p 	= 	new Element('p').update(ev.luogo);
									tcorp.appendChild(a);
									tcorp.appendChild(p);
								});
								tooltip.appendChild(ttop);
								tooltip.appendChild(tcorp);
								tooltip.appendChild(tbottom);
								tooltip.style.position = "absolute";
								tooltip.style.bottom 	= 	(li.getWidth()-1)/2 + 'px';
								tooltip.style.left 		= 	-(li.getWidth()-1)*6/2 + 'px';
								tooltip.zIndex 			= 	10000;
								li.update('<a href="javascript:void(0)" rel="' + cellid + '" class="calendar-event">'+index+'</a>');
								tooltip.hide();
								li.appendChild(tooltip);
								li.observe('mouseover', jtip.showtip);
							}
							
						});
					}
				}
			);
		
	},
	
	
	//	Total number of days in a month
	total_days : function()
	{	
		switch(this.month)
		{
			case 1: // Check for leap year
				if ((  this.date_obj.getFullYear() % 4 == 0
					&& this.date_obj.getFullYear() % 100 != 0)
					|| this.date_obj.getFullYear() % 400 == 0)
					return 29;
				else
					return 28;
			case 3:
			case 5:
			case 8:
			case 10:
				return 30;
			default:
				return 31;
		}
	},

	
	//	Change to a new month
	change_month : function(mo)
	{		
		
		cal = this;
		
		if (current_month != '')
		{
			cal.date_obj.setMonth(current_month);
			cal.date_obj.setYear(current_year);
		
			current_month	= '';
			current_year	= '';
		}
					
		var newMonth = cal.date_obj.getMonth() + mo;
		var newDate  = cal.date_obj.getDate();
		
		
		if (newMonth == 12)
		{
			cal.date_obj.setYear(cal.date_obj.getFullYear() + 1)
			newMonth = 0;
		}
		else if (newMonth == -1)
		{
			cal.date_obj.setYear(cal.date_obj.getFullYear() - 1)
			newMonth = 11;
		}
		
		if (newDate > 28)
		{
			var newYear = cal.date_obj.getFullYear();
			
			switch(newMonth)
			{
				case 1: // Check for leap year
					if ((newYear % 4 == 0 && newYear % 100 != 0) || newYear % 400 == 0)
					{
						if (newDate > 29) newDate = 29;
					}
					else
					{
						if (newDate > 28) newDate = 28;
					}
				case 3:
				case 5:
				case 8:
				case 10:
					if (newDate > 30) newDate = 30;
				default:
					if (newDate > 31) newDate = 31;
			}
		}
		
		cal.date_obj.setDate(newDate);
		cal.date_obj.setMonth(newMonth);
		new_mdy	= cal.date_obj.getFullYear() + '' + cal.date_obj.getMonth() + '' + cal.date;
		highlight = (cal.selected_date == new_mdy) ? true : false;
		cal = new calendar(cal.id, cal.date_obj, highlight, true);
		document.getElementById('cal' + cal.id).innerHTML = cal.write();
		cal.add_events();
	},
	
	//	Finalize the date string
	date_str : function(time)
	{
		var month = this.month + 1;
		if (month < 10)
			month = '0' + month;
			
		var day		= (this.date  < 10) 	?  '0' + this.date		: this.date;
		var minutes	= (this.minutes  < 10)	?  '0' + this.minutes	: this.minutes;
			
		if (format == 'us')
		{
			var hours	= (this.hours > 12) ? this.hours - 12 : this.hours;
			var ampm	= (this.hours > 11) ? 'PM' : 'AM'
		}
		else
		{
			var hours	= this.hours;
			var ampm	= '';
		}
		
		if (time == 'y')
		{
			return this.year + '-' + month + '-' + day + '  ' + hours + ':' + minutes + ' ' + ampm;		
		}
		else
		{
			return this.year + '-' + month + '-' + day;
		}
	}	
}


