var options = {
	'durationLogoStandAnimation'	: 1000 // en milliseconde
}


// 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
		});
	}
	
	
	// zoom sur la plan d'exposition
	if ($('plan')) {
		var timerZoom;
		$('plan').getElements('img.logo-stand').addEvents({
			'mouseenter' : function (event) {
				timerZoom = zoomPicture.delay(500, this, [event]);
			},
			'mouseleave' : function (event) {
				$clear(timerZoom);
				$('zoom').setStyle('display', 'none');
			}
		});
	}
};

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 Scrolling = new Class({
	
	options : {
		pixelPerSecond	: 120,
		imagePerSecond	: 20
	},
	
	initialize : function (el) {
		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 = /\/([^\/]*\..{3})/.exec(this.get('src'));
	var name = reg[1];
	//var img = new Element('img', {alt: '', src: '/cache/stand/grand-' + name});
	
	$('zoom').set('html', '<img src="/images/loading.gif" alt="" class="loading" />')
		.setStyle('display', 'block');
	var img = new Asset.image('/cache/stand/grand-' + name, {alt: '', onload: function (img) {
		$('zoom').set('html', '').adopt(img);
	}});
}
















