SC.ProductComparison = function () {
	this.cmsBoxId = '';
	return this;
}
SC.ProductComparison.doAjax = function (action, productId, cmsBoxId)
{
	var url = SC.ProductComparison.modulePageUrl + '?ModuleName=com.summercart.productcomparison&load=table&action=' + action;
	url += '&ProductID=' + productId;
	url += '&CMSBoxID=' + SC.ProductComparison.cmsBoxId;
	$.get(url,
		'',
		SC.ProductComparison.ajaxCallback,
		'json'
	);
}
SC.ProductComparison.ajaxCallback = function (data)
{
	if (data['error'] != '') {
		alert(data['error']);
		return;
	}
	SC.ProductComparison.currentCompareList = data['compareList'];
	
	$('.comparable .compareAddButton').show();
	$('.comparable .compareRemoveButton').hide();
	$('.comparable .compareAddButton input[type=checkbox]').attr('checked', false);
	$('.comparable .compareRemoveButton input[type=checkbox]').attr('checked', false);
	
	for (key in SC.ProductComparison.currentCompareList) {
		productId = SC.ProductComparison.currentCompareList[key];
		$('.compareAddButton.product' + productId).hide();
		$('.compareRemoveButton.product' + productId).show();
		$('.compareAddButton.product' + productId + ' input[type=checkbox]').attr('checked', 'checked');
		$('.compareRemoveButton.product' + productId + ' input[type=checkbox]').attr('checked', 'checked');
	}
	
	$('.ProductComparisonBox').html(data['box']);
	$('.ProductComparisonDetailsBox').html(data['product']);
	
	if (data['productAction'] == 'add') {
		SC.ProductComparison.animateAdd(data['productId']);
	} else if (data['productAction'] == 'remove') {
		// Remove data column for given product
		$('.products-comparison .product_' + data['productId']).remove();
		if ((SC.ProductComparison.currentCompareList.length < 2) && ($('.show-comparison').length > 0)) {
			$('.show-comparison').colorbox.close();
		}
	}
	SC.ProductComparison.boldDifferentAttributes();
}
SC.ProductComparison.add = function (productId)
{
	SC.ProductComparison.doAjax('dmAjaxAddProduct', productId);
	return false;
}
SC.ProductComparison.remove = function (productId)
{
	SC.ProductComparison.doAjax('dmAjaxRemoveProduct', productId);
	return false;
}
SC.ProductComparison.animateAdd = function (productId)
{
	var source = SC.ProductComparison.getAnimateSource();
	if (source.length != 1) {
		return;
	}
	
	var sourcePosition = source.offset();
	var transferWidth = source.width();
	var transferHeight = source.height();
	var image = $('img.product-image-' + productId);
	var imageHtml = '';
	if (image.length > 0) {
		imageHtml += '<img src="' + image.attr('src') + '" />';
		transferWidth = image.width();
		transferHeight = image.height();
		sourcePosition = image.offset();
	}
	
	var target = SC.ProductComparison.getAnimateTarget();
	if (target.length != 1) {
		return;
	}
	var targetPosition = target.offset();
	
	if ($('#comparisonTransfer').length == 0) {
		$('body').append('<div id="comparisonTransfer">' + imageHtml + '</div>');
	}
	
	$('#comparisonTransfer').css({
		position: 'absolute',
		top: sourcePosition.top + 'px',
		left: sourcePosition.left + 'px',
		width: transferWidth + 'px',
		height: transferHeight + 'px',
		border: '2px solid',
		'z-index': '9999',
		opacity: '0.7',
		'-moz-opacity': '0.7'
	});
	
	var duration = Math.abs(sourcePosition.top - targetPosition.top);
	duration = duration < 300 ? 300 : duration;
	
	$('#comparisonTransfer').animate({
		top: targetPosition.top + 'px',
		left: targetPosition.left + 'px',
		width: target.width() + 'px',
		height: target.height() + 'px'
	}, duration, function () {$(this).remove();});
}
SC.ProductComparison.showPopup = function(elem)
{
	var compareUrl = $(elem).attr('href');
	$.get(compareUrl, {}, function(data) {
		var content = $(data).find('.page-content');
		$(elem).colorbox({open: true, html: content.html(), maxWidth: '80%', maxHeight: '80%', onComplete: function() {
			$('.product_compared a.button-link').unbind('click').click(function() {
				var productId = $(this).attr('rel'); 
				if (productId) {
					SC.ProductComparison.remove(productId);
				}
				return false;
			});
			
			SC.ProductComparison.boldDifferentAttributes();
		}});
	}, 'html');
}
SC.ProductComparison.boldDifferentAttributes = function()
{
	$('.attribure_values').each(function() {
		var hasDifferentValues = false;
		var currentValue = null;
		$(this).find('.attribure_value').each(function() {
			var elemValue = $.trim($(this).html());
			if (currentValue == null) {
				currentValue = elemValue;
			}
			if (currentValue != elemValue) {
				hasDifferentValues = true;
			}
		});
		
		if (hasDifferentValues && ($(this).find('.attribure_value>strong').length == 0)) {
			$(this).find('.attribure_value').addClass('comparison-different-values').wrapInner('<strong />');
		} else if (!hasDifferentValues && ($(this).find('.attribure_value>strong').length > 0)) {
			$(this).find('.attribure_value').removeClass('comparison-different-values').html($(this).find('.attribure_value>strong').html());
		}
	});
}

$(function() {
	SC.ProductComparison.boldDifferentAttributes();
});
