module ParserCommon
 include ActionView::Helpers::TagHelper

  def menu_level(page, current, menu, edit = false)
    res = ''
    if page.ordered_and_visible_children.size > 0
      res << "<ul class='"
      res << menu.values["class_#{current}"] rescue nil
      res << "'>"
      i = nil
      i = 1 if menu.values["li_incremental_#{current}"]
      if menu.values['home'] && current == 1
        res << menu_li(page, current, menu, i, edit)
      end
      page.ordered_and_visible_children.each do |child|
        res << menu_li(child, current, menu, i, edit)
        i += 1 if i
      end
      res << "</ul>"
    end
    res
  end
  
  def menu_li(page, current, menu, i, edit)
    res = "<li class="
    res << menu.values["li_class_#{current}"] rescue nil
    res << "_#{i}" if i
    res << ">"
    res << "<a href='#{edit ? admin_page_path(page.id) : page.full_name}'><span>#{page.i18n_variable[I18n.locale]}</span></a>"
    if page.ordered_and_visible_children.size > 0 && current <= menu.levels
      res << "<span class='dot'></span>"
      res << menu_level(page, current + 1, menu, edit)
    end unless (page.is_home? rescue nil)
    res << "</li>"
  end

  # ad_banners
  def parse_ad_banners(body = nil, page = nil, id = nil)
    body.css('ad_banner').each do |banner|
      res = ''
      ad_banner = AdBanner.find(banner["id"]) rescue nil
      if ad_banner
        res << "<script type='text/javascript'>
                $(document).ready(function(){ $('#slideshow-#{ad_banner.title.dehumanize}').cycle({delay: -1000, fx: '#{ad_banner.ad_fx.nil?? 'fade': ad_banner.ad_fx}', timeoutFn: getTimeout }); });
                </script>"
        res << "<div id='slideshow-#{ad_banner.title.dehumanize}'>"
        printable_ad_images = []
        ad_banner.ad_images.each do |ad_image|
          if ad_image.display?
            ad_image.weight.times do
              printable_ad_images << ad_image
            end
          end
        end
        printable_ad_images.shuffle!
        printable_ad_images.each  do |ad_image|  #TODO Need Reflact
          res << "<img src='#{ad_image.file}' "
          res << "alt='#{ad_image.title || ' '}' "
          res << "time_to_next='#{ad_banner.transition_msec}' "
          res << "link_open='#{ad_image.link_open}' "
          # res << "link_url='#{(ad_image.direct_to_after_click?? ad_image.out_link : ad_banner.context) || ' '}' "
          res << "link_url='#{(ad_image.out_link || ad_banner.context || ' ')}' "
          res << "/>"          
        end
        res << "</div>"
      end
      fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
      banner.swap(fragment)
    end
  end

  # page_images
  def parse_images(body, page)
    body.css('.page_image').each do |page_image|
      # image = page.custom_images.detect{|image| image.name.eql?(tag.attr['name']) }
      # image = page.design.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
      image = page.design.images.detect{|image| image.name.eql?(File.basename(page_image['src'])) } unless image
      if image
        res = "<img src=#{image.file.url} "
        page_image.attributes.each do |l|
          res << "#{l[0]}='#{l[1]}' "
        end
        res << '>'
      end
      fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
      page_image.swap(fragment)
    end
  end

  # page_menu
  def parse_menu(body, page, edit=nil)
    page_menu = body.css('.page_menu').first
    home = get_homepage
    menu = page.design.layout.menu
    fragment = Nokogiri::HTML::DocumentFragment.new(body, menu_level(home, 1, menu, edit))
    page_menu.swap(fragment)
  end

  # sub_menus
  def parse_sub_menus(body = nil, page = nil, id = nil)
    body.css('sub_menu').each do |sub_menu|
      res = ''
      res << "<div class='category_list'>"
      res << "<h3 class='h3'>#{page.i18n_variable[I18n.locale]}</h3>"
      res << "<ul class='list'>"
      page.ordered_and_visible_children.each do |child|
        res << "<li>"
        res << "<a href='#{child.full_name}'>#{child.i18n_variable[I18n.locale]}</a>"
        res << "</li>"
      end
      res << "</ul>"
      res << "</h3>"
      res << "</div>"
      fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
      sub_menu.swap(fragment)
    end
  end

  # page_menu
  def parse_footer(body, page, edit=nil)
    page_footer = body.css('.page_footer').first
    res = "<div, id='footer', class='footer'>"
    res << @site.footer[I18n.locale]
    res << "</div>"
    fragment = Nokogiri::HTML::DocumentFragment.new(body ,res)
    page_footer.swap(fragment) rescue nil
  end

end
