/*

	Search.init, by MM-one.com
	Versione 3.1
	
	New: opzione fixes -> default: true, sovrascrive nel prototipo del calendario la funzione per il posizionamento, con un
	workaround per IE7.
	
	Bugfix: errore che impediva il caricamento della pagina, risolto il problema creando ogni volta un calendario diverso
	
	Per effettuare il setup di Search.init inserire un oggetto contenente le varie opzioni come argomento della funzione
	OPZIONI VALIDE:
	from -> contiene l'id dell'elemento input con la data iniziale
	to -> contiene l'id dell'elemento input con la data finale
	trigger_from -> contiene l'id del secondo trigger per la data iniziale
	trigger_to -> contiene l'id del secondo trigger per la data finale
	override -> se true cambia il prototipo di Calendar inserendo un "workaround" per IE7
	cursor -> cambia il cursore degli elementi from,to,trigger_from,trigger_to. se impostato a null lascia quelli
			di default del browser.
*/

Number.prototype._0 = function(zeros){
	zeros = zeros || '0';
	return (this<10?zeros:'') + this;
};

Search = {
	options:{
		from:"bform[checkin]",
		to:"bform[checkout]",
		trigger_from:"f_trigger_a",
		trigger_to:"f_trigger_b",
		override:false,
		fixes:true,
		cursor:'pointer'
	},
	
	theFirstDate:null,
	theSecondDate:null,
	
	calendarSetup:function(){
		var cal_from = Calendar.setup({
			inputField     :    Search.options.from,           //*
			ifFormat       :    "%d-%m-%Y",
			showsTime      :    false,
			button         :    Search.options.trigger_from,
			weekNumbers	   :	false,
			step           :    1,
			firstDay       :    1,
			dateStatusFunc :	Search.noOldDays,
			onUpdate	   :	Search.upDate,
			electric       :    false,
			onClose		   :	Search.destroy
		});
		var cal_to = Calendar.setup({
			inputField     :    Search.options.to,           //*
			ifFormat       :    "%d-%m-%Y",
			showsTime      :    false,
			button         :    Search.options.trigger_to, 
			weekNumbers	   :	false,
			step           :    1,
			firstDay       :    1,
			dateStatusFunc :	Search.noOldDaysTheSecond,
			onUpdate       :    Search.upDateTheSecond,
			electric       :    false,
			onClose		   :	Search.destroy
		});
		var cal_from2 = Calendar.setup({
			inputField     :    Search.options.from,           //*
			ifFormat       :    "%d-%m-%Y",
			showsTime      :    false,
			weekNumbers	   :	false,
			step           :    1,
			firstDay       :    1,
			dateStatusFunc :	Search.noOldDays,
			onUpdate	   :	Search.upDate,
			electric       :    false,
			onClose		   :	Search.destroy
		});
		var cal_to2 = Calendar.setup({
			inputField     :    Search.options.to,           //*
			ifFormat       :    "%d-%m-%Y",
			showsTime      :    false,
			weekNumbers	   :	false,
			step           :    1,
			firstDay       :    1,
			dateStatusFunc :	Search.noOldDaysTheSecond,
			onUpdate       :    Search.upDateTheSecond,
			electric       :    false,
			onClose		   :	Search.destroy
		});
	},
	init:function(){
		if(arguments.length == 1 && typeof(arguments[0]) == "object"){
			for(var ind in arguments[0]){
				Search.options[ind] = arguments[0][ind];
			}
		}
		
		if(Search.options.override){
			for(o in Overrides){
				Calendar.prototype[o] = Overrides[o];
			}
		}
		if(Search.options.fixes){
			for(o in Fixes){
				Calendar.prototype[o] = Fixes[o];
			}
		}
		Search.setCursor();
		Search.calendarSetup();
		
		try{
			Search.theFirstDate = Search.trySetDate(Search.options.from);
		}catch(e){
			Search.theFirstDate = new Date();
		};
		
		try{
			Search.theSecondDate = Search.trySetDate(Search.options.to);
		}catch(e){
			Search.theSecondDate = new Date();
			Search.theSecondDate.setDate(theSecondDate.getDate()+1);
		};
		
	},
	
	trySetDate:function(elem){
		var kalendar = new Calendar();
		kalendar.create();
		kalendar.parseDate(document.getElementById(elem).value);
		dt = new Date(kalendar.date.getFullYear(),kalendar.date.getMonth(),kalendar.date.getDate());
		kalendar.destroy();
		return dt;
	},
	
	noOldDays:function(date,y,m,d){
		var blockDate = new Date();
		blockDate.setDate(blockDate.getDate()-1);
		if(date<(blockDate)){
			return true;
		}else{
			return false;
		}
	},
	upDate:function(cal){
		Search.theFirstDate = cal.date;
		if(Search.theSecondDate.getTime()<=cal.date.getTime()){
			Search.theSecondDate = cal.date;
			Search.theSecondDate = new Date(Search.theFirstDate.getFullYear(),Search.theFirstDate.getMonth(),Search.theFirstDate.getDate());
			Search.theSecondDate.setDate(Search.theSecondDate.getDate()+1);
			var theString = Search.theSecondDate.getDate()._0()+"-"+(Search.theSecondDate.getMonth()+1)._0()+"-"+Search.theSecondDate.getFullYear();
			document.getElementById(Search.options.to).value = theString;
		}
	},
	upDateTheSecond:function(cal){
		Search.theSecondDate = cal.date;
	},
	noOldDaysTheSecond:function(date,y,m,d){
		
		var fgiorno = Search.theFirstDate.getDate()+1;
		var fmese = Search.theFirstDate.getMonth();
		var fanno = Search.theFirstDate.getFullYear();
		
		var secondBlockDate = new Date(fanno,fmese,fgiorno);
	
		if(date<secondBlockDate){
			
			return true;
		}else{
			return false;
		}
	},
	destroy:function(cal){
		cal.destroy();
	},
	setCursor:function(){
		if(Search.options.cursor != null){
			document.getElementById(Search.options.from).style.cursor = Search.options.cursor;
			document.getElementById(Search.options.to).style.cursor = Search.options.cursor;
			document.getElementById(Search.options.trigger_from).style.cursor = Search.options.cursor;
			document.getElementById(Search.options.trigger_to).style.cursor = Search.options.cursor;
		}
	}
};

