module PhoneHelper
  
  
  def timer_watcher(element_type=nil, element_id=nil, key=nil )
    time_watch = ("timer_watcher('"+(@p_customer.id if @p_customer).to_s+"','"+(@admin_space).to_s+"', '"+element_type.to_s+"','"+element_id.to_s+"','"+key.to_s+"');")
    raw ('<script type="text/javascript">')+time_watch+("setInterval(function(){ "+time_watch+"}, 5000);")+('</script>')
  end
  
  
  def edit_watcher(element_type, element_id, key='' )
    raw ('<script type="text/javascript">')+("edit_watcher('"+element_type+"', '"+element_id.to_s+"','"+key+"');")+("setInterval(function(){ edit_watcher('"+element_type+"', '"+element_id.to_s+"','"+key+"');}, 1000);")+('</script><div id="edit_watcher_result"></div>')
  end
  def hashtag_link(hash)
    link_to " ##{hash} ", hashtag_public_image_actu_path(hash.to_slug), :target => "_blank"
  end
  
  def parse_hashtag(text)
    text.gsub(Hashtag::REGEX) {hashtag_link($1)}
  
  end
  
  def flash_js
    r = raw ("$('#flashs').append('"+ escape_javascript(bootstrap_flash) +"');flash_delay();")
    flash[:notice] = nil
    flash[:error] = nil
    flash[:success] = nil
    
    r
  end
  
  
  def forum_avatar_url(user)
    if user
      user.avatar_url
    else
      "/default_avatar.png"
    
    end
  end
  
  def number_to_phone(phone)
    phone = phone.to_s
    if phone.size == 10
      return "#{ phone[0..1]} #{ phone[2..3]} #{ phone[4..5] } #{ phone[6..7] } #{ phone[8..9] }"
    else
      phone
    end
  end

  
  def format_time(time, options = {})
    r = ""
    time = (Time.at( time.to_i )).utc
    
    d = time.day - 1
    if d > 1
      r+= d.to_s
      r+= "jours "
    elsif d > 0
      r+= "1 jour "
    end
    
      
    r+=time.strftime("%H heures %M minutes"+(" et %S secondes" if !options[:skip_s]).to_s) 
    return r
  end
  
  def format_time_new(time, options = {})
    r = ""
    time = (Time.at( time.to_i )).utc
    
    d = time.day - 1
    
    
    h = (d * 24)+time.strftime("%H").to_i 
    
    if h > 0
      r+="#{h} H "+time.strftime("%M min"+(" et %S S" if !options[:skip_s]).to_s) 
    else
      r+=time.strftime("%M min"+(" et %S S" if !options[:skip_s]).to_s) 
    end
    return r
  end
  
  
  def short_format_time(time)
    r = ""
    time = (Time.at( time.to_i )).utc
    
    d = time.day - 1

    
    h = (d * 24)+time.strftime("%H").to_i 
    
      
    r+= "#{h}:"+time.strftime("%M:%S") 
    
    
    
    return r
  end
  
end
