var BFF = {

	offset: [],
	offset_fix: Prototype.Browser.Gecko ? 1 : 0, // mozilla-fix: offset der li's wird um 1 pixel falsch berechnet
	offset_fix_ie: Prototype.Browser.IE ? 1 : 0, // ie-fix: offset der li's wird um 1 pixel falsch berechnet
	yContainer: 0,
	max_steps: 3,
	blockactive: 'blockactive',
	li_elements: [],
	li_element_opener: [],
	li_element_opener_follower: [],
	ul_container: [],
	container: [],
	activeStep: [],
	spacer: [],
	headline: [],
	scroller: [],
	interval: [],
	persistentData: [],
	curStep: 1,
	preload: [],
	loadedStep: 0,
	isBrowserIE6: false,
	preloadDestTLC: "",
	preloadDepTLC: "",
	preloadMonthYear: "",
	singularTextStep1: "",
	singularTextStep2: "",
	pluralTextStep1: "",
	pluralTextStep2: "",
	maxVisibleItems: 17,
	itemHeight: 22,
	allAirberlin: 0,
	sortOrder: "alpha",
	direction: "Reiseziel",
	language: "de",
	isHotelspecial: false,

	init: function(event)
	{
		if(Prototype.Browser.IE6=Prototype.Browser.IE &&parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6)
			BFF.isBrowserIE6 = true;
		
			
		BFF.ul_container[1] = $$('#step1 ul');
		BFF.ul_container[2] = $$('#step2 ul');
		BFF.ul_container[3] = $$('#step3 ul');
		BFF.container[1] = $('step1');
		BFF.container[2] = $('step2');
		BFF.container[3] = $('step3');
		BFF.spacer[1] = $('spacer1');
		BFF.spacer[2] = $('spacer2');
		BFF.spacer[3] = $('spacer3');
		BFF.spacer[4] = $('spacer4');
		BFF.headline[1] = $('headline1');
		BFF.headline[2] = $('headline2');
		BFF.headline[3] = $('headline3');
		BFF.scroller[1] = $('scroll1');
		BFF.scroller[2] = $('scroll2');
		BFF.scroller[3] = $('scroll3');

		//mouseWheel
		BFF.bff_container = $$('div.bff_container');
		
		// mozilla		
		Event.observe(BFF.container[1], 'DOMMouseScroll', BFF.mouseWheel1);
		Event.observe(BFF.container[2], 'DOMMouseScroll', BFF.mouseWheel2);

		// IE/Opera
		Event.observe(BFF.container[1], 'mousewheel', BFF.mouseWheel1);
		Event.observe(BFF.container[2], 'mousewheel', BFF.mouseWheel2);
		
		BFF.yContainer = BFF.container[1].cumulativeOffset()[1];
	},
	mouseWheel1: function(event)
	{
		if(BFF.li_element_opener[1])
			BFF.mouseWheel(event,1);
	},
	mouseWheel2: function(event)
	{
		if(BFF.li_element_opener[2])
			BFF.mouseWheel(event,2);
	},
	mouseWheel: function(event,stepID)
	{
		//alert("mouseWheel");
				var delta = 0;
				if (!event) /* For IE. */
					event = window.event;
				if (event.wheelDelta) { /* IE/Opera. */
					delta = event.wheelDelta/120;
					/** In Opera 9, delta differs in sign as compared to IE. */
					if (window.opera)
						delta = -delta;
				} else if (event.detail) { /** Mozilla case. */
					/** In Mozilla, sign of delta is different than in IE.
					* Also, delta is multiple of 3.
					*/
					delta = -event.detail/3;
				}

				/** If delta is nonzero, handle it.
				* Basically, delta is now positive if wheel was scrolled up,
				* and negative, if wheel was scrolled down.
				*/
				if (delta)
					{
						delta *= -40;
						BFF.move(stepID,delta);
					}

				/** Prevent default actions caused by mouse wheel.
				* That might be ugly, but we handle scrolls somehow
				* anyway, so don't bother here..
				*/
				if (event.preventDefault)
					event.preventDefault();

				event.returnValue = false;
	},
	initStep: function(stepID)
	{	
		try
		{
			BFF.li_elements[stepID] = $$('#step' + stepID + ' ul li');
			BFF.li_element_opener[stepID] = $$('#step' + stepID + ' ul li span.bff_month');
			BFF.li_element_opener_follower[stepID] = $$('#step' + stepID + ' ul li div.bff_month_follower');
			
			BFF.scroll(stepID);
			BFF.curStep = stepID;
			
			// eventuell Spacer loeschen
			if (stepID == 1)
			{
				BFF.spacer[4].hide();
				BFF.spacer[3].hide();
				BFF.spacer[2].hide();
				BFF.spacer[1].hide();
			}
			
			if(stepID < 3)
			{		
				if (stepID == 1)
				{
			
					BFF.ul_container[1].invoke('show');
					BFF.ul_container[2].invoke('hide');				
					BFF.ul_container[3].invoke('hide');
					$('scroller3').style.height = "427px";
						
					BFF.headline[1].addClassName('active');
					BFF.headline[3].removeClassName('active');
					BFF.headline[3].removeClassName('active');
					BFF.container[1].addClassName(BFF.blockactive);
					BFF.container[2].removeClassName(BFF.blockactive);	
					BFF.container[3].removeClassName(BFF.blockactive);	
								
				
					try{
						var h = BFF.ul_container[1][0].getHeight();
						var countValues =  parseInt(((h - 407) / 24 ), 10);
						var top = 0;
						BFF.ul_container[1][0].setStyle({ top: top + 'px' });
						BFF.unsetScroll(2);	
						if(countValues > 0){
							$('col1ScrollDown').innerHTML = BFF.getScrollerText(countValues, 1);
							$('col1ScrollDown').className = 'scroll';
							$('col1ScrollUp').className = 'scrollup_inactive';
							//BFF.positionSpacer(1);
						}
					}
					catch(e){
						//alert(e.message);
					}
					//BFF.showSpacer(3);	
					////////////////////
					BFF.li_elements[1].each(function(element)
					{
						element.onclick = function()
						{
							BFF.unselectActiveStep(1);
							BFF.activeStep[1] = element;
							BFF.activeStep[2] = null;
							BFF.activeStep[3] = null;
											
							if (BFF.container[1]) 
								BFF.container[1].addClassName(BFF.blockactive);
							
							BFF.activeStep[1].addClassName('active');
							BFF.activeStep[1].select('input[type="radio"]').each(function(radio)
							{
								radio.checked = true;
								BFF.loadStep((1 + 1), radio.value);
							});
						}
					});
				} else {
					$('col2ScrollDown').className = 'scroll';
					$('col2ScrollUp').className = 'scrollup_inactive';
					//BFF.move(2, 0, true);
					BFF.li_elements[2].each(function(element)
							{
								element.onclick = function()
								{
									BFF.unselectActiveStep(2);
									BFF.activeStep[2] = element;
									BFF.activeStep[3] = null;
									
									
									if (BFF.container[2]) 
										BFF.container[2].addClassName(BFF.blockactive);
										
									BFF.activeStep[2].addClassName('active');
									BFF.activeStep[2].select('input[type="radio"]').each(function(radio)
									{
										radio.checked = true;
										BFF.loadStep((2 + 1), radio.value);
										
									});
									
								}
							});
				}
				
				if(BFF.preloadDestTLC != "" && stepID == 1)
				{
					var elemToSelect = document.getElementById('DEST_' + BFF.preloadDestTLC);
					if (elemToSelect && elemToSelect.onclick) {
						elemToSelect.onclick();
					}
					var predecessor = elemToSelect.previousSiblings();
					if (predecessor.length >= BFF.maxVisibleItems) {
						BFF.move(1, (predecessor.length-BFF.maxVisibleItems+1) * BFF.itemHeight, true);
					}
					BFF.preloadDestTLC = "";
				}
				
				if(BFF.preloadDepTLC != "" && stepID == 2)
				{
					var elemToSelect = document.getElementById('DEP_' + BFF.preloadDepTLC);
					if (elemToSelect && elemToSelect.onclick) {
						elemToSelect.onclick();
					}
					var predecessor = elemToSelect.previousSiblings();
					if (predecessor.length >= BFF.maxVisibleItems) {
						BFF.move(2, (predecessor.length-BFF.maxVisibleItems+1) * BFF.itemHeight, true);
					}
					BFF.preloadDepTLC = "";
				}
	
			} else {
				BFF.li_element_opener[stepID].each(function(element)
				{
					element.onclick = function()
					{
						parent_element = element.up('li',0);
						data_element = parent_element.down('ul',0);
						
						if (parent_element.hasClassName('open'))
						{
							parent_element.removeClassName('open');
							parent_element.addClassName('closed');
							data_element.innerHTML ="";
							$('scroller3').style.height = objHandlerBFF.curHeight + "px";
							BFF.scroll(3);
						}
						else if (parent_element.hasClassName('closed'))
						{
							parent_element.removeClassName('closed');
							parent_element.addClassName('open');
							
							BFF.showFlightData(parent_element.readAttribute('id'), data_element);				
						}
					}
				});		  
				
				BFF.li_element_opener_follower[stepID].each(function(element)
				{
					//alert("follower");
					element.onclick = function()
					{
						
						parent_element = element.up('li',0);
						data_element = parent_element.down('ul',0);
						
						if (parent_element.hasClassName('open'))
						{
							parent_element.removeClassName('open');
							parent_element.addClassName('closed');
							data_element.innerHTML ="";
							$('scroller3').style.height = objHandlerBFF.curHeight + "px";
							BFF.scroll(3);
						}
						else if (parent_element.hasClassName('closed'))
						{
							parent_element.removeClassName('closed');
							parent_element.addClassName('open');
							
							BFF.showFlightData(parent_element.readAttribute('id'), data_element);				
						}
					}
				});
				
				if(BFF.preloadMonthYear != "" && stepID > 2)
				{
					var elemToSelectParent = document.getElementById(BFF.preloadMonthYear);
					if (elemToSelectParent) {
						var elemToSelect = elemToSelectParent.down('span',0);
						if (elemToSelect && elemToSelect.onclick) {
							elemToSelect.onclick();
						}
					}
					
					BFF.preloadMonthYear = "";
				}
					
			}
		}
		catch(e){
			//alert(e.message);
		}
	},
	showFlightData: function(descriptor, data_element)
	{
		BFF.li_element_opener[3].each(function(element)
		{
			parent_element = element.up('li',0);
			data_element = parent_element.down('ul',0);
			data_element.innerHTML = "<li style='display:none'>&nbsp;</li>";
			if(parent_element.readAttribute('id') != descriptor)
			{
				parent_element.removeClassName('open');
				parent_element.addClassName('closed');
			}	
			else
			{
				parent_element.removeClassName('closed');
				parent_element.addClassName('open');
				xajax_getPriceFinderFlights(descriptor, allAirberlin, direction, BFF.language, BFF.isHotelspecial);
			}
		}); 
	//	if(BFF.persistentData[descriptor])
	//		BFF.setFlightData(descriptor);
	//	else
	//		xajax_getPriceFinderFlights(descriptor);
			
	},
	// Bei Auswahl eines Wertes die naechste Spalte laden
	loadStep: function(stepID, descriptor)
	{
		try
		{
			switch(stepID)
			{
			
				case 2:
					xajax_setPriceFinderDeparture(descriptor, allAirberlin, sortOrder, direction, BFF.language, BFF.isHotelspecial);
					BFF.headline[2].addClassName('active');				
					BFF.headline[3].removeClassName('active');
					BFF.ul_container[2].invoke('show');				
					BFF.ul_container[3].invoke('hide');
					BFF.container[1].addClassName(BFF.blockactive);
					BFF.container[2].removeClassName(BFF.blockactive);	
					BFF.container[3].removeClassName(BFF.blockactive);
					$('scroller3').style.height = "427px";
					BFF.unsetScroll(2);		
						
				break;
				
				case 3:
					xajax_setPriceFinderDate(descriptor, allAirberlin, direction, BFF.language, BFF.isHotelspecial);
					BFF.container[1].removeClassName(BFF.blockactive);	
					BFF.container[2].addClassName(BFF.blockactive);
					BFF.headline[3].addClassName('active');
					BFF.ul_container[3].invoke('show');		
					BFF.unsetScroll(3);		
					$('dateBlock').innerHTML = '<img src="img/waitani.gif" />';  
					$('scroller3').style.height = objHandlerBFF.curHeight + "px";
						
				break;
				
				case 4:
					BFF.li_elements[stepID] = $$('#step' + stepID + ' ul li');
					BFF.li_element_opener[stepID] = $$('#step' + stepID + ' ul li span.bff_month');
					BFF.li_element_opener_follower[stepID] = $$('#step' + stepID + ' ul li div.bff_month_follower');
				break;
				
			}
			
			BFF.scroll(stepID - 1);
			BFF.showSpacer(stepID - 1);
			BFF.positionSpacer(stepID - 1);
	//		BFF.positionSpacer(stepID);
		}
		catch(e)
		{
			//alert(e.message);
		}
	},
	unselectActiveStep: function(stepID)
	{
		if (BFF.activeStep[stepID])
		{
			BFF.activeStep[stepID].removeClassName('active');
			BFF.activeStep[stepID].select('input[type="radio"]').each(function(radio){
				radio.checked = false;
			});
		}
	},

	// Scroll-Leiste entfernen
	unsetScroll: function(stepID)
	{
		for(var i = stepID; i <= 3; i++)
		{
			BFF.scroller[i].hide();
			BFF.ul_container[i][0].undoPositioned();
		}
	},

	// Scrolleiste anzeigen
	scroll: function(stepID)
	{
		if(stepID >= 3)
			return;
		
		var h = BFF.ul_container[stepID][0].getHeight();

		if (h < BFF.container[stepID].select('.bff_scroller')[0].getHeight())
		{
			BFF.scroller[stepID].hide();
			BFF.ul_container[stepID][0].undoPositioned();
			return false;
		}

		BFF.scroller[stepID].show();
			
		BFF.ul_container[stepID][0].makePositioned();
		
		$('col' + stepID + 'ScrollDown').observe('mousedown', BFF.scrolldown.bindAsEventListener(this, stepID));
		$('col' + stepID + 'ScrollDown').observe('mouseup', BFF.scrollstop.bindAsEventListener(this, stepID));
		$('col' + stepID + 'ScrollUp').observe('mousedown', BFF.scrollup.bindAsEventListener(this, stepID));
		$('col' + stepID + 'ScrollUp').observe('mouseup', BFF.scrollstop.bindAsEventListener(this, stepID));
	},

	// Runter scrollen
	scrolldown: function(ev,step)
	{
		if (BFF.interval[step] != "")
		{
			clearInterval(BFF.interval[step]);
			BFF.interval[step] = "";
		}

		BFF.interval[step] = setInterval(
			function() {
				BFF.move(step, 5);
			},
			10
		);
	},

	// Hoch scrollen
	scrollup: function(ev,step)
	{
		if (BFF.interval[step] != "")
		{
			clearInterval(BFF.interval[step]);
			BFF.interval[step] = "";
		}

		BFF.interval[step] = setInterval(
			function() {
				BFF.move(step, -5);
			},
			10
		);
	},
	scrollstop: function(ev,step)
	{
		if(BFF.interval[step])
		{
			clearInterval(BFF.interval[step]);
			BFF.interval[step] = "";
		}
	},
	setScrollerText: function(singularTextStep1, singularTextStep2, pluralTextStep1, pluralTextStep2)
	{
		BFF.singularTextStep1 = singularTextStep1;
		BFF.singularTextStep2 = singularTextStep2;
		BFF.pluralTextStep1 = pluralTextStep1;
		BFF.pluralTextStep2 = pluralTextStep2;
	},
	getScrollerText: function(countValues, step)
	{
		scrollerText = countValues;
		if(countValues != 1)
		{
			if(step == 1)
				scrollerText = parseInt(countValues) + " " + BFF.pluralTextStep1;
			else
			 	scrollerText = parseInt(countValues) + " " + BFF.pluralTextStep2;
		}
		else
		{
			if(step == 1)
				scrollerText = "1 " + BFF.singularTextStep1;
			else
			 	scrollerText = "1 " + BFF.singularTextStep2;
		}
		return scrollerText;
	},
	move: function(step,amount,preload)
	{
		try{
			if (preload == undefined) preload = false;
			var h = 0;
			if (step == 3)
			{
				BFF.li_element_opener[3].each
				(
					function(element)
					{
						parent_element = element.up('li',0);
						h += parent_element.getHeight();
					}
				);
			}
			else
				h = BFF.ul_container[step][0].getHeight();
	
			var top = (parseInt(BFF.ul_container[step][0].getStyle('top')) ? parseInt(BFF.ul_container[step][0].getStyle('top')) : 0) - amount;
			if (top < 5 && (preload || (Math.abs(top)) < Math.abs(h - BFF.container[step].select('.bff_scroller')[0].getHeight() + 32)) )
			{
				if (step < 3 && !preload) {
					if (document.getElementById('col' + step + 'ScrollDown').className = "scroll_inactive") document.getElementById('col' + step + 'ScrollDown').className = "scroll";
					if (document.getElementById('col' + step + 'ScrollUp').className = "scrollup_inactive") document.getElementById('col' + step + 'ScrollUp').className = "scrollup";
				}
				
				var countValues =  parseInt(((h - 407 + top) / 24 ), 10);
				
				if(step < 3)
					$('col' + step + 'ScrollDown').innerHTML = BFF.getScrollerText(countValues, step);
		
				BFF.ul_container[step][0].setStyle({ top: top + 'px' });
				BFF.positionSpacer(step);
			} else {
				if (step < 3) {
					if (amount > 0 && document.getElementById('col' + step + 'ScrollDown').className == "scroll")
						document.getElementById('col' + step + 'ScrollDown').className = "scroll_inactive";
					if (amount < 0 && document.getElementById('col' + step + 'ScrollUp').className == "scrollup")
						document.getElementById('col' + step + 'ScrollUp').className = "scrollup_inactive";
				}
			}
		}
		catch(e){
			//alert(e.message);
		}
	},
	showSpacer: function(stepID)
	{
		if(stepID == 2)
		{
			BFF.spacer[3].show();
			BFF.spacer[2].show();
			BFF.spacer[4].show();
		}
		else if(stepID == 1)
		{
			BFF.spacer[4].hide();
			BFF.spacer[3].hide();
			BFF.spacer[2].hide();
			BFF.spacer[1].show();
		}
		else if(stepID == 3)
		{
			BFF.spacer[4].hide();
			BFF.spacer[3].hide();
			BFF.spacer[2].hide();
			BFF.spacer[1].hide();
		}
	},
	positionSpacer: function(step)
	{
		if(step >= 3)
			return;
		
		if(BFF.activeStep[1])
			BFF.offset[1] = BFF.activeStep[1].cumulativeOffset()[1] - BFF.yContainer + BFF.offset_fix + BFF.offset_fix_ie;
		else
			return;
		if(BFF.activeStep[2])
			BFF.offset[2] = BFF.activeStep[2].cumulativeOffset()[1] - BFF.yContainer + BFF.offset_fix + 1;

		if (step == 1)
			BFF.offset[step] += BFF.offset_fix_ie - 1;
		
		if (step == 1)
		{
			BFF.spacer[1].setStyle({
				'top' : (BFF.offset[1]-2) + 'px'
			});
		}

		var sub = 2;
		var sub2 = 0;
		var off = 22;
		if (BFF.offset_fix) {
			sub = 4;
			off = 23;
			sub2 = 2;
		}

		if (step < 3 && BFF.offset[2])
		{
			var top = Math.min(BFF.offset[1],BFF.offset[2]);
			var height = Math.abs(BFF.offset[1] - BFF.offset[2]) + off;

			BFF.spacer[2].setStyle({
				'top' : (top-sub) + 'px',					// Mozialla -4
				'height' : height + 'px'
			});
			BFF.spacer[3].setStyle({
				'top' : (BFF.offset[2]-sub) + 'px'		// Mozialla -4
			});

		}
		if(step == 2)
		{
			if (BFF.spacer[4].style.display != "none") {
				BFF.spacer[4].setStyle({
					'top' : (BFF.offset[2]-sub2) + 'px'
				});
			}
		}
	}

};

Event.observe(window, 'load', BFF.init);


function step3IE6Hack()
{
	document.getElementById('step3').style.display = 'none';
	document.getElementById('step3').style.display = 'block';
}