document.observe('dom:loaded',
	function ()
	{
		try
		{
			var minHeight = 800;
			var wrapper = $('wrapper');
			var module = $$('div.module').last();
			var content = module.down('div.module-content');

			if (!module.hasClassName('main-module') && wrapper.getHeight() < minHeight)
			{
				content.setStyle({ height: (content.getHeight() + (minHeight - wrapper.getHeight())).toString() + 'px' });
			}

			$$('a[href^="#"]').each(
				function (link)
				{
					Event.observe(link, 'click', function (event) { event.stop(); });
				}
			);

			Lightview.options.controller.setNumberTemplate = Language.getTranslation('#{position} di #{total}');
		}
		catch (error)
		{
		}
	}
);

Object.extend(Ajax.Request.prototype, {
	_aborted: false,
	abort: function ()
	{
		if (!this._complete)
		{
			this.transport.onreadystatechange = Prototype.emptyFunction;
			this._aborted = true;

			try
			{
				this.transport.abort();
			}
			catch (error)
			{
			}

			this.respondToReadyState(4);
		}
	}
});

Object.extend(Form.Methods, {
	setFocus: function (form, element)
	{
		try
		{
			form = $(form);
			element = $(element);

			element.focus();
		}
		catch (error)
		{
		}
	},
	clear: function (form)
	{
		try
		{
			form.getElements().each(
				function (element)
				{
					switch (element.type)
					{
						case 'text':
						case 'select-one':
							element.clear();

							break;
					}
				}
			);

			form.submit();
		}
		catch (error)
		{
		}
	},
	submitTo: function (form, element, url, fragment)
	{
		try
		{
			form = $(form);
			element = $(element);
			url = Object.isString(url) ? url : window.location.href;
			fragment = Object.isString(fragment) ? '#' + fragment : '';

			form.elements['focus'].value = element.name;
			form.action = url + fragment;

			form.submit();
		}
		catch (error)
		{
		}
	}
});

Element.addMethods();

var Language = {
	getTranslation: function (string)
	{
		return (LANG.keys().include(string)) ? LANG.get(string) : string;
	}
}

var Highlightable = Class.create({
	element: null,
	scope: '',
	initialize: function (element)
	{
		this.element = element;
		this.scope = element.identify() + '-scope';

		if (!Prototype.Browser.IE)
		{
			element.setStyle({ opacity: 0.4 });

			Event.observe(element, 'mouseenter', this.highlight.bind(this));
			Event.observe(element, 'mouseleave', this.lowlight.bind(this));
		}
	},
	highlight: function (event)
	{
		Effect.Queues.get(this.scope).invoke('cancel');

		new Effect.Appear(this.element, { duration: 0.2, to: 1.0, queue: { scope: this.scope }});
	},
	lowlight: function (event)
	{
		new Effect.Fade(this.element, { duration: 0.6, from: 1.0, to: 0.4, queue: { position: 'end', scope: this.scope }});
	}
});

var ScrollingMenu = Class.create({
	slowness: 5,
	limit: 3,
	fps: 30,
	element: null,
	elementWidth: null,
	list: null,
	listWidth: null,
	leftTolerance: null,
	rightTolerance: null,
	range: null,
	targetLeft: null,
	deceleration: null,
	executer: null,
	initialize: function (element)
	{
		this.element = element;
		this.list = element.down('ul');

		Event.observe(element, 'mousemove',
			function (event)
			{
				var firstItem = this.list.down('li:first-child');
				var lastItem = this.list.down('li:last-child');
				var listPadding = firstItem.positionedOffset().left;
				var listWidth = lastItem.positionedOffset().left + lastItem.getWidth() + listPadding;

				if (this.elementWidth == null || this.listWidth != listWidth)
				{
					this.elementWidth = element.getWidth();
					this.listWidth = listWidth;
					this.leftTolerance = listPadding + (firstItem.getWidth() * 0.9);
					this.rightTolerance = (lastItem.getWidth() * 0.9) + listPadding;
					this.range = (this.listWidth - this.elementWidth) / 2;
				}

				if (this.listWidth > this.elementWidth)
				{
					var mouseX = event.pointer().x - this.element.positionedOffset().left;

					if (this.leftTolerance > 0 && mouseX <= this.leftTolerance)
					{
						mouseX = 0;
					}
					else if (this.rightTolerance > 0 && mouseX > (this.elementWidth - this.rightTolerance))
					{
						mouseX = this.elementWidth - 1;
					}

					var offset = mouseX - Math.round(this.elementWidth / 2);
					var fraction;
					var targetLeft;

					if (offset < 0)
					{
						fraction = Math.min(Math.abs(offset / (Math.round(this.elementWidth / 2) - this.leftTolerance)), 1);
						targetLeft = this.range - (this.range * fraction);
					}
					else
					{
						if (offset < (this.elementWidth - 1) - Math.round(this.elementWidth / 2))
						{
							fraction = Math.min(Math.abs(offset / (Math.round(this.elementWidth / 2) - (this.elementWidth - this.rightTolerance))), 1);
						}
						else
						{
							fraction = 1;
						}

						targetLeft = this.range + (this.range * fraction);
					}

					this.targetLeft = Math.round(targetLeft);
					this.deceleration = (fraction * this.slowness) + this.limit;

					if (this.executer == null)
					{
						this.executer = new PeriodicalExecuter(this.scroll.bind(this), (1 / this.fps));
					}
				}
			}.bind(this)
		);

		this.list.select('li').each(
			function (element)
			{
				new Highlightable(element);
			}
		);
	},
	scroll: function ()
	{
		var offset = this.element.scrollLeft - this.targetLeft;
		var scrollLeft = Math.round(this.element.scrollLeft - (offset / this.deceleration));

		if (offset != 0)
		{
			if (scrollLeft == this.element.scrollLeft)
			{
				scrollLeft += (offset > 0) ? -1 : 1;
			}

			this.element.scrollLeft = scrollLeft;
		}
		else
		{
			this.executer.stop();

			this.executer = null;
		}
	}
});

