/* themes widget
 *
 * Usage: include this file in the blog post page, then place divs with id: relatedposts, relatedlinks
 */

function make_themes_list(list) {
	var len = list.length;
	var html = "";
	var d = new Date();
	var curr_date = d.getDate();
	var curr_month = d.getMonth() + 1;
	var curr_year = d.getFullYear();
	var maxcount = 0;
	var mincount = -1;
    $.each(list, function(i,item) {
	  if (item.count > maxcount) {
	    maxcount = item.count;	
	  }
	  if (item.count < mincount || (mincount == -1)) {
	    mincount = item.count;	
	  }
    });
	
	for (var i = 0; i < len; i++) {
		var rank = 11 - Math.ceil(( (list[i].count - mincount) / maxcount ) * 10);
		if (rank < 1) {
			rank = 1;
		}
                if (rank > 10) {
    			rank = 10;
		}
		html += "<li class='rank" + rank + "'><span class='bookmark'>"
		if (list[i].count > 0) {
			html +="<a href='http://themes.headshift.com/tags/" + list[i].name + "?from=2005-01-01&to=" + curr_year + "-" + curr_month + "-" + curr_date + "'>" + list[i].name + 
			"</a>";
		} else {
			html += list[i].name;
		}
		html += "</span></li>";
	}
	return html;
}

function populateThemes(items) {
	jQuery('#recent-themes-list').html(make_themes_list(items));
}

var today=new Date();
var lastweektime= today - 604800000; //today now is set to be 3 days into the future
var lastweek = new Date;
lastweek.setTime(lastweektime);

function formatDate(mydate) {
	var theyear=mydate.getFullYear();
	var themonth=mydate.getMonth()+1;
	var theday=mydate.getDate();
	return theyear + "-" + themonth + "-" + theday;
}

jQuery.getJSON("/recenttags?from=" + formatDate(lastweek) + "&to=" + formatDate(today) + "&limit=18", populateThemes );
 
//jQuery.getJSON("/recenttags?from=2009-06-16&to=2009-06-23&limit=18", populateThemes );
