// Function VSUtilities() is an object that contains a variety of utility methods for processing video search results metadata.
// These methods are available in the global VSUTIL object.
var VSUtilities = function() {
	
	// PUBLIC METHODS:
	this.playVideo = function (videoUrl) {
		window.open(videoUrl, '', 'width=800,height=700,location=no,menubar=no,resizable=yes,scrollbars=yes,left=10,top=10');
	}
	// Function getDateString() takes the dateFound string, in RFC 2822 format (see http://www.faqs.org/rfcs/rfc2822),
	// and returns a long-format string suitable for display in a web page.  For example "Thursday, March 09, 2006 12:53:14 PM".
	this.getDateString = function(dateFound) {
		var theDate = new Date(dateFound);
		return(theDate.toLocaleString());
	}
	
	// Function getShortDateString() takes the dateFound srting, in RFC 2822 format (see http://www.faqs.org/rfcs/rfc2822),
	// and returns a short-format string representing the elapsed time.  For example, "1 minute ago", "3 days ago", "2 years ago", etc..
	this.getShortDateString = function(dateFound) {
		var theDate = new Date(dateFound);
		var now = new Date();
		var elapsedTime = now.getTime() - theDate.getTime();
		var seconds = Math.round(elapsedTime/1000);
		if (seconds < 60) { return(seconds+" sec"+(seconds==1?'':'s')+" old"); }
		var minutes = Math.round(elapsedTime/(1000*60));
		if (minutes < 60) { return(minutes+" min"+(minutes==1?'':'s')+" old"); }	
		var hours = Math.round(elapsedTime/(1000*60*60));	
		if (hours < 24) { return(hours+" hr"+(hours==1?'':'s')+" old"); }
		var days = Math.round(elapsedTime/(1000*60*60*24));
		if (days < 30) { return(days+" day"+(days==1?'':'s')+" old"); }	
		var months = Math.round(elapsedTime/(1000*60*60*24*30));
		if (months < 12) { return(months+" month"+(months==1?'':'s')+" old"); }	
		var years = Math.round(elapsedTime/(1000*60*60*24*30*12));
		return(years+" yr"+(years==1?'':'s')+" old");
	}
	
	// Function getRuntimeString() takes the runtime string, which represents the duration of the video in seconds, 
	// and returns an appropriate human-readable string.
	this.getRuntimeString = function(runtime) {
		var totalSeconds = parseInt(runtime);
		var minutes = Math.floor(totalSeconds/60);
		var seconds = totalSeconds - (minutes * 60);
		return(minutes+"m:"+seconds+"s");
	}

	// Function getTagsHTML() takes two arguments, the string containing the comma-delimited list of tag names and the 
	// the maximum number of tags to return in the response.  This function returns an HTML string where each tag is
	// a clickable link.
	this.getTagsHTML = function(tags, maxTags) {
		var tagArray = tags.split(",", maxTags);
		var theHTML = '';
		var theTag = '';
		for (var i=0; i < tagArray.length; i++) {
			theTag = VSUTIL.trim(tagArray[i]);
			theHTML += '<nobr><a href="javascript:getVideos(\'tag:&quot;'+this.escapeHTMLString(theTag)+'&quot;\'); void(0);">'+this.truncateString(this.escapeHTML(theTag),15)+'</a></nobr>&nbsp;&nbsp;<wbr>';
		}
		return(theHTML);
	}
	
	// Function getRatingButtons() returns the HTML that will draw a simple set of buttons to allow the user to rate a video
	// by 1, 2, 3, 4 or 5.
	this.getRatingButtons = function(id) {
		var theHTML = '&nbsp;';
		for (var i=1; i <= 5; i++) {
			theHTML += '<a href="javascript:submitRating('+id+','+parseInt(i)+'); void(0);">'+i+'</a>&nbsp;';
		}
		return(theHTML);
	}
	
	// Function truncateString() takes two arguments, the original string and the maximum number of characters to return
	// in the new string.  This function returns a truncated version of the string, appended with an ellipsis, if the 
	// original string is longer than maxChar characters.  Note that the use of HTML entities in the string may cause 
	// the length of the truncated string to vary.  A check is provided in this method to avoid truncating html entities.
	this.truncateString = function(string, maxChar) {
		if (string && (string.length > maxChar)) {
			var index = string.indexOf(";", maxChar);
			if ((index >= 0) && ((index - maxChar) < 10)) { maxChar = index+1; }
			return(string.substr(0, maxChar)+'...');
		}
		else { return(string); }
	}
	
	// Function escapeHTML() takes a string as an argument and returns a new string where the following characters are replaced
	// by their HTML entity equivalents: &, <, >, ", '.
	this.escapeHTML = function(string) {
		var newstr = string.replace(/&/g,"&amp;");
		newstr = newstr.replace(/</g,"&lt;");
		newstr = newstr.replace(/>/g,"&gt;");
		newstr = newstr.replace(/'/g,"&#039;");
		newstr = newstr.replace(/"/g,"&quot;");
		return(newstr);
	}
	
	// Function escapeString() takes a string as an argument and returns a new string where all single and double quotes in the original
	// string have been escaped.
	this.escapeString = function(string) {
		var newstr = string.replace(/'/g,"\'");
		return(newstr.replace(/"/g,'\"'));
	}
	
	// Function escapeHTMLString takes a string as an argument and returns a new string where the following characters are replaced
	// by their HTML entity equivalents: &, <, >, ", '.  Additionally, single and double quotes are double escaped.
	this.escapeHTMLString = function(string) {
		var newstr = string.replace(/&/g,"&amp;");
		newstr = newstr.replace(/</g,"&lt;");
		newstr = newstr.replace(/>/g,"&gt;");
		newstr = newstr.replace(/'/g,"\\&#039;");
		newstr = newstr.replace(/"/g,"\\&quot;");
		return(newstr);		
	}

	// Function escapeRegExp takes a string as an argument and returns a new string where the following regular expression special
	// characters are preceded by a backslash: ^ $ . * + ? = ! : | \ / ( ) [ ] { }
	this.escapeRegExp = function(string) {
		var newstr = string.replace(/\^/g,"\^");
		newstr = newstr.replace(/\$/g,"\$");
		newstr = newstr.replace(/\./g,"\.");
		newstr = newstr.replace(/\*/g,"\*");
		newstr = newstr.replace(/\+/g,"\+");
		newstr = newstr.replace(/\?/g,"\?");
		newstr = newstr.replace(/\=/g,"\=");
		newstr = newstr.replace(/\!/g,"\!");
		newstr = newstr.replace(/\:/g,"\:");
		newstr = newstr.replace(/\|/g,"\|");
		newstr = newstr.replace(/\\/g,"\\");
		newstr = newstr.replace(/\//g,"\/");
		newstr = newstr.replace(/\(/g,"\(");
		newstr = newstr.replace(/\)/g,"\)");
		newstr = newstr.replace(/\[/g,"\[");
		newstr = newstr.replace(/\]/g,"\]");
		newstr = newstr.replace(/\{/g,"\{");
		newstr = newstr.replace(/\}/g,"\}");
		return(newstr);		
	}
	
	// Function trim() removes any leading or trailing spaces from the specified string.
	this.trim = function(string) {
		while(''+string.charAt(0) == ' ') string = string.substring(1, string.length);
		while(''+string.charAt(string.length-1)==' ') string = string.substring(0, string.length-1);
		return(string);
	}
}
VSUTIL = new VSUtilities();
