if (!michigirl) var michigirl = {};

michigirl.archive = function() {

	var dom = global.dom,
		event = global.event,
		domdone = false;

	function init() {

		var sel = $('archiveselectordisabled');
		if (!sel || !sel.nextSibling) {
			// reset timeout to try again later - unless window.onload fired then bail
			// this could happen when there are no archive dropdowns to display, so the container <div> will never exist
			if (!domdone) window.setTimeout(init,20);
			return;
		}

		// update container id to apply styles
		sel.id = 'archiveselector';

		// get year/month lists
		var lists = sel.getElementsByTagName('ul');
		if (lists.length != 2) return;

		// create select boxes
		var pl = sel.getElementsByTagName('p');
		createselect(pl[0],'Year','archiveyear',getlistdata(lists[0]));
		createselect(pl[1],'Month','archivemonth',getlistdata(lists[1]));

		// remove <ul> nodes
		sel.removeChild(lists[1]);
		sel.removeChild(lists[0]);
	}

	function getlistdata(el) {

		var uriregexp = /(\/archive\/.+)$/,
			geturi = function(href) {
				var r = uriregexp.exec(href);
				return (r) ? r[1] : '';
			};

		var list = el.getElementsByTagName('a'),
			rows = [];

		for (var i = 0,j = list.length;i < j;i++) {
			var item = list[i];
			rows[rows.length] = {
				uri: geturi(item.href),
				text: item.firstChild.nodeValue,
				selected: (item.className == 'sel')
			};
		}

		return rows;
	}

	function createselect(el,labeltext,selectid,list) {

		// build <option> elements
		var optionlist = [];
		for (var i = 0,j = list.length;i < j;i++) {
			var item = list[i];
			var attrib = { value: item.uri };
			if (item.selected) attrib.selected = 'selected';
			optionlist[optionlist.length] = dom.node('option',attrib,[item.text]);
		}

		var select = dom.node('select',{ id: selectid },optionlist);
		event.add(select,'change',changeview);

		// remove child nodes of <p>, add <label> and constructed <select>
		dom.removechildren(el);
		el.appendChild(dom.node('label',{ 'for': selectid },[labeltext]));
		el.appendChild(document.createTextNode('\n')); // adds a visual gap between <label> and <select>
		el.appendChild(select);
	}

	function changeview() { window.location.href = this.options[this.selectedIndex].value; }

	init();
	event.add(window,'load',function() { domdone = true; });
}();