/*
 * @author Alf Scotland
 * @version $Id: comparison.js 9699 2010-02-08 12:53:59Z khuth $
 */
var blocked = false;

function toggleCompare(id, selector) {
	if(blocked == false) {
		blocked = true;
		loadingBar(true);
		var action = selector.checked ? 'Add' : 'Remove';
		now = new Date();
		var url = comparisonUrl + 'a/action' + action + '/id/' + id + '/ref/ajax/' + now.getTime();
		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				var json = transport.responseJSON;
				if (json.request == 'requestFailure') {
					if (selector.checked)
						selector.checked = false;

					var code = json.exceptionCode;
					alert(comparisonErrorMessages[code]);
				}

				// show or hide comparison information
				$$('.comparisonInfo').each(function(n) {
					json.products > 0 ? n.show() : n.hide();
				});

				$$('.comparisonInfo span').each(function(n) {
					n.update(json.products == 1 ? comparisonInfomationValues[0] : comparisonInfomationValues[1])
				});

				// update number of products in the comparison
				$$('span.comparisonProductCount').each(function(n) {
					n.update(json.products);
				});

				// enable or disable comparison buttons
				$$('a.comparisonButton').each(function(n) {
					n.onclick = function() {
						return json.products > 0;
					};
					json.products > 0 ? n.setOpacity(1) : n.setOpacity(0.5);
				});
			},
			onComplete: function() {
				blocked = false;
				loadingBar(false);
			}
		});
	}else {
		selector.checked ? selector.checked = false : selector.checked = true;
	}
}

function clearComparison(callback) {
	if(blocked == false) {
		blocked = true;
		loadingBar(true);
		now = new Date();
		var url = comparisonUrl + 'a/actionClear/' + 'ref/ajax' + now.getTime();
		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				if (transport.responseJSON.request == 'requestSuccess') {
					if (!callback)
						history.back();
					else
						window.location.href=callback;
				} else {
					alert(transport.responseJSON.request);
					alert(transport.responseJSON.exceptionMessage);
				}
			},
			onComplete: function() {
				blocked = false;
				loadingBar(false);
			}
		});
	}
}

function removeFromComparison(id) {
	if(blocked == false) {
		blocked = true;
		loadingBar(true);
		now = new Date();
		var url = comparisonUrl + 'a/actionRemove/id/' + id + '/ref/ajax' + now.getTime();
		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				if (transport.responseJSON.request == 'requestSuccess') {
					if (transport.responseJSON.products == 0)
						history.back();
					else {
						location.reload();
					}
				}
			},
			onComplete: function() {
				blocked = false;
				loadingBar(false);
			}
		});
	}
}

function checkCheckbox(id){
	box = document.getElementById(id);

	if (box)
		box.checked = true;
}