/* 	http://facet.cocolog-nifty.com/divers/monthlyArchiveSelector01a.js
	made by facet
	01a00 @ 2004-11-10 (backNumberSelector01a.js)
	01a07 @ 2005-09-07
*/

// * START_DATE syntax : "YYYY/MM"
function MakeMonthlyArchiveSelectorHtml(BASE_URL, START_DATE, TITLE) {
	if (BASE_URL.charAt(BASE_URL.length - 1) != '/') BASE_URL += '/';
	var Y = START_DATE.substring(0, 4);
	var M = START_DATE.substring(5);
	if(M.charAt(0) == "0") {
		M = M.substring(1);
	}
	var TOTAL_M = getTotalMonths(Y, M);
	if (!TITLE) TITLE = "Monthly Archive";
	
	var now = new Date();
	var y = now.getFullYear();
	var m = now.getMonth() + 1;
	var totalM = getTotalMonths(y, m);

	var html = '';
	html += '<select id="monthly-archive-selector" onchange="GoToSelectedArchive(this);">';
	html += '<option value="title">' + TITLE + '</option>';
	while (totalM >= TOTAL_M) {
		html += '<option value="' + BASE_URL + get4D(y) + '/' + get2D(m) + '/">';
		html += get4D(y) + '/' + get2D(m);
		html += '</option>';
		m--;
		if (m==0) {
			m = 12;
			y--;
		}
		totalM = getTotalMonths(y, m);
	}
	html += '</select>';
	document.write(html);
	
	function getTotalMonths(y, m) {
		return parseInt(y * 12) + parseInt(m);
	}
	function get4D(n) {
		if (n < 0) return "0000";
		if (n > 9999) return "9999";
		n = "000" + n;
		return 	n.substring(n.length - 4);
	}
	function get2D(n) {
		if (n < 0) return "00";
		if (n > 99) return "99";
		n = "0" + n;
		return  n.substring(n.length - 2);
	}
}
function GoToSelectedArchive(select) {
	var i=select.selectedIndex;
	if (i==0) return false;
	var Options = select.getElementsByTagName('option');
	location.href = Options[i].value;
}	
