var SelectionBox = new Class({
Implements: [Events, Options, Chain],
	options: {
		    oid: null,
		    adults: null,
		   children: null,
		   cma: null,
		   cna:null,
		   hid: null,
		 duration:null,
		  airport_id: null,
		 direct: null
	  },
	initialize: function(options){
		this.setOptions(options||null);
		this.submit_ns1();
		this.submit_ns2();
if (this.options.duration!=null && this.options.duration>0)	this.submit_ns3();

	   	this.startListeners();
	if (this.options.direct==1) {
		if (this.options.adults!=null && this.options.adult_ages!=null && this.options.oid!=null)  {  GetDepAirports(this.options.oid,this.options.adult_ages,this.options.children_ages,'depairports_options',0); }
	if (this.options.duration!=null && this.options.duration>0) {
		if (this.options.airport_id!=null) {  GetHotels(this.options.airport_id,this.options.oid,'hotels_options',0); }
	}
		if (this.options.hid!=null) { GetAvailability(this.options.airport_id,this.options.hid,this.options.oid,'dates_prices_selector',0); }
	}
  },
	  startListeners: function() {
		    var action = function() {
			new Fx.Reveal('partysize_panel').reveal();
			new Fx.Reveal('depairport_panel').dissolve();
	if (this.options.duration!=null && this.options.duration>0) {		new Fx.Reveal('accommodation_panel').dissolve(); }
			new Fx.Reveal('dates_prices_panel').dissolve();

		    }.bind(this);
	    window.addEvent('domready',action);
	},

	submit_ns1: function(){
		$('ns1_controller').addEvent('click',function(){
		var adults=document.getElementById('adults_selector').options[document.getElementById('adults_selector').selectedIndex].value;
		var children=(this.options.cna==1 || this.options.cna==undefined) ? document.getElementById('children_selector').options[document.getElementById('children_selector').selectedIndex].value : 0;
		var err=0;
		var adult_ages=new Array();
		var children_ages=new Array();
	if (adults>0) {
		for (i=0;i<adults;i++) {
			var aobj="adult_age"+i;
			ab="ab"+i;
			 $(ab).removeClass('active2');
				if ($(aobj).value=="") {
					err++; 
					$(ab).toggleClass('active2',true);
				 }
				else {
					if($(aobj).value<parseInt(this.options.cma)) {
						err++;
						 $(ab).toggleClass('active2',true);
						alert('Adult must be at least '+this.options.cma+' years old!');
					}
					else {
						adult_ages[i]=$(aobj).value;
					}
				}
			}
		}
	if (children>0) {
		for (i=0;i<children;i++) {
			var cobj="child_age"+i;
			cb='cb'+i;
			$(cb).removeClass('active2');
			if ($(cobj).value=="")  {
				err++; 
				 $(cb).toggleClass('active2',true);
			 }
			else { 
				 if ($(cobj).value> parseInt(this.options.cma)) { 
					 $(cb).toggleClass('active2',true);
					err++;
					alert('Child must be maximum '+this.options.cma+' years old!');
				}
					else {
						if ($(cobj).value!="") { children_ages[i]=$(cobj).value; }
					}
			}
		}
	}
	if (err>0) return false;
	else {

	// we need to pass ages` arrays as strings to get_depairports.php file so they can be put into SESSION....

		var aa="";
		var ca="";
		if (adult_ages.length>0) { aa=adult_ages.join('|');}
		if (children_ages.length>0) { ca=children_ages.join('|');}

	if (this.options.duration!=null && this.options.duration>0) {	$('accommodation_panel').dissolve(); }
		$('dates_prices_panel').dissolve();
		var oid=this.options.oid; 
		new Fx.Reveal('depairport_panel', {
			onComplete: function() { 

			new Fx.Reveal('depairport_panel_loader', {
					
					onComplete: function() {
						 GetDepAirports(oid,aa,ca,'depairports_options',1); 	

						$('line1').addClass('non_act_txt');
						$('line1').removeClass('act_txt');

						$('line2').removeClass('non_act_txt');
						$('line2').addClass('act_txt');
}
				}).reveal();
			 }
		}).dissolve();

	  }
	}.bind(this));		
	},
//---------------------------------------------------------------------
	submit_ns2: function() {
		$('ns2_controller').addEvent('click',function(){
		var depairport=$$('input[name=dep_airport_id]:checked').map(function(e) { return e.value; }); 
		if (depairport=="" || depairport=="undefined") {
			alert ("Please select your departure airport!");
			return false;
		}
		else {
		// now that we got depairport selected, we can get list of hotels:
		this.options.airport_id=depairport;
		var da=depairport;
		var oid=this.options.oid;
		$('dates_prices_panel').dissolve();

	if (this.options.duration!=null && this.options.duration>0) {
		new Fx.Reveal('accommodation_panel', {
			onComplete: function() { 

			new Fx.Reveal('accommodation_panel_loader', {
					onComplete: function() {  
						GetHotels(da,oid,'hotels_options',1);	


						$('line2').addClass('non_act_txt');
						$('line2').removeClass('act_txt');

						$('line3').removeClass('non_act_txt');
						$('line3').addClass('act_txt');
	}
				}).reveal();
			 }
		}).dissolve();
	}
	else {
		var hotel_id=9999;
		var oid=this.options.oid;
		var da=this.options.airport_id;
		new Fx.Reveal('dates_prices_panel', {
			onComplete: function() { 

			new Fx.Reveal('dates_prices_panel_loader', {
					onComplete: function() { 

					GetAvailability(da,hotel_id,oid,'dates_prices_selector',1);


						$('line2').addClass('non_act_txt');
						$('line2').removeClass('act_txt');

						$('line4').removeClass('non_act_txt');
						$('line4').addClass('act_txt');
	}
				}).reveal();
			 }
		}).dissolve();
	}

		}
		}.bind(this));
	},
//---------------------------------------------------------------------
	submit_ns3: function() {
		$('ns3_controller').addEvent('click',function(){
		var hotel_id=$$('input[name=hotel_id]:checked').map(function(e) { return e.value; }); 
		if ((hotel_id=="" || hotel_id=="undefined") && this.options.duration>0) {
			alert ("Please select your preferred hotel!");
			return false;
		}
		else {
		// now that we got hotel selected, we can get list of dates and prices for selected offer/partysize/dep_airport/hotel:

		this.options.hotel_id=hotel_id;
		var oid=this.options.oid;
		var da=this.options.airport_id;
		new Fx.Reveal('dates_prices_panel', {
			onComplete: function() { 

			new Fx.Reveal('dates_prices_panel_loader', {
					onComplete: function() { 	

					GetAvailability(da,hotel_id,oid,'dates_prices_selector',1,this.options.duration);	

						$('line3').addClass('non_act_txt');
						$('line3').removeClass('act_txt');

						$('line4').removeClass('non_act_txt');
						$('line4').addClass('act_txt');
}
				}).reveal();
			 }
		}).dissolve();
		}
		}.bind(this));
	}	

});

