
(function($, undefined) {

	var _imagesUrl = lf.getConfig("imagesUrl");
	var _ajaxUrl   = lf.getConfig("ajaxUrl");
	
	lf.locationSearch = {

		searchCallbackFn: null,
	    showPopupCloseButton: true,

		setSearchCallbackFunction: function(fn) {
			if(typeof fn == "function") {
				this.searchCallbackFn = fn;
			}
		},

		
		runSearchCallbackFunction: function() {
			if(typeof this.searchCallbackFn == "function") {
				this.searchCallbackFn();
			}
		},


		showSpinnyIcon: function() {
			$("#locSearchResults").html('<img src="' + _imagesUrl + 'spinny_blue.gif" />');
		},


		setLocationIdForType: function(locId, locType) {
			switch(locType) {
			case '1':  // country
				$("#ad_loc_country").val(locId);
				$("#ad_loc_state").val();
				$("#ad_loc_city").val();
				break;

			case '2':  // state
				$("#ad_loc_state").val(locId);
				$("#ad_loc_city").val();
				break;

			case '3':  // city
				$("#ad_loc_city").val(locId);
				break;

			default:
				$("#ad_loc_country").val('');
				$("#ad_loc_state").val('');
				$("#ad_loc_city").val('');
			}

			this.runSearchCallbackFunction();
		},


		searchSiblings: function(locId) {
			this.showSpinnyIcon();
			var that = this;
			
			var locationContent = $.ajax({
				type:     "GET",
				url:      _ajaxUrl + "shoes/location_search_siblings.php",
				dataType: 'json',
				data: ({ 
					'locId':           locId,
					'resultTemplate':  'shoes/loc/location_results',
					'showCloseButton': this.showPopupCloseButton
				}),

				success: function(msg){
					that.setFilterLink(msg.locationString);
					$("#locSearchResults").html(msg.locResults);
					that.setLocationIdForType(msg.loc_id, msg.loc_type);
				}
			});
		},
			  

		drillDown: function(parentLocId) {
			var that = this;
			$("#ad_loc_id").val(parentLocId);
			this.showSpinnyIcon();
			
			var locationContent = $.ajax({
				type:     'GET',
				url:      _ajaxUrl + 'shoes/location_search_children.php',
				dataType: 'json',
				data: ({ 
					'parentLocId': parentLocId,
					'resultTemplate': 'shoes/loc/location_results',
					'showCloseButton': this.showPopupCloseButton
				}),

				success: function(msg){
				that.setFilterLink(msg.locationString);
				$("#locSearchResults").html(msg.locResults);
				that.setLocationIdForType(msg.loc_id, msg.loc_type);
			}
			});
		},


		goBack: function(currentLocId, getLocType) {
			this.showSpinnyIcon();
			var that = this;
			
			var locationContent = $.ajax({
				type:     'GET',
				url:      _ajaxUrl + 'shoes/location_search_go_back.php',
				dataType: 'json',
				data: ({ 
					'locId'          : currentLocId,
					'getLocType'     : getLocType,
					'resultTemplate' : 'shoes/loc/location_results',
					'showCloseButton': this.showPopupCloseButton
				}),

				success: function(msg){
					that.setFilterLink(msg.locationString);
					$("#locSearchResults").html(msg.locResults);
					that.setLocationIdForType(msg.loc_id, msg.loc_type);
				}
			});
		},
			  

		selectCity: function(locId) {
			var that = this;
			$("#ad_loc_id").val(locId);
			this.hideLocPopup();

			var locationContent = $.ajax({
				type:     'GET',
				url:      _ajaxUrl + 'shoes/location_search_select_city.php',
				dataType: 'json',
				data: ({ 
					'locId': locId
				}),

				success: function(msg){
				that.setFilterLink(msg.locationString);
				that.setLocationIdForType(msg.loc_id, msg.loc_type);
			}
			});
		},


		/**
		 * Gets a list of locations and populates the popup,
		 * but does not update the filter link
		 */
		quietInit: function(locId) {
			var locationContent = $.ajax({
				type:     "GET",
				url:      _ajaxUrl + "shoes/location_search_siblings.php",
				dataType: 'json',
				data: ({ 
					'locId':           locId,
					'resultTemplate':  'shoes/loc/location_results',
					'showCloseButton': this.showPopupCloseButton
				}),

				success: function(msg){
				$("#locSearchResults").html(msg.locResults);
				this.setLocationIdForType(msg.loc_id, msg.loc_type);
			}
			});
		},

			  

		setFilterLink: function(locName) {
			var nameString = '';

			if(locName.length > 30) {
				nameString = "<span class='font10'>"+locName.substring(0, 30)+"...</span>";
			} else if(locName.length > 24) {
				nameString = "<span class='font10'>"+locName+"</span>";
			} else if(locName.length > 20) {
				nameString = "<span class='font11'>"+locName+"</span>";
			} else {
				nameString = locName;
			}

			$("#locSearchAnchor").html(nameString);
		},  
			  

		hidePopupCloseButton: function() {
			this.showPopupCloseButton = false;
		},


		displayLocPopup: function() {
			var locPopup = $("#locPopup");
			var filterAnchorPosition = $("#locSearchAnchor").offset();
			locPopup.css( { 'position': 'absolute',
				'top': filterAnchorPosition.top - 25,
				'left': filterAnchorPosition.left + $("#locSearchAnchor").width() + 10
			});

			locPopup.fadeIn("fast");
		},


		hideLocPopup: function() {
			$("#locPopup").hide("fast");
		}
	};
			
})(jQuery);

