/**
 * Creates a new VideoItem object
 */
function VideoItem( videoObject )
{

	if ( videoObject )
	{
		this.myVideoObject = videoObject;
	
		this.myDate = new Date();
		
		this.myId			= this.myVideoObject.myId;
		this.myTitle		= this.myVideoObject.myTitle;
		this.myDescription	= this.myVideoObject.myDescription;
		this.myDate.setISO8601(this.myVideoObject.myDate);
		this.myTags			= this.myVideoObject.myTags;
		for ( var i in this.myTags ) { this.myTags[i] = this.myTags[i].trim().toLowerCase(); }
		this.myThumb		= this.myVideoObject.myImages;
		this.mySeason		= null;
		this.myLength		= this.myVideoObject.myLength;
	}

}


/*
 * Player control
 */


/** 
 * Loads the video in the main player
 */
VideoItem.prototype.play = function ()
{

	playVideo( this );
	
	ensureTruncates();

} // END of play()


/** 
 * Loads the video in the main player
 */
VideoItem.prototype.load = function ()
{

	loadVideo( this );
	
	ensureTruncates();
	
} // END of load()



/*
 * Fancy Accessors
 */


VideoItem.prototype.getId			= function () { return this.myId; };
VideoItem.prototype.getTitle		= function () { return this.myTitle; };
VideoItem.prototype.getDescription	= function () { return this.myDescription; };
VideoItem.prototype.getDate			= function () { return this.myDate; };
VideoItem.prototype.getTags			= function () { return this.myTags; };
VideoItem.prototype.getThumb		= function () { return this.myThumb; };
VideoItem.prototype.getStill		= function () { return this.myStill; };
VideoItem.prototype.getLength		= function () { return this.myLength; };


/** 
 * Search for a tag in this video.
 * @return true if found, false if not. 
 */
VideoItem.prototype.hasTag = function ( tagKey )
{
	for ( var j in this.myTags )
	{						
		if ( this.myTags[j] == tagKey )
		{
			return true;
		}	
	
	}
	return false;
	
} // END of hasTag


/** 
 * Search the tags for a recognized series tag, return the first one.
 * @return the name of a series to which this video belongs.
 */
VideoItem.prototype.getShowKey = function ()
{
	
	for (var i in allShows)
	{		
		for ( var j in this.myTags )
		{	
			if (this.myTags[j] == allShows[i].myKey.toLowerCase())
			{

				if ( !this.myTags[j].isIgnorable() )
				{
					return allShows[i].myKey;
				}
			}
		}	
	
	}
	
	
	for (var i in allThemes)
	{
		for ( var j in this.myTags )
		{
			if (this.myTags[j] == allThemes[i].myKey.toLowerCase())
			{

				if ( !this.myTags[j].isIgnorable() )
				{
					return allThemes[i].myKey;
				}
			}
		}	
	}
	
	return "Not defined";
	
} // END of getShowKey


/** 
 * Search the tags for a recognized series tag, return the first one.
 * @return the name of a series to which this video belongs.
 */
VideoItem.prototype.getShowName = function ()
{

	for (var i in allShows)
	{
		for ( var j in this.myTags )
		{
			if (this.myTags[j].toLowerCase() == allShows[i].myKey.toLowerCase())
			{
				if ( !this.myTags[j].isIgnorable() )
				{
					return allShows[i].myName;
				}
			}
		}

	}

	for (var i in allThemes)
	{
		for ( var j in this.myTags )
		{
			if (this.myTags[j].toLowerCase() == allThemes[i].myKey.toLowerCase())
			{
				if ( !this.myTags[j].isIgnorable() )
				{
					return allThemes[i].myName;
				}
			}
		}

	}

	return null;

} // END of getShowName


/** 
 * Lazily returns the season this video belongs to.
 */
VideoItem.prototype.getSeason = function()
{
	
	if (!this.mySeason)
	{
		this.mySeason = this.restOfTagStartingWith("s-");
	}
	
	return this.mySeason;

} // END of getSeason


/**
 * Returns the value of the video's tag after the matched portion.
 * Returns value only for the first tag matched for specified hint.
 * If no match is found returns null.
 * @param aHint the part of the tag to match and not return 
 * @returns 
 */
VideoItem.prototype.restOfTagStartingWith = function( aHint )
{
		
	var oneTag;
	var hintLength = aHint.length;
		
	// iterate through each tag
	for (var i in this.myTags)
	{			
		
		oneTag = this.myTags[i];

		// no spaces and lowercase
		tagToMatch = oneTag.trim().toLowerCase().substring(0, hintLength);

		// everything but the prefix
		tagToReturn = oneTag.trim().substring(hintLength, oneTag.length);
		
		// look for the special season marker tag
		if (tagToMatch == aHint)
		{
			return tagToReturn;	
		}

	}

	// If it doesn't find a season, return null
	return null;
		
} // END of restOfTagStartingWith


/**
 * Spits back a video row.
 * @param isSpecialPage if true will include show
 */
VideoItem.prototype.getHTML = function ( isShowDescriptive )
{
	
	var html = "";
	var url = ( isShowDescriptive ) ? "#s=" + this.getShowKey() : "";
	var showOrTheme = ( this.getShowKey().isTheme() ? "Theme: " : "Show: ");
	var showLink = ( isShowDescriptive && this.getShowKey() ? "<a href=\""+url+"\" onclick=\"clickHandler(this); return false;\" class=\"menuVideoShowName\">" + showOrTheme + this.getShowName()+"</a>":"");
	
	html += "\
		<table class=\"menuVideoRow\"><tbody><tr>\
				<td><a href=\"#commandPlayNow&v=" + this.getId() + "\" onClick=\"clickHandler(this); return false;\"><img src=\"" + this.myThumb + "\" class=\"menuVideoThumb\" /></a></td>\
				<td>" + showLink + "\
					<a href=\"#commandPlayNow&v=" + this.getId() + "\" onClick=\"clickHandler(this); return false;\"><div class=\"menuVideoTitle\">" + this.myTitle + "</div></a>\
					<div class=\"menuVideoLength\">("+millisecondsToTime(this.myLength)+")</div>\
					<div class=\"menuVideoDescription episodeTruncatable\">" + this.myDescription  + "</div>\
					<div class=\"menuVideoTags shortTruncatable\">Themes: "+this.getTagHTML()+"</div>\
				</td>\
			</tr></tbody></table>";

	return html;

} // END of getHTML


VideoItem.prototype.getTagHTML = function()
{

	var result = "";

	for (var i in this.myTags)
	{

		if ( !this.myTags[i].isIgnorable() )
		{
			result += "<a href=\"#s="+this.myTags[i]+"\" onClick=\"clickHandler(this); return false;\">"+this.myTags[i]+"</a>, ";
		}

	}

	return (result.substr(0, result.length - 2));

}