﻿$jq.namespace('MatchCore');

MatchCore.AjaxGeo = function() {
	var loadingOpt = { value : '', text : 'Loading...' };
	var countryCodesWithPostal = [1, 2, 224];
	
	var selectedCountryCode = 224;
	var selectedStateCode = -1;
	var selectedCityCode = -1;
	
	var countryList;
	var stateList;
	var cityList; 
	var postalCodeField;
	
	var countriesJson = null;
	var statesJson = null;
	
	var country_changed_handlers = [];
	
	var service = new MatchCore.ServiceProxy(MatchCore.Application.resolveUrl('~/rest/MainService.ashx'));
	
	var country_changed = function() {
		selectedStateCode = -1;
		selectedCityCode = -1;
		
		checkPostalVisibility();
		
		fillStateList();
	};
	
	var state_changed = function() {
		selectedCityCode = -1;
		fillCityList();
	};
	
	var get_selectedCountryCode = function() {
		var countryCode = $jq('option:selected', countryList).val();
		
		if (countryCode == null)	//get first value
			countryCode = $jq('option', countryList).val();
		
		return countryCode;
	};
	
	var fillCountryList = function() {
		if (countryList)
			service.invoke({
				method : 'GetAllCountries',
				success : handle_fillCountryList
			});
	};
	
	var checkPostalVisibility = function() {
		if (postalCodeField) {
			var countryCode = get_selectedCountryCode();
			var showPostal = false;
			
			for (var i = 0; i < countryCodesWithPostal.length; i++) {
				if (countryCode.toString() == countryCodesWithPostal[i].toString()) {
					showPostal = true;
					break;
				}
			}
			
			if (showPostal)
				$jq(postalCodeField).parents('.postalPane').show();
			else
				$jq(postalCodeField).parents('.postalPane').hide();
		}
	};
	
	var handle_fillCountryList = function(response) {
		$jq(countryList).clearOptions();
		
		if (response[0].Code) {
			countriesJson = response;
			
			for (var i = 0; i < response.length; i++) {
				$jq(countryList).addOption(
					{ value : response[i].Code, text : response[i].Name, selected : (response[i].Code.toString() == selectedCountryCode.toString()) }
				);
			}
			
			checkPostalVisibility();
			
			fillStateList();
		}
		else alert('Error loading countries');
	};
	
	var fillStateList = function() {
		if (stateList) {
			$jq(stateList).clearOptions();
			$jq(stateList).addOption(loadingOpt);
			
			$jq(cityList).clearOptions();
			$jq(cityList).addOption(loadingOpt);
			
			var countryCode = get_selectedCountryCode();
				
			var currentCountry = findCountry(countryCode);
			
			if (currentCountry != null) {
				if (currentCountry.HasStates) {
					$jq(stateList).parents('.statePane').show();
					
					service.invoke({
						method : 'GetStatesByCountryCode',
						data : {
							'countryCode' : countryCode
						},
						success : handle_fillStateList
					});
				}
				else {
					$jq(stateList).parents('.statePane').hide();

					fillCityListNoStates();
				}
			}
		}
	};
	
	var handle_fillStateList = function(response) {
		$jq(stateList).clearOptions();
		
		if (response[0].Code) {
			statesJson = response;
			
			for (var i = 0; i < response.length; i++) {
				$jq(stateList).addOption(
					{ value : response[i].Code, text : response[i].Name, selected : (response[i].Code.toString() == selectedStateCode.toString()) }
				);
			}
			
			fillCityList();
		}
		else alert('Error loading states');
	};
	
	var fillCityList = function() {
		if (cityList) {
			$jq(cityList).clearOptions();
			$jq(cityList).addOption(loadingOpt);
			
			var countryCode = $jq('option:selected', countryList).val();
			
			if (countryCode == null)	//get first value
				countryCode = $jq('option', countryList).val();
				
			var stateCode = $jq('option:selected', stateList).val();
			
			if (stateCode == null)	//get first value
				stateCode = $jq('option', stateList).val();
			
			service.invoke({
				method : 'GetCitiesByStateCodeAndCountryCode',
				data : {
					'stateCode' : stateCode,
					'countryCode' : countryCode
				},
				success : handle_fillCityList
			});
		}
	};
	
	var fillCityListNoStates = function() {
		if (cityList) {
			var countryCode = $jq('option:selected', countryList).val();
			
			if (countryCode == null)	//get first value
				countryCode = $jq('option', countryList).val();
			
			service.invoke({
				method : 'GetCitiesByCountryCode',
				data : {
					'countryCode' : countryCode
				},
				success : handle_fillCityList
			});
		}
	};
	
	var handle_fillCityList = function(response) {
		$jq(cityList).clearOptions();
		
		for (var i = 0; i < response.length; i++) {
			$jq(cityList).addOption(
				{ value : response[i].Code, text : response[i].Name, selected : (response[i].Code.toString() == selectedCityCode.toString()) }
			);
		}
	};
	
	var findCountry = function(code) {
		for (var i = 0; i < countriesJson.length; i++) {
			if (countriesJson[i].Code == code)
				return countriesJson[i];
		}
		
		return null;
	};
	
	var findState = function(code) {
		for (var i = 0; i < statesJson.length; i++) {
			if (statesJson[i].Code == code)
				return statesJson[i];
		}
		
		return null;
	};
	
	return {
		init : function(config) {
			postalCodeField = $jq(config.postalField);
			countryList = config.countryList;
			stateList = config.stateList;
			cityList = config.cityList;
			
			/*
			if (config.selectedCountryCode)
				selectedCountryCode = config.selectedCountryCode;
				
			if (config.selectedCityCode)
				selectedCityCode = config.selectedCityCode;
				
			if (config.selectedStateCode)
				selectedStateCode = config.selectedStateCode;
			*/
			
			//wireup event handlers
			$jq(countryList).bind('change', country_changed);
			$jq(stateList).bind('change', state_changed);
		},
		
		render : function() {
			//render waiting state
			
			$jq(countryList).addOption(loadingOpt);
			$jq(stateList).addOption(loadingOpt);
			$jq(cityList).addOption(loadingOpt);
			
			if (countryList)
				fillCountryList();
		},
		
		get_countryList : function() {
			return countryList;
		}
	};
	
};
