
window.addEvent('domready', function() {
	var path = '/images/site/skin/halls';
	var areas, preload, images, assetImgs;
	
	if ($('map') && $('img-halls')) {
		areas = $('map').getElements('area');
		
		images = [];
		areas.each(function (el) {
			var id = el.get('id');
			images.push(path + '/halls-'+ id +'.png');
		});
		var newImages = [];
		assetImgs = new Asset.images(images, {
			onProgress : function (counter, index) {
				newImages.push(images[index]);
			},
			onComplete : function () {
				areas.extend($('map').getParent().getElements('p a')).addEvents({
					'mouseenter' : function (event) {
						var id = this.get('id').replace('link-', '');
						if (newImages.contains(path + '/halls-'+ id +'.png')) {
							$('img-halls').set('src', path + '/halls-'+ id +'.png');
						}
					},
					'mouseleave' : function (event) {
						$('img-halls').set('src', path + '/halls.png');
					}
				});
			}
		});
	}
	
	
	if ($('diffuser-savoir-faire')) {
		var carrousel = new Carroussel($('onglet-savoir-faire').getElements('li'), $('informations-vp'));
	}
	
});



var Carroussel = new Class({
	
	Binds : ['manualRotate', '_completeRotate', '_autoRotate'],
	
	initialize : function (titles, parentContent)
	{
		this._rotationActive = true;
		this._displayed = null;
		this._currentTitle = null;
		this._timer = null;
		
		this._titles = titles;
		this._parentContent = parentContent;
		
		if (titles.length == 0) {
			return;
		}
		var lis = this._parentContent.getElements('li');
		lis.setStyle('display', 'none').set('tween', {onComplete: this._completeRotate});
		
		this._currentTitle = this._titles[0];
		this._currentTitle.addClass('active');
		this._displayed = $('content-'+this._titles[0].get('id'));
		this._displayed.setStyles({'display': 'block'});
		
		this._parentContent.setStyle('position', 'relative');
		var positionTop = lis[0].getStyle('top').toInt();
		if (!positionTop) {
			positionTop = 0;
		}
		lis.setStyles({'position': 'absolute', 'top': positionTop, 'left': 0});
		
		this._titles.addEvent('click', this.manualRotate);
		
		this._startRotateTimer();
	},
	
	
	manualRotate : function(event)
	{
		$clear(this._timer);
		var el = $(event.target);
		if (el.get('tag') != 'li') {
			el = el.getParent('li');
			if (!el) {
				return;
			}
		}
		this._rotationActive = false;
		this._startRotate(el);
	},
	
	_startRotate : function(title)
	{
		if (this._currentTitle == title) {
			return;
		}
		this._currentTitle.removeClass('active');
		this._currentTitle = title;
		title.addClass('active');
		
		var newDisplayed = $('content-' + title.get('id'));
		if (newDisplayed) {
			this._displayed.setStyle('z-index', 0).tween('opacity', 0);
			newDisplayed.setStyles({'opacity':0, 'display':'block', 'z-index': 5}).tween('opacity', 1);
		}
	},
	
	_completeRotate : function(el)
	{
		if (el.getStyle('opacity') == 0) {
			return;
		}
		this._displayed.setStyles({'display':'none'});
		this._displayed = el;
		
		this._startRotateTimer();
	},
	
	_startRotateTimer : function()
	{
		$clear(this._timer);
		if (!this._rotationActive) {
			return;
		}
		this._timer = this._autoRotate.delay(7000);
	},
	
	_autoRotate : function()
	{
		if (!this._currentTitle) {
			return;
		}
		next = this._currentTitle.getNext();
		if (next) {
			this._startRotate(next);
			return;
		}
		
		this._startRotate(this._titles[0]);
	}
	
});