var LoginTooltip = {
	label: null,
	tooltip: null,
	close: null,
	form: null,
	scope: 'login-tooltip-scope',
	initialize: function ()
	{
		this.label = $('login-label');
		this.tooltip = $('login-tooltip');

		if (this.label != null && this.tooltip != null)
		{
			this.close = this.tooltip.down('.tooltip-close');
			this.form = this.tooltip.down('form');

			Event.observe(this.label, 'mousedown', this.show.bind(this, null));
			Event.observe(this.close, 'mousedown', this.hide.bind(this));
			Event.observe($('reset-password-label'), 'mousedown', this.showResetPasswordForm.bind(this, $('reset-password-form-wrapper')));
			Event.observe($('reset-password-form-back-button'), 'mousedown', this.showLoginForm.bind(this, $('login-form-wrapper')));
		}
	},
	show: function (element, event)
	{
		if (!this.tooltip.visible())
		{
			new Effect.Appear(this.tooltip, {
				duration: (element == null) ? 0.4 : 0.0,
				to: 0.98,
				queue: { scope: this.scope, limit: 1 },
				afterSetup: function ()
				{
					Wishlist.abortRequest();
					Wishlist.tooltip.hide();

					this.tooltip.setStyle({
						top: (this.label.positionedOffset().top + this.label.getHeight() + 6).toString() + 'px',
						left: (this.label.positionedOffset().left + parseInt(this.label.getWidth() / 2) - 27).toString() + 'px'
					});
				}.bind(this),
				afterFinish: function ()
				{
					element = (element == null) ? this.form.findFirstElement() : element;

					element.focus();
				}.bind(this)
			});
		}
	},
	hide: function (event)
	{
		new Effect.Fade(this.tooltip, { duration: 0.2, queue: { scope: this.scope, limit: 1 }});
	},
	showResetPasswordForm: function (wrapper, event)
	{
		this.form = wrapper.down('form');

		new Effect.Appear(wrapper, {
			duration: 0.0,
			afterSetup: function ()
			{
				$('login-form-wrapper').hide();
			},
			afterFinish: function ()
			{
				this.form.findFirstElement().focus();
			}.bind(this)
		});
	},
	showLoginForm: function (wrapper, event)
	{
		this.form = wrapper.down('form');

		new Effect.Appear(wrapper, {
			duration: 0.0,
			afterSetup: function ()
			{
				$('reset-password-form-wrapper').hide();
			},
			afterFinish: function ()
			{
				this.form.findFirstElement().focus();
			}.bind(this)
		});
	}
}

