var YearGallery = Class.create(
{
	initialize: function(max)
	{
		if($$('div#main.gallery').length<=0 || !swfobject)
		{
			return;
		}

		this.max       = parseInt(max);
		this.scrolling = false;
		this.player    = $('gallery-player'); 

		var p = this.playlists = $$('.playlist-container ul');
		var b = this.backs     = $$('.btn_playlist-back');
		var n = this.nexts     = $$('.btn_playlist-next');

		for(var cpt=0, len=p.length; cpt<len; cpt++)
		{
			this.loadPlaylist(cpt);
		}
	},
	
	loadPlaylist: function(cpt)
	{
		var p     = this.playlists[cpt];
		var b     = this.backs[cpt];
		var n     = this.nexts[cpt];
		var files = p.select('a');

		if(files.length>this.max)
		{
			b.setStyle({opacity: .5, cursor: 'default'});
			b.observe('mouseover', function(){
				if(parseInt(p.getStyle('left')) < 0)
				{
					this.addClassName('over');
				}
			});
			b.observe('mouseout', function(){ this.removeClassName('over'); });
			b.observe('click', this.scrollBack.bindAsEventListener(this, p, b, n));

			n.observe('mouseover', function(){
				if(parseInt(p.getStyle('left'))>-files.length*60+this.max*60)
				{
					n.addClassName('over');
				}
			}.bind(this));
			n.observe('mouseout', function(){ this.removeClassName('over'); });
			n.observe('click', this.scrollNext.bindAsEventListener(this, p, b, n));
			
		}
		else
		{
			[b, n].invoke('setStyle', {opacity:0, cursor: 'default'});

			b.observe('click', function(event){ event.stop(); });
			n.observe('click', function(event){ event.stop(); });
		}
		
		files.each(function(f, i){
			f.observe('click', this.playFile.bindAsEventListener(this, f, i, cpt));
		}.bind(this));
	},
	
	playFile: function(event, f, i, cpt)
	{
		event.stop();

		this.playlists[cpt].select('a').invoke('removeClassName', 'selected');
		f.addClassName('selected');

		var playlist = '3D/playlist1.xml';
		var flashvars = {
			PlayListXML_path: '\'http://isart.e-spiration.fr/flash/player/playlists/galleries/year/' + playlist + '\'',
			_showMainBar: true,
			_skin: 'Default',
			_mainbarAutoHide: true,
			_mainbar_Alpha: 100,
			_mainbar_Top: '#333333',
			_mainbar_Bottom: '#000000',
			_loadSkin: false,
			_thumbnailCarousel: false,
			_fixedWidth: 320,
			_fixedHeight: 240,
			_videoProportions: true,
			_autoSize: true,
			_autoPlay: true,
			_loop: false,
			_bufferTime: 0,
			_buttonToolTips: false,
			_buttonsColor: '#FFFFFF',
			_volumeAudio: 60,
			_seekBar_color: '#ffa707',
			_showEmailButton: false,
			_showEmbedButton: false,
			_showFullscreenButton: true,
			_showPlayListButton: false,
			_showTimerButton: true,
			_showVideoPropButton: false,
			_showVolumeButton: true,
			_fontFamily: 'Default Embedded Font',
			_fontSize: 8,
			_emailSubjectTitle: '',
			_emailBodyText: '',
			_buttonToolTipsList: ['stop','play','pause','played time / all time','sound volume','playlist','html code and link','send to friend','video properties','fullscreen mode'],
			_addexterpl: false,
			_plbgcolor: '#000000',
			_plwidth: 320,
			_plheight: 240,
			_ownerLogoIMGPath: 'logo.png',
			_ownerLogoAlpha: 0,
			_ownerLogoLink: '#',
			_rightClickMenuCopyright: 'VL',
			_rightClickMenuCopyrightLink: '#',
			_rightClickMenuCopyrightTarget: '_blank',
			_objectID: 'video'
		};

		var params = {
		  allowFullScreen: 'true',
		  wmode: 'transparent',
		  quality: 'high',
		  allowScriptAccess: 'always',
		  swliveconnect: true,
		  allowFullScreen: true,
		  swfversion: '8.0.0.0'
		};
			
		swfobject.embedSWF('http://isart.e-spiration.fr/flash/player/player.swf', 'gallery-player', '320', '240', '9.0.115', '/scripts/lib/swfobject/expressInstall.swf', flashvars, params);
	},
	
	scrollBack: function(event, p, b, n)
	{
		event.stop();
		
		if(!this.scrolling)
		{
			this.scrolling = true;

			if(parseInt(p.getStyle('left'))<0)
			{
				new Effect.MoveBy(p, 0, this.max*60, {duration: .25, afterFinish: function(){
					n.setStyle({opacity: 1, cursor: 'hand'});
					if(parseInt(p.getStyle('left'))>=0)
					{
						b.removeClassName('over');
						b.setStyle({opacity: .5, cursor: 'default'});
					}
					this.scrolling = false;
				}.bind(this)});
			}
			else
			{
				this.scrolling = false;
			}
		}
	},
	
	scrollNext: function(event, p, b, n)
	{
		event.stop();

		if(!this.scrolling)
		{
			this.scrolling = true;

			var stop = -p.select('a').length*60+this.max*60;

			if(parseInt(p.getStyle('left'))>stop)
			{
				new Effect.MoveBy(p, 0, -this.max*60, {duration: .25, afterFinish: function(){
					b.setStyle({opacity: 1, cursor: 'hand'});
					if(parseInt(p.getStyle('left'))<=stop)
					{
						n.removeClassName('over');
						n.setStyle({opacity: .5, cursor: 'default'});
					}
					this.scrolling = false;
				}.bind(this)});
			}
			else
			{
				this.scrolling = false;
			}
		}
	}
});

