var months=['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Decembre'];
var full_days=['Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi','Dimanche'];
var days="LMMJVSD";
var today=new Date();
var CurrentDoW = today.getDay()-1; // Day of Week (0:Sunday - 6:Saturday)-1
if (CurrentDoW==-1) CurrentDoW+=7; // for sunday, -1 -> 6 => (1:Monday - 7:Sunday)
var CurrentDate = today.getDate(); // 1 - 31
var CurrentMonth = today.getMonth(); // 0 - 11
var CurrentYear = today.getFullYear(); // YYYY

var c_m = CurrentMonth;
var c_y = CurrentYear;



function sont_du_mois(item) { // ne fonctionne pas avec IE !!!
	return (item.year==c_y && item.month==c_m+1); // c_m = 1 - 12
}


function check_day(array,day) {
	var return_array = new Array();
	
	for (k=0; k<array.length; k++){
		if (array[k].day==day){
			return_array["confirmed"] = array[k].confirmed;
			return_array["starttime"] = array[k].starttime;
			return_array["endtime"] = array[k].endtime;
			return_array["type"] = array[k].type;
		}
	}
	return return_array;
}


function filter(array, year, month){
	var returnArray = new Array();
	
	for (n=0;n<array.length; n++){
		if (array[n].year==year && array[n].month==month+1){ // c_m = 1 - 12
			returnArray.push(array[n]);
		}	
	}
	return returnArray;
}




function Calendar(y,m){
	
	var dates = filter(c_date,y,m);

	var FoM = (new Date(y, m, 1)).getDay()-1; // First day of Week (0:Sunday - 6:Saturday)-1
	if (FoM==-1) FoM+=7; // for sunday, -1 -> 6 => (1:Monday - 7:Sunday)
	var LoM = (new Date((new Date(y, m+1,1))-1)).getDate(); // Last date of Month

	var HTMLstr = "";

	HTMLstr += "<table class='calendar_nav'><tr>";
	HTMLstr += "<td><a href='javascript:prevMonth()' class='a_menu'> << </a></td>";
	HTMLstr += "<td class='calendar_month'>" + months[m] + " - " + y + "</td>";
	HTMLstr += "<td><a href='javascript:nextMonth()' class='a_menu'> >> </a></td>";
	HTMLstr += "</tr></table>";

	HTMLstr += "<table class='calendar'>";
	HTMLstr += "<tr>";

	for(s=0;s<7;s++){
		HTMLstr+="<th class='calendar'>"+days.substr(s,1)+"</th>";
	}

	HTMLstr += "</tr>";
	HTMLstr += "<tr>";

	for(i=1;i<=42;i++){ // i=cellindex
	
		j = i-FoM;

		var x = "";
		if(j>0 && j<=LoM) x += j;
		if (j==CurrentDate && m==CurrentMonth && y==CurrentYear) x = "<span id='today'>"+x+"</span>";
		result=check_day(dates,j);
		if (result["confirmed"]!=undefined){
			HTMLstr += "<td class='calendar' id='"+result["type"]+"'>";
			// HTMLstr += x;
			HTMLstr += "<a class='popup' href='#'>"+x;
			HTMLstr += "<span>"
			if(result["type"]=="concert") HTMLstr += "Evénement public";
			else {
				if (result["confirmed"]==true) HTMLstr += "Evénement privé";
				else HTMLstr += "Evénement privé non confirmé";
			}
			HTMLstr += "</span></a>";
			
			HTMLstr += "</td>";
		}
		else HTMLstr += "<td class='calendar'>"+x+"</td>";

		if(((i)%7==0)&&(i<36)) HTMLstr += "</tr><tr>";
	}
	HTMLstr += "</tr>"
	HTMLstr += "</table>"
	
	
	calendar_div = document.getElementById("calendar_content");
	calendar_div.innerHTML = HTMLstr;
}


function initCalendar(){
	Calendar(CurrentYear, CurrentMonth);
}


function nextMonth(){
	c_m++;
	if (c_m > 11){
		c_m -= 12;
		nextYear();
	}
	Calendar(c_y, c_m);
}


function prevMonth(){
	c_m--;
	if (c_m < 0){
		c_m += 12;
		prevYear();
	}
	Calendar(c_y, c_m);
}

function prevYear(){
	c_y--;
	Calendar(c_y, c_m);
}

function nextYear(){
	c_y++;
	Calendar(c_y, c_m);
}
