var fixContenuHeights = function() {
	var c = new Array();
	var listeHeight = 0;

	if($('contexte')) c.push('contexte');

	if($('principal')) {
		c.push('principal');
		if($('aside')) c.push('aside');
		if($('liste')) listeHeight = $('liste').getHeight();

	} else {
		if($('liste')) c.push('liste');
	}
	
	var contenuHeight = parseInt($('contenu').getStyle('min-height').replace('px', '')) - listeHeight;

	c.each(function(eId) {
		var e = $(eId);
		e.setStyle({ 'height': '' });
		if(e.getHeight() > contenuHeight) contenuHeight = e.getHeight();
	});

	c.each(function(eId) {
		var e = $(eId);
		var newHeight = contenuHeight;
		var cPaddingTop = parseInt(e.getStyle('padding-top').replace('px', ''));
		var cPaddingBottom = parseInt(e.getStyle('padding-bottom').replace('px', ''));
		newHeight -= (cPaddingTop + cPaddingBottom);
		
		e.setStyle({ 'height': newHeight + 'px' });
	});
}

var fixSliceHeights = function(sliceIn) {
	var newHeight = 0;

	sliceIn.select('div.slice').each(function(e) {
		e.setStyle({ 'height': '' });
		if(e.getHeight() > newHeight) newHeight = e.getHeight();
	});
	
	sliceIn.select('div.slice').each(function(e) {
		e.setStyle({ 'height': newHeight + 'px' });
	});
}

var qualifyImage = function(image) {
	var result = '';
	if(image.width > image.height) result = 'horizontal'
	else if(image.width < image.height) result = 'vertical'
	else result = 'carre';
	return result;
}

var initClick = function(e) {
	var link = e.down('a');
	
	if(link) {
		var href = link.href;
		e.addClassName('actif');
		
		Event.observe(e, 'click', function(event) {
			if(event.element().tagName != 'A') document.location = href;
		});
	}
}

var initUtilListe = function(utilListe) {
	Event.observe(utilListe.down('input.vide'), 'focus', function(event) {
		var inp = event.element();
		inp.removeClassName('vide');
		vide[inp.identify()] = inp.value;
		inp.value = '';
	});

	Event.observe(utilListe.down('input.vide'), 'blur', function(event) {
		var inp = event.element();
		
		if(inp.value == '') {
			inp.addClassName('vide');
			inp.value = vide[inp.identify()];
		}
	});
	
	Event.observe(utilListe.down('div.btn input'), 'click', function(event) {
		var inp = event.element().up('#util-liste').down('input.vide');
		if(inp.hasClassName('vide')) event.stop();
	});
}

var initVitrineDiaporama = function(vitrine) {
	var diapoDuree = 5000;

	var getNext = function(index) {
		var newIndex = index+1;
		if(newIndex >= diapoArray.size()) newIndex = 0;
		return newIndex;
	}

	var getPrev = function(index) {
		var newIndex = index-1;
		if(newIndex <= -1 ) newIndex = diapoArray.size()-1;
		return newIndex;
	}

	var showDiapo = function(n) {
		//Effect.Fade(diapoArray[diaporama.actif], { from: 1, to: 0, duration: 0.5 });
		//Effect.Appear(diapoArray[n], { from: 0, to: 1, duration: 0.5 });
		
		//diapoArray[n].setStyle({ 'top': '361px' });
		
		diapoArray[diaporama.actif].setStyle({ zIndex: 199 });

		diapoArray[n].setStyle({ 'top': diaporamaHeight + 5 + 'px' });
		diapoArray[n].setStyle({ zIndex: 200 });
		diapoArray[n].show();
		
		var defaultTransition = Effect.Transitions.Expo.easeOut;

		new Effect.Move(diapoArray[diaporama.actif], {
			x: 0, y: 0-diaporamaHeight-5, mode: 'absolute',
			transition: defaultTransition,
			duration: 0.8
		});

		new Effect.Move(diapoArray[n], {
			x: 0, y: 0, mode: 'absolute',
			transition: defaultTransition,
			duration: 0.8
		});

		clearTimeout(diaporama.timer);
		diaporama.timer = setTimeout(nextDiapo, diapoDuree);
		diaporama.actif = n;
	}

	var nextDiapo = function() {
		showDiapo(getNext(diaporama.actif));
	}
	
	var diaporama = vitrine.down('ul.diaporama');
	var diapoArray = diaporama.select('li.diapo');
	var diaporamaWidth = diaporama.getWidth();
	var diaporamaHeight = diapoArray.first().getHeight();

	if(diapoArray.size() > 1) {
		if(diaporama.hasClassName('aleatoire')) {
			diapoArray.first().hide();
		
			diapoArray = diapoArray.sortBy(function(s) { 
				return Math.floor(Math.random()*10000);
			});
		}

		diapoArray.each(function(diapo, index) {
			diapo.index = index;
			diapo.show();
			diapo.absolutize();
			diapo.setStyle({ zIndex: 199 });
			if(((index+1) % 2) == 0) diapo.addClassName('pair');
			initClick(diapo);
			
			if(index > 0) {
				diapo.hide()
			} else {
				diapo.show();
				diapo.setStyle({ 'top': '0px' });
			}
		});
		
		diaporama.actif = 0;
		diaporama.timer = setTimeout(nextDiapo, diapoDuree);
	}
}

var initSiteDomLoaded = function(event) {
	if($$('video-ecran')) {
		initVideo();
	}

	if($('social-liste')) {
		initUtilListe($('social-liste'));
	}
	
	if($('doc-en-cours') && $('vitrine') && $('vitrine').hasClassName('prod')) {
		initClick($('vitrine').down('div.generique'));
	}
	
	if($$('ul.prod-list li.prod')) {
		$$('ul.prod-list li.prod').each(function(e) {
			initClick(e);
		});
	}

	if($$('ul.cat-list li.cat')) {
		$$('ul.cat-list li.cat').each(function(e) {
			initClick(e);
		});
	}
	
	if($$('table.cal-list tr.cal')) {
		$$('table.cal-list tr.cal').each(function(e) {
			initClick(e);
		});
	}
	
	if($$('#vitrine.diaporama')) {
		initVitrineDiaporama($$('#vitrine.diaporama').first());
	}
	
	Event.observe(document, 'mousemove', function(event) {
		if($('tip') && $('tip').isActif) {
			$('tip').setStyle({
				'left': (Event.pointerX(event) - ($('tip').getWidth() / 2)) + 'px',
				'top': (Event.pointerY(event) - $('tip').getHeight() - 12 ) + 'px'
			});
		}
	});

	fixContenuHeights();
}

var initSiteAllLoaded = function(event) {
	isAllLoaded = true;

	if($$('ul.photo-list img')) {
		$$('ul.photo-list img').each(function(e) {
			e.addClassName(qualifyImage(e));
		});
	}

	fixContenuHeights();
	
	$$('div.slicein3').each(function(e) {
		fixSliceHeights(e);
	});
}

Event.observe(document, 'dom:loaded', initSiteDomLoaded, false);
Event.observe(window, 'load', initSiteAllLoaded, false);

var isAllLoaded = false;
var vide = new Array();
