class ProductsController < Spree::BaseController
  resource_controller
  helper :taxons
  before_filter :load_data, :only => :show
  actions :show, :index

  index.before do
    @product_cols = 3
    @hpage = {
      :name => "Homepage"
    }
  end
  
  show.before do
    @hpage = {
      :name => "View product #{@product.name}"
    }
  end

  def change_image
    @product = Product.available.find_by_param(params[:id])
    img = Image.find(params[:image_id])
    render :partial => 'image', :locals => {:image => img}
  end

  private
  def load_data
    
    return unless permalink = params[:taxon_path]
    @taxon = Taxon.find_by_permalink(params[:taxon_path].join("/") + "/")
  end
  
  def collection
    if params[:taxon]
      @taxon = Taxon.find(params[:taxon])
      
      @search = Product.active.scoped(:conditions =>
                                        ["products.id in (select product_id from products_taxons where taxon_id in (" +
                                          @taxon.descendents.inject( @taxon.id.to_s) { |clause, t| clause += ', ' + t.id.to_s} + "))"
                                        ]).new_search(params[:search])
    else
      @search = Product.active.new_search(params[:search])
    end

    @search.per_page = Spree::Config[:products_per_page]
    @search.include = :images

    @product_cols = 3
    @products ||= @search.all
  end
end
