/*
 * Calendar for Mootools

 */

window.addEvent('domready', function() {

	var elements = $$('input');
	$$(elements).each(function(el){
  if (el.hasClass('ncalendar')) {
    el.addEvent('click', function(event) {
				new Calendar(el);
			});
	}
  });

});

var Calendar = new Class({
    initialize: function(el,open) {
      this.input = $(el);
      this.month_name = ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Décembre'];
      this.day_name = ['L','M','M','J','V','S','D'];
			this.create_calendar();
    },
    create_calendar: function(fecha) {

      if ($('new_calendar')) $('new_calendar').remove();

			 if (!fecha) {
      	this.today = new Date();
      	this.today.setDate(1);
      } else {this.today = fecha;}

      this.next_m = this.today.getMonth();
      this.next_m++;

      // capa contenedora //
      this.div = new Element('div');
      this.div.addClass('calendar');
      position = this.input.getCoordinates();

      this.div.setStyles({ 'font-size':'.8em','padding-top':'1em','opacity':'0',cursor: 'pointer', 'position': 'absolute', 'top':(position.top+position.height)+'px', 'left':(position.left)+'px'});
      this.div.makeDraggable();
      this.div.setProperty('id', 'new_calendar');

			// mes ańo y navegacion
      var div = new Element('div');
      div.setStyles({ 'podding':'1em','cursor':'move','text-align':'center','height':'30px'});
      div.injectInside(this.div);

      var titulo = new Element('strong');
      titulo.appendText(this.month_name[this.today.getMonth()]+' ' + this.today.getFullYear());
      titulo.injectInside(div);

     	var next = new Element('img');
      next.setProperty('src', '/Images/next.gif');
      next.setStyle('cursor','pointer');
      next.injectAfter(titulo);

      var before = new Element('img');
      before.setProperty('src', '/Images/prev.gif');
      before.setStyle('cursor','pointer');
			before.injectBefore(titulo);

  	var localThis = this;

			// boton de cerrado
			var close = new Element('img');
			close.setProperty('src', '/Images/cancel.gif');
			close.setStyles({'cursor':'pointer'});
			close.injectAfter(next);

			close.addEvent('click', function(e) {
          localThis.div.remove();
  		});


			// navegación por meses => next
			next.addEvent('click', function(e) {
          var fecha = localThis.today;
     	    fecha.setMonth(localThis.next_m,1);
          localThis.div.remove();
          localThis.create_calendar(fecha);
  		});
  		// navegación por meses => before
			before.addEvent('click', function(e) {
          var fecha = localThis.today;
     	    fecha.setMonth(localThis.next_m-2,1);
          localThis.div.remove();
          localThis.create_calendar(fecha);
  		});

      this.table = new Element('table');
      this.table.setStyles({'width':'190px','margin':'0 auto'});
			this.table.injectInside(this.div);

			var thead = new Element('thead');
			thead.injectInside(this.table);

   		var tr = new Element('tr');
      tr.injectInside(thead);

      this.day_name.each(function (day) {
				var td = new Element('th');
				td.appendText(day);
				td.injectInside(tr);
			});

			var tbody = new Element('tbody');
			tbody.injectInside(this.table);

			var first_day = this.today;
			var last_day = this.today;
			this.month = this.today.getMonth();
   		var tr = new Element('tr');
			tr.injectInside(tbody);

  		var day=0;

			/* primera semana */
			first_day.setDate(1);

			if (first_day.getDay() != 0) {
				for (var i= 1; i <= 7; i++) {
	       if (day) {
					day++;
					first_day.setDate(day);
	    		if (first_day.getDay() == 1) break; // domingo rompemos!
				 } else {
		      if (first_day.getDay() == i) {
		       this.create_td(tr,1,first_day);
					 day = 2;
					 first_day.setDate(day);
					}
	       }
	      	this.create_td(tr,day,first_day);
				}
			} else {
			  for (var i= 1; i <= 6; i++) {
			  this.create_td(tr,'','');
			  }
			  this.create_td(tr,1,first_day);
				day = 2;
			}

			var tr = new Element('tr');
			tr.injectInside(tbody);

			/* demás dias */
      var fecha_s = this.today;
			if (!day) var day = 1;
     	for (var i = day; i <= 31; i++) {

				fecha_s.setDate(i);
				var mes = fecha_s.getMonth();

				if ( mes == this.month) {
	      	if (fecha_s.getDay() == 1) {
						var tr = new Element('tr');
						tr.injectInside(tbody);
					}
					this.create_td(tr,i,fecha_s);
				}
			}

			this.div.injectAfter(this.input);
			this.effect(this.div,'show');
    },
		create_td: function(tr,i,fecha) {
        var localThis = this;
				var td = new Element('td');
				if (fecha) {
				  var dia = fecha.getDate();
				  
				  var mes = (fecha.getMonth()+1);
				  
				  if (dia <= 9) var dia = "0"+ dia;
				  if (mes <= 9) var mes = "0"+ mes;
//				  alert(dia + "-"+mes);

        	td.setProperty('id', dia + '/'+ mes +'/'+			fecha.getFullYear());
        }
				td.setStyles({'padding':'0em','text-align':'center'});
		var dateselect33=dia+"/"+mes+"/"+fecha.getFullYear();
				var jTest=verifDate(dateselect33);
///	ajout ced
if ((jTest.toString()!="0") & (jTest.toString()!="6")){		
        td.addEvent('click', function(e) {
				
					localThis.input.value = this.id;
					localThis.effect(localThis.div,'fade');
					localThis.div.remove();
  			});

  			td.addEvent('mouseover', function(e) {
						 this.addClass('dayselected');
  			});
  			td.addEvent('mouseout', function(e) {
						 this.removeClass('dayselected');
  			});
		}
    		if (i == 0) { i = ''; td.addClass('noday');  }
    		if (fecha) {
    			if (fecha.getDay()==0) td.addClass('sunday');
				if (fecha.getDay()==6) td.addClass('sunday');
    		}
  		  td.appendText(i);
				td.injectInside(tr);
		},
		effect: function(div,op) {
		  var ef = new Fx.Style(div, 'opacity', {
				duration: 500,
				transition: Fx.Transitions.quartInOut
			});
			if (op == 'fade') { ef.start(1,0);} else { ef.start(0,1);	}
		}
});
function verifDate(chaineDate){
	var ladate = (chaineDate).split("/");
	var unedate = new Date(eval(ladate[2]),eval(ladate[1])-1,eval(ladate[0]));
	return unedate.getDay();
}
