var transBoxMovieInfo = new Array();
var transBoxMovieUrl = new Array();
var movieUniqueURL = new Array();
var pagerCache = new Array();

/*--------------- Flash Player ---------------*/
/*--------------------------------------------*/
function getFlashMovie(movieName) {
  if (typeof(movie_flashplayer_script) != 'undefined' && movie_flashplayer_script == true) {
    if ($.browser.msie) {
      return window[movieName];
    } else {
      return document[movieName];
    }
  }

  if ($.browser.msie) {
    return window[movieName + (movie_flashplayer_script == true ? "" : "-object")];
  } else {
    return document[movieName + (movie_flashplayer_script == true ? "" : "-embed")];
  }
}


/*---------------  Movie Panel  ---------------*/
/*---------------------------------------------*/
function MovieReplayBinder() {
  $("a.replay").click(function() {
    getFlashMovie("makememovie").replay();

    return false;
  });
}

function MoviePanelInit() {
  $('div#movie-panel-wrapper').fadeTo(1, 0, function() {
    $('div#movie-panel-wrapper').css({
      'z-index': -1000
    });

    // Fix MAKEME-582
    if (os.indexOf("Mac") < 0 || !$.browser.mozilla) {
      $('div#movie-panel-wrapper').show();
    }
  });

  $('div#movie-panel-wrapper .close-button').click(function() {
    $('div#movie-panel-content').html("");
    $('div#movie-panel-wrapper').fadeTo(200, 0, function() {
      $('div#movie-panel-wrapper').css({
        'z-index': -1000
      });

      // Fix MAKEME-582
      if (os.indexOf("Mac") >= 0 && $.browser.mozilla) {
        $('div#movie-panel-wrapper').hide();
      }

      $('.movie-flash-player-container').css('visibility', 'visible');
    });
  });
}

function UpdateMovieTabLinks(movie_id) {
  $("a.movie-tab-link").each(function() {
    var orgHref = $(this).attr('href').split("#");
    $(this).attr('href', orgHref[0] + "#" + movie_id);
  });
}


/**
 * When the movie in player is changed, some elements
 * on the page are also need to be changed.
 * @param string data
 *   should be a Json string
 */
function FlashPlayerCallback(data, type) {
  var callbackData = Drupal.parseJson(data);
  if (callbackData.status) {
    // update unique url
    $("#uniqueURL .url").html(callbackData.unique_url);
    if(typeof(makeme_comment_ajax_login_url) != 'undefined' && callbackData.comment_ajax_login) {
    	makeme_comment_ajax_login_url = callbackData.comment_ajax_login;
    }
    // update movie tab
    UpdateMovieTabLinks(callbackData.movie_id);

    // change page title and tab to movie title by GJ
    if (callbackData.movie_title) {
      
      var movie_title = decodeURIComponent(callbackData.movie_title);//.replace('+', ' ');     
      var movie_title_cut = decodeURIComponent(callbackData.movie_title_cut);//.replace('+', ' ');
      var page_title = decodeURIComponent(callbackData.page_title);//.replace('+', ' ');
      var cur_title_name = (movie_title + ' | ' + page_title).replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
      if(jQuery.browser.msie) {
      	window.document.title = cur_title_name;
      }
      else {
      	document.title = cur_title_name;
      }
     
      // If sIFR is enabled
      if($('.main-tab-only-one').hasClass("sIFR-replaced")) {
        sIFR.replacements['.tabs-wrapper ul.primary li.active'][0].replaceText(movie_title_cut);
      } 
      else {
        $('.main-tab-only-one').html(movie_title_cut);
      }
    }

    if (type == 'internal') {
      if (typeof(movieThumbList) != 'undefined' && callbackData.is_current_mission == true) {
        movieThumbList.update(data);
      }

      if (typeof(previousMovieThumbList) != 'undefined' && callbackData.is_current_mission == false) {
        previousMovieThumbList.update(data);
      }
    if (typeof MakemeComment != 'undefined') {
      callbackData.read_comments = decodeURIComponent(callbackData.read_comments).replace('+', ' ');
      callbackData.write_comment = decodeURIComponent(callbackData.write_comment).replace('+', ' ');
    MakemeComment.reset(callbackData);
    }
    }
  }
}

