jQuery.noConflict();
jQuery(document).ready(function($){
	if($("#blognav").length == 1) {	
		var years = new Array();
		var month_numbers = new Array("12","11","10","09","08","07","06","05","04","03","02","01");
		var month_names = new Array("December", "November", "October", "September", "August", "July", "June", "May", "April", "March", "February", "January");
		var lastyear = '';
		var thisyear = '';
		var cum_entries = 0;
		var show_open = 5;
    var today     = new Date();
		$("[navlink='true'][date]").each(function(i) {
			thisyear = $(this).attr("date").substring(0,4);
			var thisMonth = $(this).attr("date").substring(5,7);
			thisMonth--;
			var thisDay   = $(this).attr("date").substring(8,10);
		  var thisDate  = new Date(thisyear, thisMonth, thisDay);
		  if( today < thisDate ) {
		    $(this).addClass('removeMe');
		  }
			if(thisyear != lastyear) {
				years.push(thisyear);
			}
			lastyear = thisyear;
		});
		$("[navlink='true']:hidden").remove();
		$(".removeMe").remove();
		$(years).each(function(y,year){
			var yearlist = $("[navlink='true'][date^='" + year + "']").wrapAll("<li" + ((cum_entries >= show_open) ? " class='closed'" : "") + "><ul></ul></li>");
			$(yearlist[0]).parent().before("<span>" + year + " (" + yearlist.length + ")</span>");
			$(month_numbers).each(function(m,month) {
				var monthlist = $("[navlink='true'][date^='" + year + "-" + month + "']").wrapAll("<li" + ((cum_entries >= show_open) ? " class='closed'" : "") + "><ul></ul></li>");
				if(monthlist != undefined) {
					$(monthlist[0]).parent().before("<span>" + month_names[m] + " (" + monthlist.length + ")</span>");
					cum_entries += monthlist.length;
				}
			});
		});
		$("[navlink='true'][date] a[href]").each(function(i) {
			if($(this).attr("href")==jQuery.url.attr("file")) {
				$(this).parents("li.closed").removeClass("closed");
			}
		});
		// fix a few things that TreeView does not do correctly when I add the "closed" class to list items through jQuery DOM manipulation before applying TreeView
		$("#blognav").treeview();
		$("li.expandable").not(".closed").each(function() {
			$(this).removeClass('expandable');
			$(this).addClass('collapsable');
		});
		$("div.hitarea").not(".closed-hitarea").each(function() {
			$(this).removeClass('expandable-hitarea');
			$(this).addClass('collapsable-hitarea');
		});
		$("li.lastExpandable").not(".expandable").each(function() {
			$(this).removeClass('lastExpandable');
			$(this).addClass('lastCollapsable');
		});
		$("div.lastExpandable-hitarea").not(".expandable-hitarea").each(function() {
			$(this).removeClass('lastExpandable-hitarea');
			$(this).addClass('lastCollapsable-hitarea');
		});
		$("#blognav").show();
		// prevent selecting
		var _preventDefault = function(evt) { evt.preventDefault(); };
		$("#blognav").bind("dragstart", _preventDefault).bind("selectstart", _preventDefault);
		
		$("a","#blognav").css('color','#ffffff');
	}
});

