function show_format(id) {
	new Ajax.Request('/format/show/'+id, {asynchronous:true, evalScripts:true}); return false;
}
function show_video_format(id) {
 	new Ajax.Request('/media/format/'+id, {asynchronous:true, evalScripts:true}); return false;
}

function hide_all_popups() {
  //$$('div.popup').each(function(elm) { elm.hide(); });
}

function refresh_dashboard(last_log_id, last_discover_id, last_discover_status) {
  new Ajax.Request('/dashboard/index?encoded_video='+document.getElementsByClassName('encoded_video').length+'&video='+document.getElementsByClassName('video').length+'&last_log='+last_log_id+'&job='+document.getElementsByClassName('job').length+'&last_discover='+last_discover_id+'&last_discover_status='+last_discover_status+'&download='+document.getElementsByClassName('download').length, {asynchronous:true, evalScripts:true, method:'get'});
}

function progressPulse(percent) {
    if(percent == "unknown") percent = "0%";
    $("progress_bar").style.width = percent;
}

function fillDefaultValueFormat() {
    $('format_video_bitrate').value = "300";
    $('format_audio_bitrate').value = "64";
    $('format_width').value = "320";
    $('format_height').value = "240";
}

function disable(id) {
    $(id).disabled = true;
}

function enable(id) {
    $(id).disabled = null;
}

function AudioSettings(state) {
    if(state == 0) {
	    disable('format_specs_audio_codec');
	    disable('format_specs_audio_bitrate');
	    disable('format_specs_sample_rate');
	  } else {
	    enable('format_specs_audio_codec');
	    enable('format_specs_audio_bitrate');
	    enable('format_specs_sample_rate');
	  }
}

function VideoSettings(state) {
    if(state == 0) {
	    disable('format_specs_video_bitrate');
	    disable('format_specs_fps');
	    disable('format_specs_width');
	    disable('format_specs_height');
	  } else {
	    enable('format_specs_video_bitrate');
	    enable('format_specs_fps');
	    enable('format_specs_width');
	    enable('format_specs_height');
	  }
}

function autoFillFormatForm(value) {
  if(value == 0) { return false; }
  new Ajax.Request('/format/show/'+value+'?auto_fill=true', {method:'get', asynchronous:true, evalScripts:true}); return false;
}

function showEditLinkIfOwner(format) {
  if($('format_edit')) {
    new Ajax.Request('/format/'+format, {method:'get', asynchronous:true, evalScripts:true}); return false;
  }
}

function getApproxSize(video, format) {
  if(video == "0") { return false; }
  new Ajax.Request('/video/' + video + '?approximative_size=true&format_id='+format, {method:'get', asynchronous:true, evalScripts:true}); return false;
}

function showVideoInfo(video) {
  $('loading_'+video).show();
	new Ajax.Request('/video/' + video, {method:'get', asynchronous:true, evalScripts:true}); return false;
}

function showEncodedVideoInfo(video) {
  $('loading_'+video).show();
	new Ajax.Request('/encoded_video/' + video, {method:'get', asynchronous:true, evalScripts:true}); return false;
}

function selectItem(selectId, condValue) {
    s = $(selectId);
    for(i=0; i<= s.options.length -1; i++) {
        if(s.options[i].value == condValue) { s.options[i].selected = true;}
    }
}

function replaceFileInputByMedia(access) {
  new Ajax.Request('/media/add_to_session?from=encode&access='+access, {asynchronous:true, evalScripts:true}); return false;
}

function captureKeyPress(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var key = String.fromCharCode(code);
	if(key == "s") {
	  if($('quick_send_form')) { $('quick_send_form').show(); $('url').focus(); }
	}
}

function show_dates_in_words(time_server) {
  var strongs = document.getElementsByTagName('strong');
  for (var i=0; i<strongs.length; i++) {
    if (strongs[i].className.match(/\blog_date\b/i)) {
      strongs[i].innerHTML = get_local_time_for_date(strongs[i].title, time_server);
    }
  }
}

function get_local_time_for_date(time, time_server) {
  system_date = new Date(time);
  user_date = new Date(time_server);
  delta_minutes = Math.floor((user_date - system_date) / (60 * 1000));
  if (Math.abs(delta_minutes) <= (8*7*24*60)) { // eight weeks... I'm lazy to count days for longer than that
    distance = distance_of_time_in_words(delta_minutes);
    return distance + ' ago';
  } else {
    return 'on ' + system_date.toLocaleDateString();
  }
}

// a vague copy of rails' inbuilt function, 
// but a bit more friendly with the hours.
function distance_of_time_in_words(minutes) {
  if (minutes.isNaN) return "";
  minutes = Math.abs(minutes);
  if (minutes < 1) return ('less than a minute');
  if (minutes < 50) return (minutes + ' minute' + (minutes == 1 ? '' : 's'));
  if (minutes < 90) return ('about 1 hour');
  if (minutes < 1080) return ('about '+Math.round(minutes / 60) + ' hours');
  if (minutes < 1440) return ('one day');
  if (minutes < 2880) return ('about one day');
  else return (Math.round(minutes / 1440) + ' days')
}
