var contentId;
var myGallery = new Array();
var rubId;
var multiG = new Array();
var contentVisited = new Array();
var contentTmp = 0;
var TipsArray = new Array();
var slideArray = new Array();
var posContent = {'1': '1024', '2': '2048'};
var ieGalleries = [];
window.addEvent('domready', function(){
	var scroll = new Fx.Scroll('bloc', {
		wait: false,
		duration: 4000,
		offset: {'x': 0, 'y': 0},
		transition: Fx.Transitions.Quad.easeInOut,
		wheelStops: false
	});
	
	ie8 = false;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ 
	    var ieversion = new Number(RegExp.$1) 
	    if (ieversion >= 8) ie8 = true;
	}
	
	if (window.gecko) scroll.set([0,0]);
	
	var menu = new Fx.Style('menu', 'opacity');	
	var leftPos = {'0' : '871px', '1' : '1875px', '2' : '2899px'};
	var posMenu = {'0' : '871', '1' : '1875', '2' : '2899'};
	
	
	$('link0').addEvent('click', function(event) {
		event = new Event(event).stop();
		hideContent();
		scroll.toElement('content0', {onComplete: insertLinkHome('0')});
		menu.start(1, 0);
		contentTmp = 0;
	});
		
	$('link7').addEvent('click', function(event) {
		event = new Event(event).stop();
		contentVisited.push('7');
		hideContent();
		content = contentTmp == 1?2:1;
		scroll.toElement('content' + content, {onComplete: showContent('content-inner7', content)});
		contentTmp = content;
		menu.start(1, 0);	
	});
	
	$('link8').addEvent('click', function(event) {
		event = new Event(event).stop();
		contentVisited.push('8');
		hideContent();
		content = contentTmp == 1?2:1;
		scroll.toElement('content' + content, {onComplete: showContent('content-inner8', content)});
		
		contentTmp = content;
		menu.start(1, 0);
		
	});
	
	$('link1').addEvent('click', function(event) {
		event = new Event(event).stop();
		contentVisited.push('1');
		hideContent();		
		content = contentTmp == 1?2:1;
		scroll.toElement('content' + content, {onComplete: showContent('content-inner1', content)});
		contentTmp = content;
		menu.start(1, 0);	
	});
		
	$('link2').addEvent('click', function(event) {
		event = new Event(event).stop();
		contentVisited.push('2');
		hideContent();
		content = contentTmp == 1?2:1;
		scroll.toElement('content' + content, {onComplete: showContent('content-inner2', content)});
		contentTmp = content;
		menu.start(1, 0);
	});
		
	$('link3').addEvent('click', function(event) {
		event = new Event(event).stop();
		contentVisited.push('3');
		hideContent();
		content = contentTmp == 1?2:1;
		scroll.toElement('content' + content, {onComplete: showContent('content-inner3', content)});
		contentTmp = content;
		menu.start(1, 0);
	});
	
	$('link4').addEvent('click', function(event) {
		event = new Event(event).stop();
		contentVisited.push('4');
		hideContent();
		content = contentTmp == 1?2:1;
		scroll.toElement('content' + content, {onComplete: showContent('content-inner4', content)});
		contentTmp = content;
		menu.start(1, 0);
	});
	
	$('link5').addEvent('click', function(event) {
		event = new Event(event).stop();
		contentVisited.push('5');
		hideContent();
		content = contentTmp == 1?2:1;
		scroll.toElement('content' + content, {onComplete: showContent('content-inner5', content)});
		contentTmp = content;
		menu.start(1, 0);
	});
	
	$('link6').addEvent('click', function(event) {
		event = new Event(event).stop();
		contentVisited.push('6');
		hideContent();
		content = contentTmp == 1?2:1;
		scroll.toElement('content' + content, {onComplete: showContent('content-inner6', content)});
		contentTmp = content;
		menu.start(1, 0);
	});
	
	offsets = {'0' : {'x': 100, 'y': -20}, '1' : {'x': 75, 'y': -70}, '2' : {'x': 80, 'y': -80}, '3' : {'x': 110, 'y': -40}, '4' : {'x': 110, 'y': -75}, '5' : {'x': 105, 'y': -35}, '6' : {'x': 107, 'y': -30}, '7' : {'x': 100, 'y': -30}, '8' : {'x': 120, 'y': -28}};
	if ((window.ie && !ie8) || window.opera) offsets = {'0' : {'x': 100, 'y': 60}, '1' : {'x': 75, 'y': 10}, '2' : {'x': 80, 'y': 5}, '3' : {'x': 110, 'y': 20}, '4' : {'x': 110, 'y': 30}, '5' : {'x': 105, 'y': 50}, '6' : {'x': 107, 'y': 45}, '7' : {'x': 100, 'y': 30}, '8' : {'x': 120, 'y': 18}};
	
	TipsArray[0] = new TipsX3($('link0'), {
			className: 'menu',
			id: 'menu',
			fixed: true,
			offsets: offsets[0],
			initialize:function(){
				this.fx = new Fx.Elements(this.toolTip, {wait: false, duration: 2000, transition: Fx.Transitions.Back.easeOut});
				this.toolTip.setStyle('width', '0');
			},
			onShow: function(toolTip) {
				toolTip.setStyle('visibility', 'visible');
				if (window.ie6) PngFixImg();
				this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 155]}});
 
			},
			onHide: function(toolTip) {
				this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 0]}});
			}		
		});
	
	TipsArray[7] = new TipsX3($('link7'), {
			className: 'menu',
			id: 'menu',
			fixed: true,
			offsets: offsets[7],
			initialize:function(){
				this.fx = new Fx.Elements(this.toolTip, {wait: false, duration: 2000, transition: Fx.Transitions.Back.easeOut});
				this.toolTip.setStyle('width', '0');
			},
			onShow: function(toolTip) {
				toolTip.setStyle('visibility', 'visible');
				if (window.ie6) PngFixImg();
				this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 170]}});
 
			},
			onHide: function(toolTip) {
				if (!contentVisited.contains('7')) this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 0]}});
			}		
		});
	
	TipsArray[8] = new TipsX3($('link8'), {
			className: 'menu',
			id: 'menu',
			fixed: true,
			offsets: offsets[8],
			initialize:function(){
				this.fx = new Fx.Elements(this.toolTip, {wait: false, duration: 2000, transition: Fx.Transitions.Back.easeOut});
				this.toolTip.setStyle('width', '0');
			},
			onShow: function(toolTip) {
				toolTip.setStyle('visibility', 'visible');
				if (window.ie6) PngFixImg();
				this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 100]}});
 
			},
			onHide: function(toolTip) {
				if (!contentVisited.contains('8')) this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 0]}});
			}	
		});
	
	TipsArray[1] = new TipsX3($('link1'), {
			className: 'menu',
			id: 'menu',
			fixed: true,
			offsets: offsets[1],
			initialize:function(){
				this.fx = new Fx.Elements(this.toolTip, {wait: false, duration: 2000, transition: Fx.Transitions.Back.easeOut});
				this.toolTip.setStyle('width', '0');
			},
			onShow: function(toolTip) {
				toolTip.setStyle('visibility', 'visible');
				if (window.ie6) PngFixImg();
				this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 272]}});
 
			},
			onHide: function(toolTip) {
				if (!contentVisited.contains('1')) this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 0]}});
			}		
		});
	
	TipsArray[2] = new TipsX3($('link2'), {
			className: 'menu',
			id: 'menu',
			fixed: true,
			offsets: offsets[2],
			initialize:function(){
				this.fx = new Fx.Elements(this.toolTip, {wait: false, duration: 2000, transition: Fx.Transitions.Back.easeOut});
				this.toolTip.setStyle('width', '0');
			},
			onShow: function(toolTip) {
				toolTip.setStyle('visibility', 'visible');
				if (window.ie6) PngFixImg();
				this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 217]}});
 
			},
			onHide: function(toolTip) {
				if (!contentVisited.contains('2')) this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 0]}});
			}	
		});
	
	TipsArray[3] = new TipsX3($('link3'), {
			className: 'menu',
			id: 'menu',
			fixed: true,
			offsets: offsets[3],
			initialize:function(){
				this.fx = new Fx.Elements(this.toolTip, {wait: false, duration: 2000, transition: Fx.Transitions.Back.easeOut});
				this.toolTip.setStyle('width', '0');
			},
			onShow: function(toolTip) {
				toolTip.setStyle('visibility', 'visible');
				if (window.ie6) PngFixImg();
				this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 133]}});
 
			},
			onHide: function(toolTip) {
				if (!contentVisited.contains('3')) this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 0]}});
			}		
		});
	
	TipsArray[4] = new TipsX3($('link4'), {
			className: 'menu',
			id: 'menu',
			fixed: true,
			offsets: offsets[4],
			initialize:function(){
				this.fx = new Fx.Elements(this.toolTip, {wait: false, duration: 2000, transition: Fx.Transitions.Back.easeOut});
				this.toolTip.setStyle('width', '0');
			},
			onShow: function(toolTip) {
				toolTip.setStyle('visibility', 'visible');
				if (window.ie6) PngFixImg();
				this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 255]}});
 
			},
			onHide: function(toolTip) {
				if (!contentVisited.contains('4')) this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 0]}});
			}		
		});
	
	TipsArray[5] = new TipsX3($('link5'), {
			className: 'menu',
			id: 'menu',
			fixed: true,
			offsets: offsets[5],
			initialize:function(){
				this.fx = new Fx.Elements(this.toolTip, {wait: false, duration: 2000, transition: Fx.Transitions.Back.easeOut});
				this.toolTip.setStyle('width', '0');
			},
			onShow: function(toolTip) {
				toolTip.setStyle('visibility', 'visible');
				if (window.ie6) PngFixImg();
				this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 118]}});
 
			},
			onHide: function(toolTip) {
				if (!contentVisited.contains('5')) this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 0]}});
			}		
		});

	TipsArray[6] = new TipsX3($('link6'), {
			className: 'menu',
			id: 'menu',
			fixed: true,
			offsets: offsets[6],
			initialize:function(){
				this.fx = new Fx.Elements(this.toolTip, {wait: false, duration: 2000, transition: Fx.Transitions.Back.easeOut});
				this.toolTip.setStyle('width', '0');
				
			},
			onShow: function(toolTip) {
				toolTip.setStyle('visibility', 'visible');
				if (window.ie6) PngFixImg();
				this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 150]}});
 
			},
			onHide: function(toolTip) {
				if (!contentVisited.contains('6')) this.fx.start({'0': {'width': [toolTip.getStyle("width").toInt(), 0]}});
			}	
		});	
	
	timer = setTimeout("startGallery()", 1000);
	document.onselectstart = new Function ("return false");
	if (window.sidebar) {
		document.onmousedown=selectDisabled;
		document.onclick=clickEnabled;
	}
	
	if ($('SSlink8')) $('SSlink8').cloneEvents($('link8'), 'click');
	if ($('SSlink6')) $('SSlink6').cloneEvents($('link8'), 'click');
	if ($('SSlink6_1')) $('SSlink6_1').cloneEvents($('link8'), 'click');
	
	$('menu0').addClass('displayNone');
	
	if (window.ie6) {
		PngFixImg();
		PngFixBkground();
		TB_show('Avertissement','#TB_inline?height=200&width=400&inlineId=avertissement');
	}
});

