var Favorites = Class.create({
	initialize: function(clicked)
	{
		this.clicked = clicked;
		this.container = $$('#favoritesJsContainer')[0];
		this.items = new Array();
		this.start();
	},
	
	start: function()
	{
		this.load();
		
		if (this.container)
		{
			this.build();
		}
	},
	
	load: function()
	{
		if (readCookie('favorites'))
		{
			this.items = eval(unescape(readCookie('favorites')));
			document.fire('newhey:favoritsLoaded');			
		}
	},
	
	build: function()
	{	
		this.container.update('');
		if (this.items.length > 0)
		{
			for (var i=0; i < this.items.length; i++)
			{
				var swatch = new Element('div',{className: 'swatch'});
			
				var image = new Element('div', {className: 'image tile'});
				var link = new Element ('a', {href:'/swatch/' + this.items[i].sku + '.html', className: 'lbOn'});
				image.appendChild(link);
				
				image.appendChild(new Element('div', {className: 'zoom'}));
				
				link.appendChild(new Element('img', {src: this.items[i].thumbnailUrl}));
				swatch.appendChild(image);
				
				var links = new Element('div', {className: 'links'});
				links.appendChild(new Element('p').update(this.items[i].name));
				
				link = new Element('a', {href: '/search/print/' + this.items[i].sku + '.html'}).update('Printable version');
				p = new Element('p');
				p.appendChild(link);
				links.appendChild(p);
				
				link = new Element('a', {href: '/request/sample.html?emailUpdates=true&request=' + this.items[i].sku}).observe('click', this.requestClicked.bindAsEventListener(this, i)).update('Request Sample');
				p = new Element('p');
				p.appendChild(link);
				links.appendChild(p);
				
				link = new Element('a', {href: '#'}).update('Remove favourite').observe('click', this.removeClicked.bindAsEventListener(this, i));
				p = new Element('p');
				p.appendChild(link);
				links.appendChild(p);
				
				swatch.appendChild(links);
				
				this.container.appendChild(swatch);
			}
			setupZoom();
		}
		else
		{
			var content = new Element('div', {className: 'commingSoon'});
			content.appendChild(new Element('h1').update('You currently have no items in your favourites'));
			this.container.appendChild(content);
		}
	},
	
	add: function(name, sku, thumbnailUrl, imageUrl)
	{
		if (this.items.length >= 15)
		{
			alert('You can only have 15 favourites');
			return false;
		}
		
		for (key in this.items)
		{
			if (name == this.items[key].name)
			{
				alert('Item already in favourites');
				return false;
			}
		}
		
		var item = {
			name: name,
			sku: sku,
			thumbnailUrl:thumbnailUrl,
			imageUrl:imageUrl	
		};
		
		this.items[this.items.length] = item;
		alert(name + ' added to favourites');
		this.save();
	},
	
	requestClicked: function(event, index)
	{
		this.clicked.click(this.items[index].name, this.items[index].sku, this.items[index].thumbnailUrl, this.items[index].imageUrl).bind(this.clicked);
	},
	
	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('favorites');
	},
	
	save: function()
	{
		createCookie('favorites', escape(this.items.toJSON()), 31);
	}
});

var Clicked = Class.create({
	initialize: function()
	{
		this.item = {};
		this.load();
	},
	
	load: function()
	{
		if (readCookie('clicked'))
		{
			this.item = eval(unescape(readCookie('clicked')));
			this.item = this.item[0];
		}
	},
	
	click: function(name, sku, thumbnailUrl, imageUrl)
	{
		this.item = {
			name: name,
			sku: sku,
			thumbnailUrl:thumbnailUrl,
			imageUrl:imageUrl	
		};
		
		this.save();
	},

	save: function()
	{
		var temp = new Array();
		temp[0] = this.item;
		
		createCookie('clicked', escape(temp.toJSON()), 31);
	}
});

var RequestJS = Class.create({
	initialize: function(container, favorites, clicked) {
	
		this.favorites = favorites;
		this.clicked = clicked;
		this.container = $(container);
		this.start();
	},
	
	start: function() {
		this.items = new Array();
		
		this.items[0] = this.clicked.item;
		this.items[0].checked = true;
		
		this.items = this.items.concat(this.favorites.items);
		
		this.request = $$('input[name="request"]')[0];
		
		if (this.request)
		{
			this.build();
		}
	},
	
	build: function() {
		for (var i=0; i < this.items.length; i++)
		{
			if (i && this.items[i].name == this.clicked.item.name)
			{
				continue;
			}
			var swatch = new Element('div',{className: 'requestSwatch'});
			
			var checkbox = new Element('input', {type: 'checkbox', name: this.items[i].sku});
			
			checkbox.observe('change', this.writeValue.bindAsEventListener(this, this.items[i].sku));
			
			if (this.items[i].checked)
			{
				checkbox.writeAttribute('checked');
			}
			swatch.appendChild(checkbox);
			var image = new Element('div', {className: 'image'});
			var link = new Element ('a', {href:this.items[i].imageUrl});
			image.appendChild(link);
			link.appendChild(new Element('img', {src: this.items[i].thumbnailUrl}));
			swatch.appendChild(image);
			
			var name = new Element('div', {className: 'name'});
			name.appendChild(new Element('p').update(this.items[i].name))
			swatch.appendChild(name);
			
			this.container.appendChild(swatch);
		}
		
		$$('fieldset.last').each(function(last){
			last.setStyle({clear:'both'});
		});
		
		setupZoom();
	},
	
	writeValue: function(event, memo)
	{
		var string = "";
		for (var i=0; i < this.items.length; i++)
		{
			if (this.items[i].sku == memo)
			{
				this.items[i].checked = !this.items[i].checked;
				
			}
			
			if (this.items[i].checked)
			{
				string = string + this.items[i].name + ', ';
			}
		}
		
		this.request.setValue(string);
		
	}
});

document.observe('dom:loaded', function(){
	clicked = new Clicked();
	favorites = new Favorites(clicked);
	
	if ($('requestJS'))
	{
		var jsUI = new RequestJS($('requestJS'), favorites, clicked);
	}
})