var Wishlist = {
	label: null,
	tooltip: null,
	close: null,
	loader: null,
	wrapper: null,
	content: null,
	addProductButton: null,
	messageTooltip: null,
	request: null,
	maxProducts: 8,
	requestUrl: BASE_URL + LANGUAGE + '/products/wishlist/',
	downloadUrl: BASE_URL + LANGUAGE + '/products/wishlist/download/',
	productUrl: BASE_URL + LANGUAGE + '/products/?id=#{lampId}&version_id=#{versionId}&name=#{name}',
	initialize: function ()
	{
		this.label = $('wishlist-label');
		this.tooltip = $('wishlist-tooltip');
		this.close = this.tooltip.down('.tooltip-close');
		this.loader = $('wishlist-loader');
		this.wrapper = $('wishlist-wrapper');
		this.content = $('wishlist-content');
		this.addProductButton = $('wishlist-add-product-button');
		this.messageTooltip = $('wishlist-message-tooltip');

		if (this.label != null && this.tooltip != null)
		{
			Event.observe(this.label, 'mousedown', this.showTooltip.bind(this));
			Event.observe(this.close, 'mousedown', this.closeTooltip.bind(this));
		}

		/*var products = [
			{ lampId: 1,  versionId: 1,   name: 'Diva',         thumbnail: 'http://rotaliana/public/upload/products/thumbnails/d2ceedf4.png', width: 66,  height: 120 },
			{ lampId: 2,  versionId: 2,   name: 'Eolo',         thumbnail: 'http://rotaliana/public/upload/products/thumbnails/1d927428.png', width: 57,  height: 120 },
			{ lampId: 76, versionId: 110, name: 'MultiBook',    thumbnail: 'http://rotaliana/public/upload/products/thumbnails/0aa8de3f.png', width: 79,  height: 120 },
			{ lampId: 10, versionId: 10,  name: 'Bpl H1',       thumbnail: 'http://rotaliana/public/upload/products/thumbnails/55fe8d47.png', width: 100, height: 120 },
			{ lampId: 77, versionId: 111, name: 'MultiPot',     thumbnail: 'http://rotaliana/public/upload/products/thumbnails/ac93ee3f.png', width: 80,  height: 120 },
			{ lampId: 14, versionId: 14,  name: 'Bubble T',     thumbnail: 'http://rotaliana/public/upload/products/thumbnails/a7b9a4fd.png', width: 53,  height: 120 },
			{ lampId: 41, versionId: 43,  name: 'Flow T1',      thumbnail: 'http://rotaliana/public/upload/products/thumbnails/556946e5.png', width: 75,  height: 120 },
			{ lampId: 3,  versionId: 3,   name: 'Campanula H1', thumbnail: 'http://rotaliana/public/upload/products/thumbnails/20198be7.png', width: 66,  height: 120 }
		];

		Cookie.set('wishlist_version_id', products.pluck('versionId').uniq().toString());*/
	},
	showTooltip: function (event)
	{
		if (!this.tooltip.visible())
		{
			new Effect.Appear(this.tooltip, {
				duration: 0.4,
				to: 0.98,
				queue: { scope: 'wishlist-tooltip-scope', limit: 1 },
				afterSetup: function ()
				{
					if (LoginTooltip.tooltip != null)
					{
						LoginTooltip.tooltip.hide();
					}

					this.tooltip.setStyle({
						top: (this.label.positionedOffset().top + this.label.getHeight() + 6).toString() + 'px',
						left: (this.label.positionedOffset().left + parseInt(this.label.getWidth() / 2) - 27).toString() + 'px'
					});
					this.content.setStyle({ visibility: 'hidden' });
				}.bind(this),
				afterFinish: function ()
				{
					this.refresh(true);
				}.bind(this)
			});
		}
		else
		{
			this.refresh();
		}
	},
	closeTooltip: function (event)
	{
		new Effect.Fade(this.tooltip, {
			duration: 0.2,
			queue: { scope: 'wishlist-tooltip-scope', limit: 1 },
			afterSetup: function ()
			{
				Effect.Queues.get('wishlist-scope').invoke('cancel');

				this.abortRequest();
			}.bind(this),
			afterFinish: function ()
			{
			}.bind(this)
		});
	},
	abortRequest: function ()
	{
		if (this.request != null)
		{
			this.request.abort();

			this.request = null;
		}
	},
	refresh: function (show)
	{
		if (this.request == null)
		{
			new Effect.Fade(this.content, {
				duration: (Prototype.Browser.IE || !!show) ? 0.0 : 0.4,
				from: (!Prototype.Browser.IE ? 1.0 : 0.0),
				queue: { scope: 'wishlist-scope', limit: 1 },
				afterSetup: function ()
				{
					this.wrapper.setStyle({
						width: this.wrapper.getWidth().toString() + 'px',
						height: this.wrapper.getHeight().toString() + 'px'
					});
					this.content.setStyle({
						width: 'auto',
						height: 'auto'
					});
				}.bind(this),
				afterFinish: function ()
				{
					this.request = new Ajax.Request(this.requestUrl, {
						onCreate: function ()
						{
							this.content.update('').setStyle({ visibility: 'visible' });
							this.loader.show();
						}.bind(this),
						onComplete: function (response)
						{
							this.content.appendChild(Builder.node('h4', { className: 'tooltip-title' }, [Language.getTranslation('Wishlist')]));

							if (response.status == 200)
							{
								var products = response.responseJSON;

								if (products.length > 0)
								{
									this.content.appendChild(Builder.node('ul', { id: 'wishlist-options', className: 'float-container' }));

									$('wishlist-options').appendChild(Builder.node('li', [
										Builder.node('a', { href: this.downloadUrl }, [Language.getTranslation('Scarica PDF')])
									]));

									if (products.length > 2)
									{
										$('wishlist-options').appendChild(Builder.node('li', [
											Builder.node('a', { href: 'javascript:void(0);', onclick: 'Wishlist.clear();' }, [Language.getTranslation('Rimuovi tutto')])
										]));
									}

									var productList = this.content.appendChild(Builder.node('ul', { className: 'tooltip-grid' }));

									products.each(
										function (product)
										{
											productList.appendChild(Builder.node('li', [
												Builder.node('a', { href: this.productUrl.interpolate({ lampId: product.lampId, versionId: product.versionId, name: product.name }) }, [
													Builder.node('span', { className: 'tooltip-item-thumbnail', style: 'background-image: url(' + product.thumbnail + ');' }),
													Builder.node('span', { className: 'tooltip-item-label' }, [product.name])
												]),
												Builder.node('div', { className: 'wishlist-remove-product-button' }, [
													Builder.node('a', { href: 'javascript:void(0);', onclick: 'Wishlist.removeProduct(' + product.versionId + ');' }, [Language.getTranslation('Rimuovi')])
												])
											]));
										}.bind(this)
									);

									productList.select('li').each(
										function (element)
										{
											new Highlightable(element);
										}
									);

									var columnCount = parseInt(this.maxProducts / 2);
									var contentWidth = ((120 + 4) * Math.max(((products.length < columnCount) ? products.length : columnCount), 2)) - 4;

									this.content.setStyle({ width: contentWidth.toString() + 'px' });
								}
								else
								{
									this.content.appendChild(Builder.node('p', { className: 'tooltip-text no-wrap' }, [Language.getTranslation('La tua wishlist non contiene prodotti.')]));
								}
							}

							new Effect.Morph(this.wrapper, {
								style: {
									width: this.content.getWidth().toString() + 'px',
									height: this.content.getHeight().toString() + 'px'
								},
								duration: 0.4,
								queue: { scope: 'wishlist-scope', limit: 1 },
								afterSetup: function ()
								{
									this.loader.hide();
								}.bind(this),
								afterFinish: function ()
								{
									new Effect.Appear(this.content, {
										duration: (!Prototype.Browser.IE ? 0.4 : 0.0),
										from: (!Prototype.Browser.IE ? 0.0 : 1.0),
										queue: { scope: 'wishlist-scope', limit: 1 },
										afterSetup: function ()
										{
											this.wrapper.setStyle({
												width: 'auto',
												height: 'auto'
											});
										}.bind(this),
										afterFinish: function ()
										{
											this.request = null;
										}.bind(this)
									});
								}.bind(this)
							});
						}.bind(this)
					});
				}.bind(this)
			});
		}
	},
	addProduct: function (productId)
	{
		if (this.request == null)
		{
			var productIds = Cookie.get('wishlist_version_id');

			productIds = (Object.isString(productIds) && productIds != '') ? productIds.split(',') : [];

			if (productIds.include(productId))
			{
				this.showMessageTooltip(Language.getTranslation('Il prodotto è già presente nella tua wishlist.'));
			}
			else if (productIds.length >= this.maxProducts)
			{
				this.showMessageTooltip(Language.getTranslation('La tua wishlist può contenere al massimo #{max} prodotti.').interpolate({ max: this.maxProducts }));
			}
			else
			{
				productIds.push(productId);

				Cookie.set('wishlist_version_id', productIds.uniq().join(','));

				this.showMessageTooltip(Language.getTranslation('Il prodotto è stato aggiunto alla tua wishlist.'));

				if (this.tooltip.visible())
				{
					this.refresh();
				}
			}
		}
	},
	removeProduct: function (productId)
	{
		if (this.request == null)
		{
			var productIds = Cookie.get('wishlist_version_id');

			productIds = (Object.isString(productIds) && productIds != '') ? productIds.split(',') : [];

			Cookie.set('wishlist_version_id', productIds.without(productId).uniq().join(','));

			this.refresh();
		}
	},
	clear: function ()
	{
		if (this.request == null)
		{
			Cookie.set('wishlist_version_id', '');

			this.refresh();
		}
	},
	showMessageTooltip: function (message)
	{
		var scope = 'wishlist-message-scope';

		new Effect.Appear(this.messageTooltip, {
			duration: 0.4,
			to: 0.98,
			queue: { scope: scope, limit: 1 },
			afterSetup: function ()
			{
				this.messageTooltip.down('p').innerHTML = message;
				this.messageTooltip.setStyle({
					top: (this.addProductButton.positionedOffset().top - parseInt(this.addProductButton.getHeight() / 2)).toString() + 'px',
					left: (this.addProductButton.positionedOffset().left - this.messageTooltip.getWidth() - 6).toString() + 'px'
				});
			}.bind(this),
			afterFinish: function ()
			{
				new Effect.Fade(this.messageTooltip, {
					delay: 4.0,
					duration: 0.4,
					queue: { scope: scope, limit: 1 },
					afterFinish: function ()
					{
						this.messageTooltip.down('p').innerHTML = '';
					}.bind(this)
				});
			}.bind(this)
		});
	}
}

