if(document.location.href.match("/en/")) {
	var	monthAr = new Array(
			"January", 
			"February",
			"March", 
			"April", 
			"May", 
			"June", 
			"July", 
			"August", 
			"September", 
			"October", 
			"November", 
			"December"
	);
} else {
	var	monthAr = new Array(
			"Janvier", 
			"Février",
			"Mars", 
			"Avril", 
			"Mai", 
			"Juin", 
			"Juillet", 
			"Août", 
			"Septembre", 
			"Octobre", 
			"Novembre", 
			"Décembre"
	);
}

function __DATETIME_GENERATE_CALENDAR(){
	function hasAnAct(cal, day) {
		for(k=0; cal.actAr[k]; k++){
			if(cal.actAr[k].date.getFullYear() == cal.year && cal.actAr[k].date.getMonth() == cal.month && cal.actAr[k].date.getDate() == day) {
				return k;
			} 
		}
		return -1;
	}

	// First, we need first day of the month
	firstWeekDay = new Date(this.year, this.month, 1).getDay();
	// Then, we need to know the last day of the month
	//lastDayMonth = new Date(this.year, this.month+1, 0).getDate();
	//lastDayMonth = (new Date((new Date(Year, Month+1,1))-1)).getDate();
	lastDayMonth = (new Date(this.year, this.month+1, 0)).getDate();

	if(this.month == 1){
		if( (this.year % 4 == 0) ) {
			if(this.year % 100 == 0) {
				lastDayMonth = 28;
			} else if(this.year % 400 == 0) {
				lastDayMonth = 29;
			} else {
				lastDayMonth = 29;
			}
		} else {
			lastDayMonth = 28;
		}
	}
	
	// Then, we'll fill in a string array which will be used as raw data for our calendar.
	this.rawCalendar = new Array();
	for(i=0; i<firstWeekDay;  i++) this.rawCalendar.push("*");
	for(i=1; i<=lastDayMonth; i++) {
		//Sloppy, but will work
		k=hasAnAct(this, i);
		if(k>-1){
			this.rawCalendar.push("<a onclick='showDayStuff("+this.myIndex+", "+k+");' class='dayBoxActive'>"+String(i)+"</a>");
		} else {
			this.rawCalendar.push(String(i));
		}
	}
}

function __DATETIME_CLOSE(){
	this.box.parentNode.removeChild(this.box);

}

function __DATETIME_UPDATE_SOURCE() {
	monthC = String(this.month+1);
	this.srcElem.value  = (this.day < 10 ? "0"+this.day : this.day)+"/"+((monthC) < 10 ? "0"+(monthC) : (monthC))+"/"+this.year+" ";
	this.srcElem.value += (this.tf.hours < 10 ? "0"+this.tf.hours : this.tf.hours) + ":" + (this.tf.minutes < 10 ? "0"+this.tf.minutes : this.tf.minutes) + ":" + (this.tf.seconds < 10 ? "0"+this.tf.seconds : this.tf.seconds);
	finalDate = new Date(this.year, this.month, this.day, this.tf.hours, this.tf.minutes, this.tf.seconds);
	this.srcElem.setAttribute("inValue", finalDate.getTime());
}

function __DATETIME_NEXT_MONTH(nb){
	if(!nb){ mySelf = this; } else { mySelf = CALENDAR_ARRAY[window.event.srcElement.getAttribute("nb")]; }
	if(mySelf.month == 11){
		mySelf.month = 1;
		mySelf.year++;
	} else {
		mySelf.month++;
	}
	mySelf.generateCalendar();
	mySelf.drawCalendar();
}

function __DATETIME_PREV_MONTH(nb){
	if(!nb){ mySelf = this; } else { mySelf = CALENDAR_ARRAY[window.event.srcElement.getAttribute("nb")]; }
	if(mySelf.month == 0){
		mySelf.month = 11;
		mySelf.year--;
	} else {
		mySelf.month--;
	}
	mySelf.generateCalendar();
	mySelf.drawCalendar();
}