//---------------------------------------------------------------------

function GetDepAirports (oid,a_ages, c_ages, selector,scroll) {
		var JSONRequest_a = new Request.JSON({url: '/config/sel_getairports.php', 
			onComplete: function(res){ 
			if (res!=null) {
				var switch1=new Chain();
				var dpl=function() { new Fx.Reveal('depairport_panel_loader').dissolve(); };
				var dp=function() { new Fx.Reveal('depairport_panel').reveal();};
				if (scroll==1) var dpsc=function() {  new Fx.Scroll(window).toElement('depairport_panel'); };
				var gl=function() {CreateAirportList(res.results, selector);}
				switch1.chain(dpl).chain(dp).chain(gl).chain(dpsc);
				switch1.callChain.delay(500,switch1);
				switch1.callChain.delay(500,switch1);
				switch1.callChain.delay(600,switch1);
				switch1.callChain.delay(800,switch1);
				}
				else {
					alert('Sorry, no departure airports available for selected offer/party size! Please change your settings.');
					return false;
				}

		}}).send({
			method: 'get',
			data: 'oid='+oid+'&adult_ages='+a_ages+'&children_ages='+c_ages
		});
	}
//---------------------------------------------------------------------
function GetHotels(aid, oid,selector,scroll) { 

		var JSONRequest_h = new Request.JSON({url: '/config/sel_gethotels.php', 

			onComplete: function(res,text){ 
				if (res!=null) { 
					var switch2=new Chain();
					var apl=function() { new Fx.Reveal('accommodation_panel_loader').dissolve(); };
					var ap=function() { new Fx.Reveal('accommodation_panel').reveal();};
					if (scroll==1) var apsc=function() {new Fx.Scroll(window).toElement('accommodation_panel');};
					var gl3=function() {CreateHotelList(res.results, selector);}
					switch2.chain(apl).chain(ap).chain(gl3).chain(apsc);
					switch2.callChain.delay(500,switch2);
					switch2.callChain.delay(500,switch2);
					switch2.callChain.delay(600,switch2);
					switch2.callChain.delay(800,switch2);
				}
				else {
					alert('Sorry, no hotels available for selected airport! Please change your departure airport.');
					new Fx.Reveal('accommodation_panel_loader').dissolve(); 
					return false;
				}
		}
		}).send({
			method: 'get',
			data: 'aid='+aid+'&oid='+oid
		});
	}
