var Favourites = Class.create({
	initialize: function()
	{
		this.container = $$('#favouritesJsContainer')[0];
		this.request = $$('input[name="request"]')[0];
		this.items = new Array();
		this.start();
	},
	
	start: function()
	{
		this.load();
		
		if (this.container)
		{
			this.build();
		}
	},
	
	load: function()
	{
		if (readCookie('favourites'))
		{
			this.items = eval(unescape(readCookie('favourites')));
			document.fire('newhey:favouritsLoaded');			
		}
	},
	
	build: function()
	{		
		this.container.update('');
		if (this.items.length > 0)
		{
			$('topBodyCopy').update("If you have now finished getting all your favourite samples together (you can remove any by clicking the 'x' button) simply fill in the form opposite, click the 'Submit' button and we will get them straight in the post for you.");
			
			for (var i=0; i < this.items.length; i++)
			{
				var swatch = new Element('div',{className: 'fabric'});
			
				var link = new Element ('a', {href:'/products/detail/' + this.items[i].id + '.html', className: 'lbOn'});
				var image = new Element('img', {className: 'image tile', src: this.items[i].thumbnailUrl});
				
				link.appendChild(image);
				swatch.appendChild(link);
				
				new lightbox(link)

				var text = new Element('p');
				var deleteLink = new Element('a', {href: '#'});
				deleteLink.observe('click', this.removeClicked.bindAsEventListener(this, i));
				
				var deleteImage = new Element('img', {src: '/images/deleteLink.png', alt: 'Remove'});
				deleteLink.appendChild(deleteImage);
				
				var name = new Element('span').update(' ' + this.items[i].name);
				
				text.appendChild(deleteLink);
				text.appendChild(name);
				
				swatch.appendChild(text);
				
				this.container.appendChild(swatch);
			}
		}
		else
		{
			var content = new Element('div', {className: ''});
			content.appendChild(new Element('h1').update('You currently have no items in your samples, please go to the Fabrics and Cushions section to add them.'));
			$('topBodyCopy').update("To add samples to this page simply go to the <a href=\"/products/fabrics-and-cushions/fabrics/\">Fabrics &amp; Cushions sections</a>, if you see a design you like then just hit the \"Add to your samples\" button.  When you have got all your favourite samples together  simply fill in the form opposite, click the 'Submit' button and we will get them straight in the post for you.");
			this.container.appendChild(content);
		}
		
		this.writeValue();
	},
	
	add: function(name, id, thumbnailUrl, imageUrl)
	{
		if (this.items.length >= 12)
		{
			alert('Your samples is now full. If you require more than 12 samples please just call or email Wendy or Vivienne.');
			return false;
		}
		
		for (key in this.items)
		{
			if (name == this.items[key].name)
			{
				alert('Item already in your samples');
				return false;
			}
		}
		
		var item = {
			name: name,
			id: id,
			thumbnailUrl:thumbnailUrl,
			imageUrl:imageUrl	
		};
		
		this.items[this.items.length] = item;
		alert(name + ' added to your samples');
		this.save();
	},
	
	removeClicked: function(event, index)
	{
		event.stop();
		var name = this.items[index].name
		if (this.remove(index))
		{
			this.build();
			alert('Removed ' + name);
		}
	},
	
	remove: function(index)
	{
		if (this.items[index])
		{
			this.items = this.items.without(this.items[index]);
			this.save();
			return true;
		}
		
		return false;
	},
	
	clear: function()
	{
		eraseCookie('favourites');
	},
	
	save: function()
	{
		createCookie('favourites', escape(this.items.toJSON()), 31);
	},
	
	writeValue: function()
	{
		if (this.request)
		{
			var string = "";
			for (var i=0; i < this.items.length; i++)
			{
				string = string + this.items[i].name + ', ';
			}
			
			this.request.setValue(string);
		}
	}
});

document.observe('dom:loaded', function(){
	favourites = new Favourites();
})