function __DATETIME_DRAW_CALENDAR(){
	if(!this.DOMCalendar) {
		this.DOMCalendar = document.createElement("div");
		this.DOMCalendar.setAttribute("class", "calendar");
		if(!document.addEventListener) this.DOMCalendar.className="calendar";
	} else {
		// Empty the container
		this.DOMCalendar.innerHTML = "";
	}

	controlDiv  = document.createElement("div");
	calendarDiv = document.createElement("div");
	calendarDiv.setAttribute("class", "monthBox");
	controlDiv.setAttribute("class", "choiceBox");
	if(!document.addEventListener) controlDiv.className="choiceBox";
	if(!document.addEventListener) calendarDiv.className="monthBox";
	this.DOMCalendar.appendChild(controlDiv);
	this.DOMCalendar.appendChild(calendarDiv);

	// Control div shit
	prev = document.createElement("div");
	prev.innerHTML = "&lt;";
	prev.setAttribute("class", "prevMonth");
	if(!document.addEventListener) { 
		prev.className="prevMonth";
		prev.setAttribute("nb", this.myIndex);
		prev.attachEvent("onclick", __DATETIME_PREV_MONTH);
	} else {
		prev.setAttribute("class", "prevMonth");
		prev.addEventListener("click", this, false);
	}

	next = document.createElement("div");
	next.innerHTML = "&gt;";
	next.style.marginLeft = "8px";
	if(!document.addEventListener) { 
		next.className="nextMonth";
		next.setAttribute("nb", this.myIndex);
		next.attachEvent("onclick", __DATETIME_NEXT_MONTH);
	} else {
		next.setAttribute("class", "nextMonth");
		next.addEventListener("click", this, false);
	}

	monthSelect = document.createElement("select");
	monthSelect.style.marginRight = "10px";
	this.monthSelect = monthSelect;

	if(!document.addEventListener) { 
		monthSelect.className="monthSelect";
		monthSelect.setAttribute("nb", this.myIndex);
		monthSelect.attachEvent("onchange", __DATETIME_SET_MONTH);
	} else {
		monthSelect.setAttribute("class", "monthSelect");
		monthSelect.addEventListener("change", this, false);
	}

	for(i=0; monthAr[i]; i++){
		opt = document.createElement("option");
		opt.value = i;
		opt.innerHTML = monthAr[i];
		monthSelect.appendChild(opt);
	}

	monthSelect.value = this.month;

	yearSelect = document.createElement("select");
	this.yearSelect = yearSelect;
	yearSelect.setAttribute("class", "yearSelect");
	if(!document.addEventListener) yearSelect.className="yearSelect";
	today = new Date();
	yearSelect.innerHTML = "";
	for(i=today.getFullYear(); i<today.getFullYear()+10; i++){
		opt = document.createElement("option");
		opt.value = i;
		opt.innerHTML = i;
		yearSelect.appendChild(opt);
	}
	//yearSelect.addEventListener("change", this, false);
	if(!document.addEventListener) { 
		yearSelect.className="yearSelect";
		yearSelect.setAttribute("nb", this.myIndex);
		yearSelect.attachEvent("onchange", __DATETIME_SET_YEAR);
	} else {
		yearSelect.setAttribute("class", "yearSelect");
		yearSelect.addEventListener("change", this, false);
	}

	yearSelect.value = this.year;

	weekDiv = document.createElement("div");
	if(document.location.href.match("/en/")) {
		weekDiv.innerHTML = "<div class='clear'></div><div class='dayBoxDayName' style='margin-left: 2px;'>Sun</div><div class='dayBoxDayName'>Mon</div><div class='dayBoxDayName'>Tue</div><div class='dayBoxDayName'>Wed</div><div class='dayBoxDayName'>Thu</div><div class='dayBoxDayName'>Fri</div><div class='dayBoxDayName'>Sat</div>";
	} else {
		weekDiv.innerHTML = "<div class='clear'></div><div class='dayBoxDayName' style='margin-left: 2px;'>Dim</div><div class='dayBoxDayName'>Lun</div><div class='dayBoxDayName'>Mar</div><div class='dayBoxDayName'>Mer</div><div class='dayBoxDayName'>Jeu</div><div class='dayBoxDayName'>Ven</div><div class='dayBoxDayName'>Sam</div>";
	}
	controlDiv.appendChild(prev);
	controlDiv.appendChild(monthSelect);
	controlDiv.appendChild(yearSelect);
	controlDiv.appendChild(next);
	controlDiv.appendChild(weekDiv);

	for(i=0; this.rawCalendar[i]; i++){
		currDiv = document.createElement("div");
		currDiv.setAttribute("class", "dayBox");
		if(!document.addEventListener) currDiv.className="dayBox";
		if(this.rawCalendar[i] == this.day && this.month == today.getMonth() && this.year == today.getFullYear()){
			this.currDay = currDiv;
			//this.currDay.style.backgroundColor = "silver";
			this.currDay.setAttribute("class", "dayBoxToday");
			if(!document.addEventListener) this.currDay.className = "dayBoxToday";
		}

		currDiv.innerHTML = this.rawCalendar[i] != "*" ? this.rawCalendar[i] : "";
		if(this.rawCalendar[i] == "*") {
			currDiv.style.backgroundImage = "none";
		}
		
		//currDiv.addEventListener("click", this, false);
		calendarDiv.appendChild(currDiv);
	}
	
}

