 'use strict'
 
-define ['jquery-private', 'underscore', 'backbone', 'sha1', 'firebase'],  ($_, _, Backbone, sha1, __Firebase__) ->
+define ['jquery-private', 'underscore', 'backbone', 'sha1', 'firebase'],  ($j, _, Backbone, sha1, __Firebase__) ->
+  $=$j
   BACKEND_DATA_HOST = "https://qslider.firebaseio.com/"
-
   Connection = Backbone.Model.extend
     defaults: 
       from: 'unknow'

    getCurrentSlideScreenshot: () ->
    getAuthor: () ->
    getTitle: () ->
      $(document).prop('title')

  class SpeakerdeskRemote extends RemoteControlDriver 
    constructor: () ->
      super
      # The player is put inside a iframe so, let get its contents
      @container = $('.speakerdeck-iframe ').contents()
      @quantity = this.getSlideQuantity()
      console? && console.log @container

    getAuthor: () ->
      $('#talk-details h2 a').html()
          #when window.location.host.indexOf('slideshare.net')  then   $('#talk-details h2 a').html()
    getTitle: () ->
        $(document).prop('title').replace("// Speaker Deck", '').trim()

    getSlideQuantity: () ->
      $('.previews > img', @container).length

    getCurrentSlideNumber: () ->
      current_url = $('#player-content-wrapper > #slide_image', @container).prop('src')
      r = /\/slide_([0-9]+)\./gi
      if m = r.exec(current_url)
        return 1 + parseInt(m[1])
      return false 

    getCurrentSlideScreenshot: () ->
      $('#player-content-wrapper > #slide_image', @container).prop('src')
    
    jump: (num) ->
      $('.speakerdeck-iframe ')[0].contentWindow.player.goToSlide num
    next: () ->
      $('.overnav > .next', @container)[0].click()
    previous: () ->
      $('.overnav > .prev', @container)[0].click()

  class SlideshareRemote extends RemoteControlDriver
    constructor: () ->
      super
      @container = $('#svPlayerId') 
      @quantity = this.getSlideQuantity()
      console? && console.log @container
    
    getAuthor: () ->
      $('.title .h-author-name').html()

    getSlideQuantity: () ->
      if $('.slide_container').length > 0
        return $('.slide_container .slide', @container).length 
      if $('.slidesContainer').length > 0 
        return $('.slidesContainer .jsplBgColorBigfoot').length

    getCurrentSlideNumber: () ->
      n = parseInt($('.goToSlideLabel > input', @container).val())
      if n<=1
        return 1
      return n

    getCurrentSlideScreenshot: () ->
      n = this.getCurrentSlideNumber()
      if $('.slide_container > .slide').eq(n-1).find('img').length > 0 
        url = $('.slide_container > .slide').eq(n-1).find('img').prop('src')
        if url.indexOf('image')>=1
          return url
      return "http://placehold.it/320&text=#{n}"

          .css('padding', '0.5em')
        
      this.$el.html(this.template({device: @device}))
      $j('body').append this.$el
      return this
    
    toggleBoard: () ->
      $j('#qcommander').show()

  WelcomeView = Backbone.View.extend   
    tagName: 'div'
