class Admin::AuctionsController < ApplicationController
	layout "admin"
	
	protect_from_forgery :only => [:create, :update, :destroy]
	
	def index
		@auctions = Auction.find(:all, :order => :datetime)
	end

	def show
	  @auction = Auction.find(params[:id])
	end

	def item
		@item = AuctionItem.find(params[:id])
	end

	def add_item
		@item = AuctionItem.create(params[:auction_item])		
		@image = Image.create(params[:image])
		@image.update_attributes(:auction_item_id => @item.id)
    redirect_to :action => 'item', :id => @item
	end

	def delete_item
		@item = AuctionItem.find(params[:id])
		@item.images.destroy_all
		@item.destroy
		render :nothing => true
	end

  def update_positions
    params[:images].each_with_index do |id, position|
      AuctionItem.update(id, :position => position+1)
    end
    render :nothing => true
  end

	def new
	  @auction = Auction.new
	end

	def create
	  @auction = Auction.new(params[:auction])
	  if @auction.save
	    flash[:notice] = 'Auction was successfully created.'
			redirect_to :action => 'show', :id => @auction.id, :new => 'true'
	  else
	    render :action => 'new'
	  end
	end

	def edit
		@auction = Auction.find(params[:id])
		@auction.update_attributes(params[:auction])
		render :partial => "auctioninfo", :object => @auction, :layout => false
	end

	def update
	  @auction = Auction.find(params[:id])
	  if @auction.update_attributes(params[:auction])
	    flash[:notice] = 'Auction was successfully updated.'
	    redirect_to :action => 'show', :id => @auction
	  else
	    render :action => 'edit'
	  end
	end

  def delete
			Auction.find(params[:id]).destroy
			render :nothing => true
  end
end