var Overrides = {
	showAtElement:function (el, opts) {
	var self = this;
	var p = Calendar.getAbsolutePos(el);
	if (!opts || typeof opts != "string") {
		this.showAt(p.x, p.y + el.offsetHeight);
		return true;
	}
	function fixPosition(box) {
		if (box.x < 0)
			box.x = 0;
		if (box.y < 0)
			box.y = 0;
		var cp = document.createElement("div");
		var s = cp.style;
		s.position = "absolute";
		s.right = s.bottom = s.width = s.height = "0px";
		document.body.appendChild(cp);
		var br = Calendar.getAbsolutePos(cp);
		document.body.removeChild(cp);
		if(document.body && document.body.scrollTop) {
			br.x += document.body.scrollLeft;
		} else if (document.documentElement &&	document.documentElement.scrollTop) {
			br.x += document.documentElement.scrollLeft
		} else {
			br.x += window.scrollX;
		}
		var tmp = box.x + box.width - br.x;
		if (tmp > 0) box.x -= tmp;
	};
	this.element.style.display = "block";
	Calendar.continuation_for_the_fucking_khtml_browser = function() {
		var w = self.element.offsetWidth;
		var h = self.element.offsetHeight;
		self.element.style.display = "none";
		var valign = opts.substr(0, 1);
		var halign = "l";
		if (opts.length > 1) {
			halign = opts.substr(1, 1);
		}
		// vertical alignment
		switch (valign) {
			case "T": p.y -= h; break;
			case "B": p.y += el.offsetHeight; break;
			case "C": p.y += (el.offsetHeight - h) / 2; break;
			case "t": p.y += el.offsetHeight - h; break;
			case "b": break; // already there
		}
		// horizontal alignment
		switch (halign) {
			case "L": p.x -= w; break;
			case "R": p.x += el.offsetWidth; break;
			case "C": p.x += (el.offsetWidth - w) / 2; break;
			case "l": p.x += el.offsetWidth - w; break;
			case "r": break; // already there
		}
		p.width = w;
		p.height = h + 40;
		self.monthsCombo.style.display = "none";
		fixPosition(p);
		self.showAt(p.x, p.y);
	};
	if (Calendar.is_khtml)
		setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
	else
		Calendar.continuation_for_the_fucking_khtml_browser();
	}
};


//Contiene dei bugfix per IE7.
var Fixes = {
	showAtElement:function (el, opts) {
		var self = this;
		var p = Calendar.getAbsolutePos(el);
		if (!opts || typeof opts != "string") {
			this.showAt(p.x, p.y + el.offsetHeight);
			return true;
		}
		function fixPosition(box) {
			if (box.x < 0)
				box.x = 0;
			if (box.y < 0)
				box.y = 0;
			var cp = document.createElement("div");
			var s = cp.style;
			s.position = "absolute";
			s.right = s.bottom = s.width = s.height = "0px";
			document.body.appendChild(cp);
			var br = Calendar.getAbsolutePos(cp);
			document.body.removeChild(cp);
			if (document.documentElement.scrollLeft || document.documentElement.scrollLeft == 0){
				br.x +=	document.documentElement.scrollLeft;
			}else {
				br.x += window.scrollX;
			}
			if (document.documentElement.scrollTop || document.documentElement.scrollTop == 0){
				br.y +=	document.documentElement.scrollTop;
			}else {
				br.y += window.scrollY;
			}
			var tmp = box.x + box.width - br.x;
			if (tmp > 0) box.x -= tmp;
			tmp = box.y + box.height - br.y;
			if (tmp > 0) box.y -= tmp;
		};
		this.element.style.display = "block";
		Calendar.continuation_for_the_fucking_khtml_browser = function() {
			var w = self.element.offsetWidth;
			var h = self.element.offsetHeight;
			self.element.style.display = "none";
			var valign = opts.substr(0, 1);
			var halign = "l";
			if (opts.length > 1) {
				halign = opts.substr(1, 1);
			}
			// vertical alignment
			switch (valign) {
				case "T": p.y -= h; break;
				case "B": p.y += el.offsetHeight; break;
				case "C": p.y += (el.offsetHeight - h) / 2; break;
				case "t": p.y += el.offsetHeight - h; break;
				case "b": break; // already there
			}
			// horizontal alignment
			switch (halign) {
				case "L": p.x -= w; break;
				case "R": p.x += el.offsetWidth; break;
				case "C": p.x += (el.offsetWidth - w) / 2; break;
				case "l": p.x += el.offsetWidth - w; break;
				case "r": break; // already there
			}
			p.width = w;
			p.height = h + 40;
			self.monthsCombo.style.display = "none";
			fixPosition(p);
			self.showAt(p.x, p.y);
		};
		if (Calendar.is_khtml)
			setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
		else
			Calendar.continuation_for_the_fucking_khtml_browser();
	}
};

var append = function(func,callback){
	return function(){
		try{
			func();
		}catch(e){};
		try{
			callback();
		}catch(e){};
	}
};
