module ParserLayout
  require 'nokogiri'

  def parse_body(layout)
    body = Nokogiri::HTML(layout.body)

    body.css('.page_content').each do |content|
      layout.layout_parts.build(:name => content['name']) unless layout.layout_parts.detect{|layout_part| layout_part.name.eql?(content['name'])}
    end

    body.css('.page_menu').each do |menu|
      layout.build_menu(:levels => 0, :values => {}) unless layout.menu
      layout.menu.levels = i = menu['level'].to_i
      layout.menu.values.merge!({'home' => menu['home']}) if i == 1
      layout.menu.values.merge!({"id_#{i}" => menu['id'], "class_#{i}" => menu['class'], "li_class_#{i}" => menu['li_class'], "li_incremental_#{i}" => menu['li_incremental']})
    end
    
  end

  def parse_body_for_images(design)
    body = Nokogiri::HTML(design.layout.body)

    body.css('.page_image').each do |page_image|
      image = design.images.where( file: File.basename(page_image['src']))[0]
      image.update_attributes(:html_id => page_image['id'], :html_class => page_image['class'], :in_html => true) if image
    end
    
  end


end