function __DATETIME_SET_DAY(evt){
	evt.target.style.backgroundColor = "silver";
	if(this.currDay){
		this.currDay.style.backgroundColor = "";
	}
	
	this.currDay = evt.target;
	this.day = parseInt(this.currDay.innerHTML);
}

function __DATETIME_SET_MONTH(){
	cal = CALENDAR_ARRAY[window.event.srcElement.getAttribute("nb")];
	cal.month = cal.monthSelect.value;
	cal.generateCalendar();
	cal.drawCalendar();
}

function __DATETIME_SET_YEAR(){
	cal = CALENDAR_ARRAY[window.event.srcElement.getAttribute("nb")];
	cal.year = cal.yearSelect.value;
	cal.generateCalendar();
	cal.drawCalendar();
}

function dateTimePicker(input){
	this.close			  = __DATETIME_CLOSE;
	this.handleEvent      = __DATETIME_HANDLE_EVENT;
	this.generateCalendar = __DATETIME_GENERATE_CALENDAR;
	this.drawCalendar     = __DATETIME_DRAW_CALENDAR;
	this.updateSource     = __DATETIME_UPDATE_SOURCE
	this.nextMonth        = __DATETIME_NEXT_MONTH;
	this.prevMonth        = __DATETIME_PREV_MONTH;
	this.setDay           = __DATETIME_SET_DAY;
	this.setMonth         = function(month) { this.month = month; this.generateCalendar(); }
	this.setYear          = function(year)  { this.year  = year;  this.generateCalendar(); }

	this.rawCalendar = null;
	if(input && input.getAttribute("type") == "text"){
		this.currentDate = input.getAttribute("inValue") ? new Date(parseInt(input.getAttribute("inValue"))) : new Date();		
	} else {
		this.currentDate = new Date();		
	}
	this.year        = this.currentDate.getFullYear();
	this.month       = this.currentDate.getMonth();
	this.day         = this.currentDate.getDate();

	this.srcElem     = input;
	this.DOMCalendar = null;
	this.currDay     = null;

	this.myIndex     = -1;
	this.actAr		 = new Array();

	this.generateCalendar();
	this.drawCalendar();

	// TEMP TEMP
	//this.tf = new timeField(this.srcElem);
	this.box = document.createElement("div");
	this.box.setAttribute("class", "theCalendar");
	if(!document.addEventListener) this.box.className="theCalendar";
	this.box.appendChild(this.DOMCalendar);
	/*okBtn = document.createElement("input");
	okBtn.setAttribute("class", "okBtn");
	okBtn.setAttribute("type",  "button");
	okBtn.setAttribute("value", "Changer");
	okBtn.addEventListener("click", this, false);

	closeBtn = document.createElement("input");
	closeBtn.setAttribute("class", "closeBtn");
	closeBtn.setAttribute("type",  "button");
	closeBtn.setAttribute("value", "Annuler");
	closeBtn.addEventListener("click", this, false);
	this.box.style.left = (parseInt(getOffsetLeft(this.srcElem))-200)+"px";
	this.box.style.top  = (parseInt(getOffsetTop(this.srcElem))-200)+"px";
	this.tf.addTo(this.box);
	this.box.appendChild(okBtn);
	this.box.appendChild(closeBtn);*/
	
	this.underCal = document.createElement("div");
	this.underCal.setAttribute("class", "underCal");
	if(!document.addEventListener) this.underCal.className="underCal";
	this.underCal.style.display = "none";

	this.box.appendChild(this.underCal);

	foot = document.createElement("div");
	foot.setAttribute("class", "calendarBottom");
	if(!document.addEventListener) foot.className = "calendarBottom";
	this.box.appendChild(foot);

	if(this.srcElem && this.srcElem.getAttribute("type") == "text"){
		document.getElementsByTagName("body")[0].appendChild(this.box);
	} else {
		this.srcElem.parentNode.replaceChild(this.box, this.srcElem);
	}
}