var MainMenu = {
	initialize: function ()
	{
		var labels = [$('mounting-types-submenu-label'), $('designers-submenu-label'), $('publications-submenu-label')];
		var wrappers = [$('mounting-types-submenu-wrapper'), $('designers-submenu-wrapper'), $('publications-submenu-wrapper')];
		var scope = 'main-menu-scope';

		labels.each(
			function (label, i)
			{
				var wrapper = wrappers[i];

				labels[i].observe('mousedown',
					function (wrapper)
					{
						if (wrapper.visible())
						{
							new Effect.Fade(wrapper, {
								duration: 0.4,
								queue: { scope: scope, limit: 1 },
								afterFinish: function ()
								{
									this.removeClassName('active');
								}.bind(this)
							});
						}
						else
						{
							new Effect.Appear(wrapper, {
								duration: 0.4,
								queue: { scope: scope, limit: 1 },
								afterSetup: function ()
								{
									labels.invoke('removeClassName', 'active');
									wrappers.without(wrapper).invoke('hide');

									this.addClassName('active');
								}.bind(this)
							});
						}
					}.bind(label, wrapper)
				);
			}
		);

		$$('div.scrolling-menu').each(
			function (element)
			{
				new ScrollingMenu(element);
			}
		);

		MountingTypesSubmenu.initialize();
	}
}

