NetR = typeof NetR === 'undefined' ? {} : NetR;

NetR.VideoCarousel = function (el) {
	var t = this;
	this.el = $(el);
	this.list = this.el.find('.video');
	this.linkList = $('<ul id="news-list" class="video-list">');

	this.setupVideos();

};

NetR.VideoCarousel.prototype = {
	addVideo: function(video, videoId, videoDiv) {
		var videoWrapper = video.find('.video-player');
		videoWrapper.html('<div id="'+videoDiv+'">');
		swfobject.embedSWF("http://www.streamio.se/projectViewerEmbedded.swf", videoDiv, "384", "258", "8", "/flash/expressInstall.swf", {projectId: videoId}, {allowfullscreen:true});
	},
	setupVideos: function() {

		var t = this;

		this.list.each(function(e) {

			var $t = $(this);
			var _t = this;

			//Get all interesting data
			this.headline = $t.find('h2 a').clone();
			this.intro = $t.find('.intro');
			this.thumbnail = $t.find('.thumbnail');
			this.videoIdWrapper = $t.find('.video-id');
			this.videoId = this.videoIdWrapper.text();
			this.videoPlayer = $t.find('.video-player');
			this.videoDiv = 'video-player-' + (e+1);

			//Modify some of the data
			this.li = $('<li class="news-item">').append(this.thumbnail, this.headline, this.intro, '<span class="arrow"></span>');
			this.videoIdWrapper.remove();

			//Add to the linkList
			t.linkList.append(this.li);

			//Hide all but the first video.
			if (e) {
				$t.hide();
			} else {
				t.addVideo($t, this.videoId, this.videoDiv);
				this.li.addClass('active-video video-hover');
				t.activeVideo = $t;
			}

			this.li.hover(function() {
				$(this).addClass('video-hover');
			}, function() {
				if (!$(this).hasClass('active-video')) {
					$(this).removeClass('video-hover');
				}
			});

			//Change between videos
			this.li.click(function(e) {
				e.preventDefault();

				if (t.activeVideo != $t) {

					t.activeVideo.hide();
					t.list.each(function() {
						this.li.removeClass('active-video');
						this.li.removeClass('video-hover');
					});
					t.activeVideo.find('.video-player').html('<div></div>');
					$t.show();
					t.addVideo($t, _t.videoId, _t.videoDiv);
					t.activeVideo = $t;
					_t.li.addClass('active-video');
					_t.li.addClass('video-hover');

				}

			});

		});
		
		//Add the list with titles, thumbnails and intro texts to the DOM.
		$('#content-2').append(this.linkList);
	}
};
