var current_pos = 0;

function hash() {
  return location.hash.replace("#", "");
}

function init(){
  // redirect direct or external link.
  if (document.referrer != null && document.referrer != "" && ! document.referrer.match(/^https?:\/\/(www\.)?suzukishin.jp/)) { 
    var matches = hash().match(/(\d{4})\.(\d{2})\.(\d{2})\.(\d{2}\.\d{2}\.\d{2}\.\d{3})/);
    if (matches) {
      var url = "/photo/" + matches[1] + "/" + matches[2] + "/" + matches[3] + "/#" + matches[4];
      location.href = url;
      return;
    }
  }
  $(document).ready(initReady);
}

function initReady() {
  var h = hash();
  for (var i = 0; i < items.length; i++) {
    if(h == items[i]){
      current_pos = i;
      break;
    }
  }

  $(document).shortkeys({
    'j':itemNavigationNext,
    'n':itemNavigationNext,
    'k':itemNavigationPrev,
    'p':itemNavigationPrev
  });
}

function itemNavigation(i) {
  current_pos = i;
  if (i < 0) {
    if (prev_link != null) {
      location.href = "/photo/" + prev_link;
    } else {
      current_pos = 0;
    }
  } else if (items.length <= i) {
    if (next_link != null) {
      location.href = "/photo/" + next_link;
    } else {
      current_pos = items.length;
    }
  } else {
    location.href = '#' + items[i];
  }
}

var itemNavigationNext = function() {
  itemNavigation(current_pos + 1);
}

var itemNavigationPrev = function() {
  itemNavigation(current_pos - 1);
}
 
init();
