               }
               JName.some selector, option, (err, names)=>
                 if names
-                  urls = (name.name for name in names) 
+                  urls = (name.name for name in names)
                   sitemapName =  @generateSitemapName groupPageNumber, skip
                   content = @generateSitemapString urls
                   @saveSitemap sitemapName, content

    groupSelector = {
      privacy:'public'
    }

    generateMainSitemap = (sitemapNames)=> 
      name = "sitemap.xml"
      content = @generateSitemapString sitemapNames, true
      @saveSitemap name, content

    JGroup.count groupSelector, (err, groupCount)=>

module.exports = class SitemapGeneratorWorker extends EventEmitter
  constructor: (@bongo, @options = {}) ->

  generateSitemapString: (urls)->
    # sitemap.xml beginning and ending parts
    sitemap = '<?xml version="1.0" encoding="UTF-8"?>
      <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
      xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">'
    sitemapFooter = '</urlset>'

    # while generating main sitemap, we don't need hashbang in the url.
    for url in urls
      sitemap += "<url><loc>#{@options.uri.address}/#!/#{url}</loc></url>"
    sitemap += sitemapFooter
    return sitemap

  generateSitemapIndexString: (sitemapNames)->
    # sitemapindex.xml beginning and ending parts
    sitemap = '<?xml version="1.0" encoding="UTF-8"?>
      <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
    sitemapFooter = '</sitemapindex>'

    # while generating main sitemap, we don't need hashbang in the url.
    for sitemapURL in sitemapNames
      url = "<sitemap><loc>#{@options.uri.address}/#{sitemapURL}</loc></sitemap>"
      console.log url
      sitemap += "<sitemap><loc>#{@options.uri.address}/#{sitemapURL}</loc></sitemap>"
    sitemap += sitemapFooter
    return sitemap

  generateSitemapName: (groupPageNumber, skip)->
    return  "sitemap_" + groupPageNumber + "_" + skip + "_" + (skip + NAMEPERPAGE) + ".xml"

  saveSitemap: (name, content)->
    {JSitemap} = @bongo.models
    JSitemap.update {name}, $set : {content}, {upsert : yes}, (err)->
      console.log err if err

  generate:=>
