/* Static JavaScript code for the Product List Attributes module */

var plac = new Array();
var pla_ajax = false;

function pla_sortNumeric(arg1, arg2)
{
	return (arg1 - arg2);
}

function pla_addProductCombination(id_product, id_combination, array_id_attribute, quantity, minimal_quantity, price, price_display)
{
	// Create an associative array with the data for this combination.
	element = new Array();
	element['id_combination'] = id_combination;
	// slice() causes the array to be copied.  We do not just want to copy a
	// pointer to the array because our copy of the array is modified below.
	element['array_id_attribute'] = array_id_attribute.slice();
	element['quantity'] = quantity;
	element['minimal_quantity'] = minimal_quantity;
	element['price'] = price;
	element['price_display'] = price_display;

	// Sort the array of id_attribute values.  This will make lookups faster.
	element['array_id_attribute'].sort(pla_sortNumeric);

	// Associate this associative array with the product in the overall plac
	// array.
	if (plac[id_product] == null)
	{
		plac[id_product] = new Array();
	}
	plac[id_product].push(element);
}

function pla_lookupProductCombination(id_product, array_id_attribute)
{
	combination = false;

	if (plac[id_product] instanceof Array)
	{
		// Sort the array_id_attribute parameter.
		local_array_id_attribute = array_id_attribute.slice();
		local_array_id_attribute.sort(pla_sortNumeric);

		// Outer loop over all combinations for this product.
		for (i = 0; i < plac[id_product].length; i++)
		{
			// If the combination doesn't have the same number of attributes as
			// we are looking for, ignore it.
			if (local_array_id_attribute.length == plac[id_product][i]['array_id_attribute'].length)
			{
				// All elements in array_id_attribute must exist in this
				// combination for there to be a match.
				match = true;
				for (j = 0; j <= local_array_id_attribute.length; j++)
				{
					if (local_array_id_attribute[j] != plac[id_product][i]['array_id_attribute'][j])
					{
						// Any mismatch means we can ignore this combination.
						match = false;
						break;
					}
				}

				// If the combination matched, remember it.  Also, it is
				// unnecessary to look for more combinations in this case.
				if (match)
				{
					combination = new Array();
					combination['id_combination'] = plac[id_product][i]['id_combination'];
					combination['quantity'] = plac[id_product][i]['quantity'];
					combination['minimal_quantity'] = plac[id_product][i]['minimal_quantity'];
					combination['price'] = plac[id_product][i]['price'];
					combination['price_display'] = plac[id_product][i]['price_display'];
					break;
				}
			}
		}
	}
	else
	{
		combination = false;
	}

	return combination;
}

function pla_lookupProductAttributes(id_product)
{
	array_id_attribute = new Array();

	$('select[name^="pla_attribute_' + id_product + '_"]').each(function() {
		id_attribute = parseInt($(this).val());
		if (!isNaN(id_attribute) && (id_attribute >= 1))
		{
			array_id_attribute.push(id_attribute);
		}
	});

	return array_id_attribute;
}

