xml.instruct! :xml, :version => "1.0" 
xml.rss(:version => "2.0", 'xmlns:atom' => "http://www.w3.org/2005/Atom") do
  xml.channel do
    xml.atom(:link, :href => formatted_news_documents_url(:since => params[:since], :format => :rss), :rel => "self", :type => "application/rss+xml")
    xml.title @feed_title
    xml.description @feed_title
    xml.link documents_url
    xml.language I18n.locale
    
    for document in @documents
      xml.item do
        xml.title document.title
        xml.description document.pretty_body.gsub(/(\.\.\/)+\/assets/, "/public/assets")
        xml.pubDate document.published_at.to_s(:rfc822)
        xml.link document_url(document)
        xml.guid document.id
        xml.speaker document.speaker
        xml.organization do
          xml.title document.organization.name
          xml.link document.organization.gc_link
        end
        
        if document.has_video?('irekia') || document.has_videos?('irekia') || document.has_photos?('irekia') || document.has_cover_photo?
          xml.multimedia_iframe_src irekia_iframe_path(:action => 'document', :id => document.id, :locale => I18n.locale, :only_path => false)
        end
        
        mpg = document.videos[:mpg][I18n.locale.to_sym]
        unless mpg.empty?
          xml.videos_hq do
            mpg.each do |v|
              xml.item do
                xml.utl Document::MULTIMEDIA_URL + v
                xml.size number_to_human_size(File.size(Document::MULTIMEDIA_PATH + v))
              end
            end
          end
        end
        
        if document.has_audios?('irekia')
          audios_to_be_shown = document.audios('irekia')[I18n.locale.to_sym]
          audio_template_url = Document::MULTIMEDIA_URL
          if audios_to_be_shown.length > 0
            xml.audios do
              audios_to_be_shown.each do |f|
                xml.item do
                  xml.url "#{audio_template_url}/#{f}"
                  xml.size number_to_human_size(File.size(Document::MULTIMEDIA_PATH + f))
                end                 
              end
            end
          end
        end
        
        files_to_show = document.attached_files('irekia', I18n.locale)
        if files_to_show.length > 0
          xml.attached_files do
            files_to_show.each do |f|
              xml.item do
                xml.file_type file_type(f)
                xml.url url_for(request.protocol + request.host_with_port + f.file.url.strip_version)
                xml.size number_to_human_size(f.file_file_size)
              end
            end
          end
        end
        
        
        for tag in document.tag_list
          xml.category tag
        end
        
        if document.cached_related.present?
          related_by_keywords = JSON.parse(document.cached_related)
          xml.related do
            related_by_keywords[0..7].each do |rel_document|
                xml.item do
                  xml.type rel_document["type"]
                  xml.id rel_document["id"]
                  xml.title rel_document["title"]
                  xml.link rel_document["link"].present? ?  rel_document["link"] : url_for(:controller => rel_document["controller"], :action => "show", :id => rel_document["id"], :t => 1, :only_path => false)
                end 
            end
          end
        end
                
      end
    end
  end
end
