var switchMovie = Class.create(
{
	initialize: function(obj)
	{
		var self = this;
		this.obj = obj;
		
		this.currentVideo = 0;
		
		this.allVideos = $$('.'+this.obj.videoFile);
		this.videosLength = this.allVideos.length;
		
		
		
		this.allVideos.each(function(elm,i){
			var videoUrl = $(elm).getAttribute('href');
			var videoTitle = $(elm).getAttribute('title');
			
			$(elm).observe('click',function(ev){
				Event.stop(ev);
				$(self.obj.videoPlayer).changeMovieJs(videoUrl);
				self.updateText(videoTitle);
				
				self.currentVideo = i;
			});
		});
		
		if($(this.obj.nextBtn)){
		$(this.obj.nextBtn).observe('click',function(ev){
			Event.stop(ev);

			self.currentVideo = self.currentVideo+1;
			
			if(self.currentVideo == self.videosLength){
				self.currentVideo = 0;
			}
			var videoUrl = $(self.allVideos[self.currentVideo]).getAttribute('href');
			var videoTitle = $(self.allVideos[self.currentVideo]).getAttribute('title');
			$(self.obj.videoPlayer).changeMovieJs(videoUrl);
			self.updateText(videoTitle);
			
		});
		}
		
		if($(this.obj.previousBtn)){
		$(this.obj.previousBtn).observe('click',function(ev){
			Event.stop(ev);

			self.currentVideo = self.currentVideo-1;
			
			if(self.currentVideo == -1){
				self.currentVideo = (self.videosLength-1);
			}
			
			var videoUrl = $(self.allVideos[self.currentVideo]).getAttribute('href');
			var videoTitle = $(self.allVideos[self.currentVideo]).getAttribute('title');
			
			$(self.obj.videoPlayer).changeMovieJs(videoUrl);
			self.updateText(videoTitle);
			
		});
		}
		
		this.updateText($(this.allVideos[0]).getAttribute('title'));
	},
	
	updateText: function(text){
		if($(this.obj.textArea)){
			$(this.obj.textArea).update(text);
		}	
	}

});

Event.observe(window, 'load', function()
{
	if($('videoPlayer')){
		new switchMovie(
		{
			videoPlayer: 'videoPlayer',
			videoFile : 'videoFile',
			textArea: 'right',
			nextBtn: 'nextBtn',
			previousBtn: 'previousBtn'
		});
	}
	
});
