/* Copyright 2009 Palm, Inc.  All rights reserved. */

var DateFormatter = {
	today: $L("today"),
	yesterday: $L("yesterday"),
	dayStrings: [$L("Sunday"), $L("Monday"), $L("Tuesday"), $L("Wednesday"), $L("Thursday"), $L("Friday"), $L("Saturday")],
	
	getTimeSeparator: function(){
		return ":";
	},
	
	getDateSeparator: function(){
		return "/";
	},
	
	getDay: function(day) {
		return this.dayStrings[day];
	},
	
	formatDate: function(d) {
		//QDLogger.log( "DateFormatter::formatDate");
		var now = new Date();
		var sep = this.getDateSeparator();
		var offsetFromToday = this.dayOffset(now.getTime(), d.getTime());
		//QDLogger.log( "DateFormatter::formatDate", "offset: ", offsetFromToday  , now.getTime());
		
		// if we're in the current week, return special strings
		if (offsetFromToday == 0)
			return this.today;
		else if (offsetFromToday == 1)
			return this.yesterday;
		else if(offsetFromToday < 7 && offsetFromToday > 0)
			return this.getDay((now.getDay() - offsetFromToday) % 7); 
		else 
			return d.getMonth() + sep + d.getDate() + sep + d.getFullYear();
			
	},
	
	formatTime: function(d) {
		var timeSep = this.getTimeSeparator();
		var minutes = d.getMinutes();
		return d.getHours() + timeSep 
			+ (minutes >= 10 ? minutes : "0"+minutes); 
			
	},
	
	formatDuration: function(d) {
		var hours = d.getHours();
		var minutes = d.getMinutes();
		var seconds = d.getSeconds();
		return (hours ? hours + $L(" hr&nbsp;&nbsp;") : "")
			+ (minutes ? minutes +$L(" min&nbsp;&nbsp;") : "") 
			+ (seconds ? seconds +$L(" sec"): "0 sec"); 
	},
	
	dayOffset: function(d1, d2) {
		//QDLogger.log( "DateFormatter::dayOffset", d1  , d2);
		if (d1 == d2)
			return 0
		else 
			return Math.round((d1-d2)/864e5);
	}
}	
