var Calendar = new Class({
    initialize: function(el,Config) {
      	this.input = $(el);

		if( Config ){
			var nb_date = Config["tab_date"].length;
			var tab_date_min = new Array();
			var tab_date_max = new Array();
			for(var i = 0; i < nb_date; i++){
				tab_date_min[i] = Config["tab_date"][i]["minDate"];
				tab_date_max[i] = Config["tab_date"][i]["maxDate"];
			}
			this.config = {
				Lng			: lng,
				imgNext		: 'images/calendar/next.gif',
				imgPrev		: 'images/calendar/prev.gif',
				imgCancel	: 'images/calendar/close.gif',
				maxDate		: Config["tab_date"][nb_date-1]["maxDate"],
				minDate		: Config["tab_date"][0]["minDate"],
				tab_maxDate		: tab_date_max,
				tab_minDate		: tab_date_min,
				nb_date : nb_date,
				format		: 'd/m/y'
			};
		}else{
			var min_date = new Date();
			var max_year = ( min_date.getMonth() > 4 )? min_date.getFullYear()+1 : min_date.getFullYear();
	    	this.config = {
				Lng			: lng,
				imgNext		: 'images/calendar/next.gif',
				imgPrev		: 'images/calendar/prev.gif',
				imgCancel	: 'images/calendar/close.gif',
				maxDate		: new Date(max_year,11,31,0,0,0),
				minDate		: min_date,
				format		: 'd/m/y'
			};
		}
		
      	
		localThis = this;
				
		if ( Config ) this.config = $merge( this.config, Config );

      	this.month_name = this.config.Lng.month;
      	this.day_name =  this.config.Lng.day;
		this.create_calendar();

		var localThis = this;
		document.addEvent('mousedown', function(e) {localThis.isBlur(e)});
    },
    create_calendar: function() {

     	var position = this.input.getCoordinates();
     	if ($('ncalendar')){ $('ncalendar').destroy(); $('nFrame').destroy()}
      	// content div  //
      	this.div = new Element('div')
      		.setStyles({'top':(position.top)+'px', 'left':(position.left + position.width + 2)+'px', 'z-index':'104'})
      		.setProperty('id', 'ncalendar')
	      	.injectInside(document.body);

		this.fra = new Element('iframe')
      		.setStyles({'border':'0','background':'transparent','position':'absolute','top':(position.top)+'px', 'left':(position.left + position.width + 2 )+'px', 'z-index':'103', 'width':'205px', 'height':'177px'})
      		.setProperty('id', 'nFrame');
      	this.nav();
      	this.setdate(this.input.getProperty('value'));
		this.effect(this.div,'show');
		this.fra.injectInside(document.body);
		} ,
	nav: function (today) {
		// nav
      	this.calendardiv = new Element('div').injectInside(this.div).addClass('header');
      	this.title = new Element('span').injectInside(this.calendardiv).addClass('month');
      	// next month
      	this.next = new Element('div',{'id': 'c_next', 'html': '&gt;&gt;'}).injectAfter(this.title);
      	// before month
      	this.before = new Element('div',{'id': 'c_prev', 'html': '&lt;&lt;'}).injectBefore(this.title);
		// close
		this.close = new Element('div',{'id': 'c_close', 'html': 'X'}).injectAfter(this.next);
		// table
		this.table = new Element('table').injectInside(this.div).setProperty('cellspacing', '1').setProperty('cellpadding', '0');
		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.sup();
  		});
	},
	setdate : function(date) {
		// reset event nav
		this.next.removeEvents('click');
		this.before.removeEvents('click');
		this.title.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.title.innerHTML = this.month_name[this.today.getMonth()]+' ' + this.today.getFullYear();
  		var localThis = this;

      	this.title.addEvent('click', function (e) {
	        if ( $('listYearUl') ) $('listYear').destroy();
	        else{
		        var divy = new Element('div').injectAfter(localThis.title).setProperty('id','listYear');
		        var date = localThis.today;
		        var ul = new Element('ul').setProperty('id','listYearUl').injectInside(divy);
				
				if( Config ){
					minyear = Config["tab_date"][0]["minDate"].getFullYear();
					maxyear = Config["tab_date"][nb_date-1]["maxDate"].getFullYear();
				}else{
					minyear = localThis.config.minDate.getFullYear();
		        	maxyear = localThis.config.maxDate.getFullYear();
				}
				
		        for (var a=minyear; a<=maxyear ;a++) {
		          var li = new Element('li').set('html',a).inject(ul)
		          .setProperty('id',a)
		          .addEvent('click', function (e) {
		            localThis.tbody.destroy();
		            m = parseInt( date.getMonth() + 1 );
		            //if( m < 10) m = '0' + m
		            localThis.setdate(date.getDate()+'/'+ m +'/'+this.getProperty('id'));
		            divy.destroy();
		          });
		        }
	        }
      	});
  		var localThis = this;

		// event next
		max_date = new Date(this.config.maxDate.getMonth() + '/10/' + this.config.maxDate.getFullYear());
		if (this.today < max_date ) {
			this.next.set('html', '&gt;&gt;');
  			this.next.addEvent('click', function(e) {
	            var date = localThis.today;
				date.setMonth(localThis.next_m+1,1);
	  	        localThis.tbody.destroy();
	            localThis.setdate(date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear());
    		});
  		}else{this.next.set('html', '&nbsp;');}
		
  		// event before
  		if (this.today > this.config.minDate ) {
  			this.before.set('html', '&lt;&lt;');
  			this.before.addEvent('click', function(e) {
	            var date = localThis.today;
	       	    date.setMonth(localThis.next_m-1,1);
	            localThis.tbody.destroy();
	            localThis.setdate(date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear());
    		});
  		}else{this.before.set('html', '&nbsp;');}

		// total days the last month
		var LastMonth = new Date(this.today.getFullYear(),this.next_m-2,1,0,0,0);
		var last = LastMonth.getMonth();

		var counter = 0;
		for (var b = 1; b <= (30 +  this.config.Lng.first); 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);
				class_Css = ( LastMonth < localThis.config.minDate )? 'noday0' : 'noday';
      	  		this.create_td(tr,counter,LastMonth,class_Css);
        	}
   		}
		(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);
				}
				if( this.config.nb_date ){
					class_Css = 'noday0';
					for(var j = 0; j <= this.config.nb_date-1; j++){
						if( date_s >= this.config.tab_minDate[j] && date_s <= this.config.tab_maxDate[j]  ){
							class_Css = '';
						}
					}
				}else{
					if( date_s < localThis.config.minDate || date_s > localThis.config.maxDate ){
          				class_Css = 'noday0';
          			}else if(!date_s.getDay()){
						class_Css = 'sunday';
          			}else class_Css = '';
				}
				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);
				class_Css = ( NextMonth > localThis.config.maxDate )? 'noday0' : 'noday';
				if( !this.config.nb_date )
				this.create_td(tr,i,NextMonth,class_Css);
			}
    	}
		this.effect(this.tbody,'show');
    },
	create_td: function(tr,i,date,class_Css) {
    	var localThis = this;
		var td = new Element('td');
		if (date) {
			var day = date.getDate();
			var month = (date.getMonth()+1);
			//  9 to 09 or another number <= 9
			if (day <= 9) day = "0"+ day;
			if (month <= 9) month = "0"+ month;
          	var ft = localThis.config.format;

          	var tddate 	= ft.replace('d',day);
			tddate 		= tddate.replace('m',month);
			tddate 		= tddate.replace('y',date.getFullYear());

        	td.setProperty('id', tddate);
        }
		
		<!-- rajout -->
if( this.config.nb_date	){	
	for(var j = 0; j <= this.config.nb_date-1; j++){
		if( date >= this.config.tab_minDate[j] ){
			if( date <= this.config.tab_maxDate[j] ){
          			td.addEvent('click', function(e) {
         				localThis.input.value = this.id;
  						localThis.effect(localThis.div,'fade');
  						localThis.sup();
    				});
			}else{
				td.addEvent('click', function(e) {
            			message = cal_error + '\n( Max date : ';
         				//alert( message + localThis.config.maxDate.getDate() + ' / ' +( localThis.config.maxDate.getMonth() + 1 ) + ' / ' + localThis.config.maxDate.getFullYear() + ' )' );
    				});
			}
		}else{
			td.addEvent('click', function(e) {
          			message = cal_error + ' \n(Min date ';
         			//alert( message + localThis.config.minDate.getDate() + ' / ' + (localThis.config.minDate.getMonth() + 1) + ' / ' + localThis.config.minDate.getFullYear() + ' )' );
			});
		}
	}
}else{
		if (this.config.minDate <= date) {
          	if (this.config.maxDate >= date) {
          		td.addEvent('click', function(e) {
         			localThis.input.value = this.id;
  					localThis.effect(localThis.div,'fade');
  					localThis.sup();
    			});
    		} else {
            	td.addEvent('click', function(e) {
            		message = cal_error + '\n( Max date : ';
         			alert( message + localThis.config.maxDate.getDate() + ' / ' +( localThis.config.maxDate.getMonth() + 1 ) + ' / ' + localThis.config.maxDate.getFullYear() + ' )' );
    			});
          	}
  		} else {
          	td.addEvent('click', function(e) {
          		message = cal_error + ' \n(Min date ';
         		alert( message + localThis.config.minDate.getDate() + ' / ' + (localThis.config.minDate.getMonth() + 1) + ' / ' + localThis.config.minDate.getFullYear() + ' )' );
    		});
		}
}
<!-- fin rajout -->
		/*var clone_date = this.config.tab_minDate[0];
		var nb_jour = this.config.tab_minDate[0].diff(this.config.tab_maxDate[this.config.nb_date-1]);
		for(var j = 1; j <= nb_jour; j++){
			if( clone_date >  ){
				
			}
			clone_date.increment();
		}*/		
		<!-- rajout -->
        /*if (this.config.minDate <= date) {
          	if (this.config.maxDate >= date) {
          		td.addEvent('click', function(e) {
         			localThis.input.value = this.id;
  					localThis.effect(localThis.div,'fade');
  					localThis.sup();
    			});
    		} else {
            	td.addEvent('click', function(e) {
            		message = cal_error + '\n( Max date : ';
         			alert( message + localThis.config.maxDate.getDate() + ' / ' +( localThis.config.maxDate.getMonth() + 1 ) + ' / ' + localThis.config.maxDate.getFullYear() + ' )' );
    			});
          	}
  		} else {
          	td.addEvent('click', function(e) {
          		message = cal_error + ' \n(Min date ';
         		alert( message + localThis.config.minDate.getDate() + ' / ' + (localThis.config.minDate.getMonth() + 1) + ' / ' + localThis.config.minDate.getFullYear() + ' )' );
    		});
        }*/
  		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');

  		  	td.appendText(i);
			td.injectInside(tr);
		},
		isBlur: function(event) {
		    var coord = this.div.getCoordinates();
		    if( event.page.x < (coord.left-5)
		     || event.page.x > (coord.left+coord.width+5)
		     || event.page.y < (coord.top-5)
		     || event.page.y > (coord.top+coord.height+5 ) )
		    this.sup();
		},
		effect: function(div,op) {
			var ef = new Fx.Tween(div, {
				duration: 200
			});
			(op == 'fade')? ef.start('opacity',1,0): ef.start('opacity',0,1);
		},
		sup: function( obj ){
			$$(this.div,this.fra).destroy();
		},
		validate_date: function (date) {
		  	var regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
		  	return date.test(regex);
		}
});

