function displayAttributeSorted(currentSelect, all)
{
	if (productattribute_display == 'sequence' && productattribute_display_sequence == 'all')
		all = true; // refresh allways all below attributes in this configuration

	var span_class = 'productattribute_unique'; // use for display unique attribute in text format

	var unique_option_length = '1'; // use for estimate unique attribute
	if (productattribute_display == 'sequence' && productattribute_display_sequence == 'step')
		unique_option_length = '2';

	if (productattribute_display == 'sequence')
	{
		// if the attribute has got only one choice this function continue the treatment
		if (currentSelect.children("option").length == unique_option_length && productattribute_display_unique == 'text')
		{
			currentSelect.hide();
			if (currentSelect.find('option[value=0]').length)
				currentSelect.find('option[value=0]').remove();
			currentSelect.find('option[value!=0]').attr('selected','selected'); // select first option
			if (currentSelect.parent().children('span.'+span_class).length == 0)
				currentSelect.before('<span class="ontop '+span_class+'">'+currentSelect.find('option[value!=0]').text()+'</span>'); // display a simple text instead of a select box
			findCombination();
			if (productattribute_display_sequence == 'step')
				displayAttributeSorted(currentSelect, true); // recall the function with current select
			else
				displayAttributeSorted(currentSelect.parent().next("p.groupsorted").children("select")); // recall the function with next select
			return true;
		}

		// Nothing to do if last select option
		if (currentSelect.parent().next("p.groupsorted").length < 1)
		{
			$("#availability_statut").show(); // show availability info
			findCombination();
			return false;
		}
		else
			$("#availability_statut").hide(); // hide availability info

		if (productattribute_display_sequence == 'step') // hide all next attributes
			currentSelect.parent().nextAll("p.groupsorted").children("select, span").fadeOut('fast');
		else if (productattribute_display_sequence == 'all') // hide only next attribute
			currentSelect.parent().next("p.groupsorted").children("select, span").fadeOut('fast');

		var idNextAttrGroup = currentSelect.parent().next("p.groupsorted").children('select').attr('id').replace('group_', '');
	}

	// Nothing to do if selected option has not value
	if (parseInt(currentSelect.find("option:selected").attr("value")) < 1)
	{
		findCombination();
		return false;
	}

	// display a loading image
	currentSelect.after('<span class="productattribute_wait"><img src="'+loader_dir+'flower.gif" alt="'+productattribute_wait+'" /></span>');

	var id_product = $("input[@name=id_product]").attr("value");
	var idCurrentAttrGroup = currentSelect.attr('id').replace('group_', '');

	var params = 'id_product=' + id_product + '&current=' + idCurrentAttrGroup;

	// get all attributes values to send it in AJAX
	$("p.groupsorted").children("select").each(function(i) {
		id_attribute_group = $(this).attr("name").replace('group_', '');
		id_attribute = $(this).find("option:selected").attr('value');
		params += '&' + id_attribute_group + '=' + id_attribute;
	});

	// get data by Ajax
	$.ajax({
		type: "GET",
		url: productattribute_dir + "ajax.php",
		data: params,
		async: false,
		cache: false,
		dataType: "json",
		success: function(data)
		{
			if (productattribute_display == 'sequence')
				doAction = false;

			// received json cluster with 2 levels depth
			for (id_group in data) {
				if (parseInt(id_group) == parseInt(idCurrentAttrGroup))
					continue; // nothing to change on current select option

				if (productattribute_display == 'sequence' &&  productattribute_display_sequence == 'step')
					var html = '<option value="0" selected="selected">'+productattribute_option+'</option>';
				else
					var html = '';

				if (productattribute_display == 'sequence' && !doAction)
				{
					if (parseInt(idNextAttrGroup) == parseInt(id_group))
						doAction = true;
					else
						continue;
				}

				var $select = $("select#group_"+id_group); // get the select
				var attributeSelected = parseInt($select.find('option:selected').attr('value'));

				var attributes = data[id_group][0]; // hack for select the object at second level
				$.each(attributes, function(id_attribute,text) {
					html += '<option value="'+id_attribute + '"';
					if (parseInt(id_attribute) == attributeSelected && productattribute_display_sequence != 'step')
						html += ' selected="selected"';
					html += '>'+text+'</option>';
				});
				$select.children("option").each(function (i) {
					$(this).remove(); // delete all actual options
				});

				$select.parent('p.groupsorted').children('span.'+span_class).remove(); // delete all option in simple text
				$(".productattribute_wait").fadeOut('fast');

				$select.html(html).fadeOut('fast');

				if ($select.children('option').length == unique_option_length && productattribute_display_unique == 'text')
				{
					$select.find('option[value!=0]').attr('selected','selected'); // select first option
					if ($select.parent().children('span.'+span_class).length == 0)
						$select.before('<span class="onbottom '+span_class+'">'+$select.find('option[value!=0]').text()+'</span>'); // display a simple instead of a select box
				}
				else
				{
					if (productattribute_display == 'sequence' && productattribute_display_sequence == 'step')
					{
						if (parseInt(idNextAttrGroup) == parseInt(id_group)  && parseInt(currentSelect.find('option:selected').attr('value')) > 0)
						{
							$select.fadeIn('normal');
						}
					}
					else
						$select.fadeIn('normal');
				}
			}
			if (typeof displayShaft == 'function')
				displayShaft();
			findCombination(); // refresh display using function in product.js
			if (all == true)
				displayAttributeSorted(currentSelect.parent().next("p.groupsorted").children("select"), all); // recall the function with next select
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			alert(productattribute_error);
		}
	});
	findCombination(); // refresh display using function in product.js
}

$(function()
{
	// for attribute selects on product page
	if ($('#attributes p select').length > 0)
	{
		if ((typeof productattribute_display != 'undefined' && productattribute_display == 'sequence')
			&& (typeof productattribute_display_sequence != 'undefined' && productattribute_display_sequence == 'step'))
		{
			$('#attributes p select').fadeOut('fast', function() { // hide all options
				$('#attributes p:first select').slideDown('fast'); // display first attribute
				//$("#availability_statut").hide(); // hide availability
			});
			refreshAll = false;
		}
		else
			refreshAll = true;
		displayAttributeSorted($('#attributes p:first select'), refreshAll);
	}
});
