class Admin::PagePartsController < ApplicationController

  layout "site_editor"
  
  before_filter :authenticate_user!
  before_filter :is_admin?
  before_filter :set_current_item
  
  def show
    @part = PagePart.find(params[:id])
    respond_to do |format|
      format.html { 
        render 'admin/items/index' 
      }
      format.js  {}
    end
  end

  def new
   
  end

  def edit
    @part = PagePart.find(params[:id])
    @module_apps = ModuleApp.all(:conditions => {:enable_frontend => true})
    @module_app = @part.module_app ? @part.module_app : @module_apps[0]
    @r_tag = @part.public_r_tag.blank? ? LIST[:public_r_tags][0] : @part.public_r_tag
    @tag_objects = @r_tag.classify.constantize.all rescue nil
      case @module_app.key
        when 'bulletin'
          @categories =  BulletinCategory.all
        when 'web_resource'
          @categories =  WebLinkCategory.all
      end
  end

  def create

  end

  def update
    @part = PagePart.find(params[:id])
    if @part.update_attributes(params[:page_part])
      flash.now[:notice] = t('admin.update_success_content')
      @part.save
      respond_to do |format|
        format.html { 
          redirect_to admin_page_url( @part.page )
        }
        format.js  {
          @item = @part.page
        }
      end
    else
      render :action => "edit"
    end
  end

  def destroy
    @item = Page.find(params[:id])
    @item.destroy
    redirect_to admin_items_url( :parent_id => @item.parent_id )
  end
  
  def reload_widgets
    @module_app = ModuleApp.find(params[:id])
    case @module_app.key
      when 'announcement'
        @categories =  BulletinCategory.all
      when 'web_resource'
        @categories =  WebLinkCategory.all
    end
    respond_to do |format|
      format.js  {}
    end
  end

  def reload_r_tag_options
    @r_tag = (ModuleApp.find(params[:id]) rescue nil) || params[:id]
    @tag_objects = @r_tag.classify.constantize.all rescue nil
    respond_to do |format|
      format.js  {}
    end
  end
  
end