

/*
 * 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
 */
 
var dayini;
var monthini;
var yearini;
			

window.addEvent('domready', function() {
	var el='pt';
	$$('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;}

		nav='pt';

			lng['pt'] = {
      	month : ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
      	day : ['S','T','Q','Q','S','S','D'],
      	first: 1 // Monday
			}

			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 // First day of week => 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['fr'] = {
      	month : ['Janvier', 'Février', 'Mars', 'Avril', 'Peuvent', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre','Décembre'],
      	day : ['L','M','M','J','V','S','D'],
      	first: 1 // Monday
			}
			lng['it']= {
      	month : ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Possono', '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 = (!lng[nav])? lng['en'] : lng =  lng[nav] ;
      /* configuration */
      if (!Config)
	      this.config = {
						Lng: lng,
					  //imgNexts: 'img/nexts.png',
					  //imgPrevs: 'img/prevs.png',
					  imgNexts: 'img/application_go.gif',
					  imgPrevs: 'img/application_after.gif',
					  /*imgCancels: 'img/cancels.gif',*/
					  //imgNext: 'img/next.png',
					  //imgPrev: 'img/prev.png',
					  imgNext: 'img/mes_seguinte.gif',
					  imgPrev: 'img/mes_anterior.gif',
					  imgCancel: 'img/cancel.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').setProperty('id', 'ncalendar').injectAfter(this.input);
     //      this.div = new Element('div').setStyles({'top':(position.top+position.height)+'px', 'left':(position.left)+'px'}).setProperty('id', 'ncalendar').injectAfter(this.input);
	// this.div.makeDraggable();
	  
	  
	  
	  
	  
		var iW = 550;
		var iH = 390;
		
	  			
		// ex3
		this.div.setStyles({
			opacity: 0.1
		});
				 
			var d = this.div.makeDraggable({
			onStart: function(el) {
				var size = el.getCoordinates();
				var _x = iW - size.width - 1; // based on border, margin -> getStyle().toInt();
				var _y = iH - size.height - 1; // based on border, margin -> getStyle().toInt();
				d.limit = {x: [1, _x], y: [1,_y]};
			}
			});
			
			
			
			
			
      
	  
	  
	  
	  var convdate = new Date();
		
	
	  if(this.input.getProperty('value')!=""){
			convdate= this.input.getProperty('value').split('-');
		  
	 	  	dayini=convdate[2];
			monthini=convdate[1]-1; 
			yearini=convdate[0]; 
			
			
    		convdate = convdate[2]+"/"+convdate[1]+"/"+convdate[0];
			
	  			
			
	  }else{
	 	  	dayini=convdate.getDate();
			monthini=(convdate.getMonth());
	 	  	yearini=convdate.getFullYear(); 
			
			
		 convdate = convdate.getDate() + "/" + (convdate.getMonth()+1) + "/" + convdate.getFullYear();
		 	  
	  }
	  

	  
	  
	  this.nav(); 
      //this.setdate(this.input.getProperty('value'));
	  this.setdate(convdate);
			this.effect(this.div,'show');
		} ,
		nav: function (today) {
		  // nav
      this.calendardiv = new Element('div').injectInside(this.div);
	  
	  
      this.titulo = new Element('h10').injectInside(this.calendardiv);
	  
      // next month
      this.nexts = new Element('img').setProperty('src', this.config.imgNexts).injectAfter(this.titulo);
      // before month
      this.befores = new Element('img').setProperty('src', this.config.imgPrevs).injectBefore(this.titulo);
	  
      // next month
      this.next = new Element('img').setProperty('src', this.config.imgNext).injectAfter(this.titulo);
      // before month
      this.before = new Element('img').setProperty('src', this.config.imgPrev).injectBefore(this.titulo);
	  

	  
			// close
			//this.close = new Element('img').setProperty('src', this.config.imgCancel).injectAfter(this.next);
			this.close = new Element('img').setProperty('src', this.config.imgCancel).injectAfter(this.nexts);
			
			/*
			this.closes = new Element('img').setProperty('src', this.config.imgCancels).injectAfter(this.nexts);
			*/
			
			// table
			this.table = new Element('table').injectInside(this.div);
			var thead = new Element('thead').injectInside(this.table);
   		var tr = new Element('tr').injectInside(thead);

      this.day_name.each(function (day) {
				var td = new Element('th').appendText(day).injectInside(tr);
			});

			var localThis = this;
			this.close.addEvent('click', function(e) {
          localThis.div.remove();
  		});
		},
		setdate : function(date) {
			// reset event nav
			this.next.removeEvents('click');
			this.before.removeEvents('click');
			
			
			this.nexts.removeEvents('click');
			this.befores.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.next_y = this.today.getFullYear();
      this.next_y++;
	 
	  

			
			

      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());
		  

		  
  		});
			
			
			

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

		  
  		});		
			
			/*
			this.closes.addEvent('click', function(e) {
       			 localThis.input.value = '';
				 localThis.div.remove();
  		});
		*/
			
			
			
			
		


			
			var LastMonth = new Date(this.today.getFullYear(),this.next_m-2,1,0,0,0);

			var last = LastMonth.getMonth();
			// total days the last month
			var counter = 0;
			for (var b = 1; b <= 31; b++) {
			  LastMonth.setDate(b);
 				if ( LastMonth.getMonth() == last) {
 				  counter++;
 				}
			}

			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);
			var rest = (!first_day.getDay())? 6: first_day.getDay()-1;
			counter = counter - rest;
			for (var i= this.config.Lng.first; i <= 6; i++) {
			   if (first_day.getDay() == i) {
			    break;
      	 } else {
					counter++;
					LastMonth.setDate(counter);
					if (LastMonth.getMonth() == this.today.getMonth()) LastMonth.setMonth(this.today.getMonth()-1);
      	  this.create_td(tr,counter,LastMonth,'noday');
        }
   		}
			(this.config.Lng.first)? brea_k = 1:brea_k = 0;
   /* everydays */
      var date_s = this.today;
      var class_Css;
      var brea_k; // breaking week
  	  var daycounter = 0;
     	for (var i = 1; i <= 31; i++) {
    		date_s.setDate(i);
 				if (date_s.getMonth() == this.month) {
       		daycounter++;
		      if (date_s.getDay() == brea_k) {
						var tr = new Element('tr').injectInside(this.tbody);
					}
          //class_Css = (!date_s.getDay())? 'sunday' : '';
		
		  //class_Css =  (dayini==date_s.getDate() && monthini==date_s.getMonth() && yearini==date_s.getFullYear()  )? class_Css+'dayini' : class_Css;
		  class_Css =  (dayini==date_s.getDate() && monthini==date_s.getMonth() && yearini==date_s.getFullYear()  )? 'dayini' : '';
          
		  
					this.create_td(tr,i,date_s,class_Css);
				}
			}
			  this.today.setMonth(this.month);
       	this.today.setDate(daycounter);
       	var NextMonth = new Date(this.today.getFullYear(),this.today.getMonth()+1,1,0,0,0);
		    // finish month
			  var num = date_s.getDay();
			  num = (brea_k)? 7 - num: 6 - num;
			  var b;
			  b = (brea_k)? 0 : 6 ;
        if (this.today.getDay() != b) {
				  for (var i= 1; i <= (num); i++) {
				      NextMonth.setDate(i);
							this.create_td(tr,i,NextMonth,'noday');
					}
    		}
			this.effect(this.tbody,'show');
    },
		create_td: function(tr,i,date,class_Css) {
        var localThis = this;
				var td = new Element('td');
				if (date) {
				  var dia = date.getDate();
				  var mes = (date.getMonth()+1);
				  //  9 to 09 or another number <= 9
				  if (dia <= 9) dia = "0"+ dia;
				  if (mes <= 9) mes = "0"+ mes;
			//td.setProperty('id', dia + '/'+ mes +'/'+	date.getFullYear());
        	td.setProperty('id',  date.getFullYear()+'-'+mes+'-'+dia);
			
        }
        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 (class_Css) td.addClass(class_Css);
    		// Today ??
    		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');

		//a = new Element('a').injectInside(td);


  		  td.appendText(i);
		 // a.appendText(i);
				td.injectInside(tr);
		},
		effect: function(div,op) {
		  var ef = new Fx.Style(div, 'opacity', {
				duration: 500,
				transition: Fx.Transitions.quartInOut
			});
			(op == 'fade')? ef.start(1,0): ef.start(0,1);
		},
		validate_date: function (date) {
		  		var regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
		  		return date.test(regex);
		}
});














