var thisDate;
var n;

function jscalendar() {
  thisDate = new Date();
  n = new Array();
  //http://blogger-beta-widgets.blogspot.com/atom.xml?redirect=false&max-results=500&alt=json
  var it = this;
  jx.$load("http://" + document.domain + "/atom.xml?redirect=false&max-results=500&alt=json",
		function($request) {
		  var json = eval("(" + $request + ")");
		  addNotes(n, json);
		  setCalendar(n);
		  if (!location.pathname || location.pathname == "/" || location.pathname == "/index.html" || location.pathname == "/search" || location.pathname.match(/archive\.html$/) || location.pathname.match(/^\/search\/label\//)) {
		    var first;//fixed
		    for (var i = 0; i < json.feed.entry[0].link.length; i++) {
		      if (json.feed.entry[0].link[i].rel == "alternate") {
		        first = json.feed.entry[0].link[i].href;
		        break;
		      }
		    }
        jx.$load(first, 
          function($request) {
            var content = $request;
            if (content.match(/(_WidgetManager\._RegisterWidget\('_BlogView'.*)$/m)) {
              eval(RegExp.$1);
            }
            return false;
          }
        );
		  }
		  
		  var num = json.feed.openSearch$totalResults.$t - 500;
		  var start = 501;
		  while (num > 0) {
		    jx.$load("http://" + document.domain + "/atom.xml?redirect=false&max-results=500&start-index=" + start + "&alt=json",
		      function($request) {
		        json = eval("(" + $request + ")");
		        addNotes(n, json);
		        return false;
		      }
		    );
		    num -= 500;
		    start += 500;
		  }
		  
//setCalendar(n);
			return false;
		}
	);
				
}
function addNotes(n, json) {
//2008-04-13T09:55:00.001+09:00

  var ymd, y, m, d;
  for (var i = 0; i < json.feed.entry.length; i++) {
    json.feed.entry[i].published.$t.match(/(\d{4})-(\d{2})-(\d{2})/);
    ymd = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3;
    y = RegExp.$1 - 0;
    m = RegExp.$2 - 0;
    d = RegExp.$3 - 0;
    if (!n[y]) n[y] = {};
    if (!n[y][m]) n[y][m] = {};
    if (!n[y][m][d]) n[y][m][d] = "http://" + document.domain + "/search?updated-min=" + ymd + "T00%3A00%3A00%2B09%3A00&updated-max=" + ymd + "T23%3A59%3A59%2B09%3A00&max-results=500";
  }
}
function setCalendar(n) {
  top.notes = n;
  Calendar.setup(
  {
    weekNumbers : false,
    step : 1, // show every year in the year menus
    date : thisDate, // selected by default
    flat : 'calendar-container', // div element
    //range : [ parseInt(dates[0].innerHTML), thisDate.getYear()],
    showOthers : true, // show whole first/last week of month
    flatCallback : dateChanged, // what to do on date selection
    dateStatusFunc: disableDateP // which dates to show/hide how
  });
}
// Returns true for all dates lacking a note, false or a css style for those having one.
// Exception: today does not return true, even if it lacks a note. (improves navigation)
function disableDateP(date, y, m, d) {
  var now = new Date;
  if ((y == now.getFullYear()) &&
      (m == now.getMonth()) &&
      (d == now.getDate()))
    return false;
  return noteFromDate(date) ? false : true;
}

function noteFromDate(date) {
  var note = top.notes[date.getFullYear()] || {};
  note = note[date.getMonth()+1] || {};
  return note[date.getDate()];
}

function dateChanged(calendar) {
  if (calendar.dateClicked) {
    var note = noteFromDate(calendar.date);
    if (note) {
    	jx.$load('http://' + document.domain,
				function($request) {
					showPosts($request);
					return false;
				},
				"action=nextposts&widgetId=Blog1&widgetType=Blog&responseType=js&path=" + encodeURIComponent(note)
			);
    }
  }
}
function showPosts(data) {
  eval(data);
}
if (window.addEventListener) {// W3C
  window.addEventListener("load", jscalendar, false);
} else if (window.attachEvent) {// IE 5+
  window.attachEvent("onload", jscalendar);
} else {// Any other browsers
  window.onload = jscalendar;
}
