//	********************************************************************************************************
//		CHandlerBFF.js
//	********************************************************************************************************
//
//		CREATED FROM:	Amin Hatteh (idaSys)
//		CREATED AT:	02.10.2008
//		
//		UPDATES:
//
//	--------------------------------------------------------------------------------------------------------


//class CBookingHandler
//{
//public:
	function CHandlerBFF(preloadDest, preloadDep)
	{
		//public attributes
		this.getMonthName = declared_getMonthName;
		this.updateDOMFlights = declared_updateDOMFlights;
		this.updateDomData = declared_updateDomData;
		this.updateDomMonth = declared_updateDomMonth;
		this.toggleViewModeFlights = declared_toggleViewModeFlights;
		this.toggleViewModeMonth = declared_toggleViewModeMonth;
		this.showFlightAddon = declared_showFlightAddon;
		this.setAllAirberlin = declared_setAllAirberlin;
		this.closeFlightAddon = declared_closeFlightAddon;
		
		this.openLinkFlug = declared_openLinkFlug;
		this.openLinkFlugHotel = declared_openLinkFlugHotel;
		this.setEasyMode = declared_setEasyMode;
		//this.setDuration = declared_setDuration;
		//this.duration;
		
		this.preloadDest = preloadDest;
		this.preloadDep = preloadDep;
		this.countDates;
		this.curHeight = 427;
		
		this.showAllFlights = false;
		this.showAllAirberlin = true;
		this.dataAllFlights;
		
		this.showAllMonths = false;
		this.dataAllMonth;
		
		
		this.easyMode = false;
		this.objectStr = null;
		this.from = null;
		this.to = null;
		this.fromDate = null;
		this.toDate = null;
		this.reference = null;
		this.duration = null;
		
	}
	
	function declared_setEasyMode(mode)
	{
		this.easyMode = mode;
	}
	
	function declared_setAllAirberlin(allAirberlin)
	{
		this.showAllAirberlin = allAirberlin;
	}

	
	/**
	*
	**/
	function declared_openLinkFlug(from, to, fromDate, reference)
	{
		try
		{	
			var duration = document.getElementById("duration");
			duration = duration.options[duration.selectedIndex].value;
			
			datum = new Date(parseInt(fromDate.substring(4, 6), 10), 
							(parseInt(fromDate.substring(2, 4), 10) - 1), 
							(parseInt(fromDate.substring(0, 2), 10)));	

			datum = new Date(datum.getTime() + (60 * 60 * 24 * parseInt(duration, 10) * 1000));
			
			tag = datum.getDate();
			if (tag <= 9)
				tag = "0" + String (tag);
			else
				tag = String (tag);
			monat = (datum.getMonth() + 1);
			if (monat <= 9)
				monat = "0" + String (monat);
			else
				monat = String (monat);
			
			jahr = String (datum.getYear());
			if (jahr <= 9)
				jahr = "0" + String (jahr);
			else
				jahr = String (jahr);
			toDate = tag + monat + jahr;
			
			window.open("http://www.airberlin.com/site/abvakanz_c.php?LANG=deu&VON=" + from + "&NACH=" + to + "&HINDATUM=" + fromDate + "&RUECKDATUM=" + toDate, "airberlin");	
		}
		catch(e){
			//alert(e.message);
		}	
	}
	
	/**
	*
	**/
	function declared_openLinkFlugHotel(from, to, fromDate, reference)	{
		try
		{	
			var duration = document.getElementById("duration");
			duration = duration.options[duration.selectedIndex].value;

			datum = new Date(parseInt(fromDate.substring(4, 6), 10), 
							(parseInt(fromDate.substring(2, 4), 10) - 1), 
							(parseInt(fromDate.substring(0, 2), 10)));	

			datum = new Date(datum.getTime() + (60 * 60 * 24 * parseInt(duration, 10) * 1000));

			tag = datum.getDate();
			if (tag <= 9)
				tag = "0" + String (tag);
			else
				tag = String (tag);
			monat = (datum.getMonth() +1);
			if (monat <= 9)
				monat = "0" + String (monat);
			else
				monat = String (monat);
			
			jahr = String (datum.getYear());
			if (jahr <= 9)
				jahr = "0" + String (jahr);
			else
				jahr = String (jahr);
			toDate = tag + monat + jahr;
			
			window.open("dyn_step_1_flugauswahl.php?da=" + from + "&aa=" + to + "&outboundDepartureDate=" + fromDate + "&returnDepartureDate=" + toDate + "&numAdults=2&numChilds=0&reference=" + reference +"&fromPage=price_finder", "BINOLI");	
		}
		catch(e){
			//alert(e.message);
		}	
	}
		
	function declared_getMonthName(month)
	{
		switch (month)
		{
			case 1: return translationsCHandlerBFFForm["Januar"];break;
			case 2: return translationsCHandlerBFFForm["Februar"];break;
			case 3: return translationsCHandlerBFFForm["Maerz"];break;
			case 4: return translationsCHandlerBFFForm["April"];break;
			case 5: return translationsCHandlerBFFForm["Mai"];break;
			case 6: return translationsCHandlerBFFForm["Juni"];break;
			case 7: return translationsCHandlerBFFForm["Juli"];break;
			case 8: return translationsCHandlerBFFForm["August"];break;
			case 9: return translationsCHandlerBFFForm["September"];break;
			case 10: return translationsCHandlerBFFForm["Oktober"];break;
			case 11: return translationsCHandlerBFFForm["November"];break;
			case 12: return translationsCHandlerBFFForm["Dezember"];break;
		}
	}

	function declared_closeFlightAddon(reset){
		try	{
			var object = document.getElementById(this.objectStr);
			object.style.backgroundColor = "#F3F3F3";
			object.lastChild.style.backgroundImage = "url(_images/bff/bff_lastblock_price.gif)";
			object.onclick = new Function("objHandlerBFF.showFlightAddon('" + this.objectStr + "', '" + this.from + "', '" + this.to + "', '" + this.fromDate + "', '" + this.toDate + "', '" + this.reference + "', '" + this.duration	+ "');");

			object = document.getElementById(this.objectStr + "_duration");
			object.parentNode.removeChild(object);

			object = document.getElementById(this.objectStr + "_link1");
			object.parentNode.removeChild(object);

			object = document.getElementById(this.objectStr + "_link2");
			object.parentNode.removeChild(object);
			
			if(reset){
				this.objectStr = null;
				this.from = null;
				this.to = null;
				this.fromDate = null;
				this.toDate = null;
				this.reference = null;
				this.duration = null;
			}	
		}
		catch(e){
			//alert(e.message);
		}		
	}

	function declared_showFlightAddon(objectStr, from, to, fromDate, toDate, reference, duration)
	{
		try
		{
			if(this.objectStr)
				this.closeFlightAddon(false);
			
			this.objectStr = objectStr;
			this.from = from;
			this.to = to;
			this.fromDate = fromDate;
			this.toDate = toDate;
			this.reference = reference;
			this.duration = duration;
			
			var liNode;
			var selectNode;
			var otionNode;
			var divNode;
			var aNode;
			var object = document.getElementById(objectStr);
			object.style.backgroundColor = "#FFFFFF";
			object.style.border = "0px";
			
			object.onclick = new Function("objHandlerBFF.closeFlightAddon('true');");
			object.lastChild.style.backgroundImage = "url(_images/bff/bff_close.gif)";
			
			//Flug Hotel buchen
			liNode = document.createElement("li");
			liNode.setAttribute("id", objectStr + "_link1");
			liNode.style.backgroundColor = "#FFFFFF";
			liNode.style.textAlign = "right";
			liNode.style.backgroundRepeat = "repeat-y";
			liNode.style.paddingTop = "5px";
			liNode.style.lineHeight = "16px";
			
			divNode = document.createElement("div");
			divNode.style.backgroundImage = "url(_images/bff/bff_link.gif)";
			divNode.style.backgroundPosition = "100% 0px";
			divNode.style.padding = "0 28px 0 0";
			
			
			aNode = document.createElement('a');
			aNode.setAttribute("href", "javascript:objHandlerBFF.openLinkFlug('" + this.from + "', '" + this.to + "', '" + this.fromDate + "', '" + this.reference + "');");
			aNode.style.fontWeight = "bold";
			aNode.style.fontFamily = "Verdana,Geneva,Arial,Helvetica,sans-serif";
			
			aNode.innerHTML = "nur Flug buchen";
			divNode.appendChild(aNode);
			liNode.appendChild(divNode);
			object.parentNode.insertBefore(liNode, object.nextSibling);
			
			//Flug buchen
			liNode = document.createElement("li");
			liNode.setAttribute("id", objectStr + "_link2");
			liNode.style.backgroundColor = "#FFFFFF";
			liNode.style.textAlign = "right";
			liNode.style.backgroundRepeat = "repeat-y";
			liNode.style.paddingTop = "5px";
			liNode.style.lineHeight = "16px";
			
			divNode = document.createElement("div");
			divNode.style.backgroundImage = "url(_images/bff/bff_link.gif)";
			divNode.style.backgroundPosition = "100% 0px";
			divNode.style.padding = "0 28px 0 0";
			
			aNode = document.createElement('a');
			aNode.setAttribute("href", "javascript:objHandlerBFF.openLinkFlugHotel('" + this.from + "', '" + this.to + "', '" + this.fromDate + "', '" + this.reference + "');");
			aNode.style.fontWeight = "bold";
			aNode.style.fontFamily = "Verdana,Geneva,Arial,Helvetica,sans-serif";			
			aNode.innerHTML = "Flug & Hotel buchen";
			
			divNode.appendChild(aNode);
			liNode.appendChild(divNode);
			object.parentNode.insertBefore(liNode, object.nextSibling);
					
			//Reisedauer
			liNode = document.createElement("li");
			liNode.setAttribute("id", objectStr + "_duration");
			liNode.style.backgroundColor = "#FFFFFF";
			liNode.style.textAlign = "right";
			liNode.style.border = "0px";
			liNode.style.backgroundRepeat = "repeat-y";
			liNode.style.paddingTop = "5px";
			
			divNode = document.createElement("div");
			divNode.style.fontWeight = "bold";
			divNode.style.paddingRight = "4px";
			divNode.style.fontFamily = "Verdana,Geneva,Arial,Helvetica,sans-serif";			
			divNode.innerHTML = "Reisedauer&nbsp;";
			
			selectNode = document.createElement("SELECT")
			selectNode.style.width = "100px";
			selectNode.setAttribute("id", "duration");
	
			for(var i = 1; i <= 20; i++)
			{
				otionNode = document.createElement("option")
				otionNode.innerHTML = (i == 1) ? " 1 Tag":" " + i + " Tage";
				otionNode.value = i;
				
				if(i == duration)
					otionNode.selected = true;
					
				selectNode.appendChild(otionNode);
			} 
			
			divNode.appendChild(selectNode);
			liNode.appendChild(divNode);
			object.parentNode.insertBefore(liNode, object.nextSibling);
		}
		catch(e)
		{
			//alert(e.message);
		}	
		
	}
	
	function declared_toggleViewModeFlights(showAll)
	{
		try
		{
			var bestPrice = this.dataAllFlights.bestPrice;
			var dep = this.dataAllFlights.dep;
			var dest = this.dataAllFlights.dest;
			var count = this.dataAllFlights.count;
			var data = this.dataAllFlights.data;
			var element = this.dataAllFlights.element;
			var month = this.dataAllFlights.month;
			var year = this.dataAllFlights.year;
				
			var liNode;
			var spanNode;
			var divNode;
			var curElement = document.getElementById(element);
			var parentElement = curElement.parentNode;
			curElement.innerHTML = "";
	
			var showMoreFlights = false;	
			var hasLessFlights = false;
			var countFlights = 0;
			
			for(var i = 0; i < count; i++)
			{
				var curData = data[i];
				if((curData.cheapestprice == 1) || this.showAllFlights || showAll || this.showAllAirberlin)
				{
					liNode = document.createElement("li");
					
					var AttributeString = dest + "_" + dep + "_" + curData.from + "_" + curData.to + "_" + bestPrice;
					
					if(this.easyMode){
						// Palmaspecial
						if (dest == "1DAY" || dest == "2DAY" || dest == "3DAY" || dest == "4DAY" || dest == "5DAY" || dest == "6DAY" || dest == "7DAY" || dest == "8DAY" || dest == "9DAY" || dest == "10DAY" || dest == "11DAY" || dest == "12DAY" || dest == "13DAY" || dest == "14DAY" || dest == "15DAY" || dest == "16DAY" || dest == "17DAY" || dest == "18DAY" || dest == "19DAY" || dest == "20DAY" || dest == "21DAY")
					{
					destPMI = "PMI";
					liNode.onclick = new Function("openFlightlistSpecial('" + dep + "', '" + destPMI + "', '" + curData.from + "', '" + curData.to + "', '" + AttributeString + "');");
					}
					else				
						liNode.onclick = new Function("openFlightlist('" + dep + "', '" + dest + "', '" + curData.from + "', '" + curData.to + "', '" + AttributeString + "');");
					}
					else{
						idStr = "flightAddon" + i;
						liNode.setAttribute("id", idStr);
						liNode.onclick = new Function("objHandlerBFF.showFlightAddon('" + idStr + "','" + dep + "', '" + dest + "', '" + curData.from + "', '" + curData.to + "', '" + AttributeString + "', '" + curData.duration + "');");
					}
					if(BFF.offset_fix_ie == 0){
							spanNode = document.createElement("span");
							spanNode.className = "bff_day";
							spanNode.innerHTML = curData.week + ",";
							liNode.appendChild(spanNode);
							
							spanNode = document.createElement("span");
							spanNode.className = "bff_day";
							spanNode.innerHTML = curData.date;
							spanNode.setAttribute("style", "position:absolute; top:0px;left:25px;");
						}
						else{
							spanNode = document.createElement("span");
							spanNode.className = "bff_day";
							spanNode.innerHTML = curData.week + ", " + curData.date;
						}	
									
					liNode.appendChild(spanNode);
					
					divNode = document.createElement("div");
					var price = curData.price;
					if(price == bestPrice)
						divNode.className = "bff_price bff_lowest";
					else
						divNode.className = "bff_price";
						
					if(curData.operator == "AB")
						divNode.innerHTML = price + " &euro;";
					else
						if(price <= bestPrice)
								divNode.innerHTML = "<img src='_images/bff/special.gif' style='position: absolute; left:-45px; top: 0px;'>" + price + " &euro;";
							else
								divNode.innerHTML = "<img src='_images/bff/special_black.gif' style='position: absolute; left:-45px; top: 0px;'>" + price + " &euro;";
				
					liNode.appendChild(divNode);
					
					curElement.appendChild(liNode);
					countFlights++;
				}			
				
				if(curData.cheapestprice == 1)
					hasMoreFlights = true;		
				else 
					hasLessFlights = true;
			}
	
			//Auslaufen lassen
			var newHeight = eval((this.countDates * 39 ) + (countFlights * 23) - (BFF.offset_fix_ie * countFlights)) + 50;
			if(newHeight < 437)
				newHeight = 427;
			
			if(!this.showAllAirberlin && (hasLessFlights && hasMoreFlights))	
			{
				// Anzeige "Alle Flüge anzeigen"
				liNode = document.createElement("li");
				liNode.className = "allFlights";
				spanNode = document.createElement("span");
				spanNode.className = "allFlights";
				var monthName = this.getMonthName(parseInt(month, 10));
				if(showAll)
				{
					liNode.onclick = new Function("objHandlerBFF.toggleViewModeFlights(false);");
					spanNode.innerHTML = translationsCHandlerBFFForm["nur billigste Fluege anzeigen"]+"<img src='_images/bff/arrowUp.gif' style='position:relative; left:3px; top:2px;'>";
				}
				else 
				{
					liNode.onclick = new Function("objHandlerBFF.toggleViewModeFlights(true);");		
					spanNode.innerHTML = translationsCHandlerBFFForm["alle Fluege im"] + monthName + " <img src='_images/bff/arrowDown.gif' style='position:relative; left:3px; top:2px;'>";
				}
				newHeight += 27;
				liNode.appendChild(spanNode);
				curElement.appendChild(liNode);
			}		
			
			$('scroller3').style.height = newHeight + "px";
			
			curElement.setAttribute("style", "display:block");
			parentElement.setAttribute("style", "display:block");
			BFF.loadStep(4);
		}
		catch(e) {
			//alert(e.message);
		}
		
	}
	
	
	function declared_updateDOMFlights(object)
	{
		this.dataAllFlights = object.evalJSON();
		objHandlerBFF.toggleViewModeFlights(false);
	}

	function declared_updateDomData(object)
	{
		try
		{
			var jsonObject = object.evalJSON();
			var bestPrice = jsonObject.bestPrice;
			var count = jsonObject.count;			
			var data = jsonObject.data;
			var element = jsonObject.element;
			var dest = jsonObject.dest;
			
			var liNode;
			var inputNode;
			var spanNode;
			var divNode;
			var curElement = document.getElementById(element);
			curElement.innerHTML = "";
			
			for(var i = 0; i < count; i++){
				var idStr = "";
				var curData = data[i];
				liNode = document.createElement("li");

				inputNode = document.createElement("input");
				if(element == "destinationBlock")
					inputNode.name = "option1";					
				else if(element == "departureBlock")
					inputNode.name = "option2";
				
				inputNode.type = "radio";
				
				if(element == "destinationBlock"){
					idStr = 'DEST_' + curData.dest;
					inputNode.setAttribute("value", curData.dest);
					if(this.preloadDest == curData.dest){
						inputNode.setAttribute("checked", "checked")
						this.preloadDest = "";
					}
				}
				else if(element == "departureBlock"){
					idStr = 'DEP_' + curData.dep;
					inputNode.setAttribute("value", dest+"_"+curData.dep);		
					if(this.preloadDep == curData.dep){
						inputNode.setAttribute("checked", "checked")
						this.preloadDep = "";
					}
				}	
				liNode.setAttribute("id", idStr);
				liNode.appendChild(inputNode);
				
				spanNode = document.createElement("span");
				spanNode.className = "bff_country";
				spanNode.innerHTML = curData.name;
				liNode.appendChild(spanNode);
							
				divNode = document.createElement("div");
				var price = curData.price;
				if(price <= bestPrice)
					divNode.className = "bff_price bff_lowest";
				else
					divNode.className = "bff_price";
					
				if(curData.operator == "AB")	
					divNode.innerHTML = translationsCHandlerBFFForm["ab"] + " " + price + " &euro;";
				else 
					if(price <= bestPrice)
						divNode.innerHTML = "<img src='_images/bff/special.gif'/>" + price + " &euro;";	
					else
						divNode.innerHTML = "<img src='_images/bff/special_black.gif'/>" + price + " &euro;";
					
				liNode.appendChild(divNode);
				
				curElement.appendChild(liNode);
			}

			if(element == "destinationBlock")
				BFF.initStep(1);
			else if(element == "departureBlock")
				BFF.initStep(2);
		}
		catch(e) { 
			//alert(e.message);
		}	
	}
	
	
	function declared_updateDomMonth(object)
	{
		this.dataAllMonth = object.evalJSON();
		objHandlerBFF.toggleViewModeMonth(false);
	}
	
	function declared_toggleViewModeMonth(showAll)
	{
		try
		{		
			var bestPrice = this.dataAllMonth.bestPrice;
			var count = this.dataAllMonth.count;

			var data = this.dataAllMonth.data;
			var element = this.dataAllMonth.element;
			var dest = this.dataAllMonth.dest;
			var dep = this.dataAllMonth.dep;
			
			var liNode;
			var liSubNode;
			var inputNode;
			var spanNode;
			var divNode;
			var ulNode;
			var curElement = document.getElementById(element);
			curElement.innerHTML ="";
			//var parentElement = curElement.parentNode;

			this.showAllFlights = showAll;
			
			var showMoreMonth = false;
			var countMonth = 0;
			
			for(var i = 0; i < count; i++)
			{
				var curData = data[i];

				if(showAll || this.showAllAirberlin || curData.cheapestprice)
				{
					liNode = document.createElement("li");
					liNode.className = "closed"; 
					liNode.id = dest + "_" + dep + "_" + curData.month +"_"+ curData.year +"_"+ bestPrice;
	
					spanNode = document.createElement("span");
					spanNode.className ="bff_month";
					spanNode.innerHTML = this.getMonthName(curData.month) + " " + curData.year;
					liNode.appendChild(spanNode);
	
					divNode = document.createElement("div");
					divNode.className = "bff_month_price"; 
					divNode.innerHTML = translationsCHandlerBFFForm["ab"] + " " + curData.price + " &euro;";			
					liNode.appendChild(divNode);
	
					divNode = document.createElement("div");
					divNode.innerHTML = "&nbsp;";
					divNode.className = "bff_month_follower";
	
					spanNode = document.createElement("span");
					spanNode.className ="bff_zb";
					divNode.appendChild(spanNode);
	
					spanNode = document.createElement("span");
					spanNode.className ="bff_day";
					divNode.appendChild(spanNode);
	
					spanNode = document.createElement("span");
					spanNode.className ="bff_time";
					divNode.appendChild(spanNode);
					liNode.appendChild(divNode);
	
					ulNode = document.createElement("ul");
					ulNode.id = "flight" + curData.year + curData.month;
						
					liSubNode = document.createElement("li");
					liSubNode.innerHTML = "<img src='img/waitani.gif'/>";
					ulNode.appendChild(liSubNode);
					liNode.appendChild(ulNode);	
	
					curElement.appendChild(liNode);
					countMonth++;
				}
				
				if(curData.cheapestprice == 1)
					showMoreMonth = true;
			}
		
			
			if(countMonth == 0)
			{
				objHandlerBFF.toggleViewModeMonth(true);
				return;
			}
			
			if (showMoreMonth && !this.showAllAirberlin)
			{
				// Anzeige "Alle Monate anzeigen"
				liNode = document.createElement("li");
				liNode.className = "allMonths";
				
				
				spanNode = document.createElement("span");
				spanNode.className = "allMonths";
				
				if(showAll && !this.showAllAirberlin)
				{
					liNode.onclick = new Function("objHandlerBFF.toggleViewModeMonth(false);");	
					spanNode.innerHTML = translationsCHandlerBFFForm["nur billigste Monate anzeigen"] + "<img src='_images/bff/arrowUp.gif' style='position:relative; left:3px; top:2px;'>";
				}
				else
				{
					liNode.onclick = new Function("objHandlerBFF.toggleViewModeMonth(true);");	
					spanNode.innerHTML = translationsCHandlerBFFForm["alle Monate anzeigen"]+ "<img src='_images/bff/arrowDown.gif' style='position:relative; left:3px; top:2px;'>";
				}
				liNode.appendChild(spanNode);
				curElement.appendChild(liNode);
			}

			
			//Auslaufen lassen
			var newHeight = eval(countMonth * 39);
			if (showAll && !this.showAllAirberlin)
				newHeight += 27;
				
			if(newHeight < 437)
				newHeight = 427;

			this.curHeight = newHeight;		
			$('scroller3').style.height = newHeight + "px";

			this.countDates = countMonth;
			BFF.initStep(3);
		}
		catch(e) {
			//alert(e.message);
		}	

	}
//}