/*
	date_pattern:
		Y-m-d	= 2008-01-01
		y-n-j	= 08-1-1

	ABBREVIATIONS
	
	d	;	day
	e	:	end
	m	:	month
	s	:	start
	y	:	year
	
	abbr	:	abbreviation
	cal	:	calendar
	ctl	:	ctl;
	cur	:	current
	dsb	:	disabled
	dsp	:	display
	fct	:	function
	ipt	:	input
	lst	:	last
	nxt	:	next
	oth	:	other
	prv	:	previous
	slc	:	select
	sld	:	selected
	spn	:	span
	tbd	:	tbody
	tdy	:	today
	tfo	:	tfoot
	thd	:	thead
	tlt	:	translation
*/
function Calendar(arg) {
	this.dbg = false;
	this.weekStartDay = 1;
	this.isShowYearNavigation = false;
	this.isShowYearNavigationInput = false;
	
	this.disabledDatesExpression = '';
	
	this.init(arg);
}

Calendar.prototype.constructor = Calendar;

/*	METHODS	*/
Calendar.prototype = {
	init: function(arg) {
		var Calendar = this;
		if(this.dbg) { window.alert('Calendar.init();'); }
		this.setLang(arg['lang']);
		
		this.prd = new Object();
		this.prd.id = (arg['prd_id']) ? arg['prd_id'] : null;
		
		this.prd.ofr = (arg['ofr']) ? arg['ofr'] : null;
		
		/*	FUNCTION OBJECT	*/
		this.fct = new Object();
		this.fct.cpr = (arg['fct_cpr']) ? arg['fct_cpr'] : function() { return false; };
		this.fct.ofr = (arg['fct_ofr']) ? arg['fct_ofr'] : function() { return false; };
		this.fct.upd = (arg['fct_upd']) ? arg['fct_upd'] : function() { return false; };
		
		this.css_pfx = (arg['css_pfx'])	? arg['css_pfx']+'_' : 'cal_';
		
		/*	DATE OBJECT	*/
		this.date = new Object();
		this.date.now = new Date();
		
		switch(typeof(arg['date_s'])) {
			case 'string':
			(arg['date_s'] == 'now') ? this.date.s = new Date(this.date.now.getFullYear(),this.date.now.getMonth(),this.date.now.getDate()-1) : new Date(arg['date_s'].split('-')[2],(arg['date_s'].split('-')[1]-1),arg['date_s'].split('-')[0]);
			break;
			case 'object':
			this.date.s = arg['date_s'];
			break;
			default:
			this.date.s = new Date(this.date.now.getFullYear(),this.date.now.getMonth(),(this.date.now.getDate()-1));
			break;
		}
		
		switch(typeof(arg['date_e'])) {
			case 'string':
			this.date.e = new Date(arg['date_e'].split('-')[2],(arg['date_e'].split('-')[1]-1),arg['date_e'].split('-')[0]);
			break;
			case 'object':
			this.date.e = arg['date_e'];
			break;
			default:
			this.date.e = null;
			break;
		}
		
		switch(typeof(arg['date_sld'])) {
			case 'object':
			this.date.sld = arg['date_sld'];
			break;
			default:
			case 'string':
			(arg['date_sld'] == 'now' || arg['date_sld'] == undefined) ? this.date.sld = this.date.now : new Date(arg['date_sld'].split('-')[2],(arg['date_sld'].split('-')[1]-1),arg['date_sld'].split('-')[0]);
			break;
		}		
		pattern = (arg['date_pattern']) ? arg['date_pattern'] : 'Y-m-d';
		pattern = (pattern.split('-').length > 1) ? pattern.split('-') : pattern.split('/');
		
		this.date.daysPerMonth	= new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		
		this.date.pattern	= new Array();
		this.date.pattern['d']	= pattern[2];
		this.date.pattern['m']	= pattern[1];
		this.date.pattern['y']	= pattern[0];
		
		this.date.dsbWeekDays = ('object' == typeof(arg['dsb_d'])) ? arg['dsb_d'] : this.setDsbWeekDays(arg['dsb_d']);
		
		this.HTML = new Object();
		this.HTML.anchor = ('string' == typeof(arg['anchor'])) ? document.getElementById(arg['anchor']) : arg['anchor'];
		this.HTML.opener = ('string' == typeof(arg['opener'])) ? document.getElementById(arg['opener']) : arg['opener'];
		this.HTML.opener.style.cursor = 'pointer';
		
		Add_Event(this.HTML.opener,'click',function() { Calendar.show(); });
		
		this.HTML.slc = new Object();
		this.HTML.slc.d = ('string' == typeof(arg['slct_d'])) ? document.getElementById(arg['slct_d']) : arg['slct_d'];
		this.HTML.slc.m = ('string' == typeof(arg['slct_m'])) ? document.getElementById(arg['slct_m']) : arg['slct_m'];
		this.HTML.slc.y = ('string' == typeof(arg['slct_y'])) ? document.getElementById(arg['slct_y']) : arg['slct_y'];
		
		/*	INITIALISATION � LA DATE ACTUELLE	*/
		if(this.date.s == 'now') {
			now = new Date(this.date.now.getFullYear(),this.date.now.getMonth(),this.date.now.getDate()-1);
			this.date.s = now.getFullYear()+'-'+addZero((now.getMonth()+1))+'-'+addZero((now.getDate()));
		}
		
		if(this.date.e == 'now') {
			now = new Date(this.date.now.getFullYear(),this.date.now.getMonth(),this.date.now.getDate()+1);
			this.date.e = now.getFullYear()+'-'+addZero((now.getMonth()+1))+'-'+addZero((now.getDate()));
		}
		
		if(this.date.sld == 'now') {
			this.date.sld = this.date.now.getFullYear()+'-'+addZero((this.date.now.getMonth()+1))+'-'+addZero((this.date.now.getDate()));
		}
		
		if(this.date.s != null) { this.addDisabledDates(null,this.date.s); }
		if(this.date.e != null) { this.addDisabledDates(this.date.e,null); }
		
		if(navigator.appName == 'Microsoft Internet Explorer') {
			Add_Event(document,'click',function() { Calendar.isClickedOut(); });
		}
		else {
			Add_Event(document,'click',function(e) { Calendar.isClickedOut(e); });
		}
		
		Add_Event(window,'load',function() { Calendar.setHTML(); });
	},
	
	addDisabledDates: function(s,e) {
		//window.alert(s+'\n'+e);
		if(arguments.length == 1) { e = s; }
		if(s == null && e == null) { return false; }
		if(this.disabledDatesExpression != '') { this.disabledDatesExpression += "||"; }
		
		if(s != null) { s = ''+s.getFullYear()+addZero((s.getMonth()))+addZero((s.getDate())); }
		if(e != null) { e = ''+e.getFullYear()+addZero((e.getMonth()))+addZero((e.getDate())); }
		
		if(s == null) { this.disabledDatesExpression += '(ds <= '+e+')'; }
		else if(e == null) { this.disabledDatesExpression += '(ds >= '+s+')'; }
		else { this.disabledDatesExpression += '(ds >= '+s+' && ds <= '+e+')'; }
		return true;
	},
	
	isClickedOut: function(e) {
		if(!e)	{ e = window.event; element = e.srcElement; }
		else	{ element = e.target; }
		bool = false;
		while(false == bool && undefined != element) {
			if(element == this.HTML.cal || element == this.HTML.opener) { return false; }
			if(element.tagName.toLowerCase() == "html") {
				this.hide();
				return true;
			}
			element = element.parentNode;
		}
	},
	
	
	setCurDate: function(y,m,d) {
		if(this.dbg) { window.alert('Calendar.setCurDate('+y+','+m+','+d+')'); }
		this.date.cur = new Date(y,m,d);
		return true;
	},
	
	setDsbWeekDays: function(dsb_d) {
		if(this.dbg) { window.alert('Calendar.setDsbWeekDays('+dsb_d+')'); }
		this.date.dsbWeekDays = new Object();
		if(undefined != dsb_d) {
			for(i = 0; i < dsb_d.length; i++) {
				this.date.dsbWeekDays[dsb_d[i]] = true;
			}
		}
	},
	
	setPos: function() {
		this.HTML.cal.style.top = (getTop(this.HTML.anchor) + this.HTML.anchor.offsetHeight) + 'px';
		this.HTML.cal.style.left = (getLeft(this.HTML.anchor) + this.HTML.anchor.offsetWidth + 20) + 'px';
	},

	setHTML: function() {
		if(this.dbg) { window.alert('Calendar.setHTML()'); }
		/*	TABLE HEAD	*/
		thd = document.createElement('thead');
		/*	MONTH NAME AND ARROWS	*/
		thd.appendChild(document.createElement('tr'));
		thd.childNodes[0].appendChild(document.createElement('td'));
		thd.childNodes[0].appendChild(document.createElement('th'));
		thd.childNodes[0].appendChild(document.createElement('td'));
		thd.childNodes[0].childNodes[0].className = this.css_pfx + 'prv_m';
		thd.childNodes[0].childNodes[1].colSpan = "5";	//	HACK IE
		//thead.childNodes[0].childNodes[1].setAttribute('colspan','5');
		thd.childNodes[0].childNodes[1].className = this.css_pfx + 'cur_m';
		thd.childNodes[0].childNodes[2].className = this.css_pfx + 'nxt_m';
		/*	DAYS ABBREVIATIONS	*/
		thd.appendChild(document.createElement('tr'));
		thd.childNodes[1].className = this.css_pfx + 'day_abb';
		for(var i = 0; i < 7; i++) {
			thd.childNodes[1].appendChild(document.createElement('th'));
			txt = (this.date.dsbWeekDays[i+1]) ? ' - '+TLT.closed[this.lang] : '';
			thd.childNodes[1].childNodes[i].setAttribute('title',TLT.day.name[this.lang][((this.weekStartDay+i)%7)] + txt);
			thd.childNodes[1].childNodes[i].innerHTML = TLT.day.abbr[this.lang][((this.weekStartDay+i)%7)];
			thd.childNodes[1].childNodes[i].className = (this.date.dsbWeekDays[i+1]) ? 'dsb' : null;
		}
		/*	TABLE FOOT	*/
		tfo = document.createElement('tfoot');
		/*	TODAY	*/
		tfo.appendChild(document.createElement('tr'));
		tfo.childNodes[0].appendChild(document.createElement('th'));
		tfo.childNodes[0].childNodes[0].className = this.css_pfx + 'cur';
		tfo.childNodes[0].childNodes[0].innerHTML = TLT.tdy[this.lang];
		tfo.childNodes[0].childNodes[0].colSpan = '7';	//	HACK IE
		//tfoot.childNodes[0].childNodes[0].setAttribute('colspan','7');
		/*	TABLE BODY	*/
		tbd = document.createElement('tbody');
		for(i = 0; i < 6; i++) {
			tbd.appendChild(document.createElement('tr'));
			for(j = 0; j < 7; j++) {
				tbd.childNodes[i].appendChild(document.createElement('td'));
			}
		}
		/*	TABLE	*/
		this.HTML.cal = document.createElement('table');
		this.HTML.cal.className			= this.css_pfx + 'tbl';
		this.HTML.cal.style.position	= 'absolute';
		this.HTML.cal.style.display		= 'none';
		this.HTML.cal.style.zIndex		= '100';
		this.HTML.cal.appendChild(thd);
		this.HTML.cal.appendChild(tfo);
		this.HTML.cal.appendChild(tbd);
		this.HTML.cal.prv = thd.childNodes[0].childNodes[0];
		this.HTML.cal.cur = thd.childNodes[0].childNodes[1];
		this.HTML.cal.nxt = thd.childNodes[0].childNodes[2];
		this.HTML.cal.tdy = tfo.childNodes[0].childNodes[0];
		this.HTML.cal.tbd = tbd;
		document.body.appendChild(this.HTML.cal);
		this.setPos();
	},

	setInnerHTML: function() {
		var Calendar = this;
		if(this.dbg) { window.alert('Calendar.setInnerHTML()'); }
		this.date.sld = new Date(this.HTML.slc.y.value,this.HTML.slc.m.value-1,this.HTML.slc.d.value);
		
		switch(arguments.length) {
			case 2:
			var y = arguments[1];
			var m = arguments[0];
			var d = this.date.sld.getDate();
			break;
			
			case 3:
			var y = arguments[0];
			var m = arguments[1];
			var d = arguments[2];
			this.date.sld = new Date(arguments[0],arguments[1],arguments[2]);
			break;
			
			case 0:
			default:
			var y = this.date.sld.getFullYear();
			var m = this.date.sld.getMonth();
			var d = this.date.sld.getDate();
			break;
		}
		
		this.date.daysPerMonth[1] = (((y%4==0)&&(y%100!=0))||(y%400==0)) ? 29 : 28;
		
		var dsp_y	= y; 
		var dsp_m	= m; 
		var dsp_d	= 1; 
		
		var m_first_d	=  new Date(y,m,1).getDay(); 
		var offset 				= 0;
		offset = (m_first_d >= this.weekStartDay) ? m_first_d-this.weekStartDay : 7-this.weekStartDay+m_first_d ;
		
		if(offset > 0) {
			dsp_m--;
			if(dsp_m < 0) { dsp_m = 11; dsp_y--; }
			dsp_d = this.date.daysPerMonth[dsp_m]-offset+1;
		}
		
		var nxt_m = m+1;
		var nxt_m_y = y;
		if(nxt_m > 11) { nxt_m=0; nxt_m_y++; }
		var lst_m = m-1;
		var lst_m_y = y;
		if(lst_m < 0) { lst_m=11; lst_m_y--; }
		
		this.HTML.cal.cur.innerHTML	= TLT.mth.name[this.lang][m] + ' ' + y;
		this.HTML.cal.prv.onclick	= function() { Calendar.setInnerHTML(lst_m,lst_m_y); };
		this.HTML.cal.nxt.onclick	= function() { Calendar.setInnerHTML(nxt_m,nxt_m_y); };
		
		for(i = 0; i < 6; i++) {
			for(j = 0; j < 7; j++) {
				//window.alert(i+'/5 '+j+'/6\n'+dsp_y+'-'+dsp_m+'-'+dsp_d);
				if(dsp_m == this.date.sld.getMonth() && dsp_d == this.date.sld.getDate() && dsp_y == this.date.sld.getFullYear()) {
					css_class = "cur_d";
				}
				else if(dsp_m == m) {
					css_class = "cur_m";
				}
				else {
					css_class = "oth_m";
				}
				
				this.HTML.cal.tbd.rows[i].cells[j].innerHTML = dsp_d;
				this.HTML.cal.tbd.rows[i].cells[j].setAttribute('d',dsp_d);
				this.HTML.cal.tbd.rows[i].cells[j].setAttribute('m',dsp_m);
				this.HTML.cal.tbd.rows[i].cells[j].setAttribute('y',dsp_y);
				this.HTML.cal.tbd.rows[i].cells[j].setAttribute('title','');
				this.HTML.cal.tbd.rows[i].cells[j].className = this.css_pfx + css_class;
				this.HTML.cal.tbd.rows[i].cells[j].onclick = function() {
					Calendar.setCurDate(this.getAttribute('y'),this.getAttribute('m'),this.getAttribute('d'));
					Calendar.fct.upd(this.getAttribute('y'),this.getAttribute('m'),this.getAttribute('d'));
					Calendar.hide();
				};
				
				ofr = null;
				spc = null;
				date_cur = new Date(dsp_y,dsp_m,dsp_d,0,0,0);
				
				if(ofr = this.fct.ofr(date_cur)) {
					//window.alert(date_cur+': '+this.fct.cpr(ofr[2]))
					//document.getElementById('test').innerHTML += '<p>spc: '+spc+'</p>';
					spc = (this.fct.cpr(ofr[2]/10)) ? true : false;
					this.HTML.cal.tbd.rows[i].cells[j].setAttribute('title',ofr[3]);
					this.HTML.cal.tbd.rows[i].cells[j].className += (spc) ? ' spc' : ' dsb';
					this.HTML.cal.tbd.rows[i].cells[j].className += (spc && parseFloat(ofr[2])<0) ? ' promo' : '';
					if(!spc) {
						this.HTML.cal.tbd.rows[i].cells[j].onclick = null;
					}
				}
				
				if(spc == null && this.date.dsbWeekDays[j+1]) {
					this.HTML.cal.tbd.rows[i].cells[j].className += ' dsb';
					this.HTML.cal.tbd.rows[i].cells[j].setAttribute('title',((this.date.dsbWeekDays[j+1]) ? TLT.day.name[this.lang][j+1]+' - '+ TLT.closed[this.lang] : ''));
					this.HTML.cal.tbd.rows[i].cells[j].onclick = null;
				}
				dsp_d++;
				if(dsp_d > this.date.daysPerMonth[dsp_m]) { dsp_d=1; dsp_m++; }
				if(dsp_m > 11) { dsp_m=0; dsp_y++; }
				
			}
		}
		
		var current_weekday = this.date.now.getDay() - this.weekStartDay;
		if(current_weekday < 0) { current_weekday += 7; }
		
		if(this.date.dsbWeekDays[current_weekday+1]) {
			addClassName(this.HTML.cal.tdy,'dsb');
			this.HTML.cal.tdy.onclick = null;
		}
		else {
			removeClassName(this.HTML.cal.tdy,'dsb');
			this.HTML.cal.tdy.onclick = function() { Calendar.fct.upd(Calendar.date.now.getFullYear(),Calendar.date.now.getMonth(),Calendar.date.now.getDate()); Calendar.setInnerHTML(); };
		}
	},
	
	setLang: function(lang) {
		switch(lang) {
			case 'gb':
			case 'en':
			case 'eng':
			case 'en-EN':
			case 'english':
			this.lang = 'eng';
			break;
			
			case 'it':
			case 'it-IT':
			case 'ita':
			case 'italian':
			this.lang = 'ita';
			break;
			
			case 'de':
			case 'deu':
			case 'ger':
			case 'german':
			this.lang = 'ger';
			break;
			
			case 'es':
			case 'es-ES':
			case 'spa':
			case 'spanish':
			this.lang = 'spa';
			break;
			
			case 'fr':
			case 'fre':
			case 'fr-FR':
			case 'french':
			default:
			this.lang = 'fre';
			break;
		}
		return true;
	},
	
	hide: function() {
		if(this.dbg) { window.alert('Calendar.hide()'); }
		(undefined != this.HTML.cal) ? this.HTML.cal.style.display = 'none' : false;
	},
	
	show: function() {
		if(this.dbg) { window.alert('Calendar.show()'); }
		if(undefined != this.HTML.cal) {
			this.setPos();
			this.setInnerHTML();
			this.HTML.cal.style.display = 'block';
		}
	}
}
