var time_left;
var timer_id;

function show_splash() {
  if(check_cookie()) return;
  start_timer();
  $('#splash').show();
  drop_cookie();
}

function hide_splash() {
  stop_timer();
  $('#splash').hide();
}

function set_timer() {
  timer_id = window.setTimeout(timer_tick, 1000);
}

function start_timer() {
  time_left = 20;
  update_time_left();
  set_timer();
}

function stop_timer() {
  window.clearTimeout(timer_id);
}

function timer_tick() {
  time_left--;
  if(time_left == 0) {
    hide_splash();
    return;
  }
  update_time_left();
  set_timer();
}

function update_time_left() {
  $('#splash_time_left')[0].innerHTML = time_left;
}

function drop_cookie() {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + 1);
  document.cookie = 'seen_splash=1;expires=' + exdate.toGMTString();
}

function check_cookie() {
  var cookies = document.cookie.split(';');
  for(var x in cookies) {
    var c = cookies[x];
    c = c.replace(/^\s+/, '');
    if(c == 'seen_splash=1') return true;
  }
  return false;
}

$(document).ready(show_splash);