﻿var FlightResultsFilter = new function () {
    

	this.Filter = function () {

		var sDepartureDateCSV = this.DateCarousel.GetSelectedDate();

		var sSortBy = ''
		switch (dd.GetText('ddlSortResultsBy')) {
			case 'Price: Lowest first':
				sSortBy = 'price_asc';
				break;
			case 'Outbound Departure Time':
				sSortBy = 'outbounddeparturetime';
				break;
			case 'Airline: A-Z':
				sSortBy = 'flightcarrier';
				break;
		}

		var sDepartureTimeOfDayCSV = GetCSVFromPrefixedCheckboxes('chk_DepartureTOD_');
		var sArrivalTimeOfDayCSV = GetCSVFromPrefixedCheckboxes('chk_ReturnTOD_');
		//var sDepartureDateCSV = GetCSVFromPrefixedCheckboxes('chk_Date_') + ',' + f.GetValue('hidSelectedDepartureDate');
		var sNumberOfStopsCSV = GetCSVFromPrefixedCheckboxes('chk_Stops_');
		var sFlightCarrierCSV = GetCSVFromPrefixedCheckboxes('chk_FC_');
		var sDepartureAirportCSV = GetCSVFromPrefixedCheckboxes('chk_DA_');
		var sProductAttributeIDCSV = GetCSVFromPrefixedCheckboxes('chk_PA_');


		this.FlightResultsFilter.Go(sSortBy, sDepartureDateCSV, sDepartureTimeOfDayCSV, sArrivalTimeOfDayCSV, sNumberOfStopsCSV,
            sFlightCarrierCSV, sDepartureAirportCSV, sProductAttributeIDCSV);
	}


	//date carousel
	this.DateCarousel = new function () {

		this.Select = function (aSelectedDate) {
			var aDates = f.GetObjectsByIDPrefix('aDateCarousel_', 'a', 'divDateCarousel');
			for (var i = 0; i < aDates.length; i++) {
				f.SetClassIf(aDates[i], 'selected', aDates[i] == aSelectedDate);
			}

			FlightResultsFilter.Filter();
		}

		this.GetSelectedDate = function () {
			return f.GetObject('divDateCarousel') != undefined ? f.GetElementsByClassName('a', 'selected', 'divDateCarousel')[0].id.split('_')[1] : '';
		}


		this.ChangeSelectedDate = function (sSelectedDate) {
			Postback('ChangeSelectedDate', sSelectedDate)

		}

	}


	//CacheDate carousel
	this.FlightCarousel_Select = function (oLink) {

		//dont attempt if we are already posting back
		if (this.PostingBack) {
			return;
		} else {

			//show the over-lay messaage and 
			//fire off new search - but will then be a post back
			f.SetHTML('divFlightResults', '<p class="waiting">Please wait a second while we retrieve your results...</p>');
			Postback('ChangeDepartureDate', oLink.id.split('_')[1]);
		}

	}

	//full-month carousel new date selected
	this.FlightMonthCarousel_Select = function (oLink) {

		//dont attempt if we are already posting back
		if (this.PostingBack) {
			return;
		} else {

			//show the wait message and fire off new search
			ShowWaitMessage();
			ff.Call('ChangeDepartureDate', function () { Postback(); }, oLink.id.split('_')[1]);
			Postback('ChangeDepartureDate', oLink.id.split('_')[1]);
		}

	}

	//earlier/later naviagtion selected
	this.FlightMonthCarousel_Navigation = function (sDirection) {

		//get the carousel dates with back/fwd x number of weeks
		ff.Call('FlightCarouselNavigation',
							function (sHTML) {
								FlightResultsFilter.FlightMonthCarousel_NavigationDone(sHTML);
							}, sDirection);
		
		//show working overlay
		f.Show('divWiderMonthOverlay');		
	}

	this.FlightMonthCarousel_NavigationDone = function (sHTML) {
		f.GetObject('divFullMonthTableHolder').innerHTML = sHTML;
		
		//hide the working overlay
		f.Hide('divWiderMonthOverlay');
	}

	//filter webservice
	this.FlightResultsFilter = new WebService(true);
	this.FlightResultsFilter.Go = function (SortBy, DepartureDateCSV, DepartureTimeOfDayCSV, ArrivalTimeOfDayCSV, NumberOfStopsCSV,
	    FlightCarrierCSV, DepartureAirportCSV, ProductAttributeIDCSV) {

		aParams = new Array(['SortBy', SortBy], ['DepartureDateCSV', DepartureDateCSV], ['DepartureTimeOfDayCSV', DepartureTimeOfDayCSV],
            ['ArrivalTimeOfDayCSV', ArrivalTimeOfDayCSV], ['NumberOfStopsCSV', NumberOfStopsCSV], ['FlightCarrierCSV', FlightCarrierCSV],
            ['DepartureAirportCSV', DepartureAirportCSV], ['ProductAttributeIDCSV', ProductAttributeIDCSV]);
		this.RunWebService('/webservices/search.asmx', 'http://intuitivesystems', 'FlightResultsFilter', aParams, this, false);
		f.SetHTML('divFlightResults', '<p class="waiting">Please wait a second while we retrieve your results...</p>');
	}

	this.FlightResultsFilter.Done = function (oXML) {
		var sHTML = this.GetTagValue(oXML, 'FlightResultsFilterResult');
		f.SetHTML('divFlightResults', sHTML);
	}

}
