var tab_actions = {
  
  options: {
    searchPlaceholder: 'City, Zip, Cuisine Type or Restaurant Name',
    function_name: ''
  },
  
  tabClicked: function(function_name){
    
    this.options.function_name = function_name;
    
    log('setting function_name: ' + function_name + ' | ' + this.options.function_name);
    
    switch(function_name)
    {
      case 'restaurants-tab':
        log('tab_action.tabClicked | clicked: restaurants');
        
        // add geo flag to form
        jQuery('#use_geo').attr('value', 1);
        
        // set featured flag
        jQuery('#featured').val(0);
        
        // set uid
        jQuery('#use_uid').val(0);
        
        // clear favs flag
        jQuery('#favs').val(0);
        
        // get search
        this.runSearch();
        
        break;
      case 'featured-tab':
        log('tab_action.tabClicked | clicked: featured');
        
        // add geo flag to form
        jQuery('#use_geo').attr('value', 1);
        
        // set featured flag
        jQuery('#featured').val(1);
        
        // set uid
        jQuery('#use_uid').val(0);
        
        // clear favs flag
        jQuery('#favs').val(0);
        
        // get search
        this.runSearch();
        
        break;
      case 'myrestaurants-tab':
        log('tab_action.tabClicked | clicked: myrestaurants');
        
        // clear geo flag from form
        jQuery('#use_geo').attr('value', 0);
        
        // set featured flag
        jQuery('#featured').val(0);
        
        // set uid
        jQuery('#use_uid').val(1);
        
        // clear favs flag
        jQuery('#favs').val(0);
        
        // get search
        this.runSearch();
        
        break;
      case 'favourites-tab':
        log('tab_action.tabClicked | clicked: favourites');
        
        // clear geo flag from form
        jQuery('#use_geo').attr('value', 0);
        
        // set featured flag
        jQuery('#featured').val(0);
        
        // set uid
        jQuery('#use_uid').val(1);
        
        // set favs flag
        jQuery('#favs').val(1);
        
        // get search
        this.runSearch();
        
        break;
    }
    
  },
  
  runSearch: function(){
    if(advSearchFlag)
    // run adv search
    {
      this.runAdvSearch();
    }
    else
    {
      log(jQuery('#searchForm').attr('action'));
    
      // get search query
      if(jQuery('#search_str').val() == this.options.searchPlaceholder)
      {
        var search_str = '';
      }
      else
      {
        var search_str = jQuery('#search_str').val();
      }
    
      // save cookies
      setCookies('restaurants', this.options.function_name, search_str, jQuery('#current_page').val());
    
      // if search string empty, try to add geo data
      // if(search_str == '' && jQuery('#use_geo').val() == 1)
      if(jQuery('#use_geo').val() == 1)
      {
        // see if city overridden
        if(jQuery('#cityOverride').val() != '')
        {
          log('setting city override cookie: '+jQuery('#cityOverride').val());
          
          // add city cookie (save for session)
          jQuery.cookie('SEARCH_CITY_OVERRIDE', jQuery('#cityOverride').val(), {path: "/", expires: 0});
          
          // split string
          var city_arr = jQuery('#cityOverride').val().split(', ');
          
          search_str += ' @city ' + city_arr[0] + ' @state ' + city_arr[1];
        }
        else
        {
          cCode = geoip_country_code();
          
          search_str += ' @city ' + geoip_city() + ' @state ' + geoip_region();
        }
        
      }
    
      // add uid to string if searching friends
      if(jQuery('#use_uid').val() == 1)
      {
        search_str = '@memberid _uid' + jQuery('#uid').val() + ' ' + search_str;
      }
    
      // set featured search if required
      if(jQuery('#featured').val() == 1)
      {
        search_str += ' @restaurantlevel _rlev3';
      }
      
      // build search obj
      var search_obj = {
        favs: jQuery('#favs').val(),
        query: search_str,
        current_page: jQuery('#current_page').val(),
      };

      this.makeSearchCall(jQuery('#searchForm').attr('action'), search_obj);
    }
  },
  
  /**
   * runs an advanced search
   */
  runAdvSearch: function(){
    
    var search_str = '';
    
    // if search string empty, try to add geo data
    // if(search_str == '' && jQuery('#use_geo').val() == 1)
    if(jQuery('#use_geo').val() == 1)
    {
      // see if city overridden
      if(jQuery('#cityOverride').val() != '')
      {
        // split string
        var city_arr = jQuery('#cityOverride').val().split(', ');
        
        search_str += ' @city ' + city_arr[0] + ' @state ' + city_arr[1];
      }
      else
      {
        cCode = geoip_country_code();
        
        search_str += ' @city ' + geoip_city() + ' @state ' + geoip_region();
      }  
    }
    
    // add search string if set
    if(jQuery('#search_str').val() != this.options.searchPlaceholder)
    {
      search_str = jQuery('#search_str').val() + search_str;
    }
    
    advSearchObject.query = search_str;
    this.makeSearchCall('/v2/server/search/restaurantsadv.json', advSearchObject);
  },
  
  /**
   * makes the actual ajax search call
   */
  makeSearchCall: function(search_url, search_obj){
    // show loading
    jQuery('#searchResults').html('<h3>Loading...</h3>');
  
    jQuery.post(
      search_url,
      search_obj,
      function(data){
      
        // get matches
        var matches = data.matches;
      
        // clear search result area
        jQuery('#searchResults').html('');
      
        if(matches && matches.length > 0)
        {
          // parse data
          for(var i = 0; i < matches.length; i++)
          {
            if(matches[i] != null_var)
            {
              // Init city and state string
              var city_string  = '';
              var state_string = '';
              
              // Check if the city has a value
              if(matches[i].city != "")
                city_string = matches[i].city.gsub(' ','-') + '/';
              
              // Check if the state has a value
              if(matches[i].state != "")
                state_string = matches[i].state.gsub(' ','-') + '/';
              
              var link = '/restaurant/' + matches[i].restaurantid + '/' +  city_string + state_string + matches[i].title.gsub(' ','-').replace(/&/gi,'and');
              
              var rating = '';
            
              if(matches[i].rating != null_var)
              {
                for(var j = 0; j < 5; j++)
                {
                  if(j < matches[i].rating)
                  {
          					rating += '<li><img alt="rating" src="/images/v2/starActive.jpg" /></li>';
          				}
          				else
          				{
          					rating += '<li><img alt="rating" src="/images/v2/star.jpg" /></li>';
          				}
                }
          		}
              
              if(matches[i].restaurantlevel == 3)
              {
                var html =	'<div class="resultListing">'
                  + ' <a href="' + link + '"><strong>' + matches[i].title + '</strong></a>*'
                  +	' <p class="location">'
                  +	'  <span class="city">' + matches[i].city + '</span>,'
                  +	'  <span class="state">' + matches[i].state + '</span>'
                  +	' </p>'
                  + ' <ul class="rating">' + rating + '</ul>'
                  +	' <p class="subtle address">' + matches[i].address + '</p>'
                  + ' <p class="subtle cuisineType">' + matches[i].cuisine_type + '</p>'
                  +	' <h5>Food Connect Featured Listing</h5>'
                  + '</div>';
              }
              else
              {
                var html =	'<div class="resultListing">'
                  + '<a href="' + link + '"><strong>' + matches[i].title + '</strong></a>'
                  +	'<p class="location">'
                  +	'	<span class="city">' + matches[i].city + '</span>,'
                  +	'	<span class="state">' + matches[i].state + '</span>'
                  +	'</p>'
                  + ' <ul class="rating">' + rating + '</ul>'
                  +	'<p class="subtle address">' + matches[i].address + '</p>'
                  + '<p class="subtle cuisineType">' + matches[i].cuisine_type + '</p>'
                  + '</div>';
              }
            
              jQuery('#searchResults').append(jQuery(html));
              
              // insert text ad after every 3rd ad (skip first)
              if(i == 2 || i == 7)
              {
                var ad_frame = '<div class="texAdWrap" class="resultListing"><iframe src="/includes/ad_server/searchAds/restaurantSearchTextAd.php" width="100%" frameborder="0"></iframe></div>';
                
                jQuery('#searchResults').append(ad_frame);
              }
            }
          
          }
        
          // add pagers
          jQuery.tabs_v2_pager.init(data.searchInfo.total, data.searchInfo.resultsPerPage, data.searchInfo.rows);
        }
        else
        {
          emptySearch();
        }
      },
      'json'
    );
  },
  
  /**
   * runs when page initially loaded.
   */
  loadPage: function(){
    
    log('restaurants.js: loadPage');
    
    // check for cookies
    if(jQuery('#search_str').val() == this.options.searchPlaceholder && jQuery.cookie('SEARCH_ZONE') == 'restaurants' && jQuery('#' + jQuery.cookie('SEARCH_TAB')).length == 1)
    {
      // set tab active
      // make all inactive
      //jQuery('div.tab').removeClass('active');

      // make this active
      //jQuery('#' + jQuery.cookie('SEARCH_TAB')).addClass('active');
      
      // set search str
      var cookie_search_str = (jQuery.cookie('SEARCH_STRING') != 'null' ? jQuery.cookie('SEARCH_STRING') : this.options.searchPlaceholder);
      jQuery('#search_str').val(cookie_search_str);
      jQuery('#search_str').attr(cookie_search_str);
      
      // set page
      jQuery('#current_page').val(jQuery.cookie('SEARCH_PAGE'));
    }
    
    // click active tab
    jQuery('.tab.active').trigger('click');
    
  }
};