//---------------------------------------------------------------------
function GetAvailability(aid, hid, oid,selector,scroll, duration) {

		var JSONRequest_h = new Request.JSON({url: '/config/sel_getavailability.php', 
			onSuccess: function(res,text){ 
				if (res.details.length>0) {
					var switch3=new Chain();
					var dppl=function() { new Fx.Reveal('dates_prices_panel_loader').dissolve(); };
					var dpp=function() { new Fx.Reveal('dates_prices_panel').reveal();};
					if (scroll==1) var dpsc=function() {new Fx.Scroll(window).toElement('dates_prices_panel');};

					var gl4=function() {
						if (duration!=null && duration>0)	ShowAvailability(res, aid,hid,oid,selector);
						else 	ShowAvailabilityDaytrip(res, aid,oid,selector);
					}

					switch3.chain(dppl).chain(dpp).chain(gl4).chain(dpsc);
					switch3.callChain.delay(500,switch3);
					switch3.callChain.delay(500,switch3);
					switch3.callChain.delay(600,switch3);
					switch3.callChain.delay(800,switch3);
				}
				else {
					alert('Sorry, no dates available for selected criteria! Please change your departure airport or hotel/roomtype.');
					new Fx.Reveal('dates_prices_panel_loader').dissolve(); 
					return false;
				}
		}
		}).send({
			method: 'get',
			data: 'aid='+aid+'&hid='+hid+'&oid='+oid
		});
	}

//---------------------------------------------------------------------

