/**
 * @author nicola
 */
if(!ch){
	var ch = {};
}
if(!ch.exmachina){
	ch.exmachina = {};
}
if(!ch.exmachina.bravofly){
	ch.exmachina.bravofly = {};
}
ch.exmachina.bravofly.Calendar = function(){

};

ch.exmachina.bravofly.Calendar.prototype.messages = {};
ch.exmachina.bravofly.Calendar.prototype._monthMap = [];
ch.exmachina.bravofly.Calendar.prototype._rowTemplate = "<tr>"
	+ "<td fieldRef='{$Ref}' id='{$CellId}' valueon='{$v}' class='notAvailable'>{$i}</td>"
	+ "<td fieldRef='{$Ref}' id='{$CellId}' valueon='{$v}' class='notAvailable'>{$i}</td>"
	+ "<td fieldRef='{$Ref}' id='{$CellId}' valueon='{$v}' class='notAvailable'>{$i}</td>"
	+ "<td fieldRef='{$Ref}' id='{$CellId}' valueon='{$v}' class='notAvailable'>{$i}</td>"
	+ "</tr>"
;
ch.exmachina.bravofly.Calendar.prototype._template = "<table class='ta_calendar' id=\"{$CalendarId}\"><tbody>{$CalendarBody}"
    + "</tbody></table>";
ch.exmachina.bravofly.Calendar.prototype.monthsNodes = [];
ch.exmachina.bravofly.Calendar.prototype.domNode = null;
ch.exmachina.bravofly.Calendar.prototype.pNode = null;
ch.exmachina.bravofly.Calendar.prototype.setMessages = function(/* Array */ msgs){
	this.messages = Object.clone(msgs);
};
ch.exmachina.bravofly.Calendar.prototype._monthMap = [];
ch.exmachina.bravofly.Calendar.prototype.init = function(/*Object*/ initArgs){
	;(function(){
		var ts = (new Date()).getTime(),
			template = this._template.replace(/{\$CalendarId}/, "CalendarId" + ts),
			rows = "",
			rowTemplate = "",
			cellNode = null
		;
		this.pNode = $(initArgs.node);
		this.domNode = document.createElement("div");
		this._monthMap = initArgs.monthMap;
		for(var i = 0; i < 3; i++){
			rowTemplate = this._rowTemplate;
			for(var j = 0; j < 4; j++){
				rowTemplate = rowTemplate
					.replace(/{\$i}/, this._monthMap[j + 4*i].month)
					.replace(/{\$v}/, this._monthMap[j + 4*i].date)
					.replace(/{\$CellId}/, "Cell" + (j + 4*i))
					.replace(/{\$Ref}/, "departureMonths" + (j + 4*i))
				;
			}
			rows += rowTemplate;
		}
		template = template.replace(/{\$CalendarBody}/, rows);
		Element.update(this.domNode, template);
		this.pNode.appendChild(this.domNode);
		for(i = 0; i < 12; i++){
			cellNode = $("Cell" + i);
			this.monthsNodes.push(cellNode);
			cellNode.writeAttribute("id", null);
		}
		this.domNode.observe('click', this.onClickHandler.bindAsEventListener(this));
	}).call(this);
};
ch.exmachina.bravofly.Calendar.prototype.getMonthNode = function(/*String*/ ym){
	var el,
		nodes = this.monthsNodes
	;
	for(var i = 0; el = nodes[i++];){
		if(el.readAttribute("valueon").replace(/\//g, "").substring(0, 6) == ym){
			return el;
		}
	}
	return null;
};
ch.exmachina.bravofly.Calendar.prototype.disable = function(){
	this.disabled = true;
	this.disableAllMonths();
};
ch.exmachina.bravofly.Calendar.prototype.enable = function(){
	this.disabled = false;
};

ch.exmachina.bravofly.Calendar.prototype.onClickHandler = function(/* Event */ evt){
	if(this.disabled){
		return;
	}
	var trg = evt.target,
		self = ch.exmachina.bravofly.pricesearch
	;
	if(trg.tagName.toLowerCase() === "td" && trg.hasClassName("available")){
		if(!trg.hasClassName("selected")){
			this.selectMonth({ elem: trg });
		}else{
			this.unselectMonth({ elem: trg });
		}
	}
};
ch.exmachina.bravofly.Calendar.prototype.selectAllMonths = function(){
	for(var i = 0, elem; elem = this.monthsNodes[i++];){
		elem.hasClassName("available") && (elem.title = this.messages.MONTHS_ON) && (elem.addClassName("selected"));
	}
	return this;
};
ch.exmachina.bravofly.Calendar.prototype.unselectAllMonths = function(){
	for(var i = 0, elem; elem = this.monthsNodes[i++];){
		elem.hasClassName("available") && (elem.title = this.messages.MONTHS_OFF) && (elem.removeClassName("selected"));
	}
	return this;
};
ch.exmachina.bravofly.Calendar.prototype.selectMonth = function(/** Object */ args){
	var index = args.index,
		date = args.date,
		elem = date ? this.getMonthNode(date) : isNaN(index) ? args.elem : this.monthsNodes[index]
	;
	if(!elem || elem.hasClassName("selected")){
		return;
	}
	elem.addClassName("selected");
	elem.title = this.messages.MONTHS_ON;

};
ch.exmachina.bravofly.Calendar.prototype.unselectMonth = function(/** Object */ args){
	var index = args.index,
		date = args.date,
		elem = date ? this.getMonthNode(date) : isNaN(index) ? args.elem : this.monthsNodes[index];
	;
	if(!elem.hasClassName("selected")){
		return;
	}
	elem.title = this.messages.MONTHS_OFF;
	elem.removeClassName("selected");
};
ch.exmachina.bravofly.Calendar.prototype.getSelectedMonths = function(){
	var ret =  [];
	for(var i = 0; i < 12; i++){
		this.monthsNodes[i].hasClassName("selected") && ret.push(this.monthsNodes[i]);
	}
	return ret;
};
ch.exmachina.bravofly.Calendar.prototype.disableAllMonths = function(){
	for(var i = 0; i < 12; i++){
		this.monthsNodes[i].className = "notAvailable";
		this.monthsNodes[i].title = this.messages.NO_MONTHS;
	}
	return this;
};
ch.exmachina.bravofly.Calendar.prototype.enableAllMonths = function(){
	for(var i = 0; i < 12; i++){
		this.monthsNodes[i].className = "available";
		this.monthsNodes[i].title = this.messages.MONTHS_OFF;
	}
	return this;
};
ch.exmachina.bravofly.Calendar.prototype.enableMonths = function(/*Array*/ months){
	for(var i = 0, l = months.length; i < l; i++){
		this.enableMonth(months[i]);
	}
	return this;
};
ch.exmachina.bravofly.Calendar.prototype.enableMonth = function(/* String */ month){
	var node = this.getMonthNode(month);
	node.className = "available";
};
ch.exmachina.bravofly.Calendar.prototype.toString = function(){
	return "[Calendar singleton]";
}