// Function to test whether a string is empty
function isEmpty( inputStr ) { if ( null == inputStr || "" == inputStr ) { return true; } return false; }

var tab_actions = {
  
  options: {
    searchPlaceholder: 'Search for a foodie...',
    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 'friends-tab':
        log('tab_action.tabClicked | clicked: friends');
        
        // add friends flag to form
        jQuery('#friends').attr('value', 1);
        
        // get search
        this.runSearch();
        
        break;
      case 'recent-tab':
        log('tab_action.tabClicked | clicked: recent');
        
        // clear friends flag from form
        jQuery('#friends').attr('value', 1);
        
        // get search
        this.runSearch();
        
        break;
      case 'foodies-tab':
      case 'search-tab':
        log('tab_action.tabClicked | clicked: search');
        
        // clear friends flag from form
        jQuery('#friends').attr('value', 0);
        
        // get search
        this.runSearch();
        
        break;
      case 'invite-tab':
        log('tab_action.tabClicked | clicked: invite');
        
        // clear friends flag from form
        jQuery('#friends').attr('value', 0);

		// clear the pager
	    jQuery(".pagerHolder").html("");
        
		// show loading
        jQuery('#searchResults').html('<h3>Loading...</h3>');
   
        jQuery.ajax({
          type: "POST",
          url: "/invite.php",
          success: function(data) {
            jQuery('#searchResults').empty();
            jQuery('#searchResults').append(data);
          }
         });
        break;
    }
    
  },
  
  runSearch: function(){
    
    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('foodies', this.options.function_name, search_str, jQuery('#current_page').val());
    
    // add uid to string if searching friends
    if(jQuery('#friends').val() == 1)
    {
      search_str = '@memberid _uid' + jQuery('#uid').val() + ' ' + search_str;
    }
    
    // show loading
    jQuery('#searchResults').html('<h3>Loading...</h3>');
    
    jQuery.post(
      jQuery('#searchForm').attr('action'),
      {
        uid: jQuery('#uid').val(),
        friends: jQuery('#friends').val(),
        query: search_str,
        current_page: jQuery('#current_page').val()
      },
      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)
            {
              // build image
              if(matches[i].profileimage != null_var)
          		{
          			var image_url = '/image_cache.php?type=member&file=' + matches[i].profileimage + '&maxx=75&maxy=75&mode=crop';
          		}
          		else
          		{
          		  var image_url = '/image_cache.php?type=member&file=../../../images/placeholder.jpg&maxx=75&maxy=75&mode=crop';
          		}

              // build member link
              var member_link = '/member/'+matches[i].memberid+'/'+matches[i].name_display.gsub(' ','-');
              
              var contact_link = '';
              
              if(parseInt( matches[i].memberid ) == parseInt( jQuery('#uid').val() ))
              {
                // do nothing
              }
              else if(matches[i].friend == null)
              {
                contact_link = '<p><a href="javascript:friendPopup(\'add\',' + matches[i].memberid + ',0)">Send friend request</a></p>';
              }
              else
              {
                contact_link = '<p><a href="sendmessage.php?memberid=' + matches[i].memberid + '">Send a Message</a></p>';
                
                contact_link += '<p><a href="javascript:friendPopup(\'remove\',' + matches[i].memberid + ',0)">Remove Friend</a></p>';
              }
              
              var city_content = isEmpty(matches[i].city) ?  '' : 'City: ' + matches[i].city + ', ' + matches[i].state;
              var html = '<div class="foodieBox">'
                  + '<div class="foodieBoxImage">'
                    + '<a href="'+member_link+'">'
                      + '<img src="'+image_url+'" />'
                    + '</a>'
                  + '</div>'
                  + '<div class="foodieBoxInfo">'
                    + '<div class="foodieBoxName">'
                      + '<a href="'+member_link+'">'
                        + matches[i].name_display
                      + '</a>'
                    + '</div>'
                    + '<div class="foodieBoxCity">' + city_content + '</div>'
                  + '</div>'
                  + '<div class="foodieBoxDetail">' + contact_link + '</div>'
                  + '<div style="clear: both;"></div>'
                + '</div>';
              
              jQuery('#searchResults').append(jQuery(html));

              // insert text ad
              if(i == 2 || i == 7)
              {
                var ad_frame = '<div class="texAdWrap" class="resultListing"><iframe src="/includes/ad_server/searchAds/foodieSearchTextAd.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(){
    // check for cookies
    if(jQuery.cookie('SEARCH_ZONE') == 'foodies')
    {
      // set tab active
      // make all inactive
      //jQuery('div.tab').removeClass('active');

      // set search str
      if(jQuery('#search_str').val() == this.options.searchPlaceholder)
      {
        jQuery('#search_str').val(jQuery.cookie('SEARCH_STRING'));
        jQuery('#search_str').attr('value', jQuery.cookie('SEARCH_STRING'));
      }

      // set page
      jQuery('#current_page').val(jQuery.cookie('SEARCH_PAGE'));
    }

    // click active tab
    jQuery('div.tab.active').trigger('click');
  }
  
};