window.addEvent('load', function () {
	$$('.inner-content').each(function (el) {
		id = el.id.substring(13);
		arrows = {'top' : 'topArrow' + id, 'bottom' : 'bottomArrow' + id};
		makeScrollbar(id, el, $('scrollbar' + id), $('handle' + id), arrows, $('outer-scrollbar' + id));
	});
	
	$$('.texte').each(function (el) {
		id = el.id.substring(4);
		elClass = false;
		if (el.getParent().getParent().hasClass('displayNone')) {
			elClass = true;
			el.getParent().getParent().removeClass('displayNone');
		}
		arrows = {'top' : 'topArrow-text' + id, 'bottom' : 'bottomArrow-text' + id};
		makeScrollbar(el.id, el, $('scrollbar-text' + id), $('handle-text' + id), arrows, $('outer-scrollbar-text' + id));
		if (elClass) el.getParent().getParent().addClass('displayNone');
	});
	
	$$('.content-inner').setStyle('display', 'none');	
});

function showContent(id, content) {
	$('bloc-content-inner').setStyle('left', posContent[content] + 'px');
	$(id).setStyles({display:'block', opacity: 0});
	var hideEffect = $(id).effect('opacity', {duration: 2000});
	var menu = $('menu').effect('opacity', {duration: 2000});
	menu.addEvent('onComplete', function() {showTips(id);});
	hideEffect.start.delay(4000,hideEffect,1);
	insertLinkHome(id.substr(13));
	menu.start.delay(4000,menu,1);
	contentId = id;
}

