var hostname = 'http://www.isartdigital.com';

var DisplayBox = Class.create({

	initialize: function()
	{
		var opts  = this.options = {};

		this.playlists = {};
		this.displayed = null;

		this.buildGUI(opts);

		this.max = 6;

		this.initBtnScrollLeft();
		this.initBtnScrollRight();
	},

	buildGUI: function(opts)
	{
		this.getBg();

		var box   = this.getBox();
		var close = this.getBtnClose();
		var media = this.getMediaContainer();

		var next  = this.getBtnNext();
		var prev  = this.getBtnPrev();

		var desc  = this.getInfosContainer();

		window.onresize = this.updateDisplay.bind(this);
	},

	getBg: function()
	{
		var bg = $('display-box-bg');

		if(!bg){
			bg = this.bg = new Element('div', {id: 'display-box-bg', style: 'display:none;'});

			bg.setStyle({
				position  : 'fixed',
				background: '#000',
				opacity   : .50,
				zIndex    : 1000,
				top       : 0,
				left      : 0,
				width     : '100%',
				height    : '100%'
			});

			$(document.body).insert({top: bg});
		}

		return bg;
	},

	getBox: function()
	{
		var box = $('display_box-container');

		if(!box){
			box = this.box = new Element('div', {id: 'display_box-container', style: 'display:none;'});

			box.setStyle({
				position  : 'fixed',
				padding   : '20px 20px 9px',
				background: '#fff',
				zIndex    : 1001,
				display   : 'none'
			});

			$(document.body).insert({top: box});
		}

		return box;
	},

	getBtnClose: function()
	{
		var btnClose = $('btn_display-box-close');

		if(!btnClose){
			btnClose = this.close = new Element('a',   {id: 'btn_display-box-close', href: '#', className: 'btn-close'});

			this.box.insert(btnClose.update('<span>Fermer</span>'));

			btnClose.observe('click', function(event){

				event.stop();

				this.hideBox();

			}.bind(this));
		}

		return btnClose;
	},

	getBtnPrev: function()
	{
		var btnPrev = $('btn_display-box-prev');

		if(!btnPrev){
			btnPrev = this.prev = new Element('a', {id: 'btn_display-box-prev', href: '#', className: 'btn-prev'});

			this.box.insert(btnPrev.update('<span>Prev</span>'));

			btnPrev.observe('click', function(event){

				event.stop();

				var playlist = $A(this.playlists[this.displayed.playlist]);
				var total    = playlist.length;
				var pos      = playlist.indexOf(hostname + this.displayed.media);

				for(cpt=0; cpt<total; cpt++){

					if(playlist[cpt] == hostname + this.displayed.media){
						break;
					}
				}

				pos = cpt-1;
			 	if(pos<0)
			 	{
			 		pos = total-1;
			 	}

				this.updateBox(this.playlists[this.displayed.playlist][pos], this.displayed.playlist);

			}.bind(this));
		}

		return btnPrev;
	},

	getBtnNext: function()
	{
		var btnNext = $('btn_display-box-next');

		if(!btnNext){
			btnNext = this.next = new Element('a',   {id: 'btn_display-box-next', href: '#', className: 'btn-next'});

			this.box.insert(btnNext.update('<span>Next</span>'));

			btnNext.observe('click', function(event){

				event.stop();

				var playlist = $A(this.playlists[this.displayed.playlist]);
				var total    = playlist.length;
				//var pos      = playlist.indexOf(hostname + this.displayed.media);

				for(cpt=0; cpt<total; cpt++){
					if((playlist[cpt] == hostname + this.displayed.media) || (playlist[cpt] == hostname.replace(/\.com/, '.fr') + this.displayed.media)){
						break;
					}
				}

				pos = cpt+1;
			 	if(pos>=total)
			 	{
			 		pos = 0;
			 	}

				this.updateBox(this.playlists[this.displayed.playlist][pos], this.displayed.playlist);

			}.bind(this));
		}

		return btnNext;
	},

	initBtnScrollRight: function()
	{
		var btns = $$('.btn_playlist-back');

		btns.each(function(btn){

			var p = $(btn.parentNode).next().select('ul')[0];
            var h = Math.round((p.getHeight()-16) / 2)-5;

			btn.setStyle({opacity: .5, cursor: 'default', marginTop: h + 'px'});

			btn.observe('mouseover', function(){

				if(parseInt(p.getStyle('left')) < 0)
				{
					this.addClassName('over');
				}
			});

			btn.observe('mouseout', function(){ this.removeClassName('over'); });
			btn.observe('click', this.scrollBack.bindAsEventListener(this, p, btn));

		}.bind(this));
	},

	scrollBack: function(event, p, b)
	{
		event.stop();

		if(!this.scrolling)
		{
			this.scrolling = true;

			if(parseInt(p.getStyle('left'))<0)
			{

				new Effect.MoveBy(p, 0, 690, {duration: .25, afterFinish: function(){

					$(p.parentNode).next().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;
			}
		}
	},

	initBtnScrollLeft: function()
	{
		var btns = $$('.btn_playlist-next');

		btns.each(function(btn){

			var p = $(btn.parentNode).previous().select('ul')[0];
            var h = Math.round((p.getHeight()-16) / 2)-5;

			//btn.setStyle({opacity: .5});
            btn.setStyle({marginTop: h + 'px'});

			btn.observe('mouseover', function(){
				if(parseInt(p.getStyle('left')) < 0)
				{
					this.addClassName('over');
				}
			});

			btn.observe('mouseout', function(){ this.removeClassName('over'); });
			btn.observe('click', this.scrollNext.bindAsEventListener(this, p, btn));

		}.bind(this));
	},

	scrollNext: function(event, p, b)
	{
		event.stop();

		if(!this.scrolling)
		{
			this.scrolling = true;

			var stop = -p.select('a').length*105+this.max*105;

			if(parseInt(p.getStyle('left'))>stop)
			{
				new Effect.MoveBy(p, 0, -690, {duration: .25, afterFinish: function(){

					//$(p.parentNode).previous().setStyle({opacity: 1, cursor: 'hand'});

					if(parseInt(p.getStyle('left'))<=stop)
					{
						var n = $(p.parentNode).previous();

						n.removeClassName('over');
						n.setStyle({opacity: .5, cursor: 'default'});
					}
					this.scrolling = false;
				}.bind(this)});
			}
			else
			{
				this.scrolling = false;
			}
		}
	},

	getMediaContainer: function()
	{
		var media = $('display-box-media');

		if(!media){
			media = this.media = new Element('div', {id: 'display-box-media', style: 'clear:both;margin:0 0 11px;background:#000;width:320px;height:200px;'});

			this.box.insert(media);
		}

		return media;
	},

	getInfosContainer: function()
	{
		var infos = $('display-box-infos');

		if(!infos){
			infos = this.infos = new Element('div', {id: 'display-box-infos', style: 'float:left;margin:0 0 0 20px;padding:5px 0 0;'});

			this.box.insert(infos);
		}

		return infos;
	},

	loadPlaylist: function(playlist)
	{
		this.playlists[playlist.name] = playlist.medias;

		playlist.medias.each(function(media){

			$(media).observe('click', this.toggleBox.bindAsEventListener(this, $(media), playlist.name));

		}.bind(this));
	},

	toggleBox: function(event, btn, playlistName)
	{
		event.stop();

		//this.updateDisplay();

		if(this.box.visible())
		{
			new Effect.Parallel(
				[
					new Effect.Fade(this.bg,  {from: .5, to: 0,  sync: true}),
					new Effect.Fade(this.box, {from: .5, to: 0,  sync: true})
				],
				{
					duration: .2
				}
			);
		}
		else
		{
			new Effect.Parallel(
				[
					new Effect.Appear(this.bg,  {from: 0, to: .5, sync: true}),
					new Effect.Appear(this.box, {from: 0, to: 1,  sync: true})
				],
				{
					beforeStart: this.loadMedia.bind(this, btn, playlistName),
					duration   : .3
				}
			);
		}
	},

	updateBox: function(btn, playlistName)
	{
		this.loadMedia(btn, playlistName);
	},

	hideBox: function(){
		if(this.box.visible())
		{
			new Effect.Parallel(
				[
					new Effect.Fade(this.bg,  {from: .5, to: 0, sync: true}),
					new Effect.Fade(this.box, {from: .5, to: 0,  sync: true})
				],
				{
					duration: .2
				}
			);
		}
	},

	loadMedia: function(btn, playlist)
	{
		this.unloadMedia();

		var url = btn.readAttribute('href');

		if(url)
		{
			var infos = '';
			var title = btn.readAttribute('title');

			if(url.toLowerCase().indexOf('.jpg')>=0)
			{
				var preload = new Image();

				preload.onload = function(){

					if(preload && preload.width){

						this.newWidth  = preload.width;
						this.newHeight = preload.height;

						this.media.setStyle({
							width : preload.width + 'px',
							height: preload.height + 'px'
						});

						this.updateDisplay();

						if($('display-box-media'))
						{
							new Effect.Fade($('display-box-media'), {
								duration   : .2,
								from       : 1,
								to         : 0.4,
								afterFinish: function(){
									this.media.update('<img id="display-box-media" src="' + url + '" width="' + preload.width + '" height="' + preload.height + '" alt="" />');

									new Effect.Appear($('display-box-media'), {
										from    : 0.4,
										to      : 1,
										duration: .2
									});

								}.bind(this)
							});
						}
						else
						{
							this.media.update('<img id="display-box-media" src="' + url + '" alt="" />');

							new Effect.Appear($('display-box-media'), {from: 0.8, to: 1, duration: .1});
						}



						this.displayed = {
							media   : url,
							playlist: playlist
						};
					}

					preload.onload = function(){};

					return false;

				}.bind(this);

				preload.src = url;
			}
			else if((url.toLowerCase().indexOf('_trailer')>=0)||url.toLowerCase().indexOf('trailer-')>=0)
			{
				jpg = url.substr(0, url.lastIndexOf('.flv')) + '.jpg';

				this.newWidth  = 640;
				this.newHeight = 480;

				this.media.setStyle({
					width : this.newWidth  + 'px',
					height: this.newHeight + 'px'
				});

				this.updateDisplay();

				this.media.update('<object id="WSplayer" name="WSplayer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.newWidth + '" height="' + this.newHeight + '">' +
				    '<param name="movie" value="/flash/player.swf" />' +
				    '<param name="quality" value="high" />' +
					'<param name="wmode" value="transparent" />' +
					'<param name="allowScriptAccess" value="always" />' +
					'<param name="swliveconnect" value="true" />' +
					'<param name="allowFullScreen" value="true" />' +
					'<param name="swfversion" value="8.0.0.0" />' +
					'<param name="FlashVars" value="' +
						'_thumbnailTitles=[\'Video title\']&amp;' +
						'_videoType=\'File (URL)\'&amp;' +
						'_videoList=[\'' + url + '\']&amp;' +
						'_videoSubtitleList=[\'\']&amp;' +
						'_videoAdsList=[\'\']&amp;' +
						'_videoAdsLinkList=[\'\']&amp;' +
						'_previewImageList=[\'' + jpg + '\']&amp;' +
						'_thumbnailPlayList=[\'\']&amp;' +
						'_save_as=\'Flash parameters\'&amp;' +
						'PlayListXML_path=\'\'&amp;' +
						'_showMainBar=true&amp;' +
						'_skin=\'Default\'&amp;' +
						'_mainbarAutoHide=true&amp;' +
						'_mainbar_Alpha=100&amp;' +
						'_mainbar_Top=#333333&amp;' +
						'_mainbar_Bottom=#000000&amp;' +
						'_loadSkin=false&amp;' +
						'_fixedWidth=' + this.newWidth + '&amp;' +
						'_fixedHeight=' + this.newHeight + '&amp;' +
						'_videoProportions=true&amp;' +
						'_autoSize=true&amp;' +
						'_autoPlay=true&amp;' +
						'_loop=false&amp;' +
						'_videoDeblocking=0&amp;' +
						'_dualThresholdBuffering=true&amp;' +
						'_bufferTime=1&amp;' +
						'_thumbnailCarousel=false&amp;' +
						'_buttonsColor=#FFFFFF&amp;' +
						'_volumeAudio=60&amp;' +
						'_seekBar_color=#0000FF&amp;' +
						'_showBigPlayButton=true&amp;' +
						'_buttonToolTips=false&amp;' +
						'_showNowPlaying=false&amp;' +
						'_textColor=#FFFFFF&amp;' +
						'_textBGColor=#000000&amp;' +
						'_fontFamily=\'Default Embedded Font\'&amp;' +
						'_fontSize=8&amp;' +
						'_emailSubjectTitle=\'\'&amp;' +
						'_emailBodyText=\'\'&amp;' +
						'_buttonToolTipsList=[\'stop\',\'play\',\'pause\',\'played time / all time\',\'sound volume\',\'playlist\',\'html code and link\',\'send to friend\',\'video properties\',\'fullscreen mode\']&amp;' +
						'_showEmailButton=false&amp;' +
						'_showEmbedButton=false&amp;' +
						'_showFullscreenButton=true&amp;' +
						'_showPlayListButton=false&amp;' +
						'_showTimerButton=true&amp;' +
						'_showVideoPropButton=true&amp;' +
						'_showVolumeButton=true&amp;' +
						'_addexterpl=false&amp;' +
						'_plbgcolor=#000000&amp;' +
						'_plwidth=' + this.newWidth + '&amp;' +
						'_plheight=' + this.newHeight + '&amp;' +
						'_addhtmlbuts=false&amp;' +
						'_htmlplay=true&amp;' +
						'_htmlstop=true&amp;' +
						'_htmlplaynum=true&amp;' +
						'_subtitleFontAlign=\'Center\'&amp;' +
						'_subtitleFontFamily=\'Arial\'&amp;' +
						'_subtitleFontBold=false&amp;' +
						'_subtitleFontColor=#FFFFFF&amp;' +
						'_subtitleFontItalic=false&amp;' +
						'_subtitleFontSize=20&amp;' +
						'_showSubtitleFontBG=false&amp;' +
						'_subtitleFontBGColor=#000000&amp;' +
						'_ownerLogoIMGPath=\'\'&amp;' +
						'_ownerLogoAlpha=80&amp;' +
						'_ownerLogoLink=\'\'&amp;' +
						'_ownerLogoLinkTarget=\'_blank\'&amp;' +
						'_ownerLogoAlign=\'Bottom right\'&amp;' +


						'_rightClickMenuCopyright=\'ISART DIGITAL\'&amp;' +
						'_rightClickMenuCopyrightLink=\'http://www.isartdigital.com\'&amp;' +
						'_rightClickMenuCopyrightTarget=\'_blank\'&amp;' +
						'_objectID=\'WSplayer\'" />' +

					'<param name="expressinstall" value="/flash/expressInstall.swf" />' +

				    '<noscript>ISART DIGITAL</noscript>' +

				    '<!--[if !IE]>-->' +
						'<object id="innerWSplayer" name="innerWSplayer" type="application/x-shockwave-flash" data="/flash/player.swf" width="' + this.newWidth + '" height="' + this.newHeight + '">' +
					'<!--<![endif]-->' +

					'<param name="quality" value="high" />' +
				    '<param name="wmode" value="transparent" />' +
				    '<param name="allowScriptAccess" value="always" />' +
				    '<param name="swliveconnect" value="true" />' +
				    '<param name="allowFullScreen" value="true" />' +
				    '<param name="swfversion" value="8.0.0.0" />' +
				    '<param name="FlashVars" value="' +
						'_thumbnailTitles=[\'Video title\']&amp;' +
						'_videoType=\'File (URL)\'&amp;' +
						'_videoList=[\'' + url + '\']&amp;' +
						'_videoSubtitleList=[\'\']&amp;' +
						'_videoAdsList=[\'\']&amp;' +
						'_videoAdsLinkList=[\'\']&amp;' +
						'_previewImageList=[\'' + jpg + '\']&amp;' +
						'_thumbnailPlayList=[\'\']&amp;' +
						'_save_as=\'Flash parameters\'&amp;' +
						'PlayListXML_path=\'\'&amp;' +
						'_showMainBar=true&amp;' +
						'_skin=\'Default\'&amp;' +
						'_mainbarAutoHide=true&amp;' +
						'_mainbar_Alpha=100&amp;' +
						'_mainbar_Top=#333333&amp;' +
						'_mainbar_Bottom=#000000&amp;' +
						'_loadSkin=false&amp;' +
						'_fixedWidth=' + this.newWidth + '&amp;' +
						'_fixedHeight=' + this.newHeight + '&amp;' +
						'_videoProportions=true&amp;' +
						'_autoSize=true&amp;' +
						'_autoPlay=true&amp;' +
						'_loop=false&amp;' +
						'_videoDeblocking=0&amp;' +
						'_dualThresholdBuffering=true&amp;' +
						'_bufferTime=1&amp;' +
						'_thumbnailCarousel=false&amp;' +
						'_buttonsColor=#FFFFFF&amp;' +
						'_volumeAudio=60&amp;' +
						'_seekBar_color=#0000FF&amp;' +
						'_showBigPlayButton=true&amp;' +
						'_buttonToolTips=false&amp;' +
						'_showNowPlaying=false&amp;' +
						'_textColor=#FFFFFF&amp;' +
						'_textBGColor=#000000&amp;' +
						'_fontFamily=\'Default Embedded Font\'&amp;' +
						'_fontSize=8&amp;' +
						'_emailSubjectTitle=\'\'&amp;' +
						'_emailBodyText=\'\'&amp;' +
						'_buttonToolTipsList=[\'stop\',\'play\',\'pause\',\'played time / all time\',\'sound volume\',\'playlist\',\'html code and link\',\'send to friend\',\'video properties\',\'fullscreen mode\']&amp;' +
						'_showEmailButton=false&amp;' +
						'_showEmbedButton=false&amp;' +
						'_showFullscreenButton=true&amp;' +
						'_showPlayListButton=false&amp;' +
						'_showTimerButton=true&amp;' +
						'_showVideoPropButton=true&amp;' +
						'_showVolumeButton=true&amp;' +
						'_addexterpl=false&amp;' +
						'_plbgcolor=#000000&amp;' +
						'_plwidth=' + this.newWidth + '&amp;' +
						'_plheight=' + this.newHeight + '&amp;' +
						'_addhtmlbuts=false&amp;' +
						'_htmlplay=true&amp;' +
						'_htmlstop=true&amp;' +
						'_htmlplaynum=true&amp;' +
						'_subtitleFontAlign=\'Center\'&amp;' +
						'_subtitleFontFamily=\'Arial\'&amp;' +
						'_subtitleFontBold=false&amp;' +
						'_subtitleFontColor=#FFFFFF&amp;' +
						'_subtitleFontItalic=false&amp;' +
						'_subtitleFontSize=20&amp;' +
						'_showSubtitleFontBG=false&amp;' +
						'_subtitleFontBGColor=#000000&amp;' +
						'_ownerLogoIMGPath=\'\'&amp;' +
						'_ownerLogoAlpha=80&amp;' +
						'_ownerLogoLink=\'\'&amp;' +
						'_ownerLogoLinkTarget=\'_blank\'&amp;' +
						'_ownerLogoAlign=\'Bottom right\'&amp;' +


						'_rightClickMenuCopyright=\'WebStunning FLV Player\'&amp;' +
						'_rightClickMenuCopyrightLink=\'http://webstunning.com\'&amp;' +
						'_rightClickMenuCopyrightTarget=\'_blank\'&amp;' +
						'_objectID=\'WSplayer\'" />>' +

				    '<param name="expressinstall" value="/flash/expressInstall.swf" />' +
				    '<!-- The browser displays the following alternative content for users with Flash Player 7.0 and older. -->' +
				    '<div>' +
				      '<h4>Content on this page requires a newer version of Adobe Flash Player.</h4>' +
				      '<p><a href="http://www.adobe.com/go/getflashplayer"><img border="0" src="/images/get_flash_player.gif" alt="Download flash player"/></a></p>' +
				    '</div>' +
				    '<!--[if !IE]>-->' +
				'</object>' +
				'<!--<![endif]-->' +
				'</object>');
			}
			else if(url.toLowerCase().indexOf('.flv')>=0)
			{

			}
			else if(url.toLowerCase().indexOf('.pdf')>=0)
			{
				var dims = document.viewport.getDimensions();
				var top  = (dims.height-600)/2;
				var left = (dims.width-700)/2;
				var pop  = window.open(url,'Media','menubar=no, status=no, scrollbars=yes, menubar=no, width=700, height=600, top=' + top + ', left=' + left);

				pop.focus();
			}

			if(title)
			{
				this.infos.update('<strong>' + title + '</strong>');
			}
			else
			{
				this.infos.update('');
			}

			if(this.playlists[playlist].length<=1)
			{
				this.prev.hide();
				this.next.hide();
				this.infos.setStyle({margin: '0px'});
			}
			else
			{
				this.prev.show();
				this.next.show();
				this.infos.setStyle({margin: '0 0 0 20px'});
			}
		}
		else
		{
//			alert(btn)
//			this.box.setStyle({
//				width: this.options.width + 'px',
//				height: this.options.height + 'px'})
		}
	},

	unloadMedia: function()
	{
		this.resetDisplay();
	},

	resetDisplay: function()
	{
		$('display-box-media').update('');
	},

	updateDisplay: function()
	{
		var dims = document.viewport.getDimensions();

		this.bg.setStyle({
			width : dims.width  + 'px',
			height: dims.height + 'px'
		});

		var boxDims = {width: this.newWidth+40, height: this.newHeight+64};

		this.getBox().setStyle({
			top : ((dims.height - boxDims.height) / 2) + 'px',
			left: ((dims.width  - boxDims.width) / 2) + 'px'
		});
	}
});

var Navigation = Class.create(
{
	initialize: function(container,options)
	{
		this.options = Object.extend(this.options, options || {});

		this.locked = false;
		this.opened = null;
		this.opentimer  = null;
		this.closetimer = null;

		var nav = this.nav = $(container);
		var uls = this.uls = nav.select('ul');

		uls.each(function(u){
			var w = 0;
			var its = u.childElements();
			its.each(function(e){
				ew = e.childElements()[0].childElements()[0].getWidth();
				if(ew>w)
				{
					w = ew;
				}
			});
			its.invoke('setStyle', {width: w + 'px'});
		});
		uls.invoke('hide');

		Event.observe(nav, 'mousemove', function(event)
		{
			event.stop();

			if(!this.locked)
			{
				this.locked = true;

				var elt = event.element();

				if(elt.nodeName=='SPAN')
				{
					elt = $(elt.parentNode.parentNode);
				}
				else if(elt.nodeName=='A')
				{
					elt = $(elt.parentNode);
				}
				else if(elt.nodeName!='LI')
				{
					this.locked = false;
					return;
				}

				if(this.opentimer)
				{
					clearTimeout(this.opentimer);
					this.opentimer = null;
				}

				var its=$$('.selected-item').findAll(function(e){
					return !elt.descendantOf(e) && !e.descendantOf(elt) && e!=elt
				});

				its.each(function(e){
					e.select('ul').each(function(ul){ this.hideEffect(ul); }.bind(this));
					e.removeClassName('selected-item');
				}.bind(this));

				this.showEltSubMenu(elt);
			}
		}.bind(this));

		Event.observe(document, 'mouseover', this.hideSubMenus.bindAsEventListener(this));
	},

	hideSubMenus: function(event)
	{
		var elt = event.element();
		if (!this.locked && !elt.descendantOf(this.nav))
		{
			//this.timer = setTimeout(function(){
			 this.uls.each(function(ul){ this.hideEffect(ul); }.bind(this));
			//new Effect.multiple(this.uls, Effect.Appear, {from: 1, to: 0, duration: .2});
				$$('.selected-item').invoke('removeClassName','selected-item');
			//}.bind(this), 250);
		}
	},

	hideEffect: function(ul)
	{
		if(ul.visible())
		{
			if(ul.hasClassName('level2'))
			{
				ul.blindUp({duration:.15, fps:60, scaleX: false, scaleY: true, scaleContent: false, queue: {position: 'with-last', scope: 'global'}, afterFinish: function(){ this.closetimer = setTimeout(function(){ this.locked = false; clearTimeout(this.closetimer); this.closetimer = null; }.bind(this), 0); }.bind(this)});
			}
			else if(ul.hasClassName('level3'))
			{
				ul.blindUp({duration:.15, fps:60, scaleX: true, scaleY: true, scaleContent: false, queue: {position: 'front', scope: 'global'}, afterFinish: function(){ this.closetimer = setTimeout(function(){ this.locked = false; clearTimeout(this.closetimer); this.closetimer = null; }.bind(this), 0); }.bind(this)});
			}
		}
	},

	showEltSubMenu: function(elt)
	{
		elt.addClassName('selected-item');

		var ul=elt.childElements().find(function(e){return e.nodeName=='UL';});
		if (ul && !ul.visible())
		{
			if(ul.hasClassName('level2'))
			{
				ul.setStyle({'left': elt.positionedOffset().left-4 + 'px', visibility: 'visible'});
				this.opentimer = setTimeout(function(){	ul.blindDown({duration:.25, fps:60, scaleX: false, scaleY: true, scaleContent: false, queue: {position: 'end', scope: 'global'}, afterFinish: function(){ this.opentimer = setTimeout(function(){ this.locked = false; clearTimeout(this.opentimer); this.opentimer = null; }.bind(this), 0); }.bind(this)}); }.bind(this), 200);
			}
			else if(ul.hasClassName('level3'))
			{
				//ul.setStyle({'left': (elt.positionedOffset().left + elt.getWidth()-8) + 'px', top: elt.getHeight()-13 +'px', visibility: 'visible'});
				ul.setStyle({'left': (elt.positionedOffset().left + elt.getWidth()-8) + 'px', top: (elt.positionedOffset().top -14) +'px', visibility: 'visible'});
				this.opentimer = setTimeout(function(){	ul.blindDown({duration:.25, fps:60, scaleX: true, scaleY: true, scaleContent: false, queue: {position: 'front', scope: 'global'}, afterFinish: function(){ this.opentimer = setTimeout(function(){ this.locked = false; clearTimeout(this.opentimer); this.opentimer = null; }.bind(this), 0); }.bind(this)}); }.bind(this), 60);
			}
		}
		else
		{
			this.locked = false;
		}
	}
});

var PicturesAnimator = Class.create(
{
	initialize: function()
	{
		var content = $('content');
		if(!content)
		{
			return;
		}

		var type = content.className;

		if(type=='summary')
		{
			var bgs  = [];
			var imgs = $$('#content-aside td');
			imgs.each(function(img){
					var src = img.getStyle('backgroundImage');

					if(src && src!='none')
					{
						bgs.push(src.substring(4, src.length-1));
					}
			});

			new Ajax.Request(hostname + '/ajax/getimgsizes', {
				method: 'post',
				parameters: {
					imgs: bgs.join(',')
				},
				onSuccess: function(transport) {
					var sizes = transport.responseJSON;

                    if(!sizes)
                    {
                        return;
                    }

					var cpt = 0;
					imgs.each(function(img){
							var src = img.getStyle('backgroundImage');

							if(src && src!='none')
							{
								var w  = sizes[cpt][0];
								var p  = -527;

								if(w>527)
								{
									var timer = null;

									img.observe('mouseover', function(){
										img.setStyle({backgroundPosition: p + 'px 0px'});

										timer = setInterval(function(){
											p -= 527;
											if(p<=-w)
											{
												p = 0;
											}

											img.setStyle({backgroundPosition: p + 'px 0px'});

										}, 1000);
									});

									img.observe('mouseout', function(){
										p = -527;
										img.setStyle({backgroundPosition: '0px 0px'});
										clearInterval(timer);
										timer = null;
									});
								}

								cpt++;
							}
					});
				}
			});
		}
		else if(type=='page')
		{
			var bgs  = [];
			var imgs = $$('#content-aside .scroll img');

			if(imgs.length==0){
				return;
			}

			imgs.each(function(img){
					bgs.push(img.src.substring(img.src.indexOf('.fr/')+3, img.src.length));
			});

			new Ajax.Request(hostname + '/ajax/getimgsizes', {
				method    : 'post',
				parameters: { imgs: bgs.join(',') },
				onSuccess : function(transport) {
					var sizes = transport.responseJSON;
					var cpt   = 0;

					imgs.each(function(img){
						var src = img.src;
						var w   = sizes[cpt][0];
						var p   = -195;

						if(w>195)
						{
							var timer = null;
							var r     = false;

							$(img.parentNode).observe('click', function(event){

								event.stop();

								if(r==false)
								{
									r = true;

									img.setStyle({left: p + 'px'});

									timer = setInterval(function(){
										p -= 195;

										if(p<=-w)
										{
											p = 0;
										}

										img.setStyle({left: p + 'px'});

									}, 1000);
								}
								else

								{
									p = -195;
									img.setStyle({left: '0px'});
									clearInterval(timer);
									timer = null;
									r = false;
								}
							});
						}

						cpt++;
					});
				}
			});
		}
	}
});

var ColumnVideos = Class.create({
	initialize: function()
	{
	    if(typeof swfobject == 'undefined')
        {
            return;
        }

		$$('.WSplayer').each(function(v){
			swfobject.registerObject(v.identify());
		});
	}
});

var InnerNavigation = Class.create(
{
	initialize: function()
	{
		if(!$('inner-navigation'))
		{
			return;
		}

		var items   = $$('.innersubmenu a');
		var parents = $$('.innersubmenu li');
		var anchor  = unescape(self.document.location.hash.substring(1));

		if(anchor.length>0)
		{
			parents.invoke('removeClassName', 'selected');
			items.each(function(item, i){
				if($(item.parentNode.parentNode.parentNode).hasClassName('selected') && item.href.search(new RegExp(anchor, 'gi'))>0)
				{
					$(item.parentNode).addClassName('selected');
				}
			});
		}

		items.each(function(item, i){
			item.observe('click', function(){
				parents.invoke('removeClassName', 'selected');
				parents[i].addClassName('selected');
			});
		});

		//$(items[0].parentNode).addClassName('selected');
	}
});

var BackToTop = Class.create({
	initialize: function()
	{
		$$('div.anchor a').each(function(a){
			a.observe('click', this.scrollToTop.bindAsEventListener(this));
		}.bind(this));
	},

	scrollToTop: function(event)
	{
		event.stop();
		new Effect.ScrollTo('page-top', {duration: .3});
	}
});

var FlashHeader = Class.create(
{
	initialize: function()
	{
		if(typeof swfobject != 'undefined')
		{
			var src = hostname + '/flash/headers/' + $('header-flash').className + '.swf';

			swfobject.embedSWF(src, 'header-flash', 950, 132, '9.0.0', hostname + '/scripts/lib/swfobject/expressInstall.swf', null, {wmode: 'transparent'});
		}
	}
});

document.observe('dom:loaded', function(){
	new Navigation('menu');
	new FlashHeader();
	new InnerNavigation();
	new PicturesAnimator();
	//new ColumnVideos();
	new BackToTop();
});