class Admin::PhotosController < ApplicationController
layout "admin"

require 'RMagick'
include Magick

def new
	case @request.method
		when :post
			params[:photo].each_with_index do |image, name|
			end
#			render :text => params[:photo].inspect
	end
end

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

def update_description
	@photo = Photo.find(params[:id])
	@photo.update_attributes(params[:photo])
	render(:partial => "shared/photo", :object => @photo, :layout => false)
end

def add_photo

	@image = Photo.new(params[:photo])

	i = Magick::Image.from_blob(params['photo']['image'].read).first.shave!(4,4)

	img = i.change_geometry('400x>') { |cols, rows, img1|
		img1.resize!(cols, rows)
	}

	thumb = i.thumbnail(150,150)

	@image.thumb = thumb.to_blob
	@image.image = img.to_blob
	@image.title = @params['photo']['title']
	@image.save
	
	if @image.item_id
		redirect_to :controller => 'items', :action => 'show', :id => @image.item_id
	else
		redirect_to :controller => 'auctions', :action => 'item', :id => @image.auction_item_id
	end
end

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

def img
	@image = Photo.find(@params['id'])
	send_data @image.image, :type => 'image/jpeg', :disposition => 'inline'
end

def thumb
	@image = Photo.find(@params['id'])
	send_data @image.thumb, :type => 'image/jpeg', :disposition => 'inline'
	end
end