function hideContent() {
	if (contentId) $(contentId).setStyles({display:'none'});
	hideTips();
}

function startGallery() {
    $each($ES('div.myGallery'), function(galleryElement, index) {
        myGallery[galleryElement.id.substr(10)] = new gallery(galleryElement, {
            timed: false
        });
        if(window.ie) { ieGalleries.push(myGallery[galleryElement.id.substr(10)]); };
    });
   
    if(window.ie) {
        var delayFor = 0;
        $each(ieGalleries, function(ieElement, index) {
            ieElement.startSlideShow.delay(delayFor, ieElement);
            delayFor = delayFor + 500;
        });
    };
	clearTimeout(timer);
}

function randomGallery(id) {
	var ajax = new Ajax("home/random/" + id, {method: 'get',
		onComplete: function(){
			myGallery[id] = new gallery($('myGallery_' + id), {
				timed: false
			});
		},
		update: $('myGallery_' + id)
	}).request();
}

function multiGallery(parent, id) {
	var ajax = new Ajax("home/random/" + id + '/id', {method: 'get',
		onComplete: function(){
			myGallery[parent] = new gallery($('myGallery_' + parent), {
				timed: false
			});
		},
		update: $('myGallery_' + parent)
	}).request();
	if (multiG[parent]) {
		if (multiG[parent] == id) return false;
		$E('a', 'MG' + multiG[parent]).removeClass('select');
		$E('img', 'MG' + multiG[parent]).setProperty('src', 'images/puce.gif');
		$E('a', 'MG' + id).addClass('select');
		$E('img', 'MG' + id).setProperty('src', 'images/puce_black.gif');
		$('SR' + multiG[parent]).addClass('displayNone');
		$('SR' + id).removeClass('displayNone');
	} else {
		$('MG' + id).getParent().getFirst().getElement('a').removeClass('select');
		$('MG' + id).getParent().getFirst().getElement('img').setProperty('src', 'images/puce.gif');
		$E('a', 'MG' + id).addClass('select');
		$E('img', 'MG' + id).setProperty('src', 'images/puce_black.gif');
		$('SR' + id).getParent().getFirst().addClass('displayNone');
		$('SR' + id).removeClass('displayNone');
	}
	multiG[parent] = id;
	arrows = {'top' : 'topArrow' + parent, 'bottom' : 'bottomArrow' + parent};
	makeScrollbar(parent, $('inner-content' + parent), $('scrollbar' + parent), $('handle' + parent), arrows, $('outer-scrollbar' + parent));
	return true;
}

