# -*- encoding : utf-8 -*-
class Admin::SlidesController < ApplicationController
  
  	before_filter :authenticate_admin!
	load_and_authorize_resource
	
	layout "admin"

	navigation :home_pages


	def new

		@slide = Slide.new(:slider_id => params[:slider_id])
		
		
	end

	def create
	
		@slides = Slide.order(:position)
		
		
	

		@slide = Slide.new(params[:slide])
	
		
		
		
		
		
		if @slide.save
			flash[:notice] = "La slide à bien été créé."
			
			respond_to do |format|
				format.js
			end
			
		else
			
			respond_to do |format|
				format.js { render :action => :new}
			end
					
		end
		
	end

	def update
		
		@slide = Slide.find(params[:id])
		@slides = Slide.order(:position)
		
		
		
		
		
		if @slide.update_attributes(params[:slide])
			flash[:notice] = "Le slide à bien été modifiée."

			respond_to do |format|
				format.js
			end

		else
			respond_to do |format|
				format.js { render :action => :edit}
			end		
		end
		
	end

	def edit
		@slide = Slide.find(params[:id])
	end

	def show
		@slide = Slide.find(params[:id])
	end

	def destroy
		@slide = Slide.find(params[:id])
		@slide.destroy


		flash[:notice] = "Le slide à bien été supprimée."
	end
	
	
	def reorder
		i = 0
		params[:slide].each do |slide_id|
			i += 1
			slide = Slide.find(slide_id)
			slide.position = i
			slide.save
		end
		render :inline => "ok"
	end

end
