class Admin::AssetsController < ApplicationController

  layout "admin"
  before_filter :authenticate_user!
  before_filter :is_admin?
  
  def index
    @assets = Asset.all.entries
  end
  
  def show
  #TODO
  end
  
  def new
    @asset = Asset.new
    respond_to do |format|
      format.html {}
      format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/new'} }
    end
  end
  
  def edit
    @asset = Asset.find(params[:id])
    respond_to do |format|
      format.html {}
      format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/edit'} }
    end
  end
  
  def create  
    @asset = Asset.new(params[:asset])
    if @asset.save
      respond_to do |format|
        format.html { redirect_to admin_assets_url }
        format.js { render 'js/remove_pop_up_and_reload_content', :locals => {:function => 'append', :id => 'asset_tbody', :value => @asset, :values => nil, :partial => 'admin/assets/asset', :locals => nil} }
      end
    else
      respond_to do |format|
        format.html { render :action => :new }
        format.js { render 'js/reload_pop_up', :locals => {:value => @asset, :values => nil, :partial => 'admin/assets/new', :locals => {:is_html => false}} }
      end
    end
  end
  
  def update
    @asset = Asset.find(params[:id])
    if @asset.update_attributes(params[:asset])
      respond_to do |format|
        format.html { redirect_to admin_assets_url }
        format.js { render 'js/remove_pop_up_and_reload_content', :locals => {:function => 'replaceWith', :id => "asset_#{@asset.id}", :value => @asset, :values => nil, :partial => 'admin/assets/asset', :locals => nil} }
      end
    else
      respond_to do |format|
        format.html { render :action => :edit }
        format.js { render 'js/reload_pop_up', :locals => {:value => @asset, :values => nil, :partial => 'admin/assets/edit', :locals => {:is_html => false}} }
      end
    end
    
  end
    
  def destroy
    @asset = Asset.find(params[:id])
    @asset.destroy
    respond_to do |format|
      format.js { render 'js/remove_element', :locals => {:id => "asset_#{@asset.id}"} }
    end
  end
  
end
