// Youkebox JavaSript file youkebox.js
// Copyright (c) 2008-2009 Youkebox.com, All Rights Reserved.

/*global $ jQuery log pageTracker window */

var upcoming = [];
var current_video_id = '';
var current_cued_video_id = '';
var current_video_elapsed = '';
var current_video_time_left = 0;
var init = false;
var heartbeatActive = true;
var newest_chat_message_id = 0;
var thumbs_up_given = false; // TODO: hack, should rather be gotten from the server than guesstimated
var autoScrollSpeed = 100000;
var intervals = {};
var endThreshold = -10; // how may seconds to play after ending on server
var isIE = (document.compatMode && document.all); // IE 6+

// general utils

function debug(str) {
  var d = new Date();
  var debugtime;
  debugtime = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + '.' + d.getMilliseconds();
  debugtime = debugtime;
  if(typeof(log) !== 'undefined') {
    log.debug(str);
  }
}

function fixForIE(ajaxURL) {
  if(isIE) {
    ajaxURL += '&_=' + (new Date()).getTime();
  }
  return ajaxURL;
}

var YBUTILS = {
  validateEmailAddress: function(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);
  }
};

// application

function getCurrentVideoID() {
  return current_video_id;
}


// ui

function disableSkipAndThumbsUp(){
  $('#skip').addClass('inactive');
  $('#thumbs_up').addClass('inactive');
}

function enableSkipAndThumbsUp(){
  $('#skip').removeClass('inactive');
  if (!thumbs_up_given) {
    $('#thumbs_up').removeClass('inactive');
  }
}