function __DATETIME_HANDLE_EVENT(evt) {
	if(evt.type == "mousedown"){
		window.addEventListener("mousemove", this, false);
		window.addEventListener("mouseup", this, false);
		this.currX = evt.clientX;
		this.currY = evt.clientY;

		evt.target.style.cursor = "-moz-grabbing";
		
	} else if(evt.type == "mousemove"){
		this.DOMSelf.style.left = parseInt(this.DOMSelf.style.left.replace("px", ""))+(evt.clientX-this.currX)+"px";
		this.DOMSelf.style.top  = parseInt(this.DOMSelf.style.top.replace("px", "" ))+(evt.clientY-this.currY)+"px";

		this.currX = evt.clientX;
		this.currY = evt.clientY;

	} else if(evt.type == "mouseup"){
		window.removeEventListener("mousemove", this, false);
		window.removeEventListener("mouseup", this, false);

		evt.target.style.cursor = "-moz-grab";
	} else if(evt.type == "click"){
		switch(evt.target.getAttribute("class")){
			case "prevMonth"			: this.prevMonth();	break;
			case "nextMonth"			: this.nextMonth();	break;
			case "dayBox"				: this.setDay(evt); break;
			//case "okBtn"				: this.updateSource(evt.target.innerHTML); this.close(); break;
			//case "closeBtn"				: this.close(); break;
		}
	} else if(evt.type == "change"){
		switch(evt.target.getAttribute("class")){
			case "monthSelect"			: this.setMonth(parseInt(evt.target.value)); this.drawCalendar(); break;
			case "yearSelect"			: this.setYear(parseInt(evt.target.value));  this.drawCalendar(); break;
		}
	}

	evt.preventDefault();
}

function __TIMEFIELD_HANDLE_EVENT(evt) {
	if(evt.type == "click"){ 
		switch(evt.target.getAttribute("class")){
			case "hours"			: this.currAdd = this.addHour;   this.currRem = this.remHour;   break;
			case "minutes"			: this.currAdd = this.addMinute; this.currRem = this.remMinute; break;
			case "seconds"			: this.currAdd = this.addSecond; this.currRem = this.remSecond; break;
			case "timeArrowUp"		: this.currAdd(); this.update(); break;
			case "timeArrowDown"	: this.currRem(); this.update(); break;
		}
	}

	evt.preventDefault();
}

function activite(date, heure, nom, lien){
	this.date  = date;
	this.heure = heure;
	this.nom   = nom;
	this.lien  = lien;
}

var CALENDAR_ARRAY = new Array();

function replaceCalendar(){
	divCol = document.getElementsByTagName("div");
	divAr  = new Array();

	for(m=0; divCol[m]; m++){
		if(divCol[m].getAttribute("class") == "modAjCalendrier" || divCol[m].className == "modAjCalendrier" ) {	
			divAr.push(divCol[m]);
		}
	}

	for(n=0; divAr[n]; n++){
		evtArTemp = divAr[n].getElementsByTagName("i");
		evtAr = new Array();
		for(l=0; evtArTemp[l]; l++){
			evtAr.push(evtArTemp[l]);
		}
		
		newCal = new dateTimePicker(divAr[n]);


		for(j=0; evtAr[j]; j++) {
			dateExp = evtAr[j].getAttribute("date").split("/");
			newCal.actAr.push(new activite(new Date(dateExp[2], (parseInt(dateExp[1])-1), dateExp[0]), dateExp[3]+":"+dateExp[4], evtAr[j].getAttribute("nom"), evtAr[j].getAttribute("link")) );
		}

		CALENDAR_ARRAY.push(newCal);
		newCal.myIndex = CALENDAR_ARRAY.length-1;
		newCal.generateCalendar();
		newCal.drawCalendar();
	}
}

function showDayStuff(calArrayIndex, day) {
	cal   = CALENDAR_ARRAY[calArrayIndex];
	cal.oneShown = true;

	window.open(cal.actAr[day].lien, "_self");

}

