ACC.ControlDates = function(datas) {
	if(!(this instanceof ACC.ControlDates)) {
		return new ACC.ControlDates(datas);
	}
	
	this.initialize(datas);
};
ACC.extend(ACC.ControlDates, {
	initialize: function(datas) {
		this.today = this.getToday();
		this.min = datas.min ? this.getNewDate(datas.min) : null;
		this.max = datas.max ? this.getNewDate(datas.max) : null;
	},
	
	setConfig: function(datas) {
		if(datas.min) {this.min = this.getNewDate(datas.min);}
		if(datas.max) {this.max = this.getNewDate(datas.max);}
	},
	
	getToday: function() {
		var today = new Date();
		return {d: today.getDate(), m: today.getMonth() + 1, y: today.getFullYear(), date: today};
	},
	
	getNextWeekEnd: function(o) {
		o = o || this.getToday();
		var i = 5 - o.date.getDay();
		if(i < 0) {i = 6;}
		o.d = o.d + i;
		return this.getNewDate(o);
	},
	
	getNewDate: function(o) {
		o = o || {};
		if(o.method == 'add') {
			o.d = ((o.d !== undefined) ? o.d : 0) + this.today.d;
			o.m = ((o.m !== undefined) ? o.m : 0) + this.today.m;
			o.y = ((o.y !== undefined) ? o.y : 0) + this.today.y;
		} else if(o.method == 'substract') {
			o.d = this.today.d - ((o.d !== undefined) ? o.d : 0);
			o.m = this.today.m - ((o.m !== undefined) ? o.m : 0);
			o.y = this.today.y - ((o.y !== undefined) ? o.y : 0);
		} else {
			o.d = (o.d !== undefined) ? o.d : this.today.d;
			o.m = (o.m !== undefined) ? o.m : this.today.m;
			o.y = (o.y !== undefined) ? o.y : this.today.y;
		}
		var n = new Date(o.y, o.m - 1, o.d);
		return {d: n.getDate(), m: n.getMonth() + 1, y: n.getFullYear(), date: n};
	},
	
	compare: function(d1, d2) {
		d1 = Number(''+ d1.y + (d1.m < 10 ? '0'+ d1.m : d1.m) + (d1.d < 10 ? '0'+ d1.d : d1.d));
		d2 = Number(''+ d2.y + (d2.m < 10 ? '0'+ d2.m : d2.m) + (d2.d < 10 ? '0'+ d2.d : d2.d));
		return d2 - d1;
	}
});

ACC.tools.addDomReadyListener(function() {
	$(document.body).addClassName('js');
	
	ACC.promoDates = ACC.ControlDates({
		min: {},
		max: {d: 404, method: 'add'}
	});
	
	var sE = document.getElementById('search-error'),
		dest = document.getElementById('nom_ville'),
		dA = document.getElementById('jour_arrivee'),
		mA = document.getElementById('mois_arrivee'),
		yA = document.getElementById('annee_arrivee'),
		arrivee = document.getElementById('arrivee'),
		nbN = document.getElementById('nb_nuit'),
		codeAvantage = document.getElementById('code_avantage');
	
	if(sE && dest && dA && mA && yA && arrivee && nbN && Calendar._TT.DEF_DATE_FORMAT) {
		var changeDate = function() {
			var date, tmp;
			if(this.value == '99') {
				date = ACC.promoDates.today;
			} else {
				tmp = ACC.promoDates.getNewDate({d: Number(this.value), method: 'add'});
				date = ACC.promoDates.getNextWeekEnd(tmp);
			}
			writeDate(date.y, date.m, date.d);
		};
		
		var writeDate = function(y, m, d) {
			var dDisplay = d < 10 ? '0' + d : d,
				mDisplay = m < 10 ? '0' + m : m;
			arrivee.value = Calendar._TT.DEF_DATE_FORMAT.replace('%d', dDisplay).replace('%m', mDisplay).replace('%Y', y);
			dA.value = d;
			mA.value = m;
			yA.value = y;
			nbN.value = '1';
		};
		
		var prepareDate = function(y, m, d) {
			return {d: Number(d), m: Number(m), y: Number(y)};
		};
		
		var validate = function(e) {
			if(/^\s*$/.test(dest.value) || !dest.value) {
				$(sE).removeClassName('off');
				return false;
			}
			// empty codeAvantage if necessary
			if(codeAvantage && codeAvantage.value == codeAvantage.defaultValue) {
				codeAvantage.value = '';
			}
			if(nbN.value) {
				var n = Number(nbN.value),
					max = {d: 405 - n, method: 'add'},
					tmp = prepareDate(yA.value, mA.value, dA.value);
				ACC.promoDates.setConfig({max: max});
				var cMin = ACC.promoDates.compare(tmp, ACC.promoDates.min);
				if(cMin > 0) {
					writeDate(ACC.promoDates.min.y, ACC.promoDates.min.m, ACC.promoDates.min.d);
					return true;
				}
				var cMax = ACC.promoDates.compare(tmp, ACC.promoDates.max);
				if(cMax < 0) {
					nbN.value = n + c;
				}
			}
		};
		
		document.getElementById('bookingEngine').onsubmit = validate;
		
		var we0 = document.getElementById('we0'),
			we7 = document.getElementById('we7'),
			we99 = document.getElementById('we99');
		if(we0 && we7 && we99) {
			$(we0).onclick = changeDate;
			$(we7).onclick = changeDate;
			$(we99).onclick = changeDate;
		}
		
		//var today = ACC.promoDates.getToday();
		//writeDate(today.y, today.m, today.d);
	}
	
	if(codeAvantage) {
		codeAvantage.onfocus = function() {
			if(this.value == this.defaultValue) {this.value = '';}
		};
		codeAvantage.onblur = function() {
			if(/^\s*$/.test(this.value)) {this.value = this.defaultValue;}
		};
	}
});