
var SECONDS = 1000;
var MINUTES = (60 * SECONDS);
var HOURS = (60 * MINUTES);
var DAYS = (24 * HOURS);

var JSL = {};

JSL.params = {
	maxBoldTime: 59*MINUTES,
	maxFade1Time: 12*HOURS,
	maxFade2Time: 7*DAYS,
	minFade: 0,
	fade1: 96,
	fade2: 168,
	maxFontTime: 36*HOURS,
	minFontSize: 12,
	maxFontSize: 36
};

JSL.PomUnit = function(n,u,t,ts) {
	this.name = n;
	this.url = u;
	this.text = t;
	this.timestamp = ts;
}

JSL.happyTimeString = function(unit, value) {
	return '' + value + '&nbsp;' + unit + ((value==1)?'':'s') + '&nbsp;ago';
};
JSL.happyTime = function(ts) {
	var now = new Date().valueOf();
	var outs = '';
	if(ts > (now - 1*MINUTES)) {
		outs = 'just now';
	} else if (ts > (now - 60*MINUTES)) {
		var minutes = Math.round((now-ts)/MINUTES);
		outs = JSL.happyTimeString('minute', minutes);
	} else if (ts > (now - 24*HOURS)) {
		var hours = Math.round((now-ts)/HOURS);
		outs = JSL.happyTimeString('hour', hours);
	} else if (ts > (now - 7*DAYS)) {
		var days = Math.round((now-ts)/DAYS);
		outs = JSL.happyTimeString('day', days);
	} else if (ts > (now - 31*DAYS)) {
		var weeks = Math.round((now-ts)/(7*DAYS));
		outs = JSL.happyTimeString('week', weeks);
	} else {
		var months = Math.round((now-ts)/(30*DAYS));
		outs = JSL.happyTimeString('month', months);
		//outs = new Date(ts).toString();
	}
	return outs;
}

JSL.makeID = function(n) {
	return 'pomrow' + n;
}

JSL.fillTable = function(a) {
	var outs = "";
	for(var i=0; i<a.length; i++) {
		outs += '<tr class="pom_row" id="'+ JSL.makeID(i) +'">';
		outs += '<td class="pom_service">'+ a[i].name +'</td>';
		outs += '<td class="pom_text">'+ a[i].text +'</td>';
		outs += '<td class="pom_updated">'+ JSL.happyTime(a[i].timestamp) +'</td>';
		outs += '</tr>';
	}
	var t = document.getElementById('pom_table');
	t.innerHTML = outs;
}

JSL.toColor = function(color_array) {
	var f = function f(element, index, array) {
		s = element.toString(16);
		if(s.length < 2)
			s = '0' + s;
		return s;
	};
	var a = new Array();
	for(var i=0; i<color_array.length; i++) {
		a[i] = f(color_array[i]);
	}
	return '#' + a[0] + a[1] + a[2];
},

JSL.mapNum = function(amin, amax, bmin, bmax, aval) {
	var r = (JSL.bound(amin,amax,aval) - amin) / (amax - amin);
	return bmin + (r * (bmax-bmin));
}
JSL.bound = function(min, max, val) {
	return (val < min)?min:(val > max)?max:val;
}

JSL.calcWeight = function(ts) {
	var now = new Date().valueOf();
	if (now-ts < JSL.params.maxBoldTime)
		return 'bold';
	else
		return 'normal';
}
JSL.calcColor = function(ts) {
	var now = new Date().valueOf();
	var gray = 0;
	if(now-ts < JSL.params.maxFade1Time) {
		var mintime = now - JSL.params.maxFade1Time;
		var maxtime = now;
		var minfade = JSL.params.minFade;
		var maxfade = JSL.params.fade1;
		gray = Math.floor(minfade + maxfade - JSL.mapNum(mintime, maxtime, minfade, maxfade, ts));
	} else {
		var mintime = now - JSL.params.maxFade2Time;
		var maxtime = now - JSL.params.maxFade1Time;
		var minfade = JSL.params.fade1;
		var maxfade = JSL.params.fade2;
		gray = Math.floor(minfade + maxfade - JSL.mapNum(mintime, maxtime, minfade, maxfade, ts));
	}
	return JSL.toColor([gray,gray,gray]);
}

JSL.calcSize = function(ts) {
	var maxtime = new Date().valueOf();
	var mintime = maxtime - JSL.params.maxFontTime;
	var minsize = JSL.params.minFontSize;
	var maxsize = JSL.params.maxFontSize;
	var size = Math.floor(JSL.mapNum(mintime, maxtime, minsize, maxsize, ts));
	return '' + size + 'px';
}

// mmm, generators.
JSL.makeClickHandler = function(i) {
	return function() {
		alert(JSL.makeID(i));
	};
}

JSL.setAttributes = function(a) {
	for(var i=0; i<a.length; i++) {
		var id = JSL.makeID(i);
		var tr = document.getElementById(id);
		tr.style.fontWeight = JSL.calcWeight(a[i].timestamp);
		tr.style.color = JSL.calcColor(a[i].timestamp);
		tr.style.fontSize = JSL.calcSize(a[i].timestamp);

		var td = tr.getElementsByTagName('td')[1];
		//td.style.backgroundImage = 'url('+a[i].url+'/favicon.ico)';
		//td.onclick = JSL.makeClickHandler(i);
	}
}

JSL.generateFakeData = function() {
	var a = Array();
	var t = new Date().valueOf();

	a.push(new JSL.PomUnit("Twitter", 'http://twitter.com/',
			"Just got off the phone with Chuckumentary. It's a doodoo casserole over there. Everyone's out.", t - 3*MINUTES));
	a.push(new JSL.PomUnit("Facebook", 'http://facebook.com/',
			"omg late for class lol", t - 28*MINUTES));
	a.push(new JSL.PomUnit("AIM", 'http://aim.com/',
			"No, dammit, I do NOT want to cyber.", t - 4*HOURS));
	a.push(new JSL.PomUnit("Yahoo! Messenger", 'http://messenger.yahoo.com/',
			"I totally want to cyber.", t - 8*HOURS));
	a.push(new JSL.PomUnit("Skype", 'http://www.skype.com/',
			"Glad I didn't tear out my land line for this", t - 11*HOURS));
	a.push(new JSL.PomUnit("Google Chat", 'http://www.google.com/',
			"Just checking my e-mail", t - 17*HOURS));
	a.push(new JSL.PomUnit("LiveJournal", 'http://www.livejournal.com/',
			"Mood: emo", t - 2*DAYS));
	a.push(new JSL.PomUnit("Jaiku", 'http://jaiku.com/',
			"Not using this at all, to be honest. Sorry.", t - 4*DAYS));
	a.push(new JSL.PomUnit("IILWY", 'http://www.iminlikewithyou.com/',
			"I'm in like with incestuous snark parties.", t - 5*DAYS));
	a.push(new JSL.PomUnit("Pownce", 'http://pownce.com/',
			"Pretending that this service isn't totally walled off and inaccessible.", t - 13*DAYS));
	a.push(new JSL.PomUnit("MySpace", 'http://myspace.com/',
			"Rawkin' to the beatz.", t - 83*DAYS));

	return a;
}

JSL.pomStart = function() {
	JSL.pomData = JSL.generateFakeData();
	JSL.fillTable(JSL.pomData);
	JSL.setAttributes(JSL.pomData);
}


