$(function()
{
	// checked element if it exists in the shopping cart
	if('' != subrion_cart)
	{
		var cartItems = subrion_cart.split(',');
		var ids_package = new Array();

		$("input[name^='product[]']").each(function()
		{
			var id = $(this).attr('id').split('_')[2];
			var id_package = $(this).parents("div.list").attr('id').split('_')[2];

			if(esyndicat.inArray(cartItems, id))
			{
				$(this).attr("checked", "checked");
				
				if(!esyndicat.inArray(ids_package, id_package))
				{
					ids_package.push(id_package);
				}
			}
		});

		for(var j = 0; j < ids_package.length; j++)
		{
			$("#chk_pck_" + ids_package[j]).attr("checked", "checked");

			var item = $("#prd_box_" + ids_package[j] + " input[class='exist']");

			if(item.length > 0)
			{
				item.removeAttr("disabled");
			}
		}
	}
	
	// click event for packages
	// display subitems if package is checked
	$("input[name='package[]']").each(function()
	{
		var id_package = $(this).attr("id").split("_")[2];

		if($(this).attr("checked"))
		{
			esyndicat.display($("#prd_box_" + id_package), 'show');
		}

		$(this).click(function()
		{
			var action = $(this).attr('checked') ? 'show' : 'hide';
			
			if ('hide' == action)
			{
				$("#prd_box_" + id_package + " input:checked").each(function()
				{
					$(this).attr("checked", "");
				});
			}
			esyndicat.display($("#prd_box_" + id_package), action);
		});
	});

	// click events for subitems
	// if there is no any checked subitems then disable the additional element
	$("input[name='product[]'][class='new']").each(function()
	{
		$(this).click(function()
		{
			var id_product = $(this).parents("div.list").attr("id").split("_")[2];
			var item = $("#prd_box_" + id_product + " input[class='exist']");

			if(item.length > 0)
			{
				if($(this).attr("checked"))
				{
					item.attr("disabled", "");
				}
				else
				{
					var action = anyChecked(id_product) ? "" : "disabled";

					item.attr("disabled", action);
					
					if(item.attr("checked") && 'disabled' == action)
					{
						item.attr("checked", "");
					}
				}
			}
		});
	});

	// checked there are any checked elements in the package group 
	function anyChecked(id_product)
	{
		var checked = false;
		
		$("#prd_box_" + id_product + " input[class='new']").each(function()
		{
			if($(this).attr("checked"))
			{
				checked = true;
			}
		});

		return checked;
	}
});

