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

	navigation :folders
	load_and_authorize_resource

	def index
		@folder = Folder.find(params[:folder_id])
		
		@articles = @folder.articles
		if request.xhr?
			render :layout => false
			
		end
	end

	def new
		current_navigation :new_articles
		@folder = Folder.find(params[:folder_id])
		@article = Article.new(:folder => @folder)
	end
	
	def create
		current_navigation :new_articles
		
		@article = Article.new(params[:article])
		@folder = @article.folder
		
		if @article.save
			flash[:notice] = "L'article à bien été créé."
			
			respond_to do |format|
				format.js
			end
			
		else
			
			respond_to do |format|
				format.js { render :action => :new}
			end
					
		end
	end


	def edit
		current_navigation :edit_articles
		
		@article = Article.find(params[:id])
		@folder = @article.folder

	end

	def update
		current_navigation :edit_articles
		
		@article = Article.find(params[:id])
		@folder = @article.folder
		
		if @article.update_attributes(params[:article])
			flash[:notice] = "Le dossier à bien été modifié."
			
			respond_to do |format|
				format.html { redirect_to(admin_articles_path()) }
				format.js
			end
			
		else
			respond_to do |format|
				format.html { render :action => :edit}
				format.js { render :action => :edit}
			end		
		end
	end


	def destroy
		@article = Article.find(params[:id])
		@article.destroy
		@folder = @article.folder
	
		flash[:notice] = "L'article à bien été supprimé."
	end
end