var MountingTypesSubmenu = {
	initialize: function ()
	{
		var labels = $$('ul#mounting-types-submenu li');
		var wrappers = $$('div.mounting-type-products-submenu-wrapper');
		var scope = 'main-menu-scope';

		labels.each(
			function (label, i)
			{
				var wrapper = wrappers[i];

				labels[i].observe('mousedown',
					function (wrapper)
					{
						if (wrapper.visible())
						{
							new Effect.Fade(wrapper, {
								duration: 0.4,
								queue: { scope: scope, limit: 1 },
								afterFinish: function ()
								{
									this.removeClassName('active');
								}.bind(this)
							});
						}
						else
						{
							new Effect.Appear(wrapper, {
								duration: 0.4,
								queue: { scope: scope, limit: 1 },
								afterSetup: function ()
								{
									labels.invoke('removeClassName', 'active');
									wrappers.without(wrapper).invoke('hide');

									this.addClassName('active');
								}.bind(this)
							});
						}
					}.bind(label, wrapper)
				);
			}
		);
	}
}

/* home */

HighlightedProductsTooltip = {
	initialize: function ()
	{
		var label = $('highlighted-products-label');
		var tooltip = $('highlighted-products-tooltip');

		if (label != null && tooltip != null)
		{
			var scope = tooltip.identify() + '-scope';

			Event.observe(label, 'mouseenter', showTooltip.bind(tooltip, label, scope));
			Event.observe(label, 'mouseleave', hideTooltip.bind(tooltip, scope));
			Event.observe(tooltip, 'mouseenter', showTooltip.bind(tooltip, label, scope));
			Event.observe(tooltip, 'mouseleave', hideTooltip.bind(tooltip, scope));

			tooltip.select('ul li').each(
				function (element)
				{
					new Highlightable(element);
				}
			);
		}

		function showTooltip(label, scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Appear(this, {
				duration: 0.4,
				to: 0.98,
				queue: { scope: scope },
				afterSetup: function ()
				{
					this.setStyle({
						top: (label.positionedOffset().top - parseInt((this.getHeight() - label.getHeight()) / 2)).toString() + 'px',
						left: (label.positionedOffset().left - this.getWidth()).toString() + 'px'
					});
				}.bind(this)
			});
		}

		function hideTooltip(scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Fade(this, { duration: 0.2, queue: { scope: scope }});
		}
	}
}

