var imagesUrl = getImagesLoc();
var ajaxUrl   = getAjaxLoc();

var LocationSearch = {
	searchCallbackFn: null,
	showPopupCloseButton: true,
	
  setSearchCallbackFunction: function(fn) {
	  if(typeof fn == "function") {
  	  LocationSearch.searchCallbackFn = fn;
	  }
	},
	runSearchCallbackFunction: function() {
	  if(typeof LocationSearch.searchCallbackFn == "function") {
  	  LocationSearch.searchCallbackFn();
	  }
	},
	
	showSpinnyIcon: function() {
    $j("#locSearchResults").html('<img src="'+imagesUrl+'spinny_blue.gif" />');
  },
  
  setLocationIdForType: function(locId, locType) {
    c("setting locId ("+locId+") for locType ("+locType+")");
    switch(locType) {
      case '1':  // country
        $j("#ad_loc_country").val(locId);
        $j("#ad_loc_state").val();
        $j("#ad_loc_city").val();
        break;
        
      case '2':  // state
        $j("#ad_loc_state").val(locId);
        $j("#ad_loc_city").val();
        break;
        
      case '3':  // city
        $j("#ad_loc_city").val(locId);
        break;
        
      default:
        $j("#ad_loc_country").val('');
        $j("#ad_loc_state").val('');
        $j("#ad_loc_city").val('');
    }
  },
  
	searchSiblings: function(locId) {
	  LocationSearch.showSpinnyIcon();
	  
  	var locationContent = $j.ajax({
				type:     "GET",
				url:      ajaxUrl + "ad/location_search_siblings.php",
				dataType: 'json',
				data: ({ 
					'locId':           locId,
					'resultTemplate':  'ad/loc/location_results',
					'showCloseButton': LocationSearch.showPopupCloseButton
				}),
				success: function(msg){
				  c(msg);
				  LocationSearch.setFilterLink(msg.locationString);
				  $j("#locSearchResults").html(msg.locResults);
				  LocationSearch.setLocationIdForType(msg.loc_id, msg.loc_type);
				}
			});
  },
  
  drillDown: function(parentLocId) {
    $j("#ad_loc_id").val(parentLocId);
    LocationSearch.showSpinnyIcon();

    var locationContent = $j.ajax({
			type:     'GET',
			url:      ajaxUrl + 'ad/location_search_children.php',
			dataType: 'json',
			data: ({ 
				'parentLocId': parentLocId,
				'resultTemplate': 'ad/loc/location_results',
				'showCloseButton': LocationSearch.showPopupCloseButton
			}),
			success: function(msg){
			  c(msg);
				LocationSearch.setFilterLink(msg.locationString);
				$j("#locSearchResults").html(msg.locResults);
				LocationSearch.setLocationIdForType(msg.loc_id, msg.loc_type);
			}
		});
  },
  
  goBack: function(currentLocId, getLocType) {
    LocationSearch.showSpinnyIcon();
    
  	var ajaxUrl = getAjaxLoc();
  	var locationContent = $j.ajax({
			type:     'GET',
			url:      ajaxUrl + 'ad/location_search_go_back.php',
			dataType: 'json',
			data: ({ 
				'locId'          : currentLocId,
				'getLocType'     : getLocType,
				'resultTemplate' : 'ad/loc/location_results',
				'showCloseButton': LocationSearch.showPopupCloseButton
			}),
			success: function(msg){
			  c(msg);
         LocationSearch.setFilterLink(msg.locationString);
				$j("#locSearchResults").html(msg.locResults);
				LocationSearch.setLocationIdForType(msg.loc_id, msg.loc_type);
			}
		});
  },
  
  selectCity: function(locId) {
    $j("#ad_loc_id").val(locId);
    LocationSearch.hideLocPopup();
    
    var ajaxUrl = getAjaxLoc();
    var locationContent = $j.ajax({
			type:     'GET',
			url:      ajaxUrl + 'ad/location_search_select_city.php',
			dataType: 'json',
			data: ({ 
				'locId': locId
			}),
			success: function(msg){
			  c(msg);
				LocationSearch.setFilterLink(msg.locationString);
				LocationSearch.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 = $j.ajax({
			type:     "GET",
			url:      ajaxUrl + "ad/location_search_siblings.php",
			dataType: 'json',
			data: ({ 
				'locId':           locId,
				'resultTemplate':  'ad/loc/location_results',
				'showCloseButton': LocationSearch.showPopupCloseButton
			}),
			success: function(msg){
			  c(msg);
			  $j("#locSearchResults").html(msg.locResults);
			  LocationSearch.setLocationIdForType(msg.loc_id, msg.loc_type);
			}
		});
  },
  
  setFilterLink: function(locName) {
	  $j("#locSearchAnchor").html(locName);
  },  
  
  hidePopupCloseButton: function() {
	  LocationSearch.showPopupCloseButton = false;
	},
	
  displayLocPopup: function() {
    var locPopup = $j("#locPopup");
		var filterAnchorPosition = $j("#locSearchAnchor").offset();
    locPopup.css( { 'position': 'absolute',
			              'top': filterAnchorPosition.top - 25,
			              'left': filterAnchorPosition.left + $j("#locSearchAnchor").width() + 10
									});
    locPopup.fadeIn("fast");
  },
  
  hideLocPopup: function() {
    $j("#locPopup").hide("fast");
  }
}