function selectDisabled(e) {return false;}
function clickEnabled() {return true;}

function showRub(rub) {
	if (rubId == rub) return false;
	if (rubId) {
		$E('a', 'M' + rubId).removeClass('select');
		$E('img', 'M' + rubId).setProperty('src', 'images/puce.gif');
		$E('a', 'M' + rub).addClass('select');
		$E('img', 'M' + rub).setProperty('src', 'images/puce_black.gif');
		$('C' + rub).removeClass('displayNone');
		$('C' + rubId).addClass('displayNone');	
	} else {
		$('M' + rub).getParent().getFirst().getElement('a').removeClass('select');
		$('M' + rub).getParent().getFirst().getElement('img').setProperty('src', 'images/puce.gif');
		$E('a', 'M' + rub).addClass('select');
		$E('img', 'M' + rub).setProperty('src', 'images/puce_black.gif');
		$('C' + rub).getParent().getFirst().addClass('displayNone');
		$('C' + rub).removeClass('displayNone');
	}
	rubId = rub;
	id = $E('.texte', 'C' + rub).getProperty('id');
	arrows = {'top' : 'topArrow-' + id, 'bottom' : 'bottomArrow-' + id};	
	makeScrollbar(id, $(id), $('scrollbar-' + id), $('handle-' + id), arrows, $('outer-scrollbar-' + id));
	return true;
}

