function cal()
{
	this._disp_close = false;
	this._disp_time	= false;
	this._str_date	= null;
	this._target	= null;
	this._target_id	= null;
	this._callback	= null;
	this._hideCal	= false;
	this._getDays		= null;
	this._specialDays = null;
	this._specialDaysCallback = null;
	this._dayName	= new Array('l', 'm', 'm', 'j', 'v', 's', 'd');
	this._monthName = new Array('Jan', 'Fev', 'Mar', 'Avr', 'Mai', 'Juin', 'Juil', 'Aou', 'Sep', 'Oct', 'Nov', 'Dec');
	
	this.dayTime 	= 24 * 60 * 60 * 1000;
	this.selected	= new Date();
	this.showDay 	= new Date();
	this.fillDay	= null;
	this._fillId		= null;
}

cal.prototype	= {
	
	affZ:	function(val)
	{
		return parseInt(val) < 10 ? '0' + val : val;
	},
	
	initTarget:	function(target_id)
	{
		this._target_id = target_id;
		this._target = document.getElementById(target_id);
	},
	
	initClose: function(close)
	{
		this._disp_close = close;
	},
	
	initDispTime: function(time)
	{
		this._disp_time = time;
	},
	
	initStrDate: function(strDate)
	{
		this._str_date = strDate;
	},

	initCallback: function(callback)
	{
		this._callback = callback;
	},
 
	initSpecialDays: function(callback)
	{
		this._specialDaysCallback = callback;
	},
 
 	setSpecialDays: function(specialDays)
 	{
	 	this._specialDays = null;
		if (specialDays != null)
		{
	 		var tab = specialDays.split('|');
 			if (tab.length)
			{
	 			this._specialDays = tab;
			}
		}
		this.updateCal();
 	},
 	
	initHide:	function(hide)
	{
		this._hideCal = hide;
	},
	
 	initLang:	function(lang)
	{
		if (lang == 'en')
		{
			this._dayName = new Array('m', 't', 'w', 't', 'f', 's', 's');
			this._monthName 	= new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
		}
	},
 
	getLastDay: function(thisDate)
	{
		var tmpDate		= new Date();
		tmpDate.setDate(1);
		tmpDate.setMonth(thisDate.getMonth() < 11 ? thisDate.getMonth() + 1 : 0);
		if (tmpDate.getMonth() == 0)
			tmpDate.setFullYear(tmpDate.getFullYear() + 1);
		tmpDate.setTime(tmpDate.getTime() - this.dayTime);
		return tmpDate.getDate();
	},
	
	getNewCal: function()
	{
		var	newCal		= new Array();
		
		var	firstDay	= 1;
		var lastDay		= this.getLastDay(this.showDay);
		var	newDate		= new Date();
		var key 		= 0;
	
		newDate.setTime(this.showDay.getTime());
		newDate.setDate(firstDay);
		var tmpDate = newDate.getDay() == 0 ? 7 : newDate.getDay();
		for (i = 1; i < tmpDate; i++)
			newCal[key++] = ' ';
			
		for (i = 1; i <= lastDay; i++)
			newCal[key++] = i < 10 ? '0' + i : i;
		
		while(key < 42)
			newCal[key++] = ' ';
	
		newCal[key++] = null;
		
		return newCal;
	},
	
	isSpecialDay: function(searchDay)
	{
		var getDay;
		if (this._specialDays != null && searchDay != null)
			for (getDay = 0; getDay < this._specialDays.length; getDay++)
				if (parseInt(searchDay) == parseInt(this._specialDays[getDay]))
					return true;
		return false;
	},

	mkStrDate: function(date)
	{
		if (date != null)
			return date.getFullYear() + '' + date.getMonth() + '' + date.getDate();
	},

	updateCal: function()
	{
		var key		= 0;
		var newCal	= this.getNewCal();
		var now 		= new Date();
		var myNode;
		var myDay;
		var tmpDate = new Date();

		tmpDate.setFullYear(this.showDay.getFullYear());
		tmpDate.setMonth(this.showDay.getMonth());
		tmpDate.setDate(this.showDay.getDate());
		document.getElementById(this._target_id + 'calTitle').innerHTML = this._monthName[tmpDate.getMonth()] + '/' + tmpDate.getFullYear();

		for (i = 0; (myNode = document.getElementById(this._target_id + 'day' + i)); i++)
		{
			myDay = myNode.firstChild;
			if (newCal[key] != ' ' && newCal[key] != null)
			{
				tmpDate.setDate(newCal[key]);
				if (this.fillDay != null)
					myNode.onclick = this._setDate;//myNode.obj._fillDiv;
				if (this.isSpecialDay(newCal[key]) == true)
				{
					if (tmpDate.getTime() < now.getTime())
					{
						myDay.setAttribute('class', 'calDayPastEvent');
						myDay.setAttribute('className', 'calDayPastEvent');
					}
					else
					{
						myDay.setAttribute('class', 'calDayEvent');
						myDay.setAttribute('className', 'calDayEvent');
					}
				}
				else if (this.mkStrDate(tmpDate) == this.mkStrDate(now))
				{
					myDay.setAttribute('class', 'calDayToday');
					myDay.setAttribute('className', 'calDayToday');
				}
				else if (tmpDate.getTime() < now.getTime())
				{
					myDay.setAttribute('class', 'calDayStrike');
					myDay.setAttribute('className', 'calDayStrike');
				}
				else if (tmpDate.getTime() > now.getTime())
				{
					myDay.setAttribute('class', 'calDay');
					myDay.setAttribute('className', 'calDay');
				}
			}
			else if (newCal[key] != null)
			{
				myDay.setAttribute('class', 'calDay');
				myDay.setAttribute('className', 'calDay');
			}
			else
				return;
			myDay.firstChild.nodeValue = newCal[key++];
		}
	},
 
 	getDayLetter: function(dayNum)
	{
		return this._dayName[dayNum];
	},
 
 	create: function()
	{
		this.build();
		if (this._specialDaysCallback != null)
		{
			var date = this.showDay.getFullYear() + '-' + this.affZ(this.showDay.getMonth() + 1) + '-01';
			this._specialDaysCallback(date);
		}
		else
			this.updateCal();
	},
 
	_fillDiv: function(myDate)
	{
		if (myDate.type != 'click')
		{
			if (this._fillId)
			{
				this._fillId.value = this.affZ(myDate.getDate())+'/'+this.affZ(myDate.getMonth() + 1)+'/'+myDate.getFullYear() + ' ' + this.affZ(myDate.getHours()) + ':' + this.affZ(myDate.getMinutes());
			}
		}
		else
		{
			var elem = document.getElementById(this.id).firstChild.innerHTML;
			var obj = this.obj;
			obj.fillDay.setDate(elem);
			obj._fillDiv(obj.fillDay);
		}
	},


	_setTime: function(tab)
	{
		if (tab.length >= 2)
		{
			var elem = document.getElementById(this._target_id + 'calHour');
			if (elem)
				elem.innerHTML = this.affZ(parseInt(tab[0], 10));
			var elem = document.getElementById(this._target_id + 'calMinute');
			if (elem)
				elem.innerHTML = this.affZ(parseInt(tab[1], 10));
		}
	},

	show: function(event, fillDiv)
	{
		var pos = modCoord.mouseAbs(event);
		var elem = document.getElementById(this._target_id);
		if (elem)
		{
			var cal = document.getElementById(this._target_id + 'calRoot');
			if (cal)
			{
				elem.style.position = 'absolute';
				elem.style.left = (pos.x + 10) + 'px';
				elem.style.top = ((pos.y + 10) - 210) + 'px';
				cal.style.display = 'block';
				if (fillDiv.value)
				{
					var str = fillDiv.value;
					if (str.length == 16)
					{
						var tab = str.split(" ");
						var dateTab = tab[0].split("/");
						var timeTab = tab[1].split(":");
						var month = parseInt(dateTab[1], 10) - 1;

						this.showDay = new Date();
						this.showDay.setMonth(month);
						this.showDay.setFullYear(parseInt(dateTab[2], 10));

						this.fillDay = new Date();
						this.fillDay.setMonth(month);
						this.fillDay.setFullYear(parseInt(dateTab[2], 10));
						this.fillDay.setDate(parseInt(dateTab[0], 10));
						this.fillDay.setHours(parseInt(timeTab[0], 10));
						this.fillDay.setMinutes(parseInt(timeTab[1], 10));
						this._fillDiv(this.fillDay);
						this._setTime(timeTab);
						this.updateCal();
					}
				}
				else
				{
					this.showDay = new Date();
					this.fillDay = new Date();
					this.fillDay.setHours(12);
					this.fillDay.setMinutes(0);
					this._fillDiv(this.fillDay);
					this._setTime(Array(12, 0));
					this.updateCal();
				}
				this._fillId = fillDiv;
			}
		}
//		modDrag.makeDragable(document.getElementById(this._target_id + 'calHead'), document.getElementById(this._target_id + 'calRoot').parentNode);
	},

	_hide: function()
	{
		var obj = this.obj ? this.obj : this;
		if (obj)
		{
			var elem = document.getElementById(obj._target_id + 'calRoot');
			elem.style.display = 'none';
		}
	},

	build: function()
	{
		var	firstDay	= 1;
		var	showDate	= new Date();
		var	HTML;
		
		var table = document.createElement('table');
		table.id = this._target_id + 'calRoot';
		table.obj = this;
		table.setAttribute('class', 'calRoot');
		table.setAttribute('className', 'calRoot');		
		table.setAttribute('cellSpacing', 0);
		table.setAttribute('cellPadding', 0);
		if (this._hideCal == true)
			table.style.display = 'none';
	
	
		var tbody = document.createElement('tbody');

/* HEAD */
/*		var tr = document.createElement('tr');
		if (!this._disp_close)
			tr.style.display = 'none';
			
		var div = document.createElement('div');
		div.id = this._target_id + 'calClose';
		div.setAttribute('class', 'calClose');
		div.setAttribute('className', 'calClose');
		div.obj = this;
		div.onclick = this._hide;
		
		
		var td = document.createElement('td');
		td.id = this._target_id + 'calHead';
		td.setAttribute('class', 'calHead');
		td.setAttribute('className', 'calHead');
		td.setAttribute('colSpan', '7');
		td.appendChild(div);
		tr.appendChild(td);
		tbody.appendChild(tr);*/

/* Change Month */
		//crée un tableau
		var tableHead = document.createElement('table');
		tableHead.setAttribute('cellspacing', '0');
		tableHead.setAttribute('cellpadding', '0');
		tableHead.style.marginLeft = '26px';
		var tbodyHead = document.createElement('tbody');
		var trHead = document.createElement('tr');
		

		var lt = document.createElement('div');
		lt.id = this._target_id + 'calLeft';
		lt.setAttribute('class', 'calLeft');
		lt.setAttribute('className', 'calLeft');

		var gt = document.createElement('div');
		gt.id = this._target_id + 'calRight';
		gt.setAttribute('class', 'calRight');
		gt.setAttribute('className', 'calRight');

		var thLeft = document.createElement('td');
//		thLeft.setAttribute('colSpan', '2');
		thLeft.obj = this;
		thLeft.onclick = this.lastMonth;
		thLeft.appendChild(lt);

		var thMid = document.createElement('td');
//		thMid.setAttribute('colSpan', '3');
		thMid.id = this._target_id + 'calTitle';
		thMid.setAttribute('class', 'calTitle');
		thMid.setAttribute('className', 'calTitle');

		var thRight = document.createElement('td');
//		thRight.setAttribute('colSpan', '2');
		thRight.obj = this;
		thRight.onclick = this.nextMonth;
		thRight.appendChild(gt);
		
		//Creation de la case tout a droite pour y mettre le bouton close
		var thRightClose = document.createElement('td');
		thRightClose.style.verticalAlign = 'top';
		
		//Creation du bouton close
		var divClose = document.createElement('div');
		divClose.id = this._target_id + 'calClose';
		divClose.setAttribute('class', 'calClose');
		divClose.setAttribute('className', 'calClose');
		divClose.obj = this;
		divClose.onclick = this._hide;
		
		//On met le bouton close dans le th prévu a cet effet
		thRightClose.appendChild(divClose);
		
		trHead.appendChild(thLeft);
		trHead.appendChild(thMid);
		trHead.appendChild(thRight);
		trHead.appendChild(thRightClose);
		tbodyHead.appendChild(trHead);
		tableHead.appendChild(tbodyHead);
		tableHead.style.display = 'block';
//		tableHead.style.width = '100%';
		
		tr = document.createElement('tr');
		var tdHead = document.createElement('td');
//		tdHead.setAttribute('colspan', '7');
		tdHead.colSpan = '7';
		tdHead.setAttribute('class', 'calHead');
		tdHead.setAttribute('className', 'calHead');
		tdHead.appendChild(tableHead);
		tr.appendChild(tdHead);
		tbody.appendChild(tr);
		
		
		tr = document.createElement('tr');
		for (i = 0; i < 7; i++)
		{
			td = document.createElement('td');
			td.id = this._target_id + 'calNameDay';
			td.setAttribute('class', 'calNameDay');
			td.setAttribute('className', 'calNameDay');
			td.innerHTML = this.getDayLetter(i);
			tr.appendChild(td);
		}
		tbody.appendChild(tr);

		for (row = 0; row < 5; row++)
		{
			tr = document.createElement('tr');
			for (i = 0; i < 7; i++)
			{
				numtd = 7 * row + i;
				td = document.createElement('td');
				td.id = this._target_id + 'day'+numtd;
				td.obj = this;
				if (this._callback)
					td.onclick = this._callback;
//				td.onclick = this._setDate();
				td.ondblclick = this._hide;
				td.setAttribute('class', 'calTDDay');
				td.setAttribute('className', 'calTDDay');
				td.innerHTML = '<a class="calDay">&nbsp;</a>';
				tr.appendChild(td);
			}
			tbody.appendChild(tr);
		}

		tr = document.createElement('tr');
		tr.id = this._target_id + 'calHiddenRow';
		tr.setAttribute('className', 'calHiddenRow');
		tr.setAttribute('class', 'calHiddenRow');
		for (i = 0; i < 7; i++)
		{
			numtd = 7 * row + i;
			td = document.createElement('td');
			td.obj = this;
			if (this._callback)
				td.onclick = this._callback;
//			td.onclick = this._fillDiv(this.obj.fillDay);
			td.ondblclick = this._hide;
			td.setAttribute('class', 'calTDDay');
			td.setAttribute('className', 'calTDDay');
			td.id = this._target_id + 'day'+numtd;
			td.innerHTML = '<a class="calDay">&nbsp;</a>';
			tr.appendChild(td);
		}
		tbody.appendChild(tr);
		
		if (this._disp_time)
		{
			tr = document.createElement('tr');
			td = document.createElement('td');
//			td.setAttribute('colSpan', '7');
			td.colSpan = '7';
			td.id = this._target_id + 'calOther';
			td.setAttribute('class', 'calOther');
			td.setAttribute('className', 'calOther');
			td.style.textAlign = 'center';
			
			ttable = document.createElement('table');
			ttable.setAttribute('cellspacing', '0');
			ttable.setAttribute('cellpadding', '0');
//			ttable.style.width = '119px';
			ttable.style.marginTop = '2px';
			ttable.style.marginLeft = '10px';
			ttable.style.height = '26px';
			ttbody = document.createElement('tbody');
			
			ttr = document.createElement('tr');
			ttd = document.createElement('td');
			ttd.setAttribute('class', 'calUp');
			ttd.setAttribute('className', 'calUp');
			ttd.onclick = this._nextHour;
			ttd.obj = this;
			ttd.innerHTML = '&nbsp;'
			ttr.appendChild(ttd);
			ttd = document.createElement('td');
			tdiv = document.createElement('div');
			ttd.setAttribute('rowSpan', '2');
			ttd.style.width = '21px';
			ttd.style.height = '22px';
			tdiv = document.createElement('div');
			tdiv.setAttribute('class', 'calHour');
			tdiv.setAttribute('className', 'calHour');
			tdiv.id = this._target_id + 'calHour';
			tdiv.innerHTML = '00';
			ttd.appendChild(tdiv);
			ttr.appendChild(ttd);
			ttd = document.createElement('td');
			ttd.setAttribute('rowSpan', '2');
			ttd.setAttribute('class', 'calSepTime');
			ttd.setAttribute('className', 'calSepTime');
			ttd.innerHTML = 'h';
			ttr.appendChild(ttd);
			ttd = document.createElement('td');
			ttd.setAttribute('rowSpan', '2');
			ttd.style.width = '21px';
			ttd.style.height = '22px';
			tdiv = document.createElement('div');
			tdiv.setAttribute('class', 'calMinute');
			tdiv.setAttribute('className', 'calMinute');
			tdiv.id = this._target_id + 'calMinute';
			tdiv.innerHTML = '00';
			ttd.appendChild(tdiv);
			ttr.appendChild(ttd);
			ttd = document.createElement('td');
			ttd.setAttribute('class', 'calUp');
			ttd.setAttribute('className', 'calUp');
			ttd.onclick = this._nextMinute;
			ttd.obj = this;
			ttd.innerHTML = '&nbsp;'
			ttr.appendChild(ttd);
			//Cree un autre td pour y mettre le bouton ok
			ttd = document.createElement('td');
			ttd.setAttribute('rowSpan', '2');
			ttd.style.paddingLeft = '16px';
			
			//Cree le div avec le bouton ok
			var tdiv = document.createElement('div');
			tdiv.setAttribute('class', 'calOk');
			tdiv.setAttribute('className', 'calOk');
			tdiv.obj = this;
			tdiv.onclick = this._hide;
			
			ttd.appendChild(tdiv);
			ttr.appendChild(ttd);
			
			ttbody.appendChild(ttr);

			ttr = document.createElement('tr');
			ttd = document.createElement('td');
			ttd.setAttribute('class', 'calDown');
			ttd.setAttribute('className', 'calDown');
			ttd.onclick = this._prevHour;
			ttd.obj = this;
			ttd.innerHTML = '&nbsp;'
			ttr.appendChild(ttd);
			ttd = document.createElement('td');
			ttd.setAttribute('class', 'calDown');
			ttd.setAttribute('className', 'calDown');
			ttd.onclick = this._prevMinute;
			ttd.obj = this;
			ttd.style.textAlign = 'center';
			ttd.innerHTML = '&nbsp;'
			ttr.appendChild(ttd);

			ttbody.appendChild(ttr);

			ttable.appendChild(ttbody);
			td.appendChild(ttable);
			tr.appendChild(td);
			tbody.appendChild(tr);
		}
		table.appendChild(tbody);
		this._target.appendChild(table);
	},

/*
** Date / Time change
*/
	nextMonth: function()
	{
		var obj = this.obj;
		var avantavant = obj.showDay.getMonth();
		obj.showDay.setDate(1);
		var avant = obj.showDay.getMonth();
		obj.showDay.setMonth(obj.showDay.getMonth() + 1);
		
		if (obj.fillDay != null)
		{
			obj.fillDay.setMonth(obj.showDay.getMonth());
			obj.fillDay.setFullYear(obj.showDay.getFullYear());
			obj._fillDiv(obj.fillDay);
		}
		if (obj._specialDaysCallback != null)
		{
			obj._specialDays = null;
			var date = obj.showDay.getFullYear() + '-' + obj.affZ(obj.showDay.getMonth() + 1) + '-01';
			obj._specialDaysCallback(date);
		}
		else
			obj.updateCal();
	},

	lastMonth: function()
	{
		var obj = this.obj;
		var avantavant = obj.showDay.getMonth();
		obj.showDay.setDate(1);
		var avant = obj.showDay.getMonth();
		obj.showDay.setMonth(obj.showDay.getMonth() - 1);
		if (obj.fillDay != null)
		{
			obj.fillDay.setMonth(obj.showDay.getMonth());
			obj.fillDay.setFullYear(obj.showDay.getFullYear());
			obj._fillDiv(obj.fillDay);
		}
		if (obj._specialDaysCallback != null)
		{
			obj._specialDays = null;
			var date = obj.showDay.getFullYear() + '-' + obj.affZ(obj.showDay.getMonth() + 1) + '-01';
			obj._specialDaysCallback(date);
		}
		else
			obj.updateCal();
	},

	_nextMinute: function()
	{
		var obj = this.obj;
		if (obj)
		{
			var elem = document.getElementById(obj._target_id + 'calMinute');
			if (elem)
			{
				var val = parseInt(elem.innerHTML, 10);
				val = val ? val : 0;
				val = parseInt(val) >= 45 ? '00' : obj.affZ(val + 15);
				elem.innerHTML = val;
				if (obj.fillDay != null)
				{
					obj.fillDay.setMinutes(val);
					obj._fillDiv(obj.fillDay);
				}
			}
		}
	},

	_prevMinute: function()
	{
		var obj = this.obj;
		if (obj)
		{
			var elem = document.getElementById(obj._target_id + 'calMinute');
			if (elem)
			{
				var val = parseInt(elem.innerHTML, 10);
				val = val ? val : 0;
				val = val <= 0 ? '45' : obj.affZ(parseInt(val) - 15);
				elem.innerHTML = val;
				if (obj.fillDay != null)
				{
					obj.fillDay.setMinutes(val);
					obj._fillDiv(obj.fillDay);
				}
			}
		}
	},

	_setDate: function()
	{
		var obj = this.obj;

		if (obj)
		{
			var elem = this.firstChild;
			if (elem)
			{
				var val = parseInt(elem.innerHTML, 10);
				val = val ? parseInt(val) : 0;
				if (obj.fillDay != null)
				{
					obj.fillDay.setDate(val);
					obj._fillDiv(obj.fillDay);
				}
			}
		}		
	},

	_nextHour: function()
	{
		var obj = this.obj;
		if (obj)
		{
			var elem = document.getElementById(obj._target_id + 'calHour');
			if (elem)
			{
				var val = parseInt(elem.innerHTML, 10);
				val = val ? parseInt(val) : 0;
				val = val == 23 ? '00' : obj.affZ(val + 1);
				elem.innerHTML = val;
				if (obj.fillDay != null)
				{
					obj.fillDay.setHours(val);
					obj._fillDiv(obj.fillDay);
				}
			}
		}
	},

	_prevHour: function()
	{
		var obj = this.obj;
		if (obj)
		{
			var elem = document.getElementById(obj._target_id + 'calHour');
			if (elem)
			{
				var val = parseInt(elem.innerHTML, 10);
				val = val ? parseInt(val) : 0;
				val = val == 0 ? '23' : obj.affZ(val - 1);
				elem.innerHTML = val;
				if (obj.fillDay != null)
				{
					obj.fillDay.setHours(val);
					obj._fillDiv(obj.fillDay);
				}
			}
		}
	}

};


/*
** Functions
*/

function setSpecialDaysById(id, specialDaysStr)
{
	var elem = document.getElementById(id);
	if (elem)
		elem.firstChild.obj.setSpecialDays(specialDaysStr);
}