function pla_catchAddEvent(event)
{
	// The value to return.
	return_value = false;
	rel = $(this).attr('rel');
	id_product = parseInt(rel.substr(16));
	if (!isNaN(id_product) && (id_product >= 1))
	{
		array_id_attribute = new Array();
		$("select[name^='pla_attribute_" + id_product + "_']").each(function() {
			id_attribute = parseInt($(this).val());
			if (!isNaN(id_attribute) && (id_attribute >= 1))
			{
				array_id_attribute.push(id_attribute);
			}
		});

		quantity = 1;
		$("input[name='pla_quantity_" + id_product + "']").each(function() {
			quantity_field = parseInt($(this).val());
			if (!isNaN(quantity_field) && (quantity_field >= 1))
			{
				quantity = quantity_field;
			}
		});

		if (array_id_attribute.length == 0)
			combination = null;
		else
			combination = pla_lookupProductCombination(id_product, array_id_attribute);

		if (combination === false)
		{
			// This is a failsafe because the customer should ideally be unable
			// to click the Add to Cart button at all while an invalid
			// combination is selected.
			alert(pla_status_no_combination);

			// Prevent the default action.  The default may be to add the item
			// to the cart.  Without a valid combination, this should be
			// suppressed.
			return_value = false;
		}
		else
		{
			if (combination === null)
				id_combination = null;
			else
				id_combination = combination['id_combination'];

			if (pla_ajax)
			{
				ajaxCart.add(id_product, id_combination, false, this, quantity);

				// Prevent the default action, which is likely to be a duplicate
				// addition of the item to the cart.
				return_value = false;
			}
			else
			{
				// Update the href to go to the combination.
				// Do nothing if href is undefined or a zero-length string.
				href = $(this).attr('href');
				if (href)
				{
					ipa_begin = href.indexOf('&ipa=');
					if (ipa_begin == -1)
						ipa_begin = href.indexOf('?ipa=');
					if (ipa_begin >= 0)
					{
						ipa_end = href.indexOf('&', ipa_begin + 5);
						new_href = href.substr(0, ipa_begin + 5) + ((id_combination === null) ? '' : id_combination);
						if (ipa_end >= 0)
							new_href += href.substr(ipa_end);
						href = new_href;
					}
					else
					{
						if ('&' == href.charAt(href.length - 1))
							href = href + 'ipa=';
						else
							href = href + '&ipa=';
						href = href + ((id_combination === null) ? '' : id_combination);
					}

					qty_begin = href.indexOf('&qty=');
					if (qty_begin == -1)
						qty_begin = href.indexOf('?qty=');
					if (qty_begin >= 0)
					{
						qty_end = href.indexOf('&', qty_begin + 5);
						new_href = href.substr(0, qty_begin + 5) + quantity;
						if (qty_end >= 0)
							new_href += href.substr(qty_end);
						href = new_href;
					}
					else
					{
						if ('&' == href.charAt(href.length - 1))
							href = href + 'qty=' + quantity;
						else
							href = href + '&qty=' + quantity;
					}
					$(this).attr('href', href);
				}
				return_value = true;
			}
		}
	}

	return return_value;
}

function pla_catchChangeAttributeEvent(pla_product)
{
	var name = '';
	if (pla_product.length)
		name = pla_product.attr('name');
	else
		name = $(this).attr('name');
	var name_id_values = name.substr(14);
	underscore_position = name_id_values.indexOf('_');
	if (underscore_position == -1)
		id_product = parseInt(name_id_values);
	else
		id_product = parseInt(name_id_values.substr(0, underscore_position));

	if (!isNaN(id_product) && (id_product >= 1))
	{
		array_id_attributes = pla_lookupProductAttributes(id_product);
	}
	combination = pla_lookupProductCombination(id_product, array_id_attribute);

	if (combination['quantity'] == 0 && pla_products_oos[id_product] == 0)
	{
		$('#pla_block_' + id_product+' .availability').html((pla_products_quantity[id_product] > 0) ? pla_status_oos_combination : pla_status_oos_product);
		$('#pla_block_' + id_product+' .pla_ajax_add_to_cart_button:visible').fadeOut(600);
	}
	else if (!combination)
	{
		$('#pla_block_' + id_product+' .availability').html(pla_status_no_combination);
		$('#pla_block_' + id_product+' .pla_ajax_add_to_cart_button:visible').fadeOut(600);
	}
	else
	{
		$('#pla_block_' + id_product+' .availability').html(pla_status_available);
		$('#pla_block_' + id_product+' .price').html(combination['price_display']);
		$('#pla_block_' + id_product+' .pla_quantity').attr('value', combination['minimal_quantity']);
		$('#pla_block_' + id_product+' .pla_ajax_add_to_cart_button:hidden').fadeIn(600);
	}

	return true;
}

