// Classe Calendar

//initialisation du calendrier, à l'aide des paramètres :
//target -> le champs texte associé au calendrier
//$vars   -> un Json contenant les paramètres facultatifs : min_date et max_date


//constructeur
function Calendar($target,$vars){
	
		var _this 		= this;
		
		if(document.getElementById('calCont')){			
			removeDom(document.getElementById('calCont'));	
		}
		
		_this.array_day_name 	= str_day_name.split(',');
		_this.array_month_name 	= str_month_name.split(',');
		
		_this.cont 						= document.createElement('DIV');
		_this.close_calendar 			= document.createElement('DIV');
		_this.nav 						= document.createElement('DIV');
		_this.prev_year					= document.createElement('DIV');
		_this.prev_month				= document.createElement('DIV');
		_this.cur_date_name				= document.createElement('SPAN');
		_this.next_month				= document.createElement('DIV');
		_this.next_year					= document.createElement('DIV');
		_this.week						= document.createElement('DIV');
		_this.grid						= document.createElement('DIV');
		
		_this.prev_year.innerHTML 		= '|&#9668;';
		_this.prev_month.innerHTML 		= '&#9668;';
		_this.next_month.innerHTML 		= '&#9658;';
		_this.next_year.innerHTML 		= '&#9658;|';	
		
		_this.cont						.setAttribute('id','calCont');
		_this.nav						.setAttribute('id','calNav');
		_this.week						.setAttribute('id','week');
		_this.prev_year					.setAttribute('id','prevYear');
		_this.prev_month				.setAttribute('id','prevMonth');
		_this.cur_date_name				.setAttribute('id','curDateName');
		_this.next_month				.setAttribute('id','nextMonth');
		_this.next_year					.setAttribute('id','nextYear');
		_this.close_calendar				.setAttribute('id','closeCalendar');
		_this.grid.className 			= 'calGrid';
		
		_this.prev_year.className 		= 'calNavButton';
		_this.prev_month.className 		= 'calNavButton';
		_this.next_month.className 		= 'calNavButton';
		_this.next_year.className 		= 'calNavButton';
	
		_this.next_month.style.float 	= 'right';
		_this.next_year.style.float 	= 'right';
		
		_this.cont						.appendChild(_this.close_calendar);		
		_this.cont						.appendChild(_this.nav);
		_this.nav						.appendChild(_this.prev_year);
		_this.nav						.appendChild(_this.prev_month);
		_this.nav						.appendChild(_this.cur_date_name);	
		_this.nav						.appendChild(_this.next_month);	
		_this.nav						.appendChild(_this.next_year);	
		_this.cont						.appendChild(_this.week);
		_this.cont						.appendChild(_this.grid);
				
		for(i=0;i<7;i++){
		
			var day_name				= document.createElement('SPAN');
			day_name.innerHTML 			= _this.array_day_name[i].substring(0,1);
			day_name.className 			= 'dayName';
			_this.week					.appendChild(day_name);
		
		}

		for(i=0;i<42;i++){
		
			var day_case				= document.createElement('DIV');
			day_case.className 			= 'dayCase';
			day_case.id 				= 'd'+i;
			_this.grid					.appendChild(day_case);
		
		}
		
		
		document.body					.appendChild(_this.cont);
		_this.cont						.appendChild(_this.nav);
		_this.cont						.appendChild(_this.week);
		_this.cont						.appendChild(_this.grid);

		this.close_calendar.onclick 	= function(){ _this.hide() }
		this.prev_year.onclick 			= function(){ _this.addYear(-1) }
		this.prev_month.onclick 		= function(){ _this.addMonth(-1); }
		this.next_month.onclick 		= function(){ _this.addMonth(1); }
		this.next_year.onclick 			= function(){ _this.addYear(1); }	
	
		this.target						= document.getElementById($target);
	
		//par défaut, on créé this.date à la date d'ajourd'hui
		this.date = new Date();
		
		//Si le champs texte associé au calendrier possède déjà une valeur
		if(this.target.value != ''){
			
			arr_date = this.target.value.split('/');	
			
			if(arr_date.length>2){
			
				this.date.setDate(arr_date[0]);
				this.date.setMonth(Math.round(arr_date[1]-1));
				this.date.setFullYear(arr_date[2]);
			
			}
			
		}	
		
		
		this.orig_day 		= this.date.getDay();
		this.orig_date 		= this.date.getDate();
		this.orig_month 	= this.date.getMonth();
		this.orig_year 		= this.date.getFullYear();
		
		//on boucle sur les paramtres facultatifs -> $vars
		//Valeurs possibles : min_date,max_date
		for(var a in $vars){
			
			
			switch(a){
			case 'min_date':
			case 'max_date':
						
				var date_limit = $vars[a];
				var arr_date = date_limit.split('/');	
				this[a] = new Date();	
				this[a].setMonth(arr_date[1]-1);
				this[a].setDate(arr_date[0]);
				this[a].setFullYear(arr_date[2]);
				
				if(a == 'min_date'){	
				
					(arr_date[3])?this[a].setHours(arr_date[3]):this[a].setHours(0);
					(arr_date[4])?this[a].setMinutes(arr_date[4]):this[a].setMinutes(0);
					
				}	else{
					
						(arr_date[3])?this[a].setHours(arr_date[3]):this[a].setHours(23);
						(arr_date[4])?this[a].setMinutes(arr_date[4]):this[a].setMinutes(59);
					
				}

			break;
			
			default:
					
					_this[a] = $vars[a];	
			
			break;
						 
						 
			}
	
		}
		//Fin de la boucle
		
		
		//on fait apparaitre le calendrier juste au-dessus du champs texte 'target'
		var target_pos  = getElementPosition(this.target);
		
		this.cont.style.display = 'block';
		
		
		if(Math.round(target_pos.y - this.cont.offsetHeight - 5)  < 0){
			
			var y = 	Math.round(target_pos.y  + this.target.offsetHeight + 5);
			
		}	else{
			
				var y = 	Math.round(target_pos.y - this.cont.offsetHeight - 5);	
				
		}
		
		this.cont.style.left 	= target_pos.x +'px';
		this.cont.style.top 	= y +'px';
		
		this.changeDateName();
		this.drawGrid();		
}
	
	
	
