function modalize(link) {
  modal = $("#modal");
  modal.load(link.href);
  openModal(modal);
  return false;
}
// 
// function openModal(content) {
//   content.modal();
//   $(window).keyup(function(e) { if ( e.keyCode == 27 ) closeModal(); });
// }

function closeModal() {
  $(window).unbind('keyup');
  $('.modalClose').click();
}

var live_search_timeout;
var live_search_last_keypress;
var live_search_interval = 750;

$(function() {

  $(".message").each(function() {
    if($(this).attr("class") == "message") // And not combined with another class...
      $(this).effect("highlight", { "color": "#e1f4d4" }, 2000);
  });

  $(".live_search input").keyup(function(e) {
    if ( e.keyCode == 224 || e.keyCode == 91 || e.keyCode == 9 || (e.keyCode >=37 && e.keyCode <= 40) ) return;

    if ( $(this).val().length == 1 ) return;

    if ( e.keyCode == 13 ) {
      window.clearTimeout(live_search_timeout);
    }
    else {
      window.clearTimeout(live_search_timeout);
            
      if(live_search_last_keypress) {
        live_search_interval = (new Date() - live_search_last_keypress) > 500 ? 1000 : 400;
      }
      
      live_search_last_keypress = new Date();
      live_search_timeout = window.setTimeout(function() {
        $(".live_search").trigger("submit");
      }, live_search_interval);
    }
  });

  $(".live_search").submit(function() {
    var query = $(this).serialize();
    $(".preserve_location").each(function() {
      this.href = mergeParams(this.href, query);
    });
    $(this).next("table").load(window.location + "?" + query);
    return false;
  });

});

function mergeParams(url, params) {
  var location = null;
  var query_string = null;
  
  if(/\?/.test(url)) {
    var capture = url.match(/^(.*?)\?(.*)$/);
    location = capture[1];
    query_string = capture[2];
  } else {
    location = url;
  }
  
  if(query_string) {
    query_string = params;
  } else {
    query_string = params;
  }
  
  // Transform query-string...
  
  return location + "?" + query_string;
}

function deleteWithModal(link) {
  modal = $("#delete_modal");
  modal.load(link.href);
  openModal(modal);
  return false;
}

// function openModal(content) {
//   content.modal();
//   $(window).keyup(function(e) { if ( e.keyCode == 27 ) closeModal(); });
// }

function closeModal() {
  $(window).unbind('keyup');
  $('.modalClose').click();
}

// Flash player scrubbing
// Get the flash player object
function getPlayer() { return $('#video_player_container')[0]; }

// Flash callback, run when the player is ready. Sets up event listeners
function playerReady() {
  getPlayer().addViewListener('START_SCRUB', 'setStartTime');
  getPlayer().addViewListener('END_SCRUB', 'setEndTime');
}

// Event handler for setting the start time of a cut. Accesses a form element with id start_time.
function setStartTime(info) {
  if ( info['time'] || info['time'] == 0) $('.start_time').val(info['time']);
}

// Event handler for setting the end time of a cut. Accesses a form element with id end_time.
function setEndTime(info) {
  if ( info['time'] || info['time'] == 0) $('.stop_time').val(info['time']);
}

// Function to turn on/off the cut point scrubbers.
function toggleScrubbers() { return getPlayer().toggleScrubbers(); }

function checkToggleStatus(){
  if ($('#toggle_crop').attr('checked'))
  {
    $('#download_type').html('Select Download');
    $(".cropping_instructions").show();
  }
  else
  {
    $('#download_type').html('Instant Download');
    $(".cropping_instructions").hide();
  }
  
}

$(document).ready(function() {
  checkToggleStatus();
  $("#toggle_crop").click(function() {
    toggleScrubbers();
    $(".cropping_instructions").toggle();
    $(".video_file_download.flv, .video_file_download.custom, #original_source").toggle();
    checkToggleStatus();
  });
});