var options = {
	'durationLogoStandAnimation'	: 1000, // en milliseconde
	'zoom'							: {width: 150, height: 150} // Taille de l'image de zoom des logo stand sur le plan
}


// action effectuée lorsque le DOM est prèt
var autoload = function ()
{
	// sur un stand, fait apparaitre le logo
	var logosStands = $$('div.logo'); 
	if (logosStands.length) {
		logosStands.setStyle('opacity', 0);
		logosStands.set('tween', {duration: window.options.durationLogoStandAnimation}).tween('opacity', 1);
	}
	
	
	// simulation du target="_blank" Tssss
	$$('a.target-blank').set('target', '_blank');
	
	
	// efface le contenu du champ subscribe
	if ($('email_subscribe')) {
		
		$('email_subscribe').store('initValue', $('email_subscribe').get('value'));
		
		var fncFocus = function (event) {
			this.value = '';
			this.removeEvent('focus', fncFocus);
		};
		
		var fncBlur = function (event) {
			if (this.value == '') {
				this.value = this.retrieve('initValue');
				$('email_subscribe').addEvent('focus', fncFocus);
			} else {
				this.removeEvent('focus', fncFocus);
			}
		};
		
		$('email_subscribe').addEvent('focus', fncFocus);
		$('email_subscribe').addEvent('blur', fncBlur);
	}
	
	
	// plan virtuel IFTS
	if ($('plan-ifts')) {
		var planIfts = new Swiff('/docs/IFTS.swf', {
			container	: 'plan-ifts',
			width		: 1024,
			height		: 800
		});
	}
	
	
	// plan virtuel IFTS
	if ($('bandeau-flash')) {
		var planIfts = new Swiff('/swf/Bandeau.swf', {
			container	: 'bandeau-flash',
			width		: 965,
			height		: 60
		});
	}
	
	// Poiurquoi exposer
	if ($('pourquoi-exposer-flash')) {
		var planIfts = new Swiff('/swf/pourquoi-exposer.swf', {
			container	: 'pourquoi-exposer-flash',
			width		: 300,
			height		: 60
		});
	}
	
	
	// zoom sur la plan d'exposition
	if ($('plan')) {
		var timerZoom;
		$('plan').getElements('img.logo-stand').addEvents({
			'mouseenter' : function (event) {
				timerZoom = zoomPicture.delay(300, this, [event]);
			},
			'mouseleave' : function (event) {
				$clear(timerZoom);
				$('zoom').setStyle('display', 'none');
			}
		});
	}
	
	// éditeur de text avancé
	var textareas = $$('form textarea.mooRTE');
	if (textareas.length) {
		textareas.moorte('create');
	}
	
	var tips = new Tips($$('ul#list-stands li img.active-tip'), {
		'title' : function (el) {
			var html;
			//console.log();
			var id = el.get('id').replace(/[^0-9]*/g, '');
			if ($('tips-exponent-' + id)) {
				html = $('tips-exponent-' + id).get('html');
				$('tips-exponent-' + id).dispose();
				return html;
			}
			return '';
		},
		'onShow' : function (tip) {
			tip.setStyle('display', 'block');
		},
		'onHide' : function (tip) {
			tip.setStyle('display', 'none');
		},
		'className'	: 'tip-1'
	});
	
	var tipsLinks = new MyTips($$('a.tips'));
};


var load = function () {
	// srcolling des logos
	var logos = new Scrolling($('logos'));
	//logos.lunch();
}

window.addEvent('domready', autoload);
window.addEvent('load', load);




var initTinyMCE = function ()
{
	if (window.tinyMCE != undefined) {
		tinyMCE.init({
			theme								: "advanced",
			mode								: "textareas",
			plugins								: "table,fullscreen",
			theme_advanced_buttons2_add			: "separator,forecolor,backcolor",
			theme_advanced_buttons2_add_before	: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
			theme_advanced_buttons3_add_before	: "tablecontrols,separator",
			theme_advanced_buttons3_add			: "advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
			theme_advanced_toolbar_location		: "top",
			theme_advanced_toolbar_align		: "left",
			theme_advanced_path_location		: "bottom",
			language							: "fr",
			auto_resize							: false, // redimensionnement automatique
			button_tile_map						: true, // toutes les images regroupées en une seule
			strict_loading_mode					: true,
			convert_fonts_to_spans				: true,
			apply_source_formatting				: true,
			entity_encoding						: "raw",
			doctype								: '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
			//theme_advanced_disable	: ""
		});
	}
};

window.addEvent('domready', initTinyMCE);



