﻿// namespace
window.LOV = window.LOV || {};
LOV.Sony = LOV.Sony || {};
LOV.Sony.Business = LOV.Sony.Business || {};

// shortcut
var sonyB = LOV.Sony.Business;

sonyB.Products = sonyB.Products || {};

sonyB.Products.init = function() {
	$('#products :checkbox').each(function(i) {
		$(this).click(sonyB.Products.onClickCompare);
	});
};
$().ready(sonyB.Products.init);

sonyB.Products.checkedCompare = 0;
sonyB.Products.maxCompare = 4;

sonyB.Products.onClickCompare = function(e) {
	if ($(this)[0].checked) {
		if (!sonyB.Products.onCompare()) {
			e.stopPropagation();
			return false;
		}
	} else {
		sonyB.Products.checkedCompare--;
		sonyB.Products.unlockCompare();
	}
};

sonyB.Products.onCompare = function() {
	if (sonyB.Products.checkedCompare < sonyB.Products.maxCompare) {
		sonyB.Products.checkedCompare++;
		if (sonyB.Products.checkedCompare == sonyB.Products.maxCompare) {
			sonyB.Products.lockCompare();
		} else {
			sonyB.Products.unlockCompare();
		}
		return true;
	} else {
		return false;
	}
};

sonyB.Products.lockCompare = function() {
	$('#products :checkbox').filter(function(i) { return !$(this)[0].checked; })
		.each(function(i) {
			$(this).attr('disabled', 'disabled');
		}
	);
};

sonyB.Products.unlockCompare = function() {
	$('#products :checkbox').filter(':disabled')
		.each(function(i) {
			$(this).removeAttr('disabled');
		}
	);
};

sonyB.Products.doCompare = function(){
	if (sonyB.Products.checkedCompare < 2) {
		alert('Selecione pelo menos dois produtos.');
	} else {
		$('#productsForm').submit();
		return false;
	}
};
