(($) ->
  $.extend playSound: ->
    $("<embed src='" + arguments[0] + "' hidden='true' autostart='true' loop='false' class='playSound'>" + "<audio autoplay='autoplay' style='display:none;' controls='controls'><source src='" + arguments[0] + "' /></audio>").appendTo "body"
  return
) jQuery

$ ->

  $(".notif_stream_header").click ->
    stream = $(this).parent()

    url = stream.attr('data-notification-setstreamisopen-url')
    stream.find('.notif_stream_content').each ->
      isopen = "false"
      content = $(this)
      if(content.hasClass("notif_stream_hidden"))
        content.removeClass("notif_stream_hidden")
        isopen = 1
      else
        content.addClass("notif_stream_hidden")
        isopen = 0
      $.get url+"?isopen="+isopen
      return      

    return

  setInterval ->
    if ($(".notif_stream").length > 0)  
      url = $(".notif_stream").attr('data-notification-get-url')
      $.getJSON url, (data) ->
        $(".notif_stream").each ->
          content = $(this).find('.notif_stream_content')
          counter = $(this).find('.notif_stream_count')
          if(data.popup == 1 && content.hasClass("notif_stream_hidden"))
            content.removeClass("notif_stream_hidden")
          if(data.popup == 0 && !content.hasClass("notif_stream_hidden"))
            content.addClass("notif_stream_hidden")
          content.html data.html
          counter.html data.notification_count
          prova = counter.html
          content.find("a.ajax-link").each ->
            $(this).click ->
              $.get $(this).attr('ajax-href')
              $(this).parent().addClass("notif_stream_hidden")
          if(data.sound == 1)
            $.playSound(data.sound_url);
          if(data.shown_register == 1)
            $.get data.shown_url
  ,5000
  
  return