function ShowAvailability (opt_results, aid,hid,oid,selector) {
	// we need to get details for current selection:
	if (opt_results.details.length>0) {
					$(selector).innerHTML='';
					var detresults=opt_results.details;
					var details_box = new Element('div', {'style': 'width:100%; position:relative; float:left; text-align:left;clear:both;margin-bottom:12px;'});
					var selection_text='<span class="big4">Your selection: </span><br><span class="big3">&#149; '+detresults[0].offer+' ('+detresults[0].duration+' day trip)<br /> &#149; departing '+detresults[0].airport+'<br />';
					if (detresults[0].hotel!='') selection_text+='&#149; '+detresults[0].hotel;
					selection_text+='<br /><br /><u>Prices shown are based on your party selected above.</u></span>';
					var dets = new Element('span', {'html': '<div style="position:relative; float:left;width:100%;">'+selection_text+'</div><div style="position:relative; float:left; width:100%; padding-top:20px; padding-bottom:10px;"><span class="big2">Available departure dates and room types:</span></div>'}).inject(details_box);
					details_box.inject($(selector));
	}

		var top_box = new Element('div', {'style': 'width:600px; position:relative; float:left; text-align:left;clear:both;height:26px;margin-bottom:6px;'});
		var spacer = new Element('div', { 'style':'position:relative; float:left;width:260px; height:26px;padding-top:5px;'}).inject(top_box);
		var spacer_bb = new Element('div', { 'style':'position:relative; float:left;width:50px;height:26px;'}).inject(top_box);
		var original_price_box = new Element('div', { 'style':'position:relative; float:left;width:130px; text-align:center; height:26px;padding-top:5px;'});
		var original_price_text = new Element('span', {'html': '<span class="big6">Original price</span>'}).inject(original_price_box);
		original_price_box.inject(top_box);
		var our_price_box = new Element('div', { 'style':'position:relative; float:left;width:95px; text-align:center; height:26px; padding-top:5px;background-color:#FFFFFF;'});
		var our_price_text = new Element('span', {'html': '<span class="big6red">Our price</span>'}).inject(our_price_box);
		our_price_box.inject(top_box);
		var spacer_opt = new Element('div', { 'style':'position:relative; float:left;width:35px;height:26px;'}).inject(top_box);

		top_box.inject($(selector));


	// here's the structure:
	// details_box
	// options_box
	//	depdate_box
	//		opt_box
	//			opt_title
	//			more_info_box
	// roomtype_box

	if (opt_results.results.length>0) {
		opt_results.results.each (function(depdate) {
			var options_box = new Element('div', {'id': 'options'+depdate.value,'class': 'options_box'});
			var depdate_box = new Element('div', {'id': 'depdate'+depdate.value,'class': 'depdate_item'});
			var dd = new Element('span', {'class':'big2','html': depdate.departure}).inject(depdate_box);
			depdate_box.inject(options_box);

			var opts=new Array();
			var opts=depdate.options.split("_|_");
			if (opts.length>0) {
			opts.each (function(option) {
				var opt=option.split("::"); 

					//roomtype format:  OPTID::OID::HID::TYPENAME::ROOMTYPE_ID::MIN_OCC::MAX_OCC::BOARD_BASIS::TOTAL_PRICE::DISCOUNT::INFANT_PRICE::SUPPLIER_ID::SPECIAL_OFFER

					var opt_box = new Element('div', {'id': 'opt'+opt[0],'class': 'opt_box'});
			if (opt[4]!="" && opt[4]!="undefined") {
					var more_info = '<a href="javascript:void(0);" onclick="ShowDesc('+opt[4]+');"><img src="/images/more_info_ico.gif" width="28" height="28" border="0" alt="'+opt[3]+'" title="'+opt[3]+'" align="left" hspace="2" /></a>';
			}
					else var more_info='';
			

					var opt_title = new Element('div', {'class': 'opt_title'});
					var opt_title_txt = new Element('span', {'class':'big3','html':more_info+' '+opt[3]}).inject(opt_title);
					opt_title.inject(opt_box);

			// now board basis:
					var opt_bb = new Element('div', { 'style':'position:relative; float:left;width:50px; text-align:center; border-right:1px solid #8a3234; height:30px; padding-top:7px;'});
					var opt_bb_text = new Element('span', {'html': '<span class="big3">'+opt[7]+'</span>'}).inject(opt_bb);
					opt_bb.inject(opt_box);
			// original price:
				
					var opt_price = new Element('div', { 'style':'position:relative; float:left;width:130px; text-align:center; border-right:1px solid #8a3234;height:30px; padding-top:7px;'});
					var opt_price_text = new Element('span', {'html': '<span class="big6">&pound;'+opt[10]+'</span>'}).inject(opt_price);
					opt_price.inject(opt_box);
			// our price:
					var opt_saving = new Element('div', { 'style':'position:relative; float:left;width:95px; text-align:center; background-color:#FFFFFF;height:30px; padding-top:7px;'});
					var opt_saving_text = new Element('span', {'html': '<span class="big6red">&pound;'+opt[8]+'</span>'}).inject(opt_saving);
					opt_saving.inject(opt_box);
			// now add button:
					var opt_btn = new Element('div', { 'style':'position:relative; float:left;width:65px; text-align:center;height:30px; padding-top:3px;padding-left:5px;'});
					var opt_btn_text = new Element('span', {'html': '<a href="javascript:void(null);" onclick="javascript:AddToShortlist('+opt[0]+');"><img src="/images/add_button.gif" width="63" height="33" border="0" alt="add to shortlist" title="add to shortlist" onmouseover="this.src=\'/images/add_button_.gif\'; return true;" onmouseout="this.src=\'/images/add_button.gif\'; return true;"/></a>'}).inject(opt_btn);
					opt_btn.inject(opt_box);
					opt_box.inject(options_box);
				});
			}
			options_box.inject($(selector));
			new Fx.Reveal($(options_box)).reveal();
		});
	}
	// now roomtypes
	if (opt_results.roomtypes!=undefined && opt_results.roomtypes.length>0) {
			var roomtype_header = new Element('div', {'class': 'roomtype_box'});
			var rd = new Element('span', {'html': '<div style="position:relative; float:left;width:100%;padding-top:20px;"><span class="big2">Roomtypes description: </span></div>'}).inject(roomtype_header);
			roomtype_header.inject($(selector));

		opt_results.roomtypes.each (function(roomtype) {
			var roomtype_box = new Element('div', {'id': 'desc_'+roomtype.rid,'class': 'opt_box'});
			var rt_box = new Element('div', {'class': 'rd_title'});
			var rd_title = new Element('span', {'class':'big3','html': '&rsaquo; '+roomtype.typename}).inject(rt_box);
			rt_box.inject(roomtype_box);

			var rd_box = new Element('div', {'class': 'rd_desc'});
			var rd_desc = new Element('span', {'class':'index','html': roomtype.description}).inject(rd_box);
			rd_box.inject(roomtype_box);

			roomtype_box.inject($(selector));
			new Fx.Reveal($(roomtype_box)).reveal();
		});
	}
}