/* news */

NewsItemGalleryTooltip = {
	initialize: function ()
	{
		var image = $('news-item-content').down('img');
		var list = $('news-item-medium-types');

		if (!Object.isUndefined(image) && !Object.isUndefined(list))
		{
			image.addClassName('pointer');

			Event.observe(image, 'mousedown', function () { Lightview.show(list.down('a.lightview').identify()); });
		}

		var labels = $$('span.news-item-medium-type-label');
		var tooltips = $$('div.medium-type-tooltip');

		for (var i = 0; i < labels.length; i++)
		{
			var label = labels[i];
			var tooltip = tooltips[i];
			var scope = tooltip.identify() + '-scope';

			Event.observe(label, 'mouseenter', showTooltip.bind(tooltip, label, scope));
			Event.observe(label, 'mouseleave', hideTooltip.bind(tooltip, scope));
			Event.observe(tooltip, 'mouseenter', showTooltip.bind(tooltip, label, scope));
			Event.observe(tooltip, 'mouseleave', hideTooltip.bind(tooltip, scope));

			tooltip.select('a').each(
				function (element)
				{
					new Highlightable(element);
				}
			);
		}

		function showTooltip(label, scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Appear(this, {
				duration: 0.4,
				to: 0.98,
				queue: { scope: scope },
				afterSetup: function ()
				{
					this.setStyle({
						top: (label.positionedOffset().top - parseInt((this.getHeight() - label.getHeight()) / 2)).toString() + 'px',
						left: (label.positionedOffset().left + label.getWidth()).toString() + 'px'
					});
				}.bind(this)
			});
		}

		function hideTooltip(scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Fade(this, { duration: 0.2, queue: { scope: scope }});
		}
	}
}

NewsItemRelatedProductsTooltip = {
	initialize: function ()
	{
		var label = $('related-products-label');
		var tooltip = $('related-products-tooltip');
		var scope = tooltip.identify() + '-scope';

		Event.observe(label, 'mouseenter', showTooltip.bind(tooltip, label, scope));
		Event.observe(label, 'mouseleave', hideTooltip.bind(tooltip, scope));
		Event.observe(tooltip, 'mouseenter', showTooltip.bind(tooltip, label, scope));
		Event.observe(tooltip, 'mouseleave', hideTooltip.bind(tooltip, scope));

		tooltip.select('ul li').each(
			function (element)
			{
				new Highlightable(element);
			}
		);

		function showTooltip(label, scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Appear(this, {
				duration: 0.4,
				to: 0.98,
				queue: { scope: scope },
				afterSetup: function ()
				{
					this.setStyle({
						top: (label.positionedOffset().top - parseInt((this.getHeight() - label.getHeight()) / 2)).toString() + 'px',
						left: (label.positionedOffset().left + label.getWidth()).toString() + 'px'
					});
				}.bind(this)
			});
		}

		function hideTooltip(scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Fade(this, { duration: 0.2, queue: { scope: scope }});
		}
	}
}

/* products */

ProductMountingTypeTooltip = {
	initialize: function ()
	{
		var labels = $$('span.product-mounting-type-label');
		var tooltips = $$('div.product-mounting-type-tooltip');

		for (var i = 0; i < labels.length; i++)
		{
			var label = labels[i];
			var tooltip = tooltips[i];
			var scope = tooltip.identify() + '-scope';

			Event.observe(label, 'mouseenter', showTooltip.bind(tooltip, label, scope));
			Event.observe(label, 'mouseleave', hideTooltip.bind(tooltip, scope));
			Event.observe(tooltip, 'mouseenter', showTooltip.bind(tooltip, label, scope));
			Event.observe(tooltip, 'mouseleave', hideTooltip.bind(tooltip, scope));

			tooltip.select('ul li').each(
				function (element)
				{
					new Highlightable(element);
				}
			);
		}

		function showTooltip(label, scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Appear(this, {
				duration: 0.4,
				to: 0.98,
				queue: { scope: scope },
				afterSetup: function ()
				{
					this.setStyle({
						top: (label.positionedOffset().top + label.getHeight()).toString() + 'px',
						left: (label.positionedOffset().left + parseInt(label.getWidth() / 2) - 27).toString() + 'px'
					});
				}.bind(this)
			});
		}

		function hideTooltip(scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Fade(this, { duration: 0.2, queue: { scope: scope }});
		}
	}
}

