class Public::CircuitsController < ApplicationController
  
  layout :get_public_layout
  
  
  def index
    @circuits = Circuit.order("name ASC").all
    @circuits_active = true
    
    if params[:search][:circuit_region_id].to_s != ""
      @circuits = @circuits.where(:circuit_region_id => params[:search][:circuit_region_id])
    
    end
    
    if params[:search][:name].to_s != ""
      @circuits = @circuits.where("name LIKE ? or city LIKE ? or cp LIKE ?","%#{params[:search][:name]}%","%#{params[:search][:name]}%","%#{params[:search][:name]}%")
    end
    
    respond_to do |format|
      format.html{
      
        params[:search][:per_page] = params[:search][:per_page] || 10
        per_page = params[:search][:per_page]
        page = (params[:page] and params[:page] != "") ? params[:page] : 1 
        @circuits = @circuits.page(page).per(per_page)
      
      }
    end
  end


  def show
    @circuits_active = true
    
    
    if @circuit = Circuit.where(:id => params[:id]).first
    
    elsif @circuit = Circuit.where(:slug => params[:id]).first
      
    else
      redirect_to public_circuits_path
    end
  end


end