//---------------------------------------------------------------------

function ShowAvailabilityDaytrip (opt_results, aid,oid,selector) {
	if (opt_results.details.length>0) {
					$(selector).innerHTML='';
					var detresults=opt_results.details;
					var details_box = new Element('div', {'style': 'width:100%; position:relative; float:left; text-align:left;clear:both;margin-bottom:12px;'});
					var selection_text='<span class="big4">Your selection: </span><br><span class="big3">&#149; '+detresults[0].offer+' ('+detresults[0].duration+' day trip)<br /> &#149; departing '+detresults[0].airport+'<br /></span>';
					var dets = new Element('span', {'html': '<div style="position:relative; float:left;width:100%;">'+selection_text+'<br /><br /><u>Prices shown are based on your party selected above.</u></div><div style="position:relative; float:left; width:100%; padding-top:20px; padding-bottom:10px;"><span class="big2">Available departure dates and room types:</span></div>'}).inject(details_box);
					details_box.inject($(selector));
	}

		var top_box = new Element('div', {'style': 'width:600px; position:relative; float:left; text-align:left;clear:both;height:26px;margin-bottom:6px;'});
		var spacer = new Element('div', { 'style':'position:relative; float:left;width:298px; height:26px;padding-top:5px;'}).inject(top_box);

		var original_price_box = new Element('div', { 'style':'position:relative; float:left;width:130px; text-align:center; height:26px;padding-top:5px;'});
		var original_price_text = new Element('span', {'html': '<span class="big6">Original price</span>'}).inject(original_price_box);
		original_price_box.inject(top_box);
		var our_price_box = new Element('div', { 'style':'position:relative; float:left;width:95px; text-align:center; height:26px; padding-top:5px;background-color:#FFFFFF;'});
		var our_price_text = new Element('span', {'html': '<span class="big6red">Our price</span>'}).inject(our_price_box);
		our_price_box.inject(top_box);
		var spacer_opt = new Element('div', { 'style':'position:relative; float:left;width:35px;height:26px;'}).inject(top_box);

		top_box.inject($(selector));

	if (opt_results.results.length>0) {
		opt_results.results.each (function(depdate) {

			var options_box = new Element('div', {'id': 'options'+depdate.value,'class': 'options_box'});
				var opt=depdate.option.split("::"); 

			//option format:  OPTID::OID::TOTAL_PRICE::DISCOUNT::INFANT_PRICE::SUPPLIER_ID::SPECIAL_OFFER

					var opt_box = new Element('div', {'id': 'opt'+opt[0],'class': 'opt_box2'});
					var opt_title = new Element('div', {'class': 'opt_title2'});
					var opt_title_txt = new Element('span', {'class':'big3','html':depdate.departure}).inject(opt_title);
					opt_title.inject(opt_box);

			// original price:
				
					var opt_price = new Element('div', { 'style':'position:relative; float:left;width:130px; text-align:center; border-right:1px solid #8a3234;height:30px; padding-top:7px;'});
					var opt_price_text = new Element('span', {'html': '<span class="big6">&pound;'+opt[4]+'</span>'}).inject(opt_price);
					opt_price.inject(opt_box);
			// our price:
					var opt_saving = new Element('div', { 'style':'position:relative; float:left;width:95px; text-align:center; background-color:#FFFFFF;height:30px; padding-top:7px;'});
					var opt_saving_text = new Element('span', {'html': '<span class="big6red">&pound;'+opt[2]+'</span>'}).inject(opt_saving);
					opt_saving.inject(opt_box);
			// now add button:
					var opt_btn = new Element('div', { 'style':'position:relative; float:left;width:65px; text-align:center;height:30px; padding-top:3px;padding-left:5px;'});
					var opt_btn_text = new Element('span', {'html': '<a href="javascript:void(null);" onclick="javascript:AddToShortlist('+opt[0]+');"><img src="/images/add_button.gif" width="63" height="33" border="0" alt="add to shortlist" title="add to shortlist" onmouseover="this.src=\'/images/add_button_.gif\'; return true;" onmouseout="this.src=\'/images/add_button.gif\'; return true;"/></a>'}).inject(opt_btn);
					opt_btn.inject(opt_box);
					opt_box.inject(options_box);
			options_box.inject($(selector));
			new Fx.Reveal($(options_box)).reveal();
		});
	}
}


