var resizeBackground = function() {
	$$('.background img')[0].setProperty('width', $$('.background')[0].getSize().x);
	$$('.background')[0].setStyle('height', document.id('wrapper-bottom').getPosition().y + 80);
}

window.addEvent('domready',function() {
	window.addEvent('resize', resizeBackground);
	resizeBackground();

	News = new Class({
		options: {
			news: null
		},

		initialize: function() {
			if (!Browser.Engine.trident && Cookie.read('news')) {
				this.options.news = Cookie.read('news');
			}

			if (this.options.news) {
				this.parseNews();
			} else {
				this.getNews();
			}
		},

		parseNews: function() {
			if (this.options.news) {
				document.id('news').getChildren()[1].set('html', this.options.news);
			}
		},

		getNews: function() {
			var News = this;

			new Request({
				url: "actualites.html?rand=" + $random(1, 1000),
				onSuccess: function(response) {
					News.options.news = response;
					if (!Browser.Engine.trident) {
						Cookie.write('news', response, 1);
					}
					News.parseNews();
				}
			}).send();
		}
	});

	Partners = new Class({
		options: {
			partners: null
		},

		initialize: function() {
			if (!Browser.Engine.trident && Cookie.read('partners')) {
				this.options.partners = Cookie.read('partners');
			}
			if (this.options.partners) {
				this.parsePartners();
			} else {
				this.getPartners();
			}
		},

		parsePartners: function() {
			if (this.options.partners) {
				document.id('partners').getChildren()[1].set('html', this.options.partners);
			}
		},

		getPartners: function() {
			var Partners = this;

			new Request({
				url: "partenaires.html?rand=" + $random(1, 1000),
				onSuccess: function(response) {
					Partners.options.partners = response;
					if (!Browser.Engine.trident) {
						Cookie.write('partners', response, 1);
					}
					Partners.parsePartners();
				}
			}).send();
		}
	});

	Menuleft = new Class({
		options: {
			menuleft: null
		},

		initialize: function() {
			if (!Browser.Engine.trident && Cookie.read('menuleft')) {
				this.options.menuleft = Cookie.read('menuleft');
			}
			if (this.options.menuleft) {
				this.parseMenuleft();
			} else {
				this.getMenuleft();
			}
		},

		parseMenuleft: function() {
			if (this.options.menuleft) {
				document.id('menu-left').set('html', this.options.menuleft);
				if (!document.id('home') && !document.id('home-link')) {
					document.id('home-link').setStyle('visibility', 'visible');
				}

				if (!/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
					Cufon.replace('.Miama', { fontFamily: 'Miama' });
					Cufon.replace('.LucidaSansUnicode', { fontFamily: 'Lucida Sans Unicode' });
					Cufon.replace('.CenturyGothic', { fontFamily: 'Century Gothic' });
					Cufon.replace('.CenturyGothicBold', { fontFamily: 'Century Gothic Bold' });
				}
			}
		},

		getMenuleft: function() {
			var Menuleft = this;

			new Request({
				url: "menu-left.html?rand=" + $random(1, 1000),
				onSuccess: function(response) {
					Menuleft.options.menuleft = response;
					if (!Browser.Engine.trident) {
						Cookie.write('menuleft', response, 1);
					}
					Menuleft.parseMenuleft();
				}
			}).send();
		}
	});

	Brefbox = new Class({
		options: {
			brefbox: null
		},

		initialize: function() {
			if (Cookie.read('brefbox')) {
				this.options.brefbox = Cookie.read('brefbox');
			}
			if (this.options.brefbox) {
				this.parseBrefbox();
			} else {
				this.getBrefbox();
			}
		},

		parseBrefbox: function() {
			if (this.options.brefbox) {
				document.id('bref-box').set('html', this.options.brefbox);
			}
		},

		getBrefbox: function() {
			var Brefbox = this;

			new Request({
				url: "bref-box.html?rand=" + $random(1, 1000),
				onSuccess: function(response) {
					Brefbox.options.brefbox = response;
					Cookie.write('brefbox', response, 1);
					Brefbox.parseBrefbox();
				}
			}).send();
		}
	});

	/*
	Scrollingpanel = new Class({
		options: {
			scrollingpanel: null
		},

		initialize: function() {
			if (Cookie.read('scrollingpanel')) {
				this.options.scrollingpanel = Cookie.read('scrollingpanel');
			}
			if (this.options.scrollingpanel) {
				this.parseScrollingpanel();
			} else {
				this.getScrollingpanel();
			}
		},

		parseScrollingpanel: function() {
			if (this.options.scrollingpanel) {
				document.id('scrolling-panel').set('html', this.options.scrollingpanel);
			}
		},

		getScrollingpanel: function() {
			var Scrollingpanel = this;

			new Request({
				url: "scrolling-panel.html?rand=" + $random(1, 1000),
				onSuccess: function(response) {
					Scrollingpanel.options.scrollingpanel = response;
					Cookie.write('scrollingpanel', response, 1);
					Scrollingpanel.parseScrollingpanel();
				}
			}).send();
		}
	});
	*/



	if (document.id('news')) {
		new News();
	}

	if (document.id('partners')) {
		new Partners();
	}

	if (document.id('menu-left')) {
		new Menuleft();
	}

	if (document.id('bref-box')) {
		new Brefbox();
	}

	/*
	if (document.id('scrolling-panel')) {
		new Scrollingpanel();
	}
	*/

	if (document.id('diaporama')) {
		/* preloading */
		var imagesDir = 'images/diaporama/';
		var holder = $('diaporama');
		images.each(function(img,i){ images[i] = imagesDir + '' + img; }); //add dir to images
		var progressTemplate = 'Loading image {x} of ' + images.length;
		var updateProgress = function(num) {
			progress.set('text',progressTemplate.replace('{x}',num));
		};
		var progress = $('progress');
		updateProgress('text','0');
		var loader = new Asset.images(images, {
			onProgress: function(c,index) {
				updateProgress('text',index + 1);
			},
			onComplete: function() {
				var slides = [];
				// put images into page
				images.each(function(im) {
					slides.push(new Element('img',{
						src:im,
						width: 488,
						height: 364,
						styles: {
							opacity:0,
							top:10,
							left:10,
							position:'absolute',
							'z-index': 10
						}
					}).inject(holder));
					holder.setStyle('background','url(logo.png) center 80px no-repeat');
				});

				var showInterval = 5000;
				var index = 0;
				progress.set('text','Images loaded.');
				(function() {slides[index].tween('opacity',1); }).delay(1000);
				var start = function() {
					(function() {
						holder.setStyle('background','');
						slides[index].fade(0);
						++index;
						index = (slides[index] ? index : 0);
						slides[index].fade(1);
					}).periodical(showInterval);
				};

				// start the show
				start();
			}
		});
	}

	var logo = document.id('logo');
	var title = document.id('title');
	if (logo && title) {
		var showInterval = 5000;
		var index = 0;
		var slides = [logo, title];
		(function() {slides[index].tween('opacity',1); }).delay(1000);
		var start = function() {
			(function() {
				slides[index].fade(0);
				++index;
				index = (slides[index] ? index : 0);
				slides[index].fade(1);
			}).periodical(showInterval);
		};
		start();
	}
});

