﻿function getYears() {
	var curYear;
	var archivedYears = new Array();
	var lastYear = 0;
	for (i = 0; i < archiveFiles.length; i++) {
		curYear = archiveFiles[i][0].substring(0, 4);
		if (curYear > lastYear) {
			archivedYears[archivedYears.length] = curYear;
			lastYear = curYear;
		}
	}

	return archivedYears;
}

function getMonths(year) {
	var curMonth;
	var archivedMonths = new Array();
	var lastMonth = 0;
	for (fId = 0; fId < archiveFiles.length; fId++) {
		if (archiveFiles[fId][0].substring(0, 4) == year) {
			curMonth = archiveFiles[fId][0].substring(4);
			if (curMonth > lastMonth) {
				archivedMonths[archivedMonths.length] = curMonth;
				lastMonth = curMonth;
			}
		}
	}
	
	return archivedMonths;
}

function getPosts(yearMonth) {
	var targetPosts = new Array();
	for (pId = 0; pId < posts.length; pId++) {
		if (posts[pId][0] == yearMonth) {
			targetPosts[targetPosts.length] = posts[pId];
		}
	}

	return targetPosts;
}

function genArchiveTree() {
	var archivedYears = getYears();

	for (yId = 0; yId < archivedYears.length; yId++) {
		document.writeln("<li id=\"li-year-" + archivedYears[yId] + "\"><a href=\"JavaScript:void(0);\" onclick=\"expandYear(" + archivedYears[yId] + ")\" class=\"link-year-expandable\">" + archivedYears[yId] + "</a>");
		document.writeln("  <ul id=\"archive-year-" + archivedYears[yId] + "\" class=\"archive-list-month\"></ul>");
		document.writeln("</li>");
	}
}

function expandYear(year) {
	var monthList = document.getElementById("archive-year-" + year);
	
	if (monthList.innerHTML == "") {
		var monHtml = "";
		var archivedMonths = getMonths(year);
		for (j = 0; j < archivedMonths.length; j++) {
			yearMonth = year.toString() + archivedMonths[j].toString();
			monHtml += "    <li id=\"li-month-" + yearMonth + "\"><a href=\"JavaScript:void(0);\" onclick=\"expandMonth(" + yearMonth + ")\" class=\"link-month-expandable\">" + monthNames[archivedMonths[j] - 1] + "</a>";
			monHtml += "      <ul id=\"archive-month-" + yearMonth + "\" class=\"archive-list-post\"></ul>";
			monHtml += "    </li>";
		}
		monthList.innerHTML = monHtml;
	}

    var yearLi = document.getElementById("li-year-" + year);
	if (monthList.className == "archive-list-month-exp") {
		monthList.className = "archive-list-month";
		yearLi.className = "";
	}
	else {
		monthList.className = "archive-list-month-exp";
		yearLi.className = "li-year-exp";
	}
}

function expandMonth(yearMonth) {
	var postList = document.getElementById("archive-month-" + yearMonth);

	if (postList.innerHTML == "") {
		var postListHtml = "";
		var monthPosts = getPosts(yearMonth);
		for (k = 0; k < monthPosts.length; k++) {
			postListHtml += "        <li><a href=\"" + monthPosts[k][3] + "\">" + monthPosts[k][2] + "</a></li>";
		}
		postList.innerHTML = postListHtml;
	}

	var yearMonthLi = document.getElementById("li-month-" + yearMonth);
	if (postList.className == "archive-list-post-exp") {
		postList.className = "archive-list-post";
		yearMonthLi.className = "";
	}
	else {
		postList.className = "archive-list-post-exp";
		yearMonthLi.className = "li-month-exp";
	}
}