//---------------------------------------------------------------------


function CreateAirportList (results, selector) {
		$(selector).innerHTML="";
		if (results.length>0) {
		$(selector).innerHTML='<div style="width:100%; position:relative; float:left; text-align:left;clear:both;height:25px;"><span class="big">Select airport:</span></div>';
		results.each (function(result) {
			var opt = new Element('div', {'id': 'dp'+result.value,'class': 'opt_item'});
			var sel=(result.selected=="selected") ? "checked" : "";
			var opt_label = new Element('span', {'html':'<input type="radio" name="dep_airport_id" value="'+result.value+'" '+sel+' id="da'+result.value+'"><label for="da'+result.value+'" class="opt_label"><span class="big3">'+result.label+'</span></label>'}).inject(opt);
			opt.inject($(selector));
			FancyForm.add(opt);
			new Fx.Reveal($(opt)).reveal();
		});
	}
}
//---------------------------------------------------------------------

function getcontent (string) {
	var lsRegExp = /\+/g;
	return unescape(String(string).replace(lsRegExp, " ")); 
}

//---------------------------------------------------------------------

function CreateHotelList (hotels,  selector) {
//		var roomtype_label = new Element('span', {'class': 'big3','html':'<input type="radio" name="roomtype_id" value="'+rt[1]+'"   id="rt'+rt[1]+'"><label for="rt'+rt[1]+'" class="opt_label2">'+rt[0]+' ('+rt[2]+'/'+rt[3]+')</label>'}).inject(roomtype_box);

		$(selector).innerHTML="";
		if (hotels.length>0) {
			$(selector).innerHTML='<div style="width:100%; position:relative; float:left; text-align:left;clear:both;height:25px;"><span class="big">Available hotels:</span></div>';
		hotels.each (function(hotel) {
			var hotel_box = new Element('div', {'id': 'hotel'+hotel.value,'class': 'hotel_item'});
			var newLabel = new Element('label', {'for': 'ht'+hotel.value, 'class':'hotel_opt', 'text':hotel.label}); 
			if (hotel.selected=="true") var sel='true'; else var sel='';
			var newCheckbox = new Element('input', {'type': 'radio', 'id': 'ht'+hotel.value, 'name': 'hotel_id', 'value': hotel.value , 'checked': sel}); 
			newLabel.adopt(newCheckbox); 
			newLabel.inject(hotel_box); 
			FancyForm.add(newCheckbox);
			var hotel_description = new Element('div', {'class': 'hotel_description'});
			var hot_desc = new Element('span', {'class':'index','html':getcontent(hotel.hotel_description)+'<a href="javascript:void(0);" onclick="javascript:GetHotelDetails('+hotel.value+');">read more &raquo;</a>'}).inject(hotel_description);
			hotel_description.inject(hotel_box);

			hotel_box.inject($(selector));
			new Fx.Reveal($(hotel_box)).reveal();
		});
	//alert($(selector).innerHTML);
	}
}
//---------------------------------------------------------------------
