/** Define initial namespace **/
App = {}

/** Video Tour **/
var pending_video = -1;
function playerReadyCallback(thePlayer) {
  App.TourPlayer.player = document.getElementById(thePlayer['id']);
  App.TourPlayer.player.addControllerListener("ITEM","App.TourPlayer.markPlaylistItem");
  if (pending_video > -1) {
    setTimeout(function () {
      App.TourPlayer.play(pending_video);
    }, 500);
  } // if
} // playerReadyCallback


App.TourPlayer = function () {
  return {
    element_prefix : null,
    title : null,
    overlay : null,
    border : null,
    container : null,
    video_container : null,
    player : null,
    videos_list : null,
    videos : null,


    init : function (prefix, initial_video) {
      var tour_player = this;
      
      this.element_prefix = prefix;
      this.title = $('#'+prefix+'_title');
      this.overlay = $('#'+prefix+'_overlay');
      this.border = $('#'+prefix+'_border_overlay');
      this.container = $('#'+prefix+'_container');
      this.video_container = $('#'+prefix+'_video');
      
      this.videos_list = $('#'+prefix+'_videos');
      
      this.videos = this.videos_list.find('a');
      this.videos.each(function () {
        var video = $(this);
        video.click(function () {
          var video_id = video.attr('video_id');
          App.TourPlayer.play(video_id);
          return false;
        });
      });

      var light_switch = $('#'+prefix+'_controls li.videotour_light_switch a');
      light_switch.click(function () {
        tour_player.overlay.toggleClass('lights_off');
        tour_player.border.toggleClass('lights_off');
        tour_player.container.toggleClass('lights_off');
        return false;
      });

      var dialog_close = $('#'+prefix+'_controls li.videotour_close a');
      dialog_close.click(function () {
        App.TourPlayer.close();
        return false;
      });

    },

    play : function (video_id, stupid_fix) {
      var tour_player = this;

      this.videos.removeClass('current');

      var video = this.videos_list.find('a[video_id='+video_id+']').addClass('current');
      var video_height = video.attr('video_height');
      var video_id_real = video.attr('video_id');
      var video_title = video.text();

      if (!tour_player.player || !this.container.is(':visible')) {
        pending_video = video_id_real;
        this.show();
        return false;
      } else {
        tour_player.player.sendEvent("ITEM", video_id_real);
      } // if

      tour_player.title.text(video_title);

      var player_height = parseInt(video_height) + 24;
      tour_player.video_container.css('height', player_height);

      var container_height = player_height + 10 + parseInt(tour_player.title.height());
      tour_player.container.css('height', container_height);
      tour_player.container.css('margin-top', parseInt(container_height / 2) * (-1));

      var border_height = container_height + 20;
      tour_player.border.css('height', border_height);
      tour_player.border.css('margin-top', parseInt(border_height / 2) * (-1));
    },

    markPlaylistItem : function (player) {
      this.videos.removeClass('current');
      this.videos_list.find('a[video_id='+player.index+']').addClass('current');
    },

    show : function (callback_function) {
      this.overlay.show();
      this.border.show();

      this.videos.removeClass('current');

      if (typeof callback_function == 'function') {
        this.container.show(null, callback_function);
      } else {
        this.container.show();
      }
    },

    close : function () {
      this.player.sendEvent("STOP","true");
      this.overlay.hide();
      this.border.hide();
      this.container.hide();
      pending_video = -1;
      return false;
    }
  }
}();