var hlAncre = function () {
	var link = document.location.href.toString();
	if (!link.test(/#b[0-9]+-[0-9]+/)
		&& !link.test(/#u[0-9]+/)) {
		return;
	}
	
	var id = link.replace(/.*#/, '');
	if ($(id)) {
		$(id).setStyle('background-color', '#FFFFFF')
			.tween('background-color', '#F0F0F0');
		$(id).setStyle('border', '2px solid #CC0000');
	}
};
window.addEvent('domready', hlAncre);






var Scrolling = new Class({
	
	options : {
		pixelPerSecond	: 120,
		imagePerSecond	: 20
	},
	
	initialize : function (el) {
		if (!$(el)) {
			return;
		}
		this.element = $(el);
		this.element.setStyle('position', 'relative');
		
		// configuration
		this.direction = 'left';
		this._pause = false;
		
		this.element.store('coordinates', this.element.getCoordinates());
		
	},
	
	lunch : function () {
		if (!this.element) {
			return;
		}
		
		this.moveElement = this.element.getFirst('ul');
		this.moveElement.setStyle('position', 'absolute');

		// cacul la largeur d'UL
		var width = 0;
		this.moveElement.getElements('li').each(
			function (li) {
				width += li.getCoordinates().width + li.getStyle('margin-left').toInt() + li.getStyle('margin-right').toInt();
			}
		);
		this.moveElement.setStyle('width', width);
		
		var bound = {
			'pause'		: this.pause.bind(this),
			'resume'	: this.resume.bind(this)
		}
		this.element.addEvent('mouseenter', bound.pause);
		this.element.addEvent('mouseleave', bound.resume);
		
		this.refresh();
	},
	
	refresh : function () {
		if (this._pause) {
			return;
		}
		
		var decale = (this.options.pixelPerSecond / this.options.imagePerSecond).toInt();
		
		var elementCoordinates = this.element.retrieve('coordinates');
		var coordinates = this.moveElement.getCoordinates(this.element);
		//console.log(decale);
		if (this.direction == 'left') {
			this.moveElement.setStyle('left', coordinates.left - decale);
			if (coordinates.right < 0) {
				this.direction = 'right';
			}
		} else if (this.direction == 'right') {
			this.moveElement.setStyle('left', coordinates.left + decale - 1);
			if (coordinates.left > elementCoordinates.width) {
				this.direction = 'left';
			}
		}
		
		$clear(this.timer);
		this.timer = this.refresh.delay(1000 / this.options.imagePerSecond, this);
	},
	
	pause : function () {
		this._pause = true;
	},
	
	resume : function () {
		this._pause = false;
		this.refresh.delay(1000 / this.options.pixelPerSecond, this);
	}
	
});



var zoomPicture = function (event) {
	var reg = /(.*)\/[0-9]*x[0-9]*\/([^\/]+\..{3})/.exec(this.get('src'));
	var name = reg[2], path = reg[1];
	
	path += '/' + options.zoom.width + 'x' + options.zoom.height + '/';
	
	$('zoom').set('html', '<img src="/images/site/loading.gif" alt="" class="loading" />')
		.setStyle('display', 'block');
	var img = new Asset.image(path + name, {alt: '', onload: function (img) {
		img.setStyle('opacity', 0);
		$('zoom').set('html', '').adopt(img);
		img.tween('opacity', 1);
	}});
};


/**
 * Cette classe étend Tips afin de gérer l'absence de l'attribus "rel" d'un lien
 */
var MyTips = new Class({
	
	Extends : Tips,
	
	initialize : function (el, options)
	{
		if (!options) {
			options = {};
		}
		options.text = function (element){
			return element.get('rel');
		};
		this.parent(el, options);
		
	}
	
});



/**
 * Class permettant de gérer les compteurs de mots dans les champs de texte
 */
var Counter = new Class({
	Binds : ['_refreshLabel'],
	
	initialize : function (el)
	{
		this._el = $(el);
		if (!el || el.get('tag') != 'textarea' && el.get('type') != 'text') {
			return;
		}
		
		this._label = $$('label[for='+ el.get('id') +']');
		if (this._label.length == 0) {
			return;
		}
		this._timer = null;
		this._label = this._label[0];
		this._text = this._label.get('html');
		this._maxLength = this._text.replace(/.*\/ ?([0-9]*).*/g, '$1');
		this._refreshLabel();
		
		this._startEvent();
	},
	
	_startEvent : function ()
	{
		this._el.addEvents({
			'change': this._refreshLabel,
			'keyup': this._refreshLabel
		});
	},
	
	_getLength : function ()
	{
		return this._el.get('value').length;
	},
	
	_refreshLabel : function (event)
	{
		$clear(this._timer);
		if (this._currentSpan) {
			this._currentSpan.setStyle('visibility', 'visible');
		}
		var txtLength = this._getLength();
		if (txtLength < 10) {
			txtLength = '0' + txtLength;
		}
		var replace;
		if (txtLength > this._maxLength) {
			replace = '<span class="red" style="color: red;">'+txtLength+'</span>';
		} else {
			replace = txtLength;
		}
		this._label.set('html', this._text.replace('00', replace));
		
		var spans = this._label.getChildren('span.red');
		if (spans.length > 0) {
			this._currentSpan = spans[0];
			this._timer = this._toggleVisibility.bind(this, [spans[0]]).delay(500);
		} else {
			this._currentSpan = null;
		}
	},
	
	_toggleVisibility : function (span)
	{
		if (span) {
			if (span.getStyle('visibility') == 'hidden') {
				span.setStyle('visibility', 'visible');
				this._timer = this._toggleVisibility.bind(this, [span]).delay(1000, [span]);
			} else {
				span.setStyle('visibility', 'hidden');
				this._timer = this._toggleVisibility.bind(this, [span]).delay(200);
			}
		}
	}
	
});
window.addEvent('domready', function () {
	var els = $$('.active-counter');
	if (els.length > 0) {
		els.each(function (el) {
			counter = new Counter(el);
		});
	}
});









