$(document).ready(function()
{	
  hideUnavailable();
});

function hideUnavailable()
{
	if (typeof allowBuyWhenOutOfStock == 'undefined' || allowBuyWhenOutOfStock)
		return;
	var choice = new Array();
	var group_names = new Array();
	
	$('div#attributes select').each(function()
	{
		choice.push($(this).val());
		group_names.push($(this).attr('id'));
	});

	$.each(group_names, function(i, group_name)
	{
		$('#'+group_name+' option').each(function()
		{
			var group_option = $(this).val();
			var tmp_choice = new Array();
			$.each(choice, function(key, value)
			{
				if (key == i)
					tmp_choice.push(group_option);
				else
					tmp_choice.push(value);
			});
			var nbAttributesEquals = 0;
			var valid = false;
      
			for (combination in combinations)
			{
				nbAttributesEquals = 0;
				for (idAttribute in combinations[combination]['idsAttributes'])
				{
					if (idAttribute != 'indexOf')
					{
						if (in_array(combinations[combination]['idsAttributes'][idAttribute], tmp_choice))
							nbAttributesEquals++;
					}
				}
				if (nbAttributesEquals == tmp_choice.length)
				{
					valid = true;
					if (typeof allowBuyWhenOutOfStock != 'undefined' && !allowBuyWhenOutOfStock && combinations[combination]['quantity'] < 1)
						valid = false;
				}
			}
			if (!valid)
			{
				$('#'+group_name+' option[value='+group_option+']').attr('disabled', 'true');
				$('#color_'+group_option).hide();
			}
			else
			{
				$('#'+group_name+' option[value='+group_option+']').removeAttr('disabled');
				$('#color_'+group_option).show();
			}
		});
	});
}
