class Admin::LinksController < ApplicationController

  layout "content"
  
  before_filter :authenticate_user!
  before_filter :find_parent_item
  before_filter :is_admin?
  
  def show
    @item ||= Link.find(params[:id])
  end
  
  def new
    @item = Link.new
    @item.is_published = true
    @item.parent_id = Page.find(params[:parent_id]).id rescue nil
  end

  def edit
    @item = Link.find(params[:id])
    @i18n_variable = @item.i18n_variable
  end

  def create
    @item = Link.new(params[:link])

    if @item.save
      flash.now[:notice] = t('admin.create_success_link')
      respond_to do |format|
        format.html { 
          redirect_to admin_link_url(@item) 
        }
        format.js  {}
      end
    else
      flash.now[:error] = t('admin.create_error_link')
      @i18n_variable = @item.i18n_variable
      render :action => "new"
    end
  end

  def update
    @item = Link.find(params[:id])

    if @item.update_attributes(params[:link])
      flash.now[:notice] = t('admin.update_success_link')
      respond_to do |format|
        format.html { 
          redirect_to admin_link_url(@item) 
        }
        format.js  {}
      end
    else
      flash.now[:error] = t('admin.update_error_link')
      @i18n_variable = @item.i18n_variable
      render :action => "edit"
    end
  end

  def destroy
    @item = Link.find(params[:id])
    @item.destroy
    @item.destroy_i18n_variable
    respond_to do |format|
      format.html { 
        redirect_to admin_items_url
      }
      format.js  {}
    end
  end
  
  def delete
    respond_to do |format|
      format.html {}
      format.js  { destroy }
    end
  end
  
end
