
var calendar = {
	id: "calendar1",
	villa_id: null,
	selected_month: new Date().getMonth(), // 0-11
    selected_year: new Date().getFullYear(), // 4-digit year
    selected_day: new Date().getDate(),
	
	elements: {},
	
	month_names: new Array('January','February','March','April','May','June','July','August','September','October','November','December'),
	day_names: new Array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
	
	write: function () 
	{
		var html = '';
		html += "<div id=\""+this.id+"\" class=\"calendar\">\n";
		total_days = calendar.get_days_in_month(this.selected_month,this.selected_year);
		first_day = calendar.get_first_day_in_month(this.selected_month,this.selected_year);
		total_weeks = Math.ceil((first_day + total_days) / 7);
		
		html += "\t<div>\n";
		html += "\t\t<table cellspacing=\"0\">\n";
		html += "\t\t\t<thead>\n";
		html += "\t\t\t\t<tr>\n";
		html += "\t\t\t\t\t<th colspan=\"7\">\n";
		html += "\t\t\t\t\t\t<a href=\"#\" class=\"prev\" onclick=\"return false;\"><img src=\"images/kal_left.gif\" border=\"0\" align=\"absmiddle\"/></a>\n";
		html += "\t\t\t\t\t\t<span>"+this.get_header_text()+"</span>\n";
		html += "\t\t\t\t\t\t<a href=\"#\" class=\"next\" onclick=\"return false;\"><img src=\"images/kal_right.gif\" border=\"0\" align=\"absmiddle\"/></a>\n";
		html += "\t\t\t\t\t</th>\n";
		html += "\t\t\t\t</tr>\n";
		html += "\t\t\t\t<tr>\n";
		for(d=0;d<this.day_names.length;d++) {
			html += "\t\t\t\t\t<td>"+this.day_names[d].substring(0,1)+"</td>\n"	
		}
		html += "\t\t\t\t</tr>\n";
		html += "\t\t\t</thead>\n";
		html += "\t\t\t<tbody>\n";
		
		cur_day = 0 - first_day;
		for(w=0;w<6;w++) {
			
			html += "\t\t\t\t<tr>\n";
			for(d=0;d<7;d++) {
				cur_class = "weekday";
				cur_date = new Date(this.selected_year, this.selected_month, cur_day).getDate();
				if (cur_day > 0 && cur_day <= total_days) {
					cur_class += " cur_month";
				}
				
				html += "\t\t\t\t\t<td class=\"" + cur_class + "\">" + cur_date + "</td>\n";
				cur_day++;
			}
			html += "\t\t\t\t</tr>\n";
			
		}
		html += "\t\t\t<tbody>\n";
		html += "\t\t</table>\n";
		html += "\t</div>\n";
		html += "</div>";
		
		document.write(html);
		
		browselinks = $A($$("#"+this.id+" div > table > thead > tr > th > a"));		
		Event.observe(browselinks.first(), "click", this.goto_prev_month.bind(this));
		Event.observe(browselinks.last(), "click", this.goto_next_month.bind(this));
		
		this.elements.header = $A($$("#"+this.id+" div > table > thead > tr > th > span")).first();
		this.elements.days = $$("#"+this.id+" > div > table > tbody > tr > td");
		
		if (this.editable == 1) {
			var this_calendar = this;
			
			$A(this.elements.days).each(function(item,i) {
				 Event.observe(item, "click", this_calendar.toggle_day.bind(this_calendar, i))
			});
			
			$(this.id).addClassName("editable");
		}
		
		this.update();
	},
	
	toggle_day: function(day_index)
	{
		total_days = calendar.get_days_in_month(this.selected_month,this.selected_year);
		first_day = calendar.get_first_day_in_month(this.selected_month,this.selected_year);
		
		cur_day = day_index - first_day;
		_date = new Date(this.selected_year,  this.selected_month, cur_day);
		
		var this_calendar = this;
		
		new Ajax.Request('calendarhandle.php?villa='+this.villa_id+'&date='+_date.getDate()+"-"+(_date.getMonth()+1)+"-"+_date.getFullYear(), {   
			onSuccess: function(transport) { 
				if (transport.responseText == "401") {
					alert("Geen toegang"); return;
				} else if (transport.responseText == "200") {
					$(this_calendar.elements.days[day_index]).addClassName("booked");
				} else {
					$(this_calendar.elements.days[day_index]).removeClassName("booked");
				}
			}
		}); 
	},
	
	goto: function(month, year)
	{
		this.selected_month = month;
		this.selected_year = year;
		
		this.update();
	},
	
	update: function()
	{
		this.elements.header.innerHTML = this.get_header_text();
		
		var this_calendar = this;
		
		new Ajax.Request('calendarhandle.php?villa='+this.villa_id+'&month='+(this.selected_month+1)+'-'+this.selected_year, {   
			onSuccess: function(transport) { 
				if (transport.responseText == "401") {
					alert("Geen toegang"); return;
				} else {
					booked_days = transport.responseText.split(";");
					
					total_days = calendar.get_days_in_month(this_calendar.selected_month,this_calendar.selected_year);
					first_day = calendar.get_first_day_in_month(this_calendar.selected_month,this_calendar.selected_year);
		
					cur_day = 0 - first_day;
					i=0;
					for(w=0;w<6;w++) {
						for(d=0;d<7;d++) {
							cur_date = new Date(this_calendar.selected_year, this_calendar.selected_month, cur_day);
							cur_full_date = cur_date.getDate();
							cur_full_date+= "-" + (cur_date.getMonth() + 1);
							cur_full_date+= "-" + cur_date.getFullYear();
							
							el = $(this_calendar.elements.days[i]);
							
							el.addClassName("weekday");
							el.innerHTML = cur_date.getDate();
							if (cur_day > 0 && cur_day <= total_days) {
								el.addClassName("cur_month");
							} else {
								el.removeClassName("cur_month");
							}
							
							if (booked_days.in_array(cur_full_date)) {
								el.addClassName("booked");
							} else {
								el.removeClassName("booked");
							}
							cur_day++;
							i++;
						}
					}
					
					for(i=0;i<booked_days.length;i++) {
						
					}
				}
			}
		}); 
	},
	
	get_header_text: function()
	{
		return this.get_month_name(this.selected_month)+" "+this.selected_year;
	},
	
	goto_prev_month: function()
	{
		prev_year = this.selected_year;
		prev_month = this.selected_month - 1;
		if (prev_month < 0) {
			prev_year--;
			prev_month = 11;
		}
		
		this.goto(prev_month,prev_year);
		
		return false;
	},
	
	goto_next_month: function()
	{
		prev_year = this.selected_year;
		prev_month = this.selected_month + 1;
		if (prev_month > 11) {
			prev_year++;
			prev_month = 0;
		}
		
		this.goto(prev_month,prev_year);

		return false;
	},
	
	get_days_in_month: function(month, year) 
	{
		return 32 - new Date(year, month, 32).getDate();
	},
	
	get_first_day_in_month: function(month, year) 
	{
		return new Date(year, month, -1).getDay();
	},
	
	get_month_name: function(month) 
	{
    	return this.month_names[month];
	},
	
	get_day_name: function(day) 
	{
    	return this.day_names[day];
	}
}

Array.prototype.in_array = function (element) 
{
  for (var values in this) 
  {
    if (this[values] == element) 
    {
      return true;
    }  
  }
  return false;
};
