var page = window.location.pathname;
var updating = false;
var subs = [];
var section = '';

function updateCal(m, y) {
  doUpdateCal(m, y);
  jumpToMonth(m, y);
  return false;
}

function doUpdateCal(m, y) {
  new Ajax.Updater('calendar', path+'/c/calendar', {
    method:     'get',
    parameters: 'yr=' + y + '&mon=' + m + '&sub=' + section
	      + '&parent=' + page + '&top=' + path
  });
  calYear = y;
  calMonth = m;
}

function checkCalendar() {
  var id = ids[skip];
  var dir = getDirection(getIdFromDate(calYear, calMonth), id);
  var ym = getYearMonthDay(id);
  y = ym[0];
  m = ym[1];
  //alert(id+"\n"+dir+"\n"+calYear+"\n"+calMonth+"\n"+y+"\n"+m);
  if (dir == 'down' && (y < calYear || m < calMonth)
   || dir == 'up' && (y > calYear || m > calMonth)) {
    doUpdateCal(m, y);
  }
}

function searchFocus(elm) {
  if (elm.value == 'search') elm.value = '';
}

function searchBlur(elm) {
  if (elm.value == '') elm.value = 'search';
}

function scrollDown() {
  if (skip > ids.length - numShow || updating) {
    return false;
  }
  updating = true;

  doScrollDown(skip, skip+numShow);

  skip++;
  checkLinks();
  checkCalendar();
  updating = false;
  return false;
}

function doScrollDown(oldOffset, newOffset) {
  if (isEmpty(oldOffset) || isEmpty(newOffset)) {
    return false;
  }
  var newId = ids[newOffset]
  var newDiv = newId;
  var oldDiv = ids[oldOffset];
  var newSub = '';
  if (subs.length) {
    oldDiv = subs[oldOffset]+'_'+oldDiv;
    newSub = subs[newOffset];
    newDiv = newSub+'_'+newId;
  }

  Effect.SlideUp('entry_' + oldDiv);

  var addDiv = document.createElement('div');
  addDiv.id  = 'entry_' + newDiv;
  $('entries').appendChild(addDiv);

  new Ajax.Request(path+'/c/entry', {
    method:     'get',
    parameters: 'id=' + newId + '&sub=' + newSub,
    onSuccess:   function (req) {
      addDiv.innerHTML = req.responseText;
      Effect.Appear(addDiv);
    }
  });

}

function scrollUp() {
  if (!skip || updating) {
    return false;
  }
  updating = true;

  skip--;
  doScrollUp(skip, skip+numShow, skip+1);

  checkLinks();
  checkCalendar();
  updating = false;
  return false;
}

function doScrollUp(newOffset, oldOffset, insertOffset) {
  if (isEmpty(newOffset) || isEmpty(oldOffset) || isEmpty(insertOffset)) {
    return false;
  }
  var newId = ids[newOffset];
  var newDiv = newId;
  var newSub = '';
  var oldDiv = ids[oldOffset];
  var insertDiv = ids[insertOffset];
  if (subs.length) {
    oldDiv = subs[oldOffset]+'_'+oldDiv;
    newSub = subs[newOffset];
    newDiv = newSub+'_'+newId;
    insertDiv = subs[insertOffset]+'_'+insertDiv;
  }

  Effect.Fade('entry_'+oldDiv);

  var addDiv = document.createElement('div');
  addDiv.id  = 'entry_' + newDiv;
  $('entries').insertBefore(addDiv, $('entry_' + insertDiv));

  new Ajax.Request(path+'/c/entry', {
    method:     'get',
    parameters: 'id=' + newId + '&sub=' + newSub,
    onSuccess:   function (req) {
      addDiv.innerHTML = req.responseText;
      Effect.Appear(addDiv);
    }
  });

}

function checkLinks() {
  var dl = $('downLink').style;
  var dt = $('downText').style;
  var ul = $('upLink').style;
  var ut = $('upText').style;

  if (skip > ids.length - numShow - 1) {
    dl.display = 'none';
    dt.display = 'inline';
  }
  else {
    dt.display = 'none';
    dl.display = 'inline';
  }

  if (skip) {
    ut.display = 'none';
    ul.display = 'inline';
  }
  else {
    ul.display = 'none';
    ut.display = 'inline';
  }

}

function jumpToMonth(newM, newY) {
  var dir = getDirection(ids[skip], getIdFromDate(newY, newM));
  var start = skip;
  var end = ids.length;
  if (dir == 'up') {
    start = 0;
    end = skip + 1;
  }
  //alert(newM+"\n"+newY+"\n"+skip+"\n"+dir);

  var nextEntry = 0;
  for (nextEntry = start; nextEntry < end; nextEntry++) {
    var ym = getYearMonthDay(ids[nextEntry]);
    if (ym[0] == newY && ym[1] == newM) break;
  }
  //alert(nextEntry+"\n"+ids[nextEntry]);

  jumpToPost(ids[nextEntry]);
}

function jumpToPost(id, sub) {
  var index;
  for (index = 0; index < ids.length; index++) {
    if (ids[index] == id && subs[index] == sub) break;
  }

  // don't do anything if this post is already at the top
  if (index == skip || updating) {
    return false;
  }
  updating = true;

  var dir = getDirection(ids[skip], id);
  var offset = Math.abs(index - skip);
  if (offset < numShow) offset = numShow;
  var start = skip;
  var end = index;
  if (dir == 'up') {
    start = index;
    end = skip;
  }
  //alert(id+"\n"+ids[skip]+"\n"+skip+"\n"+index+"\n"+offset+"\n"+dir);

  for (var i = start; i < end && i < start+numShow && i+offset < ids.length; i++) {
    if (dir == 'down') {
      doScrollDown(i, i+offset);
    }
    else {
      doScrollUp(i, i+offset, start);
    }
    //alert(skip+"\n"+index+"\n"+offset+"\n"+i+"\n"+ids[i]+"\n"+ids[i+offset]+"\n"+ids[skip]);
  }

  //alert(skip+"\n"+index);
  skip = index;
  checkLinks();
  updating = false;

  return false;
}

function getYearMonthDay(id) {
  var year = parseInt(id.substring(0,2)) + 2000;
  var month = parseInt(id.substring(2,4), 10);
  var day = parseInt(id.substring(4,6), 10);
  return [ year, month, day ];
}

function getIdFromDate(y, m, d) {
  if (!d) d = "99";
  return y.toString().substring(2,4)
	 + ((m.toString().length == 1) ? "0"+m : m) + d;
}

function getDirection(fromId, toId) {
  var ym = getYearMonthDay(fromId);
  fromY = ym[0];
  fromM = ym[1];
  fromD = ym[2];

  var ym = getYearMonthDay(toId);
  toY = ym[0];
  toM = ym[1];
  toD = ym[2];

  //alert(fromY+"\n"+fromM+"\n"+fromD+"\n"+toY+"\n"+toM+"\n"+toD);
  return (toY < fromY || (toY == fromY && toM < fromM)
	 || (toY == fromY && toM == fromM && toD < fromD))
      ? 'down' : 'up';
}

function isEmpty(s) {
  return ((s == null) || (s.length == 0));
}
