// ************************************************************************************************
// *** Constructs a MediaItem object
// ************************************************************************************************
var lastMediaItemId = 0;
var uniqueUrl = true;

function MediaItem(id, title, image, duration, ads) {
	// ************************************************************************************************
	// *** MediaItem interface
	// ************************************************************************************************
	
	// Fields
	this.contentId = id; // Media's content id
	this.title = title; // Media's title
	this.imgUrl = image; // Media's playlist image
	this.duration = duration; // Media's duration
	this.ads = ads; // Array of advertisements media items
	
	// Methods
	this.getUrl = mediaItem_GetUrl;	// Gets url for a quality
	this.setUrl = mediaItem_SetUrl; // Sets url for a quality
	
	// ************************************************************************************************
	// *** MediaItem implementation
	// ************************************************************************************************
	
	// Fields
	// ******
	this.mediaUrls = new Array(); // Holds the urls for all media item qualities
	
	// Methods
	// *******
	
	// Gets the media URL considering the quality
	function mediaItem_GetUrl(quality) {
		return this.mediaUrls[quality];
	}
	
	// Sets the media URL for the specified quality
	function mediaItem_SetUrl(quality, url)
	{
		if(url && url != "")
		{
			if(uniqueUrl)
			{
				// Adds a 'counter' to the media URL, to avoid problems with duplicate items in the playlist
				if(url.indexOf('?') > -1)
				{
					url = url.replace('?', '?' + 'uid=' + (lastMediaItemId++) + '&');
				}
				else
				{
					url += '?' + 'uid=' + (lastMediaItemId++);
				}
			}

			this.mediaUrls[quality] = url;
		}
		else
		{
			this.mediaUrls[quality] = "";
		}
	}
}