function FlashPlayerMovieOver(data, type) {
  var callbackData = Drupal.parseJson(data);
  if (callbackData.status) {

    if (typeof(movieThumbList) != 'undefined' && callbackData.is_current_mission == true) {
      movieThumbList.hideTransbox(data);
    }

    if (typeof(previousMovieThumbList) != 'undefined' && callbackData.is_current_mission == false) {
      previousMovieThumbList.hideTransbox(data);
    }
  }

}

/*----  Admin movie - activate/deactivate  ----*/
/*---------------------------------------------*/
function AdminMovieActionBinder() {
  $("a.movie-admin-action").unbind('click');
	
  $("a.movie-admin-action").click(function () {
    var o = $(this);
    var url = o.attr("href");

    $.post(
      url,
      {'ajax': true},
      function(response) {
        response = Drupal.parseJson(response);
        if (response.status == true) {
   //       o.replaceWith(response.data);
   		  $('.movie-admin-action-box').html(response.action_data);
          $(".movie-promote-action-box").html(response.promote_data);
          AdminMovieActionBinder();
          AdminMoviePromoteBinder();
        } else {
          alert(response.data);
        }
      }
    );

    return false;
  });
}
function AdminMoviePromoteBinder() {
  $("a.movie-promote-action").unbind('click');
	
  $("a.movie-promote-action").click(function () {
    var o = $(this);
    var url = o.attr("href");

    $.post(
      url,
      {'ajax': true},
      function(response) {
        response = Drupal.parseJson(response);
        if (response.status == true) {
          o.replaceWith(response.data);
   		  $('.movie-admin-action-box').html(response.action_data);
          $(".movie-promote-action-box").html(response.promote_data);
          AdminMovieActionBinder();
          AdminMoviePromoteBinder();
        } else {
          alert(response.data);
        }
      }
    );

    return false;
  });
}

/*----  Admin movie person (pic) - activate/deactivate  ----*/
/*----------------------------------------------------------*/
function AdminMoviePersonActionBinder() {
  $("a.movie-person-admin-action").unbind('click');

  $("a.movie-person-admin-action").click(function () {
    var o = $(this);
    var url = o.attr("href");

    $.post(
      url,
      {'ajax': true},
      function(response) {
        response = Drupal.parseJson(response);
        if (response.status == true) {
          o.replaceWith(response.data);
          AdminMoviePersonActionBinder();
        } else {
          alert(response.data);
        }
      }
    );

    return false;
  });
}

/*----  Unique url - copy link  ----*/
/*----------------------------------*/
function UniqueUrlCopyBinder() {
  $("#uniqueURL a.copy").click(function() {
    var url = $("#uniqueURL p.url").html();

    if (movie_unique_url_copy_swf && $.copy) {
      $.copy(url, movie_unique_url_copy_swf);
    }

    return false;
  });
}

/*----  Movie confirm page  ----*/
/*------------------------------*/
function SelectThumbLinkBinder() {
  $(".movie-select-thumb-item a").click(function() {
    $(".movie-select-thumb-item a").removeClass("selected");

    var href = $(this).attr("href").split("#");
    var selected_thumb_id = href[1];
    $("#selected-thumb-id").attr("value", selected_thumb_id);
    $(this).addClass("selected");

    return false;
  });
}

/*---------------------------------------------*/
// Global killswitch
if (Drupal.jsEnabled) {
  $(document).ready(function () {
    // movie panel init
    MoviePanelInit();
    // movie admin (activate/deactivate)
	AdminMovieActionBinder();
	AdminMoviePromoteBinder();
    AdminMoviePersonActionBinder();

    // unique url copy
    UniqueUrlCopyBinder();

    // replay
    MovieReplayBinder();

    // movie confirm page
    SelectThumbLinkBinder();


  });
}