var StudiesGallery = Class.create(
{
	initialize: function(max)
	{
		if($$('div#main.studies').length<=0 || !swfobject)
		{
			return;
		}

		this.max     = parseInt(max);
		this.players = $$('.gallery-player'); 

		var p = this.playlists = $$('.playlist-container ul');
		var b = this.backs     = $$('.btn_playlist-back');
		var n = this.nexts     = $$('.btn_playlist-next');
		
		for(var cpt=0, len=p.length; cpt<len; cpt++)
		{
			this.loadPlaylist(cpt);
		}
	},
	
	loadPlaylist: function(cpt)
	{
		var p     = this.playlists[cpt];
		var b     = this.backs[cpt];
		var n     = this.nexts[cpt];
		var files = p.select('a');

		if(files.length>this.max)
		{
			b.setStyle({opacity: .5, cursor: 'default'});
			b.observe('mouseover', function(){
				if(parseInt(p.getStyle('left')) < 0)
				{
					this.addClassName('over');
				}
			});
			b.observe('mouseout', function(){	this.removeClassName('over'); });
			b.observe('click', this.scrollBack.bindAsEventListener(this, p, b, n));

			n.observe('mouseover', function(){
				if(parseInt(p.getStyle('left'))>-files.length*60+this.max*60)
				{
					n.addClassName('over');
				}
			}.bind(this));
			n.observe('mouseout', function(){ this.removeClassName('over'); });
			n.observe('click', this.scrollNext.bindAsEventListener(this, p, b, n));
		}
		else
		{
			[b, n].invoke('setStyle', {opacity: 0, cursor: 'default'});
			
			b.observe('click', function(event){ event.stop(); });
			n.observe('click', function(event){ event.stop(); });
		}
		
		files.each(function(f, i){
			f.observe('click', this.playFile.bindAsEventListener(this, f, i, cpt));
		}.bind(this));
	},
	
	playFile: function(event, f, i, cpt)
	{
		event.stop();

		this.playlists[cpt].select('a').invoke('removeClassName', 'selected');
		f.addClassName('selected');

		var playlist = '3D/playlist1.xml';
		var flashvars = {
			PlayListXML_path: '\'http://isart.e-spiration.fr/flash/player/playlists/galleries/year/' + playlist + '\'',
			_showMainBar: true,
			_skin: 'Default',
			_mainbarAutoHide: true,
			_mainbar_Alpha: 100,
			_mainbar_Top: '#333333',
			_mainbar_Bottom: '#000000',
			_loadSkin: false,
			_thumbnailCarousel: false,
			_fixedWidth: 320,
			_fixedHeight: 240,
			_videoProportions: true,
			_autoSize: true,
			_autoPlay: true,
			_loop: false,
			_bufferTime: 0,
			_buttonToolTips: false,
			_buttonsColor: '#FFFFFF',
			_volumeAudio: 60,
			_seekBar_color: '#ffa707',
			_showEmailButton: false,
			_showEmbedButton: false,
			_showFullscreenButton: true,
			_showPlayListButton: false,
			_showTimerButton: true,
			_showVideoPropButton: false,
			_showVolumeButton: true,
			_fontFamily: 'Default Embedded Font',
			_fontSize: 8,
			_emailSubjectTitle: '',
			_emailBodyText: '',
			_buttonToolTipsList: ['stop','play','pause','played time / all time','sound volume','playlist','html code and link','send to friend','video properties','fullscreen mode'],
			_addexterpl: false,
			_plbgcolor: '#000000',
			_plwidth: 320,
			_plheight: 240,
			_ownerLogoIMGPath: 'logo.png',
			_ownerLogoAlpha: 0,
			_ownerLogoLink: '#',
			_rightClickMenuCopyright: 'VL',
			_rightClickMenuCopyrightLink: '#',
			_rightClickMenuCopyrightTarget: '_blank',
			_objectID: 'video'
		};

		var params = {
		  allowFullScreen: 'true',
		  wmode: 'transparent',
		  quality: 'high',
		  allowScriptAccess: 'always',
		  swliveconnect: true,
		  allowFullScreen: true,
		  swfversion: '8.0.0.0'
		};

		swfobject.embedSWF('http://isart.e-spiration.fr/flash/player/player.swf', this.players[cpt].identify(), '320', '240', '9.0.115', '/scripts/lib/swfobject/expressInstall.swf', flashvars, params);
	},
	
	scrollBack: function(event, p, b, n)
	{
		event.stop();

		if(!this.scrolling)
		{
			this.scrolling = true;

			if(parseInt(p.getStyle('left'))<0)
			{
				new Effect.MoveBy(p, 0, this.max*60, {duration: .25, afterFinish: function(){
					n.setStyle({opacity: 1, cursor: 'hand'});
					if(parseInt(p.getStyle('left'))<=0)
					{
						b.removeClassName('over');
						b.setStyle({opacity: .5, cursor: 'default'});
					}
					this.scrolling = false;
				}.bind(this)});
			}
			else
			{
				this.scrolling = false;
			}
		}
	},
	
	scrollNext: function(event, p, b, n)
	{
		event.stop();

		if(!this.scrolling)
		{
			this.scrolling = true;

			var stop = -p.select('a').length*60+this.max*60;
	
			if(parseInt(p.getStyle('left'))>stop)
			{
				new Effect.MoveBy(p, 0, -this.max*60, {duration: .25, afterFinish: function(){
					b.setStyle({opacity: 1, cursor: 'hand'});
					if(parseInt(p.getStyle('left'))<=-stop)
					{
						n.removeClassName('over');
						n.setStyle({opacity: .5, cursor: 'default'});
					}
					this.scrolling = false;
				}.bind(this)});
			}
			else
			{
				this.scrolling = false;
			}
		}
	}
});

document.observe('dom:loaded', function(){
	new YearGallery(5);
	new StudiesGallery(5);
});