class SearchController < ApplicationController
  
  def index

  end

  # this displays the search results
  def results
    store_location
    @search_score = {}
    
    @tt = params[:search_terms]

    if @tt == "" || @tt == nil
      redirect_to :action => 'index'
      return
    end

    @tt.tr_s!("<", "")      
    @tt.tr_s!(">", "")      
    @tt.tr_s!("'", "")      
    @tt.tr_s!("!", "")      


    #handle limits but writing conditions string
    # do nested scoping a la 1.1 here
    @limits = ""

    if @r_lim_type = params[:resource_limit_type]
      @resources = Resource.find(:all, :conditions => "resource_type_id = "+@r_lim_type )	           

      # add other limits here, like licence

    else
       @resources = Resource.find(:all)	           
    end

# NOT WORKIGN YET -- should be implemented in find ... FIX  
#     if @l_limit = params[:licence_limit] and @l_lim_type =  params[:restrict_licence_type]
#       @resources.each do |r|
#         @resources.clear
#       end      
#     end


    if not @resources or @resources.empty?
      return
    end


    # cycle over resources r
    @resources.each do |r|
      next if not r.viewable?
      @search_score[r.id] = 0.0      
      @tt.split(/,?\s+/).each do |term|        
        s = r.search_score( term )
        if s > 0 
          @search_score[r.id] += s
        else
          @search_score[r.id] = @search_score[r.id] * 0.7
        end                    
      end                   
    end                         # cycle over resources
    @search_score.delete_if { |k,v| v == 0 }
  end # results

end