ProductGalleryTooltip = {
	initialize: function ()
	{
		var labels = $$('span.product-medium-type-label');
		var tooltips = $$('div.medium-type-tooltip');

		for (var i = 0; i < labels.length; i++)
		{
			var label = labels[i];
			var tooltip = tooltips[i];
			var scope = tooltip.identify() + '-scope';

			Event.observe(label, 'mouseenter', showTooltip.bind(tooltip, label, scope));
			Event.observe(label, 'mouseleave', hideTooltip.bind(tooltip, scope));
			Event.observe(tooltip, 'mouseenter', showTooltip.bind(tooltip, label, scope));
			Event.observe(tooltip, 'mouseleave', hideTooltip.bind(tooltip, scope));

			tooltip.select('a').each(
				function (element)
				{
					new Highlightable(element);
				}
			);
		}

		function showTooltip(label, scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Appear(this, {
				duration: 0.4,
				to: 0.98,
				queue: { scope: scope },
				afterSetup: function ()
				{
					this.setStyle({
						top: (label.positionedOffset().top - parseInt((this.getHeight() - label.getHeight()) / 2)).toString() + 'px',
						left: (label.positionedOffset().left + label.getWidth()).toString() + 'px'
					});
				}.bind(this)
			});
		}

		function hideTooltip(scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Fade(this, { duration: 0.2, queue: { scope: scope }});
		}
	}
}

ProductCertificationTooltip = {
	initialize: function ()
	{
		var images = $$('span.product-certification-image');
		var tooltips = $$('div.product-certification-tooltip');

		for (var i = 0; i < images.length; i++)
		{
			var image = images[i];
			var tooltip = tooltips[i];
			var scope = tooltip.identify() + '-scope';

			Event.observe(image, 'mouseenter', showTooltip.bind(tooltip, image, scope));
			Event.observe(image, 'mouseleave', hideTooltip.bind(tooltip, scope));
		}

		function showTooltip(image, scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Appear(this, {
				duration: 0.4,
				to: 0.98,
				queue: { scope: scope },
				afterSetup: function ()
				{
					this.setStyle({
						top: (image.positionedOffset().top + image.getHeight() + 6).toString() + 'px',
						left: (image.positionedOffset().left - parseInt((this.getWidth() - image.getWidth()) / 2)).toString() + 'px'
					});
				}.bind(this)
			});
		}

		function hideTooltip(scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Fade(this, { duration: 0.2, queue: { scope: scope }});
		}
	}
}

ProductBodyColorsTooltip = {
	initialize: function ()
	{
		var label = $('product-body-colors-label');
		var tooltip = $('product-body-colors-tooltip');

		if (label != null && tooltip != null)
		{
			var close = tooltip.down('.tooltip-close');

			Event.observe(label, 'mousedown', showTooltip.bind(tooltip, label));
			Event.observe(close, 'mousedown', closeTooltip.bind(tooltip));
		}

		function showTooltip(label, event)
		{
			if (!this.visible())
			{
				new Effect.Appear(this, {
					duration: 0.4,
					to: 0.98,
					afterSetup: function ()
					{
						this.setStyle({
							top: (label.positionedOffset().top - parseInt((this.getHeight() - label.getHeight()) / 2)).toString() + 'px',
							left: (label.positionedOffset().left - tooltip.getWidth() - 6).toString() + 'px'
						});
					}.bind(this)
				});
			}
		}

		function closeTooltip(event)
		{
			new Effect.Fade(this, { duration: 0.2 });
		}
	}
}

ProductDocumentType = {
	initialize: function ()
	{

		$$('li.product-document-type').each(
			function (element)
			{
				var label = element.down('span');
				var list = element.down('ul');

				Event.observe(label, 'mousedown', toggleList.bind(list));
			}
		);

		function toggleList(event)
		{
			this.toggle();
		}
	}
}

/* designers */

DesignerProductList = {
	initialize: function ()
	{
		$('designer-products').select('li').each(
			function (element)
			{
				new Highlightable(element);
			}
		);
	}
}

/* publications */

