# -*- encoding : utf-8 -*-
class Portlet::GalleryImagesController < ApplicationController
	layout "admin"
	

	def reorder
		i = 0
		params[:order].each do |gallery_image_id|
			i += 1
			gallery_image = GalleryImage.find(gallery_image_id)
			gallery_image.position = i
			gallery_image.save
		end
		
	end

	def create
		
		@gallery_content = GalleryContent.find(params[:gallery_content_id])
		@gallery_images = []
		if params[:image_file_ids].kind_of?(Array)
			params[:image_file_ids].each do |image_file_id|
				image_file = ImageFile.find(image_file_id)
				gallery_image = GalleryImage.create(:image_file_id => image_file.id, :title => image_file.name, :description  => image_file.description, :gallery_content_id => @gallery_content.id )
				
				@gallery_images << gallery_image
			end
		end
		
	

		respond_to do |format|
		
				format.js
		
		end
	end
	
	

	def edit
		@gallery_image = GalleryImage.find(params[:id])

		if request.xhr?
			render :layout => false
		end
		
	end
	
	



	def show


	end

	def update
		@gallery_image = GalleryImage.find(params[:id])
		respond_to do |format|
	        if @gallery_image.update_attributes(params.require(:gallery_image).permit!)
				
	        	format.js 
	        else
	        	format.html { render :action => "edit", :layout => false} 
				format.js { render :action => "edit" }
	        end

		end
	end

	def destroy
		
		@gallery_image = GalleryImage.find(params[:id])
		@gallery_image.destroy
		
	
	end

 
end
