
///////////////////////////////////////////////
// Video Data Formatting
///////////////////////////////////////////////

function video_data_formatter () {

	 // turns a length in seconds into a 'hours:minute:seconds' string
	 //    ex: 121 turns into "2:01"
	 this.seconds_to_length_str = function ( seconds ) {

		  // get the number of seconds
		  var hours = Math.floor( seconds / 3600 ) ;
		  seconds = seconds - ( hours * 3600 ) ;

		  // get the number of minutes
		  var minutes = Math.floor( seconds / 60 ) ;

		  // get the leftover number of seconds
		  var seconds = seconds % 60 ;

		  // if seconds < 10, add a zero in front
		  if ( seconds < 10 )
				seconds = '0' + seconds ;

		  // just seconds
		  if ( hours == 0 && minutes == 0 )
				return seconds + 's' ;
		  // hours seconds and minutes
		  else if ( hours > 0 )
				return hours + ':' + minutes + ':' + seconds
		  // minutes and seconds
		  else
				return minutes + ':' + seconds + 'm' ;

	 }


	 // turns a date like '2009-03-16T11:03:36.000Z' into a relative time string, like '1 month'
	 this.timestr_to_relative_timestr = function ( timestr ) {
		  // date components
		  //   comps[ 0 ] => year
		  //   comps[ 1 ] - 1 => month
		  //   comps[ 2 ] => day
		  //   comps[ 3 ] => hour
		  //   comps[ 4 ] => minute
		  //   comps[ 5 ] => second
		  //   comps[ 6 ] => unused ( '000Z' turns into '000' )
		  var comps = timestr.replace(/\D/g, ' ').split( ' ' ) ; // turn non digit characters into spaces
		  return this.date_to_relative_timestr( new Date( comps[ 0 ], comps[ 1 ] - 1, comps[ 2 ], comps[ 3 ], comps[ 4 ], comps[ 5 ] ) ) ;
	 }
	 // turns a date time like: 'Fri, 17 Aug 2007 11:48:39 PDT' into a relative time string, like '1 month'
	 this.timestr2_to_relative_timestr = function ( timestr ) {
		  return this.date_to_relative_timestr( new Date( timestr ) ) ;
	 }
	 // turns a date into a string representing the difference between todays date and the date passed in
	 // always uses the greatest units of: [hours, days, weeks, months, years]
	 //    ex: 14 days ago would be '2 weeks', while 34 days ago would be '1 month ago'
	 this.date_to_relative_timestr = function ( then ) {
		  // number of milliseconds in :
		  var year_ms = 31556926000 ; // a year
		  var month_ms = 2629743830 ; // a month
		  var day_ms = 86400000 ; // a day
		  var hour_ms = 4080000 ; // an hour


		  // get the current time
		  var now = new Date() ;

		  // get the difference
		  var diff = now.getTime() - then.getTime() ;


		  // find out which units we are in and return that appropriate string
		  var year_diff = diff / year_ms ;
		  if ( year_diff > 2 )
				return Math.floor( diff / year_ms ) + ' years' ;
		  else if ( year_diff > 1 )
				return Math.floor( diff / year_ms ) + ' year' ;

		  var month_diff = diff / month_ms ;
		  if ( month_diff > 2 )
				return Math.floor( diff / month_ms ) + ' months' ;
		  else if ( month_diff > 1 )
				return Math.floor( diff / month_ms ) + ' month' ;

		  var day_diff = diff / day_ms ;
		  if ( day_diff > 2 )
				return Math.floor( diff / day_ms ) + ' days' ;
		  if ( day_diff > 1 )
				return Math.floor( diff / day_ms ) + ' day' ;

		  else
				return Math.floor( diff / hour_ms ) + ' hours' ;
	 }

}