PublicationRelatedProductsTooltip = {
	initialize: function ()
	{
		var label = $('related-products-label');
		var tooltip = $('related-products-tooltip');
		var scope = tooltip.identify() + '-scope';

		Event.observe(label, 'mouseenter', showTooltip.bind(tooltip, label, scope));
		Event.observe(label, 'mouseleave', hideTooltip.bind(tooltip, scope));
		Event.observe(tooltip, 'mouseenter', showTooltip.bind(tooltip, label, scope));
		Event.observe(tooltip, 'mouseleave', hideTooltip.bind(tooltip, scope));

		tooltip.select('ul li').each(
			function (element)
			{
				new Highlightable(element);
			}
		);

		function showTooltip(label, scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Appear(this, {
				duration: 0.4,
				to: 0.98,
				queue: { scope: scope },
				afterSetup: function ()
				{
					this.setStyle({
						top: (label.positionedOffset().top - parseInt((this.getHeight() - label.getHeight()) / 2)).toString() + 'px',
						left: (label.positionedOffset().left + label.getWidth()).toString() + 'px'
					});
				}.bind(this)
			});
		}

		function hideTooltip(scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Fade(this, { duration: 0.2, queue: { scope: scope }});
		}
	}
}

PublicationList = {
	initialize: function ()
	{
		$$('div.publications ul li').each(
			function (element)
			{
				new Highlightable(element);
			}
		);
	}
}

/* retailers */

RetailerAreasTooltip = {
	initialize: function ()
	{
		var labels = $$('span.retailer-areas-label');
		var tooltips = $$('div.retailer-areas-tooltip');

		for (var i = 0; i < labels.length; i++)
		{
			var label = labels[i];
			var tooltip = tooltips[i];
			var close = tooltip.down('.tooltip-close');

			tooltip.setStyle({ opacity: 0.98 });

			Event.observe(label, 'mousedown', showTooltip.bind(tooltip));
			Event.observe(close, 'mousedown', closeTooltip.bind(tooltip));
		}

		function showTooltip(event)
		{
			if (!this.visible())
			{
				tooltips.invoke('hide');

				new Effect.Appear(this, { duration: 0.4, to: 0.98 });
			}
		}

		function closeTooltip(event)
		{
			new Effect.Fade(this, { duration: 0.2 });
		}
	}
}

RetailerImageTooltip = {
	initialize: function ()
	{
		$$('div.retailer').each(
			function (element)
			{
				var tooltip = element.down('div.retailer-image-tooltip');

				if (tooltip != null)
				{
					var scope = tooltip.identify() + '-scope';

					Event.observe(element, 'mouseenter', showTooltip.bind(tooltip, element, scope));
					Event.observe(element, 'mouseleave', hideTooltip.bind(tooltip, scope));
				}
			}
		);

		function showTooltip(element, scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Appear(this, {
				duration: 0.4,
				to: 0.98,
				queue: { scope: scope },
				afterSetup: function ()
				{
					this.setStyle({
						top: ((this.getHeight() * -1) + 4).toString() + 'px',
						left: (parseInt((element.getWidth() - this.getWidth()) / 2)).toString() + 'px'
					});
				}.bind(this)
			});
		}

		function hideTooltip(scope, event)
		{
			Effect.Queues.get(scope).invoke('cancel');

			new Effect.Fade(this, { duration: 0.2, queue: { scope: scope }});
		}
	}
}

/* company, newsletter, register */

PrivacyPolicyTooltip = {
	initialize: function ()
	{
		var label = $('privacy-policy-label');
		var tooltip = $('privacy-policy-tooltip');

		if (label != null && tooltip != null)
		{
			var scope = tooltip.identify() + '-scope';
			var close = tooltip.down('.tooltip-close');

			Event.observe(label, 'mousedown', showTooltip.bind(tooltip, label, scope));
			Event.observe(close, 'mousedown', closeTooltip.bind(tooltip, scope));
		}

		function showTooltip(label, scope, event)
		{
			if (!this.visible())
			{
				new Effect.Appear(this, {
					duration: 0.4,
					from: 0.0,
					to: 0.98,
					queue: { scope: scope, limit: 1 },
					afterSetup: function ()
					{
						this.setStyle({
							top: (label.positionedOffset().top - parseInt((this.getHeight() - label.getHeight()) / 2)).toString() + 'px',
							left: (label.up().positionedOffset().left + label.up().getWidth() + 6).toString() + 'px'
						});
					}.bind(this)
				});
			}
		}

		function closeTooltip(scope, event)
		{
			new Effect.Fade(this, { duration: 0.2, from: 0.98, to: 0.0, queue: { scope: scope, limit: 1 }});
		}
	}
}