var heure 	= dateTahiti.getHours();
var minute	= dateTahiti.getMinutes()-1;
var seconde = dateTahiti.getSeconds();
var jour 	= dateTahiti.getDate();
var mois 	= dateTahiti.getMonth();
var nomJour = tabNomJour[dateTahiti.getDay()-1];
var annee 	= dateTahiti.getYear();
if(annee < 999) annee+=1900;

function heureTahiti(){
	if ( minute < 10 ) minute = "0" + Math.round( minute );
	else if(minute > 59){minute = "00"; heure++;}
	if ( heure < 10 ) heure = "0" + Math.round( heure );
	else if(heure > 23)heure = "00";
	heureTexte = heure + ':' + minute;
	$("heures").innerHTML = heureTexte;
	minute++;

}
function change_theme () {

	$$('.aff_ss_thm').setStyle('display','none');
	var th = $('themes_petit_pack').getProperty('value')+'neo';
	$(th).setStyle('display','block');
	var radio = $(th).getElement('span input[type=radio]');
	radio.setProperty("checked", "checked");

}
if($('jours'))$("jours").innerHTML = nomJour.substr(0,3)+ " " + jour+ " "+ tabMois[mois].substr(0,3);
if($('heures')){heureTahiti();setInterval("heureTahiti()", 60000);}

//window.addEvent('domready', function(){
var url = window.location.href;
	$$('input[name=arrival],input[name=dateArrivee]').each(function(el){
		el.addEvent('click', function(e){
			if(el.getProperty('id') == 'arrival' && url.test(/\/package\/pearl\-resort\//))
			new Calendar(el,{maxDate: new Date(2011,2,31,0,0,0),minDate : new Date(2010,10,1,0,0,0)});
			else
			new Calendar(el);
		});
	});
//});