Calendar.prototype.addMonth = function(v){

		(v)?n = v:n=1;
		this.date.addMonth(n);
		this.changeDateName();
		this.drawGrid();
		
		return 	this.date;	
		
}
	
	
Calendar.prototype.addYear = function(v){
		
		(v)?n = v:n=1;
		this.date.addYear(n);	
		this.changeDateName();
		this.drawGrid();	
	
		return 	this.date;
		
}
	
	
Calendar.prototype.changeDateName = function(){
	
		var month_name					= this.array_month_name[this.date.getMonth()];
		this.cur_date_name.innerHTML	= month_name+' '+this.date.getFullYear();		
		return 	month_name;		
}
	

//si un des paramètres min_date ou max_date a été spécifié, on calcule les possibilités d'affichage des dates
Calendar.prototype.checkLimit = function(){
	
	_this = this;
	
		if(this.min_date){
	
			if(this.date.getFullYear() == this.min_date.getFullYear() || this.date.getFullYear() == this.min_date.getFullYear()+1 && this.date.getMonth() < this.min_date.getMonth()){
				this.prev_year.className ='calNavButtonDisable';	
				this.prev_year.onclick = null;	
				
			}	else{
				
				this.prev_year.className = 'calNavButton';	
				this.prev_year.onclick = null;
				this.prev_year.onclick = function(){ _this.addYear(-1) };
				
			}
			
			
			if(this.date.getFullYear() == this.min_date.getFullYear() && this.date.getMonth() == this.min_date.getMonth()){

				this.prev_month.className = 'calNavButtonDisable';
				this.prev_month.onclick = null;	
				
			}	else{
				
				this.prev_month.className = 'calNavButton';
				this.prev_month.onclick = null;	
				this.prev_month.onclick = function(){ _this.addMonth(-1) };		
			}
		
		}	
		
		
		if(this.max_date){
			
			if(this.date.getFullYear() == this.max_date.getFullYear()|| this.date.getFullYear() == this.max_date.getFullYear()-1 && this.date.getMonth() > this.max_date.getMonth()){
			this.next_year.className = 'calNavButtonDisable';	
			this.next_year.onclick = null;	
			}	else{
				this.next_year.className = 'calNavButton';	
				this.next_year.onclick = null;
				this.next_year.onclick = function(){ _this.addYear(1) };
			}
			
			
			if(this.date.getFullYear() == this.max_date.getFullYear() && this.date.getMonth() == this.max_date.getMonth()){
				this.next_month.className = 'calNavButtonDisable';
				this.next_month.onclick = null;	
			}	else{
				this.next_month.className = 'calNavButton';
				this.next_month.onclick = null;	
				this.next_month.onclick = function(){ _this.addMonth(1) };		
			}
		
		}

}
	
	
//on dessine le calendrier :  on remplit les jours
Calendar.prototype.drawGrid = function(){
		
		//on vérifier s'il y des limites de date
		this.checkLimit();
		
		//on calcule le nombre de 'cases' / jours à faire afficher
		var num_case	= this.date.getMonthLength();
		
		
		//on recherche le nom du premier jour du mois ( lundi, mardi , etc...)
		var first_day	= this.date.getMonthFirstDay();
		
		//on initialise les jours du mois à 1
		i=1;
		
		//on boucle sur le nombre total de cases
		for(j=0;j<42;j++){
		
			var day_case = document.getElementById('d'+j);
			day_case.innerHTML = '';
			day_case.className = 'dayCase';
			real_id=first_day-1;
			
			if(real_id == -1){ 
				real_id = 6; 
			}
			
			if(j>=real_id && j<num_case+real_id){
				
				day_case.innerHTML = i;
				var date_case = new Date();
				date_case.setDate(i);
				date_case.setMonth(this.date.getMonth());
				date_case.setYear(this.date.getFullYear());
				day_case.setAttribute('date',date_case.getTime());

				if(this.min_date !='' && date_case < this.min_date  || this.max_date !='' && date_case > this.max_date){
					
					removeEvent(day_case,'click',function(e){
						
							if(window.event){ 
								var el = getEventTarget(e);
							}	else{
									el = this;	
							}	
									
							 _this.returnValue(el);  
						
						});	
					
					day_case.className = 'dayCaseDisable';
					
				}	
					else if(i == this.orig_date && this.date.getMonth() == this.orig_month &&this.date.getFullYear() == this.orig_year ){
					
						addEvent(day_case,'click',function(e){
						
							if(window.event){ 
								var el = getEventTarget(e);
							}	else{
									el = this;	
							}	
									
							 _this.returnValue(el);  
						
						});
											
									
						day_case.className = 'dayCaseSelected';
					
					}	
					
						else{

						
						addEvent(day_case,'click',function(e){
						
							if(window.event){ 
								var el = getEventTarget(e);
							}	else{
									el = this;	
							}	
									
							 _this.returnValue(el);  
						
						});
									
									
					}
						
				i++;
					
			}	else{
				
				removeEvent(day_case,'click',function(e){
						
							if(window.event){ 
								var el = getEventTarget(e);
							}	else{
									el = this;	
							}	
									
							 _this.returnValue(el);  
						
						});	
				
				day_case.className = 'dayCaseHide';	
				day_case.setAttribute('value',null);
				
			}
		}
}



Calendar.prototype.returnValue = function(cell){
		
		
		var d 					= new Date(Math.round(cell.getAttribute('date')));		
		var day 				= d.getDate().toString();
		var month 				= (d.getMonth()+1).toString();
		
		this.target.style.color = '#000000';
		
		if(day.length == 1){ 
			day = '0'+day;
		}
		
		if(month.length == 1){ 
			month = '0'+month;
		}
		
		this.target.value = day+'/'+month+'/'+d.getFullYear();
		this.hide();
			
}
	
	
	
Calendar.prototype.hide = function(){	
		
		if(_this["onClose"]){
			
			_this["onClose"].apply(this);
			
		}
		
	removeDom(this.cont);
}
	

