# -*- encoding : utf-8 -*-
module MenuItemsHelper
  
  
  def menu_item_link_by_slug(permalink, active = nil, icon = false)
    menu_id = 2

    menu_item_lang = @lang.menu_item_langs.joins(:menu_item).where(:menu_items => {:menu_id => menu_id}).find_by_permalink(permalink)
    menu_item = menu_item_lang.menu_item if menu_item_lang
    menu_item_link_force(menu_item, active, icon) if menu_item
  end
  
  
  def menu_item_link(menu_item, active = nil, icon = false )
  
    menu_item_lang = menu_item.menu_item_langs.find_by_lang_site_id(@lang.id)
    if active == nil
      if @menu_item and (@menu_item == menu_item or @menu_item.ancestors.include?(menu_item))
        active = true 
      else
        active = false
      end
    end
    if icon
      if active
        name = (image_tag(menu_item.icon_b_image_file.file.large.medium.small.thumb.url) if menu_item.icon_b_image_file).to_s
      else
        if menu_item.icon_image_file
        
          name = (image_tag(menu_item.icon_image_file.file.large.medium.small.thumb.url, :onmouseover => '$(this).attr("src", "'+((menu_item.icon_b_image_file.file.large.medium.small.thumb.url) if menu_item.icon_b_image_file).to_s+'");' , :onmouseout => '$(this).attr("src", "'+((menu_item.icon_image_file.file.large.medium.small.thumb.url) if menu_item.icon_image_file).to_s+'");')).to_s
        else
          name = ""
        end
      end
      
    else
      name = menu_item_lang.name
    end
    
    if menu_item_lang.enabled == true and menu_item_lang.visible == true
      link_to name, @one_voice_host.to_s+menu_item_path(:url => menu_item_lang.url, :lang => @lang.slug), :class => ("active" if active)
    end
  end
  
  def menu_item_link_force(menu_item, active = nil, icon = false )
  
    menu_item_lang = menu_item.menu_item_langs.find_by_lang_site_id(@lang.id)
    if active == nil
      if @menu_item and (@menu_item == menu_item or @menu_item.ancestors.include?(menu_item))
        active = true 
      else
        active = false
      end
    end
    if icon
      if active
        name = (image_tag(menu_item.icon_b_image_file.file.large.medium.small.thumb.url) if menu_item.icon_b_image_file).to_s
      else
        name = (image_tag(menu_item.icon_image_file.file.large.medium.small.thumb.url, :onmouseover => '$(this).attr("src", "'+((menu_item.icon_b_image_file.file.large.medium.small.thumb.url) if menu_item.icon_b_image_file).to_s+'");' , :onmouseout => '$(this).attr("src", "'+((menu_item.icon_image_file.file.large.medium.small.thumb.url) if menu_item.icon_image_file).to_s+'");') if menu_item.icon_image_file).to_s
      end
      
    else
      name = menu_item_lang.name
    end
    if menu_item_lang.enabled == true
      link_to raw(name), menu_item_path(:url => menu_item_lang.url, :lang => @lang.slug), :class => ("active" if active)
    end
  end
  
  
  def menu_item_url(menu_item, sym = false)
  
    menu_item_lang = menu_item.menu_item_langs.find_by_lang_site_id(@lang.id)
    
    @menu_item and (@menu_item == menu_item or @menu_item.ancestors.include?(menu_item)) ? active = true : active = false
    
    name = menu_item_lang.name+("" if sym and @menu_item and @menu_item == menu_item ).to_s
    if menu_item_lang.enabled == true and menu_item_lang.visible == true
      menu_item_path(:url => menu_item_lang.url, :lang => @lang.slug)
    end
  end
  
  def menu_item_valid_to_show(menu_item, lang, time=Time.now)
    if menu_item.menu_item_langs.find_by_lang_site_id(@lang.id).visible and menu_item.menu_item_langs.find_by_lang_site_id(@lang.id).enabled
      
      true
      
    else
      false
    end
    
  end


  def article_valid_to_show(article, lang, time=Time.now)
    if article.lang_articles.find_by_lang_site_id(@lang.id).enabled and ( !article.published_at?  or article.published_at <= time )
      
       
      true
      
    else
      false
    end
    
  end

end
