var Layout = new Class({
	
	base: null,
	
	initialize: function(base){
		$(document.body).removeClass('nojs');
		this.base = base;
		this.flash();
		this.grayscaleIcons();
		this.shadows();
		this.projectIntros();
		this.mainMenu();
		this.clientSlideshow();
		this.teamSlideshow();
		this.loadCerabox();
	},
/**
 * Effet sur les icones
 */
	grayscaleIcons: function(){
		var icons = $$('.greyscale_icon span');
		icons.each(function(icon, i){
			var parent = icon.getParent('.grayscale_icon_toggler, .sidebox');
			if(parent){
				var fx = new Fx.Tween(icon, {
					property: 'opacity',
					link: 'cancel'/*,
					duration: 'short'*/
				});
				parent.addEvents({
					'mouseover': function(){
						fx.start(0);
					},
					'mouseout': function(){
						fx.start(1);
					}
				});
			}
		});
	},
/**
 * Flash de la home
 */
	flash: function(){
		var flashvars = {            
			//xmlPath: this.base+"theme/v1/files/flash/slideshow.xml"
			xmlPath: this.base+"home_slides.xml"
		};
		var params = {
		  allowFullScreen: "true",
		  wmode: "transparent"
		};
		var attributes = {};		
		swfobject.embedSWF(this.base+"theme/v1/files/flash/main.swf", "home_flash_inner", "960", "430", "9.0.0","expressInstall.swf", flashvars, params, attributes);	
	},
	shadows: function(){
		this.mkShadows(180);
		this.mkShadows(220);
		this.mkShadows(480);
	},
/**
 * Ajoute les ombres aux images
 */
	mkShadows: function(size){
		size = size.toString();
		var selector = '.shadow_'+size;
		var imageSrc = this.base+'theme/v1/img/shadow_'+size+'.png';
		var items = $$(selector);
		var image = new Element('img', {
			src: imageSrc,
			styles: {
				'margin-bottom': '10px'
			}
		});
		items.each(function(item, i){
			item.setStyle('margin-bottom', 0);
			image.clone().inject(item, 'after');
		});
	},
	projectIntros: function(){
		$$('div.project_list div.grid_4, #projects_top div.grid_4').each(function(project){
			var imgs = project.getElements('p.image a img');
			if(imgs.length <= 1){
				project.getElement('p.dual_buttons').addClass('disabled').getElements('a').addEvent('click', function(e){ e.stop(); });
				return;
			}
			var projectIntroSlideshow = new SlideShow( project.getElement('p.image a'), {
				transition: 'fadeThroughBackground',
				delay: 5000,
				duration: 200,
				autoplay: false
			});
			project.getElement('p.dual_buttons a.prev').addEvent('click', function(e){
				e.stop();
				projectIntroSlideshow.show('previous');
			});
			project.getElement('p.dual_buttons a.next').addEvent('click', function(e){
				e.stop();
				projectIntroSlideshow.show('next');
			});
		});
	},
	mainMenu: function(){
		if( Browser.engine=='trident' && Browser.version < 8) return;
		$$('#menu li').each(function(li){
			var submenu = li.getElement('.submenu');
			if(!submenu) return;
			var a = li.getElement('a');
			submenu.setStyles({
				display: 'block',
				opacity: 0
			});
			var submenuFx = new Fx.Morph(submenu, {
				link: 'cancel'
			});
			li.addEvents({
				'mouseover': function(){
					if(submenu.getStyle('opacity') == 0){
						submenuFx.start({
							opacity: 1,
							'margin-top': [10, 5]
						});
					}
				}
			});
			(function(){
				if( a.getStyle('border-left-color') == 'transparent' ){
					submenuFx.set({
						opacity: 0
					});
				}
			}).periodical(200);
		});
	},
	
	clientSlideshow: function(){
		var container = $('clients_slideshow');
		if(!container) return;
		var clientsSlideshow = new SlideShow(container, {
			transition: 'pushRight',
			delay: 5000,
			duration: 200,
			autoplay: false
		});
		var links = $$('#clients_slideshow_btns a');
		links.each(function(link, i){
			link.addEvent('click', function(e){
				e.stop();
				clientsSlideshow.show(i);
				links.removeClass('current');
				this.addClass('current');
			});
		});
	},
	
	teamSlideshow: function(){
		var container = $('team_slideshow');
		if(!container) return;
		new SlideShow(container, {
			transition: 'pushUp',
			delay: 5000,
			duration: 400,
			autoplay: true
		});	
	},
	loadCerabox: function(){
		var links = $$('.ceraBox');
		if(links.length){
			var box = new CeraBox();
			box.addItems(links, {
				displayTitle: false,
				group: false
			});	
		}
	}
});

