var Comparison, URL;

URL = new Class({
	Implements: [Events, Options]
	, options: {
		useLocale: false
		, searchUrl: ''
		, passElementValue: false
		, requestOptions: {
			method: 'get'
			, data: ''
			, onSuccessCallback: false
		}
	}
	, initialize: function(options) {
		this.setOptions(options);
		this.options.requestOptions.onSuccess = this.onSuccess.bind(this);
	}
	, fetch: function(options) {
		this.options.requestOptions.url = options.url;
		
		request = new Request(this.options.requestOptions);
		request.send(this.options.requestOptions.data);
	}
	, onSuccess: function(responseText, responseXML) {
		if (this.options.requestOptions.onSuccessCallback) {
			this.options.requestOptions.onSuccessCallback(responseText);
		}
	}
});

Comparison = new Class({
	Implements: [Options, Events]
	, options: {
		addCompareLinkClass: ''
		, comparisonContainer: ''
		, urlOptions: {}
		, messages: {
			alreadyExistsMsg: ''
		}
	}
	, url: null
	, initialize: function(options) {
		this.setOptions(options);
		this.prepare();
	}
	, prepare: function() {
		// this.url = new URL(this.options.urlOptions);
		// $$(this.options.addCompareLinkClass).addEvent('click', this.addCompare.bind(this));
		
		$(this.options.comparisonContainer).getElements('li').each(function(container){
			// if (container.getElement('a')) {
			// 	container.getElement('a').addEvent('click', this.removeCompare.bind(this));
			// }

			if (container.getElement('img')) {
				container.getElement('img').addEvent('mouseenter', this.showTitle.bind(this));
				container.getElement('img').addEvent('mouseout', this.hideTitle.bind(this));
			}
		}, this);
	}
	, addCompare: function(event) {
		event.preventDefault();
		this.url.options.requestOptions.onSuccessCallback = this.addProductToCompareList.bind(this);
		this.url.fetch({ url: event.target.get('href')});
	}
	, addProductToCompareList: function(responseText) {
		var response, containers, thumb, _break;
		response = JSON.decode(responseText);

		switch (typeof response) {
			case 'number':
				if (response == -1) {
					// Invalid call
					return;
				} else if (response == 0) {
					// Already exists
					alert(this.options.messages.alreadyExists);
					return;
				} else {
					// Full list
					alert(this.options.messages.listFull);
					return;
				}
				break;
			case 'object':
				containers = $(this.options.comparisonContainer).getElements('li');
				_break = false;
				containers.each(function(container){
					if (_break) return;
					if (!container.getElement('img')) {
						var baseUrl, removeLink, image;
						baseUrl = this.options.baseUrl;
						removeLink = new Element('a', {
							href: response.remove
							, 'class': 'delete-compare-product'
							,  opacity: 0
						});
						removeLink.inject(container, 'top');
						removeLink.addEvent('click', this.removeCompare.bind(this));
						image = new Element('img', {
							opacity: 0
							, src: baseUrl + '/' + response.thumb
							, alt: response.name
							, width: 40
						});
						image.inject(container);
						image.addEvent('mouseenter', this.showTitle.bind(this));
						image.addEvent('mouseout', this.hideTitle.bind(this));
						container.getElement('img').tween('opacity', 1);
						removeLink.tween('opacity', 1);
						_break = true;
					}
				}, this);
				break;
		}
	}
	, showTitle: function(event) {
		var div = new Element('div', {
			text: event.target.get('alt')
			, 'class': 'compare-product-name'
			, 'opacity': 0
		});
		div.inject(event.target.getParent('li'));
		div.tween('opacity', 0.7);
	}
	, hideTitle: function(event) {
		event.target.getParent('li').getElement('div').dispose();
	}
	, removeCompare: function(event) {
		event.preventDefault();
		
		this.url.options.requestOptions.onSuccessCallback = this.removeProductFromCompareList.bind(event.target);
		this.url.fetch({ url: event.target.get('href')});
	}
	, removeProductFromCompareList: function(responseText) {
		var removed = JSON.decode(responseText);
		if (removed) {
			this.getParent('li').getElement('img').dispose();
			this.dispose();
		}
	}
	, addThumb: function(img, source) {
		img.set({
			opacity: 0
			, src: source.getParent('.item').getElement('img').get('src')
			, alt: source.getParent('.item').getElement('img').get('alt')
			, width: 40
		});
		img.tween('opacity', 1);
	}
	, elementExists: function(container, source) {
		if (container.getElement('img').get('src') == source) {
			return 1;
		} else {
			return 0;
		}
	}
});
