class MobileController < ApplicationController
  
  layout 'mobile'

  before_filter :no_footer_for_app

  def index
    date_now = Time.now
    @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page(params[:page_main]).per(15)
    @ad_banner = AdBanner.find(:all, :conditions => {:title => /Home/i})[0]
  end

  def announcement
    @page_title = t('mobile.bulletin')
    date_now = Time.now
    @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page(params[:page_main]).per(15)
  end

  def announcement_content
    @bulletin = Bulletin.find(params[:id]) rescue nil
    @page_title = @bulletin.title
  end

  def dialog_contact
    @no_menu = @no_footer = true
    @page_title = t('mobile.contact')
  end

  def dialog_copyright
    @no_menu = @no_footer = true
    @page_title = t('mobile.copyright')
  end

  def dialog_language
    @no_menu = @no_footer = true
    @page_title = t('mobile.language')
  end

  def map
    @no_menu = @no_footer = true
    @page_title = t('mobile.location')
  end

  def page
    @page_title = t('mobile.page')
    @page_contexts = get_sorted_page_from_structure
  end

  def page_content
    @page_context = PageContext.first(conditions: { id: params[:id], :archived => false }) rescue nil
    @page_title = @page_context.page.title
  end

  protected

  def no_footer_for_app
    @no_footer = true if request.path =~ /app/
  end

  def get_sorted_page_from_structure
    page_contexts = Item.structure_ordered_items.inject([]){ |pages, page|
      pages << page.page_contexts.where(archived: false).limit(1)[0] if page.is_a?(Page) && !page.page_contexts.blank?
      pages
    }
    Kaminari.paginate_array(page_contexts).page(params[:page]).per(15)
  end

end
