﻿/*
 * Calendar Emoxion
 * with Mootools
 * Manuel Garcia (thekeeper)
 * http://www.mgarcia.info
 * Version 0.2
 *
 * Copyright (c) 2007 Manuel Garcia
 * http://www.opensource.org/licenses/mit-license.php
 */

window.addEvent('domready', function() {
	$$('input.ncalendar').each(function(el){
    el.addEvent('click', function(event) {
				new Calendar(el);
			});
	});
});

var Calendar = new Class({
    initialize: function(el,open,Config) {
      this.input = $(el);
			var lng = new Object();
			
			// Firefox? IE ?
			try {  var nav = navigator.language.substr(0,2); }
			catch (e)	{ var nav = navigator.userLanguage;}
/*
			lng['es'] = {
      	month : ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
      	day : ['L','M','M','J','V','S','D'],
      	first: 1 // Monday
			}
			lng['de'] = {
      	month : ['Januar', 'Februar', 'M?rz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
      	day : ['M','D','M','D','F','S','S'],
      	first: 1 // Monday
			}
			lng['it']= {
      	month : ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'],
      	day : ['L','M','M','G','V','S','D'],
      	first: 1 // Monday
			}
			lng['en'] = {
      	month : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
				day : ['S','M','T','W','T','F','S'],
				first: 0 // Sunday
      }*/
      lng['fr'] = {
      	month : ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre','Décembre'],
      	day : ['L','M','M','J','V','S','D'],
      	first: 1 // Monday
			}
			if (!lng[nav]) { lng = lng['fr'] } else { lng =  lng[nav] }
      /* configuration */
      if (!Config) {
	      this.config = {
						Lng: lng,
					  imgNext: 'images/IconArrowRight.gif',
					  imgPrev: 'images/IconArrowLeft.gif',
					  imgCancel: 'images/IconLogout.gif'
				};
   		}

      this.month_name = this.config.Lng.month;
      this.day_name =  this.config.Lng.day;
			this.create_calendar();
    },
    create_calendar: function() {

     var position = this.input.getCoordinates();
     if ($('ncalendar')) $('ncalendar').remove();
      // content div  //
      //this.div = new Element('div').setStyles({'top':(position.top+position.height)+'px', 'left':(position.left)+'px'}).setProperty('id', 'ncalendar').injectAfter(this.input);
      // Oneego change
      this.div = new Element('div').setStyles({'position': 'absolute', 'top':'100px', 'left':'1300px', 'opacity':'0'}).setProperty('id', 'ncalendar').injectAfter(this.input);
      // Oneego change
      this.div.makeDraggable();
      this.nav();
      this.setdate(this.input.getProperty('value'));
			this.effect(this.div,'show');
		} ,
		nav: function (today) {
		  // nav
      this.calendardiv = new Element('div').injectInside(this.div);
      this.titulo = new Element('strong').injectInside(this.calendardiv);
      // Oneego added
      	// added spacers
    this.spacerRight = new Element('img').setProperty('src', 'images/spacer.gif').setStyles({ 'width':'20px','height':'10px'}).injectAfter(this.titulo);
	this.spacerLeft = new Element('img').setProperty('src', 'images/spacer.gif').setStyles({ 'width':'20px','height':'10px'}).injectBefore(this.titulo);
      // Oneego added
      // next month
      this.next = new Element('img').setProperty('src', this.config.imgNext).injectAfter(this.spacerRight);
      // before month
      this.before = new Element('img').setProperty('src', this.config.imgPrev).injectBefore(this.spacerLeft);
			// close
	// Oneego added
      	// added spacers
	this.spacerClose = new Element('img').setProperty('src', 'images/spacer.gif').setStyles({ 'width':'20px','height':'10px'}).injectAfter(this.next);
      // Oneego added
			this.close = new Element('img').setProperty('src', this.config.imgCancel).injectAfter(this.spacerClose);
			this.close.addEvent('click', function(e) {
          localThis.div.remove();
  		});
			// table
			this.table = new Element('table').injectInside(this.div);
			var thead = new Element('thead').injectInside(this.table);
   		var tr = new Element('tr').injectInside(thead);
			// day's name
      this.day_name.each(function (day) {
				var td = new Element('th').appendText(day).injectInside(tr);
			});
			
			var localThis = this;
		},
		setdate : function(date) {
			// reset event nav
			this.next.removeEvents('click');
			this.before.removeEvents('click');

			if (!this.validate_date(date)) {
        this.today = new Date();
		    this.today.setDate(1);
      } else {
      	var dateinp = date.split('/');
    		this.today = new Date(dateinp[2],dateinp[1]-1,dateinp[0],0,0,0);
			}

      this.next_m = this.today.getMonth();
      this.next_m++;

      this.titulo.innerHTML = this.month_name[this.today.getMonth()]+' ' + this.today.getFullYear();
  		var localThis = this;

			// event next
			this.next.addEvent('click', function(e) {
          var date = localThis.today;
     	    date.setMonth(localThis.next_m+1,1);
	        localThis.tbody.remove();
          localThis.setdate(date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear());
  		});
  		// event before
			this.before.addEvent('click', function(e) {
          var date = localThis.today;
     	    date.setMonth(localThis.next_m-1,1);
          localThis.tbody.remove();
          localThis.setdate(date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear());
  		});

			this.tbody = new Element('tbody').injectInside(this.table);
			var first_day = this.today;
			var last_day = this.today;
			this.month = this.today.getMonth();
   		var tr = new Element('tr').injectInside(this.tbody);

  		var day=0;

			/* first day week */
			first_day.setDate(1);
			for (var i= this.config.Lng.first; i <= 6; i++) {
			   if (first_day.getDay() == i) {
			    break;
      	 } else {
      	  this.create_td(tr,'','');
      	 }
			}
			if (this.config.Lng.first) {
          brea_k = 1;
				} else {
				  brea_k = 0;
			}
			/* everydays */
      var date_s = this.today;
      var brea_k; // breaking week

     	for (var i = 1; i <= 31; i++) {
				date_s.setDate(i);
 				if ( date_s.getMonth() == this.month) {
	      	if (date_s.getDay() == brea_k) {
						var tr = new Element('tr').injectInside(this.tbody);
					}
					this.create_td(tr,i,date_s);
				} else {
				  this.create_td(tr,'','');
				}
			}
			this.effect(this.tbody,'show');
    },
		create_td: function(tr,i,date) {
        var localThis = this;
				var td = new Element('td');
				if (date) {
				// Oneego added
				var dayOfTheWeek = date.getDay();
				// Oneego added
				  var dia = date.getDate();
				  var mes = (date.getMonth()+1);
				  //if (dia <= 9) var dia = "0"+ dia;
				  //if (mes <= 9) var mes = "0"+ mes;
        	td.setProperty('id', dia + '|'+ mes +'|'+ date.getFullYear());
        }
        td.addEvent('click', function(e) {
       			 localThis.input.value = this.id;
       			 		// Oneego added
       			 		//var dayOfTheWeek = "";
						 if(dayOfTheWeek == 0) { dayOfTheWeek = "dimanche"; }
						 else if(dayOfTheWeek == 1) { dayOfTheWeek = "lundi"; }
						 else if(dayOfTheWeek == 2) { dayOfTheWeek = "mardi"; }
						 else if(dayOfTheWeek == 3) { dayOfTheWeek = "mercredi"; }
						 else if(dayOfTheWeek == 4) { dayOfTheWeek = "jeudi"; }
						 else if(dayOfTheWeek == 5) { dayOfTheWeek = "vendredi"; }
						 else if(dayOfTheWeek == 6) { dayOfTheWeek = "samedi"; }
						 document.getElementById('DateDayW').value = dayOfTheWeek;
						 document.getElementById('DateDay').value = dia;
						 document.getElementById('jour').value = dia;
						 var TheMonth = "";
						 if(mes == 1) { TheMonth = "Jan."; }
						 else if(mes == 2) { TheMonth = "Fev."; }
						 else if(mes == 3) { TheMonth = "Mars"; }
						 else if(mes == 4) { TheMonth = "Avril"; }
						 else if(mes == 5) { TheMonth = "Mai"; }
						 else if(mes == 6) { TheMonth = "Juin"; }
						 else if(mes == 7) { TheMonth = "Juillet"; }
						 else if(mes == 8) { TheMonth = "Août"; }
						 else if(mes == 9) { TheMonth = "Sept."; }
						 else if(mes == 10) { TheMonth = "Oct"; }
						 else if(mes == 11) { TheMonth = "Nov."; }
						 else if(mes == 12) { TheMonth = "Déc."; }
						 document.getElementById('DateMonth').value = TheMonth;
						 document.getElementById('mois').value = mes;
       			 		// Oneego added
						 localThis.effect(localThis.div,'fade');
						 localThis.div.remove();
						 formupdate();
  			});
  			td.addEvent('mouseover', function(e) {
						 this.addClass('dayselected');
  			});
  			td.addEvent('mouseout', function(e) {
						 this.removeClass('dayselected');
  			});
    		if (i == 0) i = '';
				if (!date) { td.addClass('noday'); }
    		if (date) if (date.getDay()==0) td.addClass('sunday');
    		var today = new Date();
				today = today.getDate() + "/" + (today.getMonth()+1) + "/" + today.getFullYear();
				if (date) var date_td = date.getDate() + "/" + (date.getMonth()+1) + "/" + date.getFullYear();
				if (today == date_td) td.addClass('isToday');
  		  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);	}
		},
		validate_date: function (date) {
		  		var regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
		  		return date.test(regex);
		}
});