var YOUKEBOX = {
  volume: 80,
  mute: false,
  slowdown: 1,
  youkebox_id: 0,
  player: null,
  flash_params: { allowScriptAccess: "always", wmode: "opaque" },
  flash_attributes: { id: "youtubeXplayer" },
  timezone: 0,
  randomizing: true,
  member: false,
  popups: [],
  client_heartbeat: true,
  setupKeyboardListeners: function () {
    $("#new_chat_message").keydown(function (e) {
        if (e.which == 13) {
          $('#new_chat_message').submit();
          return false; // because we don't want the extra enter to appear, returning false disabled the default behaviour (and crashes ff 3.5.3?)
        }
      });
    $(document).keyup(function (e) {
      if (e.which == 27) {
        YOUKEBOX.removeUpperMostPopup();
      }
    });
  },
  removeUpperMostPopup: function () {
    console.warn('popups:' + YOUKEBOX.popups.length);
    if (YOUKEBOX.popups.length > 0) {
      YOUKEBOX.removePopup(YOUKEBOX.popups.pop());
    }
  },
  recordNewPopup: function (div_id) {
    YOUKEBOX.popups.push(div_id);
  },
  removePopup: function (div_id) {
    console.warn('removing:' + div_id);
    $('#' + div_id).remove();
  },
  setRandomizing: function (on) {
    if (on) {
      $('a#randomize').addClass('inactive');
      $('a#randomize').removeClass('active');
      YOUKEBOX.randomizing = true;
    } else {
      $('a#randomize').addClass('active');
      $('a#randomize').removeClass('inactive');
      YOUKEBOX.randomizing = false;
    }
  },
  updateYoukebox: function () {
    var url = "/youkeboxes/" + YOUKEBOX.youkebox_id + ".json?lol=wut";
    url = fixForIE(url);
    if (YOUKEBOX.client_heartbeat) {
      $.ajax({
        url: url,
        dataType: "json",
        success: function(data){
          if(data.randomizing && !YOUKEBOX.randomizing){
            YOUKEBOX.setRandomizing(true);
          }
          if (!data.randomizing && YOUKEBOX.randomizing) {
            YOUKEBOX.setRandomizing(false);
          }
        },
        complete: function (XMLHttpRequest, textStatus) {
          setTimeout(YOUKEBOX.updateYoukebox, 12000);
        }
      });
    } else {
      setTimeout(YOUKEBOX.updateYoukebox, 12000);
    }
  },
  toggleRandomize: function () {
    var url = "/youkeboxes/" + YOUKEBOX.youkebox_id + '/randomize';
    $.ajax({
      url: url,
      type: "PUT",
      dataType: "json",
      data: {
        "setting": !YOUKEBOX.randomizing
      },
      success: function(data){
        if(data.randomizing && !YOUKEBOX.randomizing){
          YOUKEBOX.setRandomizing(true);
        }
        if (!data.randomizing && YOUKEBOX.randomizing) {
          YOUKEBOX.setRandomizing(false);
        }
      },
      complete: function (XMLHttpRequest, textStatus) {
       // remove animation spinner if used
      }
    });
  },
  constrain: function(str,n){ 
    if(str.length > n){  
      var s = str.substr(0, n);
      var words = s.split(' '); 
      words[words.length-1] = '';
      str = words.join(' ') + '&hellip;';
    }
    return str;
  },
  isMuted: function() {
    if (YOUKEBOX.player) {
      if (YOUKEBOX.player.isMuted()){
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  },
  stopVideo: function() {
    if (YOUKEBOX.player) {
      YOUKEBOX.player.stopVideo();
    }
  },
  postNewVideo: function(){
    var url = $('input#youtube_url').attr('value');
    if (url === '') {
      return false;
    }
    $('input#youtube_url').attr('value', '');
    YOUKEBOX.createCuedVideo(url);
  },
  parseMessage: function(txt){
    return txt.replace(/\b(https?:\/\/[^\s)(]*)\b/, "<a href=\"$1\" target=\"$1\">$1</a>");
  },
  createCuedVideo: function(url){
    debug('posting new video: ' + url);
    $.post("/cued_videos",
            {'youtube_url': url, 'youkebox_id': YOUKEBOX.youkebox_id},
            function(data){
              if (data.youtube_id) {
                debug('succesfully posted youtube video ' + data.youtube_id);
                YOUKEBOX.trackPageview('/cued_videos/create');
              } else {
                alert(data.error);
                debug(data.error);
              }
            },
            "json"
            );
  },
  postChatMessage: function(){
    var body = $('textarea#chat_message_body').attr('value');
    if (body === '') {
      return false;
    }
    if (YBUTILS.validateEmailAddress(body)) {
      alert("If you are trying to post an email address " + body + " to a public chat, please add a bit more text to confirm your intention.");
      return false;
    }
    $('textarea#chat_message_body').attr('value', '');
    //$('textarea#chat_message_body').blur();
    debug('posting new chat message: ' + body);
    $.post(
      "/chat_messages",
      {'body': body, 'youkebox_id': YOUKEBOX.youkebox_id, 'start_from': newest_chat_message_id, 'muted': YOUKEBOX.isMuted()},
      function(data){
        YOUKEBOX.appendChatMessagesToScreen(data);
        YOUKEBOX.trackPageview('/chat_messages/create');
      },
      "json"
    );
    return false;
  },
  inviteUser: function(){
    var email = $('textarea#chat_message_body').attr('value');
    if (email === '') {
      alert('Please enter an email address or email addresses to invite a friend(s)!');
      $('textarea#chat_message_body').attr('value', 'email-of-a-friend-here!');
      $('textarea#chat_message_body').focus(); // just in case some browser doesn't support select below
      $('textarea#chat_message_body').select();
      return false;
    }
    $('textarea#chat_message_body').attr('value', '');
    debug('inviting users: ' + email);
    $.post("/youkebox_invitations/invite_many",
            {'body': email, 'youkebox_id': YOUKEBOX.youkebox_id},
            function(data){
              if (data.message) {
                debug('succesfully posted invite message ' + data.id);
                alert(data.message);
                YOUKEBOX.updateInvitations();
                YOUKEBOX.trackPageview('/youkebox_invitations/invite_many');
              } else {
                alert(data.message);
              }
            },
            "json"
            );
    return false;
  },
  appendChatMessagesToScreen: function(data){
    var current_timestamp = '';
    var user_timezone_hours = 0;
    for (var i=0; i < data.length; i++) {
      if (data[i].id > newest_chat_message_id) {
        // process timestamp here
        current_timestamp = '';
        user_timezone_hours = YOUKEBOX.stringify_number(YOUKEBOX.fix_timestamp_hours(data[i].hours));
        current_timestamp = current_timestamp.concat(user_timezone_hours);
        current_timestamp = current_timestamp.concat(':');
        current_timestamp = current_timestamp.concat(YOUKEBOX.stringify_number(data[i].minutes));
        if (data[i].nick === 'Youkebox' || data[i].nick === 'YoukeBox') {
          // system msg
          $('#chat_messages').append('<div class="system">' + current_timestamp + ' ' + YOUKEBOX.parseMessage(data[i].body) + '</div>').scrollTop($('#chat_messages').scrollTop() + autoScrollSpeed);
        } else {
          // user message
          $('#chat_messages').append('<div class="user">' + '<div><img src="' + data[i].avatar + '"/></div><span>' + current_timestamp + '</span><br/>' + '<b>' + data[i].nick + '</b> ' + YOUKEBOX.parseMessage(data[i].body) + '</div><div class="clear"></div>').scrollTop($('#chat_messages').scrollTop() + autoScrollSpeed);
        }
        newest_chat_message_id = data[i].id;
      }
    }
  },
  setMuteButtons: function() {
    if (YOUKEBOX.player) {
      if (YOUKEBOX.player.isMuted()){
        $('#mute').addClass('inactive');
        $('#unmute').removeClass('inactive');
      } else {
        $('#unmute').addClass('inactive');
        $('#mute').removeClass('inactive');
      }
    } else {
      $('#unmute').addClass('inactive');
      $('#mute').addClass('inactive');
    }
  },
  closeDedicateDialog: function(cued_video_id){
    var div_id = 'dedicate_' + cued_video_id;
    $('#' + div_id).remove();
  },
  searchDialog: function(){
    $('#search_spinner').show();
    $("#search").load("/search?q=" + encodeURIComponent($("#youtube_url").attr('value')), function(){
      $("#search").show();
      $('#search_spinner').hide();
      YOUKEBOX.recordNewPopup('search');
      YOUKEBOXSITE.addYoutubePreviews();
    });  
    $('#youtube_url').attr('value','');
  },
  initBigDialog: function(divName){
    if ($('div#' + divName).length === 0) {
      $('div#content').append('<div id="' + divName + '" class="big_dialogue" style="display:none;"/>');
    }
  },
  closeBigDialog: function(divName){
    $('div#' + divName).hide();
  },
  setupSingleToolTip: function(selector, content) {
    $(selector).qtip({
      position: {
        corner: {
          target: 'topRight',
          tooltip: 'bottomLeft'
        },
      },
      show: {
        delay: 1000
      },
      content: content,
      style: 'youkebox'
    });
  },
  setupToolTips: function () {
    $.fn.qtip.styles.youkebox = {
      position: {
        corner: {
          target: 'topRight',
          tooltip: 'bottomLeft'
        }
      },
      background: 'white',
      color: 'rgb(153, 153, 153)',
      border: {
        color: '#ccc'
      },
      tip: 'bottomLeft',
      name: 'cream'
    };  
    // play controls
    YOUKEBOX.setupSingleToolTip('#mute a', 'This button mutes audio.');
    YOUKEBOX.setupSingleToolTip('#increase_volume a', 'This button increases audio volume.');
    YOUKEBOX.setupSingleToolTip('#decrease_volume a', 'This button decreases the audio volume.');
    YOUKEBOX.setupSingleToolTip('#thumbs_up a', 'If you like the track that is playing, let the world know!');
    YOUKEBOX.setupSingleToolTip('#skip a', "If you're ready for the next track, vote to skip!");
    // links/buttons
    YOUKEBOX.setupSingleToolTip('#list_dialog_link', 'This link lets you choose from a list of most popular videos in this youkebox.');
    YOUKEBOX.setupSingleToolTip('#request_membership_link', 'If you want to request membership in this youkebox, click this link!');
    YOUKEBOX.setupSingleToolTip('#chat_message_button', 'Type a chat message to the box on the left. Press enter to post your message.');
    YOUKEBOX.setupSingleToolTip('#invite_button', 'Type or copy-paste email addresses to the box on the left. Press enter to invite people to this youkebox!');
  },
  requestMembership: function () {
    var url = '/membership_requests';
    debug('membership request');
    //$('#request_membership_link').hide();

    $.post(url,
      {'youkebox_id': YOUKEBOX.youkebox_id},
      function(data){
        if (data.message) {
          debug('succesfully created a membership request: ' + data.id);
          alert(data.message);
          YOUKEBOX.trackPageview('/membership_requests/create');
        } else {
          alert(data.message);
        }
      },
      "json"
      );
  },
  applyVolume: function () {
    if (YOUKEBOX.mute) {
      YOUKEBOX.player.setVolume(0);
    } else {
      YOUKEBOX.player.setVolume(YOUKEBOX.volume);
    }
  },
  increaseVolume: function() {
    if (YOUKEBOX.mute) {
      YOUKEBOX.mute = false;
    } else {
      YOUKEBOX.volume = YOUKEBOX.volume + 5;
    }
    YOUKEBOX.applyVolume();
    $('#mute').removeClass('inactive');
    $('#decrease_volume').removeClass('inactive');
    if (YOUKEBOX.volume == 100) {
      $('#increase_volume').addClass('inactive');
    }
  },
  decreaseVolume: function() {
    if (YOUKEBOX.mute) {
      YOUKEBOX.mute = false;
    } else {
      YOUKEBOX.volume = YOUKEBOX.volume - 5;
    }
    YOUKEBOX.applyVolume();
    $('#increase_volume').removeClass('inactive');
    if (YOUKEBOX.volume == 0) {
      $('#mute').addClass('inactive');
      $('#decrease_volume').addClass('inactive');
    }
  },
  trackPageview: function(path) {
    if(typeof(pageTracker) !== 'undefined') {
      pageTracker._trackPageview(path);
    }
  }
};

function updatePlayer() {
  //if (!YOUKEBOX.player) {
  //  YOUKEBOX.player = document.getElementById('youtube_player');
  //}
  if (YOUKEBOX.player) {
    // current video id is empty
    if (current_video_id === '') {
      YOUKEBOX.player.stopVideo();
      thumbs_up_given = false;
      return;
    }
    player_state = YOUKEBOX.player.getPlayerState();
    debug('player state is: ' + player_state);
    var currentVideoURL = YOUKEBOX.player.getVideoUrl();
    debug('currentVideoURL is:' + currentVideoURL);
    if (currentVideoURL && currentVideoURL.indexOf(current_video_id) == -1) {
      debug('stopping player because video has changed!');
      thumbs_up_given = false;
      YOUKEBOX.player.stopVideo(); // is this line necessary
      YOUKEBOX.player.loadVideoById(getCurrentVideoID());
      YOUKEBOX.trackPageview('/cued_videos/' + current_cued_video_id + '/stream');
      return;
    } else if (player_state == -1){
      debug('lets play the video');
      debug('mm... lets play' + getCurrentVideoID());
      YOUKEBOX.player.loadVideoById(getCurrentVideoID());
      return;
    } else if (player_state === 0 && current_video_time_left > 0){
      // player has ended, but the current video still has time left. time to switch to the new video then...
      YOUKEBOX.player.loadVideoById(getCurrentVideoID());
      YOUKEBOX.player.playVideo();
      return;
    } else if (player_state == 1 && playerIsInWrongPosition()){
      debug('video playing... but in the wrong time! lets cue');
      YOUKEBOX.player.seekTo(current_video_elapsed, true);
      return;
    } 
  }
}

YOUKEBOX.setUpAjaxErrorHandling = function () {
  $(document).ajaxError(function(){
      if (window.console && window.console.error) {
          console.error(arguments);
      }
  });
};

function setUpRemoteForms(){
  // making basic merb forms play with jQuery and ajax
  // function to send the jQuery form object via AJAX
  jQuery.fn.submitWithAjax = function() {
    this.submit(function() {
      // be sure to add the '.js' part so that it knows this is format:js
      $.post(this.action + '.js', $(this).serialize(), null, "script");
      return false;
    });
    return this;
  };
}

function toggleHeartbeat() {
  heartbeatActive = !heartbeatActive;
  if (heartbeatActive) { debug('heartbeat is now active');} else { debug('heartbeat is now inactive');}
}

function serverHeartbeat(){
  $.getJSON("/heartbeat",
          function(data){
            debug('server heartbeat: ' + data.heartbeat);
          });
}

function initTimeZone(){
  YOUKEBOX.timezone = (new Date().getTimezoneOffset()/60)*(-1); 
}

function initCueVideoForm() {
  $('#new_cued_video').submit(function() {
    YOUKEBOX.searchDialog();
    //YOUKEBOX.postNewVideo();
    return false;
  });
  $('#new_chat_message').submit(function() {
    YOUKEBOX.postChatMessage();
    return false;
  });
  $('#invite_button').click(function() {
    YOUKEBOX.inviteUser();
    return false;
  }); 
}

function initPlayer(videoId){
  debug('initializing player...');
  swfobject.embedSWF("http://www.youtube.com/apiplayer?" + getCurrentVideoID() +  "&enablejsapi=1&playerapiid=youtubeXplayer", "ytapiplayer", "486", "273", "8", null, null, YOUKEBOX.flash_params, YOUKEBOX.flash_attributes);
  debug('initialized player...');
}

function onYouTubePlayerReady(playerId) {
  debug("player_id from ready event is " + playerId);
  YOUKEBOX.player = document.getElementById(playerId);
  YOUKEBOX.player.addEventListener("onStateChange", "onytplayerStateChange");
  YOUKEBOX.applyVolume();
  YOUKEBOX.setMuteButtons();  
}

function onytplayerStateChange(newState) {
  if (newState == '-1') {
    debug('video unstarted...');
  }
  if (newState == '0') {
    debug('video stopped...');
  }
  if (newState == '1') {
    debug('video playing...');
  }
  if (newState == '2') {
    debug('video paused...');
  }
  if (newState == '3') {
    debug('video buffering...');
  }
  if (newState == '5') {
    debug('video cued...');
  }
}

function cuetoend() {
  if (YOUKEBOX.player) {
    var end = YOUKEBOX.player.getDuration();
    if (end !== 0) {
      YOUKEBOX.player.seekTo(end - 5, true);
    }
  }
}

function play() {
  if (YOUKEBOX.player) {
    YOUKEBOX.player.loadVideoById(getCurrentVideoID());
  }
}

function setCurrentVideoID(id) {
  if (current_video_id === '') {
    //debug('no current video set');
  } else if (current_video_id != id) {
    //debug('current video replaced by new one!');
  } else if (current_video_id == id) {
    return;
  }
  current_video_id = id;
  //debug('current video updated: ' + id);
}

function displayTime(a) {
  if (a < 0) {
    return 0;
  } else {
    var hours=Math.floor(a/3600); 
    var minutes=Math.floor(a/60)-(hours*60); 
    var seconds=a-(hours*3600)-(minutes*60); 
    var hs=''; 
    if (hours>0) { hs += hours + 'h&nbsp;'; } 
    if (minutes>0) { hs += minutes + 'm&nbsp;'; } 
    hs += seconds + 's';
    return hs;
  }
}

function updateCurrentVideoID(){
  var url = "/cued_videos/current.json?youkebox_id=" + YOUKEBOX.youkebox_id;
  url = fixForIE(url);
  var link_html = '';
  //link_html += '<img src="/images/youkebox_avatars/' + YOUKEBOX.youkebox_id + '.jpg" style="float: left; margin-right: 5px;"/>';
  //link_html += '<b>now playing</b><br />';
  if (YOUKEBOX.client_heartbeat) {
    $.ajax({
      url: url,
      dataType: "json",
      success: function(data){
              if (data.youtube_id) {
                // update data & controls
                enableSkipAndThumbsUp();
                setCurrentVideoID(data.youtube_id);
                current_cued_video_id = data.id;
                current_video_elapsed = data.elapsed;
                current_video_time_left = data.time_left;
                if (data.time_left < endThreshold) {
                  debug('video ended.');
                  disableSkipAndThumbsUp();
                  return;
                }
                // then update the display:
                //link_html += '<a href="http://www.youtube.com/watch?v=' + data.youtube_id + '" target="' + data.youtube_id + '">' + data.title + '</a>';
                //link_html += ' ' + displayTime(data.time_left) + 's';
                link_html += '<a id="now_playing_title" class="low_key" target="_blank" href="http://www.youtube.com/watch?v=' + data.youtube_id + '">' + data.title + '</a> ';
                if (data.cued_by !== '') {
                  link_html += '<span class="cued_by">by ' + data.cued_by + '</span>';
                } else {
                  link_html += '<span class="cued_by">randomized</span>';
                }
                link_html += ' <span class="time_left">' + displayTime(data.time_left) + '</span>';
                if(YOUKEBOX.member){
                  link_html += ' <a href="#" onclick="dedicateDialog(' + data.id + ');" class="mini_button">dedicate</a>';
                }
                $('span#now_playing').empty();
                $('span#now_playing').append(link_html);
                // adding qtip here gets a bit to difficult now really... let's refactor before we go ahead.
                // additionally, changing the UI itself might make the whole tooltip unnecessary. i guess
                // the whole song title should be visible anyway, even if it's a bit long.
                //
                //$('a#now_playing_title').qtip({
                //  position: {
                //    corner: {
                //      target: 'bottomMiddle',
                //      tooltip: 'topLeft'
                //    },
                //  },
                //  style: { 
                //    tip: 'topLeft',
                //    name: 'youkebox'
                //  },
                //  show: {
                //    delay: 0
                //  },
                //  content: data.title
                //});
              } else {
                link_html += '<span class="loading">nothing is playing at the moment</span><br/>&nbsp;<br/>';
                $('span#now_playing').empty();
                $('span#now_playing').append(link_html);
                //debug(data.error);
                YOUKEBOX.stopVideo();
                current_video_id = '';
                current_cued_video_id = '';
                current_video_elapsed = '';
                current_video_time_left = '';
              }
            },
      complete: function (XMLHttpRequest, textStatus) {
        updatePlayer();
        setTimeout(updateCurrentVideoID, 5000);
      }
    });
  } else {
    setTimeout(updateCurrentVideoID, 5000);
  }
}
function setChatMessagesHeight(){
  // $("#chat_messages").height($(window).height() - $("#footer").outerHeight() - $("#header").outerHeight() - $("#chat_participants").outerHeight());
  $("#chat_messages").height(512 - $("#chat_participants").outerHeight());
}

function setUpcomingHeight(){
  //$("#upcoming").height($(window).height() - $("#footer").outerHeight() - $("#header").outerHeight() - $("#video").outerHeight() - $("#now_playing").outerHeight() - $("#play_controls").outerHeight());
}

function fadeOut() {
  if (YOUKEBOX.player) {
    var current_volume = YOUKEBOX.volume;
    var fader = setInterval('fadeStep(' + (current_volume * 0.1) + ')', 100);
    setTimeout(endFade, 1000);
  }
}

function fadeStep(original_volume, amount) {
  if (YOUKEBOX.player) {
    YOUKEBOX.player.setVolume(YOUKEBOX.player.getVolume() - amount);
  }
}

endFade = function endFade() {
  if (YOUKEBOX.player) {
    YOUKEBOX.player.stopVideo();
    YOUKEBOX.player.setVolume(YOUKEBOX.volume);
  }
};

playerIsInWrongPosition = function playerIsInWrongPosition() {
  margin_of_error = 30; // in seconds
  if (Math.abs(YOUKEBOX.player.getCurrentTime() - current_video_elapsed) > margin_of_error) {
    debug('player time: ' + YOUKEBOX.player.getCurrentTime() + ', server time: ' + current_video_elapsed);
    return true;
  } else {
    return false;
  }
};

function mute() {
  if (YOUKEBOX.player) {
    YOUKEBOX.mute = true;
    YOUKEBOX.applyVolume();
    $('#mute').addClass('inactive');
    $('#decrease_volume').addClass('active');
    $('#increase_volume').addClass('active');
  }
  
}

function skip(id) {
  var url = '/cued_videos/' + id; // FIXME: instead of destroy, we could do this another way
  debug('skipping ' + id);
  $.ajax({
    type: "DELETE",
    url: url,
    success: function(msg){
      YOUKEBOX.trackPageview('/cued_videos/' + id + '/skip');
      //alert( "Deleted: " + msg );
    }
  });
}

function skipCurrentVideo() {
  var url = '/cued_videos/' + current_cued_video_id;
  debug('skipping ' + current_cued_video_id);
  disableSkipAndThumbsUp();
  $.ajax({
    type: "DELETE",
    url: url,
    data: {},
    success: function(msg){
      YOUKEBOX.trackPageview('/cued_videos/' + current_cued_video_id + '/skip');
      //alert( "Deleted: " + msg );
    }
  });
}

function thumbsUp() {
  var url = '/cued_videos/' + current_cued_video_id + '/thumbs_up';
  debug('thumbs up to ' + current_cued_video_id);
  thumbs_up_given = true;
  $('#thumbs_up').addClass('inactive');
  $.ajax({
    type: "POST",
    url: url,
    data: {},
    success: function(msg){
      YOUKEBOX.trackPageview('/cued_videos/' + current_cued_video_id + '/thumbs_up');
    }
  });
}

function rush(cued_video_id){
  var url = '/cued_videos/' + cued_video_id + '/rush';
  // TODO: remove rush button $('#thumbs_up').addClass('inactive');
  $.ajax({
    type: "POST",
    url: url,
    data: {},
    success: function(msg){
      YOUKEBOX.trackPageview(url);
    }
  });
}

function updateUpcoming(){
  var url = "/cued_videos.json?youkebox_id=" + YOUKEBOX.youkebox_id;
  url = fixForIE(url);
  if (YOUKEBOX.client_heartbeat) {
    $.ajax({
      url: url,
      dataType: 'json',
      success: function(data){
                var table, table_row, command_html;
                $('div#upcoming').empty();
                if (data.length > 0) {
                  table = $('<table id="upcoming"></table>');
                  table.appendTo($('div#upcoming'));
                  for (var i=0; i < data.length; i++) {
                    table_row = $('<tr id="cued_video_' + data[i].id + '">');
                    table_row.appendTo(table);
                    $('<td class="title"><a target="_blank" href="http://www.youtube.com/watch?v=' + data[i].youtube_id + '">' + YOUKEBOX.constrain(data[i].title, 35) + '</a></td>').appendTo(table_row);
                    $('<td class="cued_by">' + data[i].cued_by + '</td>').appendTo(table_row);
                    command_html = "";
                    if(YOUKEBOX.member){
                      command_html = command_html + ' <a href="#" onclick="dedicateDialog(' + data[i].id + ');" class="mini_button">dedicate</a>';
                      if(data[i].position > 1){
                        command_html = command_html + ' <a href="#" onclick="rush(' + data[i].id + ');" class="mini_button">rush</a>';
                      }
                      if(data[i].cued_by === 'RoboVJ') {
                        command_html = command_html + ' <a href="#" onclick="skip(' + data[i].id + ');" class="mini_button">skip</a>';
                      }
                    }
                    $('<td class="commands">' + command_html + '</td>').appendTo(table_row);
                    //$('div#upcoming').append(' <a href="#" onclick="dedicateDialog(' + data[i].id + ');" class="mini_button">down</a>');
                    //$('div#upcoming ul').append(' <input id="veto_' + data[i].id + '" type="submit" value="skip" />' + '<br />');
                    //$('input#veto_' + data[i].id).click(function() {
                    //  skipCuedVideo($(this).attr('id').replace('veto_', ''));
                    //});
                  }
                }
      },
      complete: function (XMLHttpRequest, textStatus) {
        updatePlayer();
        setTimeout(updateUpcoming, 4000);
      }
    });
  } else {
    setTimeout(updateUpcoming, 4000);
  }
}

function updatePresences(){
  var url = "/presences.json?youkebox_id=" + YOUKEBOX.youkebox_id;
  url = fixForIE(url);
  if (YOUKEBOX.client_heartbeat) {
    $.ajax({
      url: url,
      dataType: "json",
      success: function(data){
              $('#chat_participants').empty();
              for (var i=0; i < data.length; i++) {
                if(data[i].avatar !== ""){
                  //$('#chat_participants').append('<div id="presence_' + data[i].id + '" class="avatar"><img src="' + data[i].avatar + '"/></div');
                  //$('#chat_participants').append('<div><img id="presence_' + data[i].id + '" src="' + data[i].avatar + '"/>');
                  $('#chat_participants').append(data[i].nick);
                } else {
                  $('#chat_participants').append(data[i].nick);
                }
                $('#chat_participants').append(' ');
                // YOUKEBOX.setupSingleToolTip('#presence_' + data[i].id, data[i].nick);
                YOUKEBOX.setupSingleToolTip('#presence_' + data[i].id, data[i].nick);
              }
            },
      complete: function (XMLHttpRequest, textStatus) {
        setTimeout(updatePresences, 12000);
      }
    });
  } else {
    setTimeout(updatePresences, 12000);
  }
}

function updateChatMessages(){
  var url = "/chat_messages.json?youkebox_id=" + YOUKEBOX.youkebox_id + "&muted=" + YOUKEBOX.isMuted();
  if (newest_chat_message_id > 0) {
    url += '&start_from=' + newest_chat_message_id;
  }
  url = fixForIE(url);
  if (YOUKEBOX.client_heartbeat) {
    $.ajax({
            url: url,
            dataType: "json",
            success: function(data){
              YOUKEBOX.appendChatMessagesToScreen(data);
            },
            complete: function (XMLHttpRequest, textStatus) {
              setChatMessagesHeight();
              setTimeout(updateChatMessages, 2000);
            }
    });
  } else {
    setTimeout(updateChatMessages, 2000);
  }
}

YOUKEBOX.fix_timestamp_hours = function (hours) {
  var new_hours = (YOUKEBOX.timezone + hours) % 24;
  if (new_hours < 0) {
    new_hours += 24;
  }
  return new_hours;
};

YOUKEBOX.stringify_number = function (number) {
  if (number < 10) {
    return '0'.concat(number.toString());
  } else {
    return number.toString();
  }
};

function initChatMessages(){
  $("body").mouseup(function(){
      $("#chat_messages").removeClass('autoscrolling_disabled');
      autoScrollSpeed = 100000;
    });
  $("div#chat_messages").mousedown(function(){
      autoScrollSpeed = 0;
      $("#chat_messages").addClass('autoscrolling_disabled');
    });
}


function dedicateDialog(cued_video_id){
  var div_id = 'dedicate_' + cued_video_id;
  $('div#content').append('<div id="' + div_id + '" class="big_dialogue" />');
  YOUKEBOX.recordNewPopup(div_id);
  $('#' + div_id).load("/cued_videos/" + cued_video_id + "/dedications/new", function(){
    $("form.dedication").submit(function() {
      // be sure to add the '.js' part so that it knows this is format:js
      $.post(this.action + '.js', $(this).serialize(), null, "script");
      YOUKEBOX.closeDedicateDialog(cued_video_id);
      return false;
    });
   });
}

// THE TWO FOLLOWING CAN B COMBINED TO initBigDialog()

function initSearch(){
  if ($('div#search').length === 0) {
    $('div#content').append('<div id="search" class="big_dialogue" />');
  }
}

function showInvitations(){
  if ($('div#view_invitations').length === 0) {
    $('div#content').append('<div id="view_invitations" class="big_dialogue" />');
  }
  $("#view_invitations").hide();
  $("#view_invitations").load("/youkeboxes/" + YOUKEBOX.youkebox_id + "/youkebox_invitations", function(){
    $("#view_invitations").show();
    YOUKEBOX.recordNewPopup('view_invitations');
  });  
}

function inviteAgain(id) {
  debug('inviting again... ' + id);
  var url = '/youkebox_invitations/' + id + '/send_again';
  debug('inviting again, youkebox_invitation.id: ' + id);
  $.ajax({
    type: "PUT",
    url: url,
    data: {},
    success: function(msg){
      YOUKEBOX.updateInvitations();
      YOUKEBOX.trackPageview('/youkebox_invitations/' + id + '/send_again');
    }
  });
}

function cancelInvite(id) {
  var url = '/youkebox_invitations/' + id;
  debug('canceling invite, youkebox_invitation.id: ' + id);
  $.ajax({
    type: "DELETE",
    url: url,
    data: {},
    success: function(data){
      YOUKEBOX.updateInvitations();
      YOUKEBOX.trackPageview('/youkebox_invitations/' + data.id + '/destroy');
    }
  });
}

function listDialog(){
  if ( $("#cued_video_list").length === 0 ) { 
    $('div#content').append('<div id="cued_video_list" class="big_dialogue" />');
  }
  $('#cued_video_list').hide();
  $('#list_spinner').show();
  $('#cued_video_list').load(
    "/youkeboxes/" + YOUKEBOX.youkebox_id + "/youkebox_videos", 
    function(){
      $('#cued_video_list').show();
      $('#list_spinner').hide();
      YOUKEBOX.recordNewPopup("cued_video_list");
    }
  );
}

function closeListDialog(){
  $('#cued_video_list').remove();
}

function settingsDialog(){
  if ( $("#youkebox_settings").length === 0 ) { 
    $('div#content').append('<div id="youkebox_settings" class="big_dialogue" />');
  }
  $('#youkebox_settings').load(
    "/youkeboxes/" + YOUKEBOX.youkebox_id + "/settings",
    function () {
      YOUKEBOX.recordNewPopup('youkebox_settings');
    }
  );
}

function closeSettingsDialog(){
  $('#youkebox_settings').remove();
}

function getMoreCueHistory(start_from){
  var url = '/youkeboxes/' + YOUKEBOX.youkebox_id + '/cued_videos/cue_history.html?start_from' + start_from;
  $.ajax({
    url: url, 
    dataType: 'html',
    success: function(data){
      $('#list_of_cued_videos').append(data);
    }
  });  
}

YOUKEBOX.init = function () {
  YOUKEBOX.setUpAjaxErrorHandling();
  YOUKEBOX.youkebox_id = $('div#youkebox_id').attr('name'); // TODO: is this questionable use of the name property?
  debug('youkebox id:' + YOUKEBOX.youkebox_id);
  if($('div#membership').attr('name') == 'true'){
    YOUKEBOX.member = true; // TODO: is this questionable use of the name property?
  }
  updateCurrentVideoID();
  initPlayer();
  initCueVideoForm();
  initChatMessages();
  updatePresences();
  YOUKEBOX.initBigDialog('search');
  init = true;
  updateChatMessages();
  updateUpcoming();
  setUpcomingHeight(); // because song title height might have been altered by the above
  setChatMessagesHeight();
  YOUKEBOX.updateYoukebox();
  YOUKEBOX.setMuteButtons();
  initTimeZone();
  YOUKEBOX.setupKeyboardListeners();
  YOUKEBOX.setupToolTips();
};

// Functions have been defined - let's do it.

swfobject.registerObject("myId", "9.0.0", "expressInstall.swf");

$(document).ready(function(){
  YOUKEBOX.init();
  $(window).resize(setChatMessagesHeight);
  $(window).resize(setUpcomingHeight);
  setChatMessagesHeight();
  setUpcomingHeight();
  $.fn.ajaxSubmit.debug = true;
});

