﻿Type.registerNamespace("Apaq.UI");

Apaq.UI.AdvancedSearch = function() {
	this._txtDepartureCity;
	this._ddlTouristicRegion;
	this._ddlTouristicRegionChangeHandler = null;
	this._ddlSitePopulatedHandler = null;
	this._ddlActivityPopulatedHandler = null;
	this._ddlSite;
	this._ddlActivity;

	this._ddlTheme;
	this._ddlMonth;
	this._ddlBudget;
	this._ddlTrip;
	this._ddlGroupSize;
	this._ddlTravelAgency;

	Apaq.UI.AdvancedSearch.initializeBase(this);

	// register the object as disposable, so the application will call it's dispose method when needed
	if (typeof (Sys) !== "undefined") Sys.Application.registerDisposableObject(this);
}

//Departure class public methods
Apaq.UI.AdvancedSearch.prototype =
{
	//Constructor: All this class fields (described above) are populated from the parameters of this constructor. The parameters contain the ClientId ot Id of the element
	//              and $get call is used to get the actual element/control
	initialize: function(txtDepartureCity, ddlTouristicRegion, ddlSite, ddlActivity, ddlTheme, ddlMonth, ddlBudget, ddlTrip, ddlGroupSize, ddlTravelAgency) {
		Apaq.UI.AdvancedSearch.callBaseMethod(this, 'initialize');
		if (!txtDepartureCity || !ddlSite || !ddlTouristicRegion || !ddlActivity || !ddlTheme || !ddlMonth || !ddlBudget || !ddlTrip || !ddlGroupSize || !ddlTravelAgency)
			throw "Invalid arguments";

		this._txtDepartureCity = $get(txtDepartureCity);
		this._ddlTouristicRegion = $get(ddlTouristicRegion);
		this._ddlSite = $get(ddlSite);
		this._ddlActivity = $get(ddlActivity);

		this._ddlTheme = $get(ddlTheme);
		this._ddlMonth = $get(ddlMonth);
		this._ddlBudget = $get(ddlBudget);
		this._ddlTrip = $get(ddlTrip);
		this._ddlGroupSize = $get(ddlGroupSize);
		this._ddlTravelAgency = $get(ddlTravelAgency);

		this._ddlSite.disabled = true;
		this._ddlActivity.disabled = true;


		//Create the delegates
		this._ddlTouristicRegionChangeHandler = Function.createDelegate(this, this.onRegionChange);
		this._ddlSitePopulatedHandler = Function.createDelegate(this, this.onSitePopulated);
		this._ddlActivityPopulatedHandler = Function.createDelegate(this, this.onActivityPopulated);

		//associate the control events to their handlers
		$addHandler(this._ddlTouristicRegion, "change", this._ddlTouristicRegionChangeHandler);

		$find("cddSite").add_populated(this._ddlSitePopulatedHandler);
		$find("cddActivity").add_populated(this._ddlActivityPopulatedHandler);
	},

	//dispose: remove the handlers
	dispose: function() {
		if ($find("cddSite"))
		    $find("cddSite").remove_populated(this.onSitePopulated);
		if ($find("cddActivity"))
		    $find("cddActivity").remove_populated(this.onActivityPopulated);
		if (this._ddlTouristicRegionChangeHandler) {
			$removeHandler(this._ddlTouristicRegion, "change", this._ddlTouristicRegionChangeHandler);
			this._ddlTouristicRegionChangeHandler = null;
		}
		Apaq.UI.AdvancedSearch.callBaseMethod(this, 'dispose');
	},

	onRegionChange: function() {
		if (this._ddlTouristicRegion.value == '-1' || this._ddlTouristicRegion.value == Apaq.UI.Helper.GuidEmpty() || this._ddlTouristicRegion.value == Apaq.UI.Helper.GuidAll()) {
			this._ddlSite.disabled = true;
			this._ddlActivity.disabled = true;
		}
		else {
			this._ddlSite.disabled = false;
			this._ddlActivity.disabled = false;
		}
	},

	onSitePopulated: function() {
		if (this._ddlSite.length == 1 && this._ddlSite.options[0].value == "")
			this._ddlSite.disabled = true;
		else
			this._ddlSite.disabled = false;
	},

	onActivityPopulated: function() {
		if (this._ddlActivity.length == 1 && this._ddlActivity.options[0].value == "")
			this._ddlActivity.disabled = true;
		else
			this._ddlActivity.disabled = false;
	},

	hideSearchBox: function() {
		this.rePopulateSummaryPanel($get('btnShowHideSearch'));
		TogglePanelWithSummary($get('btnShowHideSearch'), 'divSearchBox', 'divSearchSummary', 'hide');
		$get('btnShowHideSearch').focus();
	},

	rePopulateSummaryPanel: function(showHideButton) {
		if ($get('divSearchSummary').style.display == "none") //repopluate before show
		{
			showHideButton.innerHTML = Apaq.UI.Helper.formatString("Modifier ma recherche", "Modify my search");

			$get('spanDepartureCity').innerHTML = this._txtDepartureCity.value;
			$get('spanDestination').innerHTML = this._ddlTouristicRegion.options[this._ddlTouristicRegion.selectedIndex].text;
			$get('spanTheme').innerHTML = this._ddlTheme.options[this._ddlTheme.selectedIndex].text;
			$get('spanMonth').innerHTML = this._ddlMonth.options[this._ddlMonth.selectedIndex].text;
			$get('spanSite').innerHTML = this._ddlSite.options[this._ddlSite.selectedIndex].text;
			$get('spanActivity').innerHTML = this._ddlActivity.options[this._ddlActivity.selectedIndex].text;
			$get('spanBudget').innerHTML = this._ddlBudget.options[this._ddlBudget.selectedIndex].text;
			$get('spanTrip').innerHTML = this._ddlTrip.options[this._ddlTrip.selectedIndex].text;
			$get('spanGroup').innerHTML = this._ddlGroupSize.options[this._ddlGroupSize.selectedIndex].text;
			$get('spanAgency').innerHTML = this._ddlTravelAgency.options[this._ddlTravelAgency.selectedIndex].text;

			$get('divSite').style.display = 'none';
			$get('divActivity').style.display = 'none';
			if (this._ddlTouristicRegion.selectedIndex != 0) {
				if (this._ddlSite.options[this._ddlSite.selectedIndex].value != '')
					$get('divSite').style.display = 'inline';
				if (this._ddlActivity.options[this._ddlActivity.selectedIndex].value != '')
					$get('divActivity').style.display = 'inline';
			}

		}
		else {
			showHideButton.innerHTML = Apaq.UI.Helper.formatString("Fermer la recherche", "Close the search");
			//fire destination change event
			Apaq.UI.Helper.fireddlChangeEvent(this._ddlTouristicRegion);

		}
	}


}

Apaq.UI.AdvancedSearch.registerClass('Apaq.UI.AdvancedSearch', Sys.Component, Sys.IDisposable);

// Notify ScriptManager that this is the end of the script.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


