/* TIMEDIFF */
function formatFullDate(postTime) {
	postTime=new Date(postTime);
	var month=['January','February','March','April','May','June','July','August','September','October','November','December'];
	var postYear=postTime.getYear();
	if (postYear<1900) postYear+=1900;
	return month[postTime.getMonth()]+' '+postTime.getDate()+", "+postYear;
}

function timeDifference(postTime) {
   try {
   	serverTime;
   } catch(e) {
   	serverTime = false;
   }
   
   if (!serverTime || serverTime<postTime) return formatFullDate(postTime);
   
   var ago=Math.floor((serverTime-postTime)/(1000*60)); // Posted ago in whole minutes rounded down
   if (ago>(60*24)) return formatFullDate(postTime); // Skip if more than 24 hours
   
   var hoursAgo=Math.floor(ago/60); //Whole hours = total minutes/(60min/hour) rounded down
   var minutesAgo=ago%60;           //Minutes = the remainder

   var message='';
   if (hoursAgo) {
      message+=hoursAgo+" hour";
      if (hoursAgo>1) message+="s";
      if (minutesAgo) message+=", ";
   }
   if (minutesAgo) {
      message+=minutesAgo+" minute";
      if (minutesAgo>1) message+="s";
   }
   message+=" ago";
   return message;
}

/* READER'S CHOICE TABS */
var MostPopularBox = new Class({
	initialize: function() {
		this.tabCt = 0;
		try {
			$S('#readersChoice ol').each(function(anOL) { this.createRCTab(anOL); },this);
			if (!this.tabCt) throw "No MostPopularBox tabs created";
			$('mostPopStoriesTab').addClassName('selected');
		} catch(e) {
			dbug.log("Most popular error: %s", e);
			$('rcTabs').style.height=0;
		}
	},
	createRCTab: function(anOL){
		var rcTab = document.createElement('H4');
		rcTab.setAttribute('id',anOL.id+'Tab');
		rcTab.appendChild(document.createTextNode(anOL.getAttribute('title')));
		rcTab.onclick = function(){
			$S('#readersChoice ol').each(function(anOL) {
				if (anOL.id == rcTab.id.replace('Tab','')) {
					$(anOL).removeClassName('hidden');
				} else {
					$(anOL).addClassName('hidden');
				}
			});
			$S('#rcTabs h4').each(function(anH4) {
				if (anH4.id == rcTab.id) {
					$(anH4).addClassName('selected');
				} else {
					$(anH4).removeClassName('selected');
				}
			});
		}
		rcTab.onmouseover = function() {rcTab.addClassName('hover')};
		rcTab.onmouseout = function() {rcTab.removeClassName('hover')};
		$('rcTabs').appendChild(rcTab);
		this.tabCt++;
	}
});


