var MediaBar = function(container,browser)
{
	this.media = [];	//Array containing all the media-elements

	this.width = '312'	//Youtube width and height
	this.height = '209';
	this.currentKey = -1;	//Currently selected item

	//Setup the parent element
	this.mediaContainer = container;
	this.mediaBrowser = browser;
}

//Adds new media the the list.
MediaBar.prototype.AddMedia = function(type, path, thumb)
{
	//Preload the images first.
	var screenshot = new Image(this.width,this.height);
	screenshot.src = path;

	var thumbImg = new Image(68,68);
	thumbImg.src = thumb;

	this.media.push({ 'type' : type, 'path' : path, 'thumb' : thumb});
}

//Shows the media box, fills the media browser
MediaBar.prototype.Render = function()
{
	var media = this.media;	//Create shortcuts
	var mediaBrowser = this.mediaBrowser;
	var mediaContainer = this.mediaContainer;

	var me = this;

	for(var i = 0; i < media.length; i++)
	{
		var cMedia = media[i];

		var img = document.createElement("img");
		img.src = cMedia.thumb;
		img.id = i;

		img.onclick = function()
		{
			me.showMedia(this.id);
		};

		mediaBrowser.appendChild(img);

	}
	this.showMedia(0);	//Show the first media
}

MediaBar.prototype.showMedia = function(key)
{
	if(this.currentKey == key)
		return;

	if(key >= this.media.length)
	{
		this.showMedia(0);
		return;
	}

	this.currentKey = key;



	switch(this.media[key].type)
	{
		case 'image':

			this.mediaContainer.innerHTML = "<img src='"+this.media[key].path+"' />";

		break;

		case 'youtube':

			this.mediaContainer.innerHTML = '<object width="'+this.width+'" height="'+this.height+'"><param name="movie" value="'+this.media[key].path+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+this.media[key].path+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+this.width+'" height="'+this.height+'"></embed></object>';

		break;
	}

	var me = this;

	if(this.autoPlay)
	{
		this.autoPlayTimer = setTimeout(
			function()
			{
				me.showMedia(++key);
			}, 5000);
	}
}

MediaBar.prototype.StartAutoPlay = function()
{
	this.autoPlay = true;
	var me = this;
	this.autoPlayTimer = setTimeout(
		function()
		{
			me.showMedia(me.currentKey + 1);
		}, 5000);
}

MediaBar.prototype.StopAutoPlay = function()
{
	this.autoPlay = false;

	clearTimeout(this.autoPlayTimer);
}