function nextCI(id) {
	child = $$('#C' + id + ' .show');
	child.removeClass('show').addClass('displayNone');
	if (child.getNext() != "" && child.getNext().getTag() == "div") child.getNext().addClass('show').removeClass('displayNone');
	else child = $('C' + id).getFirst().addClass('show').removeClass('displayNone');
}

function prevCI(id) {
	child = $$('#C' + id + ' .show');
	child.removeClass('show').addClass('displayNone');
	if (child.getPrevious() != "" && child.getPrevious().getTag() == "div") child.getPrevious().addClass('show').removeClass('displayNone');
	else child = $$('#C' + id + ' div.parent').getLast().addClass('show').removeClass('displayNone');
}

function goCI(rub, id) {
	child = $$('#C' + rub + ' .show');
	child.removeClass('show').addClass('displayNone');
	$('CI' + id).addClass('show').removeClass('displayNone');
}

function insertLinkHome(id) {
	if (id == 0) {
		var menu = $('menu').effect('opacity', {duration: 2000});
		menu.addEvent('onComplete', function() {showTips(id);});
		menu.start.delay(4000,menu,1);
	}
	
	$('menu0').injectBefore('menu' + id).removeClass('displayNone');
	if (contentId) $('menu' + contentId.substr(13)).removeClass('displayNone');
	$('menu' + id).addClass('displayNone');
}

function goTo(rub, id) {
	showRub(rub);
	goCI(rub, id);
}

function showTips(id) {
	contentVisited.each(function(tip) {
		if (id.substr(13) != tip) {
			TipsArray[tip].showT();
		} else {
			TipsArray[tip].hideT();
		}
	});
}

function hideTips() {
	TipsArray[0].hideT();	
	contentVisited.each(function(tip) {
		TipsArray[tip].hideT();
	});
}

function makeScrollbar(id, content,scrollbar,handle,arrows,display,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getSize().scrollSize.x - content.getSize().size.x):(content.getSize().scrollSize.y - content.getSize().size.y))
	if (steps <= 0) display.addClass('displayNone');
	else if (display.hasClass('displayNone')) display.removeClass('displayNone');
	if (slideArray[id]) {
		content.removeEvents();
		scrollbar.removeEvents();
		handle.removeEvents();
		if (arrows) {
			$(arrows.top).removeEvents();
			$(arrows.bottom).removeEvents();
		}
	}
	slideArray[id] = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slideArray[id].step - e.wheel * 30;	
			slideArray[id].set(step);					
		});
	}
	if (arrows) {
		$(arrows.top).addEvent('mousedown', function(event){
			this.interval = (function(event){
				var step = slideArray[id].step - 10;
				slideArray[id].set(step);
			}.bind(this).periodical(40));
		}.bind(this));
				
		$(arrows.top).addEvent('mouseup', function(event){
			$clear(this.interval);
		}.bind(this));
		
		$(arrows.top).addEvent('mouseout', function(event){
			$clear(this.interval);
		}.bind(this));
		
		$(arrows.bottom).addEvent('mousedown', function(event){
			this.interval = (function(event){
				var step = slideArray[id].step + 10;
				slideArray[id].set(step);
			}.bind(this).periodical(40));
		}.bind(this));
				
		$(arrows.bottom).addEvent('mouseup', function(event){
			$clear(this.interval);
		}.bind(this));
		
		$(arrows.bottom).addEvent('mouseout', function(event){
			$clear(this.interval);
		}.bind(this));
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slideArray[id].drag.stop()});
}