 request = require('request')
 ytdl = require('ytdl')
+playerContainer = $('#PlayerContainer');
+videoContainer = $('#video-container');
 
 itag_priorities = [ # http://en.wikipedia.org/wiki/YouTube > Comparison of YouTube media encoding options
     85,


videojs('video_player').ready ->
    @.on 'loadedmetadata', ->
        $('#PlayerContainer .duration').text(moment(@duration()*1000).format('m:ss'))
        videojs('video_player').play()

    @.on 'timeupdate', ->
        $('#PlayerContainer .progress-current').css({'width': (this.currentTime() / this.duration()) * 100 + '%'})
        $('#PlayerContainer .current-time').text(moment(this.currentTime()*1000).format('m:ss'))

    @.on 'ended', ->
        if $('#PlayerContainer .repeat').closest(".action").hasClass("active")
          videojs('video_player').currentTime(0)
          videojs('video_player').play()
        else if $('#PlayerContainer .random').closest(".action").hasClass("active")
          t = __playerTracklist[Math.floor(Math.random() * __playerTracklist.length)]
          PlayTrack(t.artist, t.title, t.cover_url_medium, t.cover_url_large)
        else

        alert 'Playback Error (' + video_error_codes[code] + ')'
        PlayNext(__currentTrack.artist, __currentTrack.title)

playerContainer.find('.volume-bg').ready ->
    playerContainer.find('.controls .volume-icon .action i.fa-volume-down').hide()
    playerContainer.find('.controls .volume-icon #action i.fa-volume-off').hide()

playerContainer.find('.progress-bg').on 'click', (e) ->
    percentage = (e.pageX - $(this).offset().left) / $(this).width()
    videojs('video_player').currentTime(percentage * videojs('video_player').duration())
    playerContainer.find('.progress-current').css({'width': (percentage) * 100 + '%'})

playerContainer.find('.volume-bg').on 'click', (e) ->
    percentage = (e.pageX - $(this).offset().left) / $(this).width()
    playerContainer.attr("data-volume", percentage)
    playerContainer.find('.volume-icon').attr("data-ismuted", 0)
    playerContainer.find('.volume-icon').find("i").removeClass("fa-volume-off").addClass("fa fa-volume-up")
    videojs('video_player').volume(percentage)
    playerContainer.find('.volume-current').css({'width': (percentage) * 100 + '%'})

playerContainer.find('.track-info .backward').on 'click', (e) ->
    PlayPrevious(__currentTrack.artist, __currentTrack.title)

playerContainer.find('.track-info .forward').on 'click', (e) ->
    PlayNext(__currentTrack.artist, __currentTrack.title)

playerContainer.find('.track-info .repeat').on 'click', (e) ->
    $(@).closest(".action").toggleClass("active")

playerContainer.find('.track-info .random').on 'click', (e) ->
    $(@).closest(".action").toggleClass("active")

playerContainer.find('.volume-icon').on 'click', (e) ->
    if(+$(@).attr("data-ismuted") == 1)
      $(@).attr("data-ismuted", 0)
      $(@).find("i").removeClass("fa-volume-off").addClass("fa fa-volume-up")
      videojs('video_player').volume(playerContainer.attr("data-volume"))
    else
      $(@).attr("data-ismuted", 1)
      $(@).find("i").removeClass("fa-volume-up").addClass("fa fa-volume-off")
      videojs('video_player').volume(0)

videoContainer.find(".ExpandButton").on "click", (e) ->
      $("#video-container").toggleClass "expanded"