Wishlist = {
	label: null,
	tooltip: null,
	loader: null,
	content: null,
	addItemButton: null,
	messageTooltip: null,
	scope: null,
	url: null,
	request: null,
	contentWidth: 217,
	contentHeight: 52,
	maxItems: 8,
	initialize: function ()
	{
		try
		{
			this.label = $('wishlist-label');
			this.tooltip = $('wishlist-tooltip');
			this.loader = $('wishlist-loader');
			this.content = $('wishlist-content');
			this.addItemButton = $('wishlist-add-item-button');
			this.messageTooltip = $('wishlist-message-tooltip');
			this.scope = 'wishlist-scope';
			this.url = BASE_URL + LANGUAGE + '/products/wishlist/index.php';

			if (this.tooltip != null)
			{
				Event.observe(this.label, 'mousedown', this.show.bindAsEventListener(this));
			}
		}
		catch (error)
		{
		}
	},
	show: function (event)
	{
		$('login-tooltip').hide();

		new Effect.Appear(this.tooltip, { duration: 0.4, to: 0.96, queue: { position: 'end', scope: this.scope, limit: 1 },
			beforeStart: function ()
			{
				this.tooltip.setStyle({
					top: (this.label.positionedOffset().top + this.label.getHeight() + 8).toString() + 'px',
					left: (this.label.positionedOffset().left + parseInt(this.label.getWidth() / 2) - 27).toString() + 'px'
				});
			}.bind(this),
			afterSetup: function ()
			{
				this.refresh();
			}.bind(this)
		});
	},
	hide: function ()
	{
		new Effect.Fade(this.tooltip, { duration: 0.4, queue: { position: 'end', scope: this.scope, limit: 1 }});
	},
	refresh: function ()
	{
		try
		{
			if (this.request == null)
			{
				this.content.update('');
				this.content.setStyle({
					width: this.contentWidth.toString() + 'px',
					height: this.contentHeight.toString() + 'px'
				});

				this.loader.clonePosition(this.content);
				this.loader.show();

				this.request = new Ajax.Request(this.url, {
					onSuccess: function (response)
					{
						this.loader.hide();

						this.content.setStyle({ width: 'auto', height: 'auto' });
						this.content.appendChild(Builder.node('div', { className: 'float-container', style: 'padding: 0px 30px 0px 0px;' }, [
							Builder.node('h4', { style: 'float: left; margin: 0px 20px; color: #FFFFFF;' }, ['Wishlist']),
							Builder.node('div', { id: 'wishlist-options', className: 'float-container', style: 'float: right; margin: 0px 20px;' }, [
								Builder.node('span', { style: 'display: block; position: absolute; cursor: pointer; top: 18px; right: 10px; background: transparent url(' + BASE_URL + 'images/close.png) no-repeat top left; width: 20px; height: 20px;', onclick: 'Wishlist.hide();' })
							])
						]));

						var items = response.responseJSON;

						if (items.length > 0)
						{
							var columnCount = Math.min(items.length, 4);
							var width = (columnCount * 150) + (8 * (columnCount + 1)) - 8;

							$('wishlist-options').appendChild(Builder.node('a', { href: BASE_URL + LANGUAGE + '/products/wishlist/pdf.php', style: 'float: left;' }, [Language.getTranslation('Scarica PDF')]));

							if (items.length > 1)
							{
								$('wishlist-options').appendChild(Builder.node('a', { href: 'javascript:void(0);', style: 'float: left; margin: 0px 0px 0px 20px;', onclick: 'Wishlist.clear();' }, [Language.getTranslation('Rimuovi tutto')]));
							}

							this.content.appendChild(Builder.node('ul', { id: 'wishlist-items', className: 'float-container', style: 'display: block; padding: 0px 0px 0px 8px; width: ' + width.toString() + 'px;' }));

							items.each(
								function (item)
								{
									$('wishlist-items').appendChild(Builder.node('li', { style: 'float: left; margin: 20px 8px 0px 0px;' }, [
										Builder.node('span', { style: 'display: block; background: transparent url(' + item.thumbnail + ') no-repeat top center; width: 150px; height: 120px;' }),
										Builder.node('span', { style: 'display: block; padding: 4px 0px; text-align: center;' }, [item.name]),
										Builder.node('a', { href: 'javascript:void(0);', style: 'display: block; text-align: center;', onclick: 'Wishlist.removeItem(' + item.id + ');' }, [Language.getTranslation('Rimuovi')])
									]));
								}
							);
						}
						else
						{
							this.content.appendChild(Builder.node('span', { className: 'empty-separator', style: 'display: block; overflow: hidden; height: 20px;' }));
							this.content.appendChild(Builder.node('span', { style: 'display: block; padding: 0px 20px;' }, [Language.getTranslation('La tua wishlist non contiene prodotti.')]));
						}

						this.request = null;
						this.contentWidth = this.content.getWidth();
						this.contentHeight = this.content.getHeight();
					}.bind(this)
				});
			}
		}
		catch (error)
		{
		}
	},
	addItem: function (itemId)
	{
		var itemIds = Cookie.get('wishlist_version_id');

		itemIds = (itemIds != null && itemIds != '') ? itemIds.toString().split(',') : [];

		if (itemIds.include(itemId))
		{
			this.showMessageTooltip(Language.getTranslation('Il prodotto è già presente nella tua wishlist.'));
		}
		else if (itemIds.length >= this.maxItems)
		{
			this.showMessageTooltip(Language.getTranslation('La tua wishlist può contenere al massimo #{max} prodotti.').interpolate({ max: this.maxItems }));
		}
		else
		{
			itemIds.push(itemId);

			Cookie.set('wishlist_version_id', itemIds.uniq().join(','));

			this.showMessageTooltip(Language.getTranslation('Il prodotto è stato aggiunto alla tua wishlist.'));

			if (this.tooltip.visible())
			{
				this.refresh();
			}
		}
	},
	removeItem: function (itemId)
	{
		var itemIds = Cookie.get('wishlist_version_id');

		itemIds = (itemIds != null && itemIds != '') ? itemIds.toString().split(',') : [];
		itemIds = itemIds.without(itemId);

		Cookie.set('wishlist_version_id', itemIds.uniq().join(','));

		this.refresh();
	},
	showMessageTooltip: function (message)
	{
		try
		{
			new Effect.Appear(this.messageTooltip, { duration: 0.4, to: 0.96, queue: { position: 'end', scope: this.scope, limit: 1 },
				beforeStart: function ()
				{
					this.messageTooltip.down('span').innerHTML = message;
					this.messageTooltip.setStyle({
						top: (this.addItemButton.positionedOffset().top - parseInt(this.addItemButton.getHeight() / 2)).toString() + 'px',
						left: (this.addItemButton.positionedOffset().left - this.messageTooltip.getWidth() - 4).toString() + 'px'
					});
				}.bind(this),
				afterFinish: function ()
				{
					new Effect.Fade(this.messageTooltip, { delay: 4.0, duration: 0.4, queue: { position: 'end', scope: this.scope, limit: 1 },
						afterFinish: function ()
						{
							this.messageTooltip.down('span').innerHTML = '';
						}.bind(this)
					});
				}.bind(this)
			});
		}
		catch (error)
		{
		}
	},
	clear: function ()
	{
		Cookie.set('wishlist_version_id', '');

		this.refresh();
	}
}

document.observe('dom:loaded', function () {
	Wishlist.initialize();
});