window.addEvent('domready', function(){
	
	/*
		Slideshow initialize
	*/ 

	var sidebar_images = ['01.jpg', '02.jpg', '03.jpg', '04.jpg', '05.jpg', '06.jpg', '07.jpg', '08.jpg', '09.jpg', '10.jpg', '11.jpg', '12.jpg', '13.jpg', '14.jpg', '15.jpg', '16.jpg', '17.jpg', '18.jpg', '19.jpg', '20.jpg'];
	
	new Slideshow('artshop-int', sidebar_images, { 
		hu: '/images/slide/mini',
		rel: "lightbox-sidebar",
		height: 92, 
		width: 166,
		delay: 1500,
		duration: 2000,
		random: true,
		linked: true,
		transition: Fx.Transitions.quartInOut
	});
	
	
	/* 
		Modal Box
	
	*/
	
	$$(".slideshow-images a").addEvent('click', function(e){
		console.log(this.href);
		this.setProperties({href: this.href.replace("slide/mini", "slide")});
		console.log(this.href);
	});
	
	loadImages(sidebar_images, "lightbox-sidebar", $("artshop-int"));
	
	/* 
		Loader 
	*/
	
	var loader = new Element('div').setProperties({id:'loader'}).setStyles({display:'none'}).inject($("content-main"), "top")
	new Element('p').set("text", "Chargement").inject(loader);
	
	current = location.href.split('/').getLast().split('.')[0];
	currentLink = current+'-link'
	var myMenu = new ImageMenu($$('#menu a'),{open:currentLink, openWidth:270, border:0, onOpen:function(e,i){}});
	
	$$(".loadMe").addEvent('click', function(e){
		e = new Event(e);
		e.stop();
		a(this);
	});
	
	$("index-link").addEvent('click', function(e){
		e = new Event(e);
		e.stop();
		myMenu.reset(-1);	
	})
})

/* */

function loadImages(images, rel, target){
	images.each(function(filename){
		var temp_link = new Element('a').setProperties({href:'/images/slide/'+filename, rel:rel}).setStyles({display:'none'}).inject(target, "bottom");
	});
}

function loadComponent(page){
	
	shop_images = ['shop-01.jpg', 'shop-02.jpg', 'shop-03.jpg', 'shop-04.jpg', 'shop-05.jpg'];
	cadeaux_images = ['cadeaux-01.jpg', 'cadeaux-02.jpg', 'cadeaux-03.jpg', 'cadeaux-04.jpg', 'cadeaux-05.jpg'];
	cartes_images = ['cartes-01.jpg', 'cartes-02.jpg', 'cartes-03.jpg', 'cartes-04.jpg', 'cartes-05.jpg'];
	reproductions_images = ['reproductions-01.jpg', 'reproductions-02.jpg', 'reproductions-03.jpg', 'reproductions-04.jpg', 'reproductions-05.jpg'];
	decoration_images = ['decoration-01.jpg', 'decoration-02.jpg', 'decoration-03.jpg', 'decoration-04.jpg', 'decoration-05.jpg'];
	
	slideshow_page_options = { 
		hu: '/images/slide/mini',
		rel: "lightbox-content",
		height: 194, 
		width: 244,
		delay: 1500,
		duration: 2000,
		random: true,
		linked: true,
		transition: Fx.Transitions.quartInOut
	};
	
	slideshow_page_options_vert = { 
		hu: '/images/slide/mini',
		rel: "lightbox-content",
		height: 244, 
		width: 194,
		delay: 1500,
		duration: 2000,
		random: true,
		linked: true,
		transition: Fx.Transitions.quartInOut
	};
	
	switch(page){
		case "shop" : 
			new Slideshow('shop-pictures', shop_images, slideshow_page_options);
			loadImages(shop_images, "lightbox-content", $("shop-pictures"));
		break;
		case "cadeaux":
			new Slideshow('cadeaux-pictures', cadeaux_images, slideshow_page_options_vert);
			loadImages(cadeaux_images, "lightbox-content", $("cadeaux-pictures"));
		break;
		case "cartes":
			new Slideshow('cartes-pictures', cartes_images, slideshow_page_options);
			loadImages(cartes_images, "lightbox-content", $("cartes-pictures"));
		break;
		case "reproductions":
			new Slideshow('reproductions-pictures', reproductions_images, slideshow_page_options);
			loadImages(reproductions_images, "lightbox-content", $("reproductions-pictures"));
		break;
		case "decoration":
			new Slideshow('decoration-pictures', decoration_images, slideshow_page_options);
			loadImages(decoration_images, "lightbox-content", $("decoration-pictures"));
		break;
	}
}

/* Formulaire de contact */ 

function loadContact(){

	var error = new Element('div').setProperties({id:'error-notifier'}).inject($("contactForm"), "top");
	var notice = new Element('div').setProperties({id:'message-notifier'}).inject($("contactForm"), "top");

	$('contactForm').addEvent('submit', function(e) {
		new Event(e).stop();
	
		var url = this.getProperty('action');
		var method = this.getProperty('method');

		new Request.HTML ({
			url: url,
			method: method,
			onComplete: function() {
				res = this.getHeader("Mailer");
				if(res == "Error"){
					error.set("html", this.getHeader("MailerError"));
					notice.set("html", "");
				}else{
					notice.set("html", "<p>Votre message à été envoyé avec succès!</p>");
					error.set("html", "");
					$("contactForm").reset();
				}
			}
		}).post(this);
	
	});

}

function a(link){
	
	if(currentLink != link.id){
		currentLink = link.id;
		$("loader").show();
		var links = $("menu").getElementsByTagName('a');
		for(i=0;i<links.length;i++)
		{
			links[i].rel = '';
		}

		new Fx.Tween('page-content',{ duration: 500, transition: Fx.Transitions.quartInOut }).start('opacity', 0);

		window.setTimeout("switchPage('"+link.href+"')", 600);
		window.setTimeout(function(){
			new Fx.Tween('page-content',{ duration: 500, transition: Fx.Transitions.quartInOut }).start('opacity', 1);
			$("loader").hide();
		}, 1000);
		if(!Browser.Engine.trident && !Browser.Engine.webkit){
			link.style.width = "270px !important";
		}
		link.rel = 'clicked';
	}
	
}

function switchPage(link){
	
	/* Changement du contenu */ 
	
	url = link; //.href;
	
	var content = new Request({
		url: url,
		method: 'get',
		evalScripts: true,
		onComplete:function(response){
			//$("page-content").setStyles({display:'none'});
			var temp = new Element('div').setProperties({id:'temp'}).setStyles({display:'none'}).injectInside(document.body);
			temp.set('html', response);
			var newEl = temp.getElement('.container');
			newEl.set('opacity', 0);
			newEl.replaces($("page-content"));
			temp.dispose();
			if(url.test('contact')){
				loadContact();
			}
			if(url.test('index')){
				loadComponent('shop');
			}
			if(url.test('cadeaux')){
				loadComponent('cadeaux');
			}
			if(url.test('cartes')){
				loadComponent('cartes');
			}
			if(url.test('reproductions')){
				loadComponent('reproductions');
			}
			if(url.test('decoration')){
				loadComponent('decoration');
			}
		}
	}).send()
}