﻿var MemberSearchModule = MemberSearchModule || {
	_evalScripts: function(moduleConainerId) {
		var moduleConainer = $(moduleConainerId);
		if (moduleConainer)
			$A(moduleConainer.getElementsByTagName("SCRIPT")).each(function(script) { eval(script.innerHTML); });
	},
	clearMarkupReferences: function(markupReferences) {
		markupReferences.each(function(element) {
			if (element)
				element.removeAttribute("id");
		});
	},
	registerClientStateBag: function() {
		var stateBagId = 'clientStateBag_' + Math.random();
		MemberSearchModule.Tools._currentStateBagId = stateBagId;
	},
	registerAsyncHandler: function(moduleConainerId) {
		var stateBagId = MemberSearchModule.Tools._currentStateBagId;

		Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(
			function(sender, args) {
				MemberSearchModule.Tools.initializeSearchRequest(stateBagId, args.get_postBackElement());
			});

		Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
			function() {
				MemberSearchModule.Tools.endSearchRequest(stateBagId);
				MemberSearchModule._evalScripts(moduleConainerId);
			});
	}
};

MemberSearchModule.Tools = MemberSearchModule.Tools || {
	_usaCountryId: "840",
	_stateBags: {},
	_currentStateBagId: null,
	initModule: function(moduleContainerId) {
		var module = this._getCurStateBag().requestInfo._module = $(moduleContainerId);
		module.parentNode.className = null;
		module.parentNode.parentNode.className = null;
		return [module];
	},
	initFiltersPanel: function(filtersPanelId, showFiltersBtnId, showTitle, hideTitle, showCss, hideCss) {
		var linkBtn = $(showFiltersBtnId);
		var panel = $(filtersPanelId);

		with (this._getCurStateBag().filtersInfo) {
			_linkBtn = linkBtn;
			_panel = panel;
			_panelHTML = _panelHTML || panel.innerHTML;
			_showTitle = showTitle;
			_hideTitle = hideTitle;
			_showCss = showCss;
			_hideCss = hideCss;
			_linkBtn.onclick = this._toggleFiltersVisibility.bind(this, this._currentStateBagId);
			if (_visible)
				this._visibleFilters(true, this._currentStateBagId);
			else
				_linkBtn.addClassName(showCss);
		}
		return [linkBtn, panel];
	},
	initAgeSlider: function(ageSliderContainerId) {
		with (this._getCurStateBag().filtersInfo) {
			if (!_resetSliderJS) {
				var sliderIdSuf = 'sliderid';
				var sliderIdHidden = $(ageSliderContainerId).descendants().find(function(element) {
					return element.tagName.toLowerCase() == 'input'
						&& element.type.toLowerCase() == 'hidden'
						&& element.id
						&& element.id.length >= sliderIdSuf.length
						&& element.id.substr(element.id.length - sliderIdSuf.length).toLowerCase() == sliderIdSuf;
				});
				_resetSliderJS = "if (window." + sliderIdHidden.value + ") window." + sliderIdHidden.value + ".resetSlider();";
			}
		}
	},
	//Called from fml and from this file
	initCountriesAndStates: function(countriesContainerId, statesContainerId, selectDefaultCountry) {
		var countriesContainer = $(countriesContainerId);
		var statesContainer = $(statesContainerId);

		if (!countriesContainer || !statesContainer) {
			this._selectDefaultCountry(selectDefaultCountry);
			return [];
		}

		with (this._getCurStateBag().statesInfo) {
			_countriesContainerId = countriesContainerId;
			_statesContainerId = statesContainerId;
			_countries = this._find(countriesContainer, 'select');
			_countries.onchange = this._visibleStates.bind(this, this._currentStateBagId);
			_statesContainer = statesContainer;
			_states = this._find(statesContainer, 'select');
			this._visibleStates(this._currentStateBagId);
		}
		this._selectDefaultCountry(selectDefaultCountry);

		return [countriesContainer, statesContainer];
	},
	_selectDefaultCountry: function(selectDefaultCountry) {
		if (!selectDefaultCountry) return;
		with (this._getCurStateBag().statesInfo) {
			if (_defCountryIdx == null)
				_defCountryIdx = this._getDefaultCountryIdx(_countries);
			_countries.selectedIndex = _defCountryIdx;
			this._visibleStates(this._currentStateBagId);
		}
	},
	_getDefaultCountryIdx: function(countries) {
		if (!countries.getAttribute) return 0;
		var defaultCountryId = countries.getAttribute('defaultCountryId');
		if (defaultCountryId) {
			for (i = 0; i < countries.options.length; i++) {
				if (countries.options[i].value == defaultCountryId) return i;
			}
		}
		return 0;
	},
	initCurrentCommunityTab: function(tabContainerId, currentCommunityTitle) {
		var tabContainer = $(tabContainerId);
		if (tabContainer) {
			this._find(tabContainer, 'a').innerHTML = currentCommunityTitle;
			return [tabContainer];
		}
		return [];
	},
	initSearchResultsHeader: function(searchResultsTitleId, searchTermContainerId, searchBarContainerId) {
		var searchResultsTitle = $(searchResultsTitleId);
		var searchTerm = $(searchTermContainerId);
		var searchBarContainer = $(searchBarContainerId);

		if (this._getCurStateBag().requestInfo._membersSearched) {
			var term = this._find(searchBarContainer, 'input').value;
			searchTerm.innerHTML = term;
			searchResultsTitle[term == '' ? "hide" : "show"]();
		}

		return [searchResultsTitle, searchTerm, searchBarContainer];
	},
	initSearchProgress: function(searchBtnContainerId, progressMsgContainerId, memberListContainerId, memberListProgressId, sortBarContainerId) {
		var searchBtn = $(searchBtnContainerId);
		var progressMsg = $(progressMsgContainerId);
		var memberList = memberListContainerId ? $(memberListContainerId) : null;
		var memberListProgress = memberListProgressId ? $(memberListProgressId) : null;
		var sortBar = sortBarContainerId ? $(sortBarContainerId) : null;

		with (this._getCurStateBag().progressInfo) {
			_searchBtn = searchBtn;
			_progressMsg = progressMsg;
			_memberList = memberList;
			_memberListProgress = memberListProgress;
			_sortBar = sortBar;
		}

		return [searchBtn, progressMsg, memberList, memberListProgress];
	},
	initSortBar: function(sortBarContainerId, visible) {
		var sortBar = $(sortBarContainerId);
		if (!visible || !this._getCurStateBag().requestInfo._membersSearched)
			sortBar.hide();
		return [sortBar];
	},
	initializeSearchRequest: function(stateBagId, postBackElement) {
		with (this._getStateBag(stateBagId)) {
			with (requestInfo) {
				if (typeof (_postBackIds[postBackElement.id]) == 'undefined') {
					var pbe = $(postBackElement);
					_postBackIds[postBackElement.id] = this._isChildOf(pbe, _module);
					_postBackIds[postBackElement.id] = _postBackIds[postBackElement.id] || this._isChildOf(_module, pbe);
				}
				if (!_postBackIds[postBackElement.id]) return;
			}
			with (progressInfo) {
				var fromSearchBtn = false;
				if (_searchBtn && _progressMsg) {
					var searchBtn = _searchBtn;
					var progressMsg = _progressMsg;
					_searchBtn = null;
					_progressMsg = null;
					fromSearchBtn = Element.descendantOf(postBackElement, searchBtn);
					if (fromSearchBtn)
						setTimeout(function() { searchBtn.hide(); progressMsg.show(); }, 100);
				}
				if (!fromSearchBtn && _memberList && _memberListProgress) {
					var memberList = _memberList;
					var memberListProgress = _memberListProgress;
					var sortBar = _sortBar;
					_memberList = null;
					_memberListProgress = null;
					_sortBar = null;
					setTimeout(function() { memberList.hide(); if (sortBar) sortBar.hide(); memberListProgress.show(); }, 100);
				}
			}
		}
	},
	_isChildOf: function(parent, child) {
		var result = false;
		parent.descendants().each(function(element) {
			result = element == child;
			if (result) throw $break;
		});
		return result;
	},
	endSearchRequest: function(stateBagId) {
		this._currentStateBagId = stateBagId;
		this._getStateBag(stateBagId).requestInfo._membersSearched = true;
	},
	_toggleFiltersVisibility: function(stateBagId) {
		this._visibleFilters(!this._getCurStateBag().filtersInfo._panel.visible(), stateBagId, true);
	},
	_visibleFilters: function(visible, stateBagId, showFiltersClick) {
		with (this._getStateBag(stateBagId).filtersInfo) {
			_panel[visible ? "show" : "hide"]();
			_visible = _panel.visible();
			if (_visible) {
				if (!_isSliderReseted) {
					eval(_resetSliderJS);
					_isSliderReseted = true;
				}
				var si = this._getStateBag(stateBagId).statesInfo;
				MemberSearchModule.clearMarkupReferences(
					this.initCountriesAndStates(si._countriesContainerId, si._statesContainerId, showFiltersClick));
			} else {
				_panel.innerHTML = _panelHTML;
				_isSliderReseted = false;
			}

			_linkBtn.innerHTML = _visible ? _hideTitle : _showTitle;
			_linkBtn[_visible ? "addClassName" : "removeClassName"](_hideCss);
			_linkBtn[_visible ? "removeClassName" : "addClassName"](_showCss);
		}
	},
	_visibleStates: function(stateBagId) {
		with (this._getStateBag(stateBagId).statesInfo) {
			if (!_statesContainer.visible())
				_states.selectedIndex = 0;
			if (_countries.options.length > 0)
				_statesContainer[_countries.options[_countries.selectedIndex].value == this._usaCountryId ? "show" : "hide"]();
		}
	},
	_getCurStateBag: function() {
		return this._getStateBag(this._currentStateBagId);
	},
	_getStateBag: function(stateBagId) {
		if (!this._stateBags[stateBagId]) {
			this._stateBags[stateBagId] = {
				filtersInfo: {
					_visible: false,
					_panel: null,
					_panelHTML: null,
					_isSliderReseted: false,
					_resetSliderJS: null,
					_linkBtn: null,
					_showTitle: "",
					_hideTitle: "",
					_showCss: null,
					_hideCss: null
				},
				statesInfo: {
					_countriesContainerId: null,
					_statesContainerId: null,
					_countries: null,
					_defCountryIdx: null,
					_statesContainer: null,
					_states: null
				},
				requestInfo: {
					_module: null,
					_postBackIds: {},
					_membersSearched: false
				},
				progressInfo: {
					_searchBtn: null,
					_progressMsg: null
				}
			};
		}
		return this._stateBags[stateBagId];
	},
	_find: function(container, tagName) {
		tagName = tagName.toLowerCase();
		return container.descendants().find(function(element) { return element.tagName.toLowerCase() == tagName; });
	}
};

// [AtlasScript]

if (typeof (Sys) != "undefined")
	Sys.Application.notifyScriptLoaded();

// [/AtlasScript]