var CaseStudyVideos = Class.create({
	mediaPlayerElem: null,
	videosElem: null,
	infoElem: null,
	infoTemplate: new Template('<h4>#{title}</h4><p>#{description}</p>'),

	initialize: function(playerId, videosId, infoId)
	{
		this.mediaPlayerElem = $(playerId);
		this.videosElem = $(videosId);
		this.infoElem = $(infoId);

		if(this.mediaPlayerElem && this.videosElem)
		{
			this.videosElem.show();
			this.videosElem.select('a').invoke('observe', 'click', this.newVideoClicked.bindAsEventListener(this));
			if(this.infoElem)
			{
				this.infoElem.show();
			}
		}
	},

	newVideoClicked: function(evt)
	{
		evt.stop();
		var liElem = evt.findElement('li');
		var ind = 0;
		this.videosElem.select('li').each(function(s, i) {
			if(s == liElem)
			{
				ind = i;
				throw $break;
			}
		});

		this.mediaPlayerElem.sendEvent("ITEM", ind);
		var itemInfo = this.mediaPlayerElem.getPlaylist()[ind];

		if(this.infoElem)
		{
			Effect.Fade(this.infoElem, {
				duration: 0.5,
				afterFinish: function()
				{
					this.infoElem.update(this.infoTemplate.evaluate(itemInfo));
					Effect.Appear(this.infoElem, { duration: 0.5 });
				}.bind(this)
			});
		}
	}
});

var mp = null;
function playerReady(obj)
{
	mp = new CaseStudyVideos(obj['id'], 'videos', 'mediaplayerInfo');
}
