///////////////////////////////////////////////
// various video related utilities
///////////////////////////////////////////////



///////////////////////////////////////////////
// YouTube
///////////////////////////////////////////////

// functions that expose youtube related constants
function youtube_page_link () { return 'http://www.youtube.com/watch?' ; }
function construct_youtube_page_url ( p_videoid ) { return youtube_page_link() + 'v=' + p_videoid ; }


// gets the youtube video id out of a youtube url
//   ex: returns '4pXfHLUlZf4' from 'http://www.youtube.com/watch?v=4pXfHLUlZf4'
//
// this function requires the get_urlparamobj(...) function defined in util_url.js
function get_youtube_videoid ( p_url ) {
	 var videoid = get_urlparamobj( p_url )[ 'v' ] ;
	 if ( videoid == undefined )
		  videoid = '' ;

	 if ( is_valid_base64( videoid, 11 ) )
		  return videoid ;
	 else
		  return '' ;
}

// verifies that a string is a valid youtube url
//    i.e. 'http://www.youtube.com/watch?v=4pXfHLUlZf4', 'www.youtube.com/watch?v=4pXfHLUlZf4', and 'youtube.com/watch?v=4pXfHLUlZf4' return true
//          while 'http://www.youtube.com/watch', 'asdf', 'http://www.google.com', etc return false
function is_valid_youtube_watch_url ( p_str ) {

	 // strip any whitespace from the beginning and end

	 // minimum length ( i.e. "youtube.com/watch?v=p9FnW-Pnea0" is 31 chars long )
	 if ( p_str.length < 31 )
		  return false ;

	 // if it starts with "http://", remove it
	 if ( p_str.substring( 0, 7 ).toLowerCase() == "http://" )
		  p_str = p_str.substring( 7 ) ;

	 // if it now starts with 'www.', remove that
	 if ( p_str.substring( 0, 4 ) == "www." )
		  p_str = p_str.substring( 4 ) ;

	 // now verify that the next 18 chars are "youtube.com/watch?"
	 //    accomodate here for other youtube root url's, like "youtube.co.uk" or what not
	 if ( p_str.substring( 0, 18 ).toLowerCase() == "youtube.com/watch?" )
		  p_str = p_str.substring( 18 ) ;
	 else 
		  return false ;

	 // now get the video id and make sure it is a valid youtube video id
	 if ( get_youtube_videoid( '?' + p_str ) != '' )
		  return true ;

	 // invalid youtube url
	 return false ;

}



///////////////////////////////////////////////
// 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 ( p_seconds ) {
		  // get the number of seconds
		  var hours = Math.floor( p_seconds / 3600 ) ;
		  p_seconds = p_seconds - ( hours * 3600 ) ;

		  // get the number of minutes
		  var minutes = Math.floor( p_seconds / 60 ) ;

		  // get the leftover number of seconds
		  var seconds = p_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 time like: 'Fri, 17 Aug 2007 11:48:39 PDT' into a relative time, like '1 month'
	 // 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_time_str = function ( p_timestr ) {
		  // 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() ;

		  // when the youtube video was published
		  var then = new Date( p_timestr ) ;

		  // 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' ;
	 }

}