module ParserCommon
 
  def menu_level(page, current, menu, edit = false)
    res = ''
    if menu.levels > 0 && current <= menu.levels
      if current != 0
        res << "<div class='rc_dm'>"
        item = rand(100000)
        res << "<a href='#{edit ? admin_page_path(page.id) : page.full_name}' class='dm_ctrl' rel='#{item}'>#{page.i18n_variable[I18n.locale]}<span class='down'></span></a>"
        if page.children.size > 0
          res << "<div id='#{item}' class='dm_list #{menu.values['class_#{current}']}'>"
          res << "<ul class='ini_list'>"
          i = nil
          i = 1 if menu.values["li_incremental_#{current}"]
          page.children.each do |child|
            res << menu_li(child, current, menu, i, edit)
            i += 1 if i
          end
          res << "</ul>"
        res << "</div>"
        end
        res << "</div>"
      else
        res << '<ul>'
        res << "<li>" + "<a href='#{edit ? admin_page_path(page.id) : page.full_name}' class='dm_ctrl'>#{page.i18n_variable[I18n.locale]}</a>" + "</li>"
        page.children.each do |child|
          res << "<li>" + menu_level(child, current + 1, menu, edit) + "</li>"
        end
        res << '</ul>'
      end
    end
  end
  
  def menu_li(page, current, menu, i, edit)
    res = "<li class="
    res << menu.values["li_class_#{current}"]
    res << "_#{i}" if i
    res << ">"
    if page.children.size > 0 
      res << menu_level(page, current + 1, menu, edit)
    else
      res << "<a href='#{edit ? admin_page_path(page.id) : page.full_name}' class='nav dm_ctrl'>#{page.i18n_variable[I18n.locale]}</a>"
    end
    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_sec}' "
          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, 0, menu, edit))
    page_menu.swap(fragment)
  end

end
