class PortfoliosController < ApplicationController


  before_filter :authenticate_admin!, :except => [:index, :show] 
  
  
  def index
    @index_images = PortfolioImage.order("Shoot_at DESC").limit(20)
    
    PortfolioImage.all.each do |user|
      #user.photo.recreate_versions!(:square)
    end
  end
  
  def show
    @slug = params[:id]
    
    if params[:id]
      @portfolio = Portfolio.find_by_slug(params[:id])
      
      @images = @portfolio.images.page(params[:page]).per(@portfolio.content_type? ? @portfolio.content_type.to_i : 1)
      
      
      @prev_page = @images.current_page.to_i > 1 ? @images.current_page.to_i-1 : @images.num_pages
      
      @next_page = @images.current_page.to_i < @images.num_pages ? @images.current_page.to_i+1 : 1
      
      
      @next_image = @portfolio.images.page(@next_page).per(1).first
      @prev_image = @portfolio.images.page(@prev_page).per(1).first
      
    else
      
    end
    
  end
  
  
  def new
    @portfolio = Portfolio.new
  
  end

  def edit
    @portfolio = Portfolio.find(params[:id])
  
  end

  def create
  	@portfolio = Portfolio.new(params[:portfolio])
    
    if @portfolio.save
     
  	else
  		render :action => "new" 
  	end
  end

  def update
  	@portfolio = Portfolio.find(params[:id])
    
    if @portfolio.update_attributes(params[:portfolio])
      
          
  	else
  		render :action => "edit" 
  	end
    
  end
  		


  def destroy
  	@portfolio = Portfolio.find(params[:id])
	  @portfolio.destroy
    
  end
  
  
end
