$j = jQuery.noConflict();

formCount = 1;
maxForms = 10;
messageLength = 160;

$j(document).ready(function()
{
	// count current product forms
	formCount = $j("div.product").length;

	// assign jquery date picker to a form field
	$j("#my_date_required").datepicker({ dateFormat: 'dd/mm/yy', changeYear: true, changeMonth: true, yearRange: '2010:2030' });
	// assign jquery validation rules to the form
	$j("#my_form").validate();

	// get the size options for the first row
	sizeOption = $j("#product_type-1").val();
	sizeValue = sizeOption.split('|');
	$j("#product_size-1").load('includes/ajax.get_size_options.php?t=' + sizeValue[0]);

	// update the preferred supplier display order
	$j("#preferred_suppliers").sortable({
		placeholder: 'highlight',
		update : function () {
			order = $j('#preferred_suppliers').sortable('serialize');
			// save the serialized result to a hidden form field
			$j("#my_suppliers").attr("value",order);
		}
	});

	// set the initial preferred supplier display order
	order = $j('#preferred_suppliers').sortable('serialize');
	// and save the serialized result to a hidden form field
	$j("#my_suppliers").attr("value",order);

	// check the number of products - if 1, hide the remove product icon
	if(formCount == 1)
	{
		$j("div.remove_item:first").hide();
	}

	// fire the new product creation when clicking the new product button
	$j("div.new_product").click(function(e) {
		get_product_form();
	});

	// set up the form submission
	$j("div.submit").click(function(e) {
		$j("#my_form").submit();
	});

	// autoload sizes as the product type is changed
	$j("#product_type-1").change(function(e) {
		thisControl = $j(this).attr("id");
		thisValue = $j(this).val();
//		alert(thisControl + ' : ' + thisValue);
		splitControl = thisControl.split('-');
		splitValue = thisValue.split('|');
		$j("#product_size-" + splitControl[1]).load('includes/ajax.get_size_options.php?t=' + splitValue[0]);
	});

	// bind form reset function, called when form is cloned
	$j("div.product").bind("reset",function() {
//		$j(this).find("[name]").val('');	//reset fields
		$j(this).find("*").each(function() {
			name = $j(this).attr("name");
			if(name)
			{
				// split the name at its dynamic break point '-'
				splitName = name.split('-');
				// create the new input name by counting + 1
				newName = splitName[0] + "-" + formCount;
				// and set the name and id fields to the new name
				$j(this).attr("name",newName);
				$j(this).attr("id",newName);
				// reset all inputs except the product colour
				if(splitName[0] == "product_quantity")
				{
					$j(this).attr("value",'');
				}
				// set all select boxes back to defaults
				if(splitName[0] == "product_size" || splitName[0] == "product_embellishment" || splitName[0] == "product_type")
				{
					// everything else if just set to it's first value
					$j(this).attr("selectedIndex",0);

					// product size needs to load from the database
					if(splitName[0] == "product_size")
					{
						$j(this).load('includes/ajax.get_size_options.php?t=1');
					}
				}
			}
		});
	});

	// remove form link
	$j("div.remove_item").click(function() {
		$j(this).parent().parent().fadeOut('',function() {
			formCount--;
			$j(this).remove();
			if(formCount == 1)
			{
				// hide the remove icon if only 1 product item is displayed
				// to prevent all products from being removed (otherwise you
				// cannot clone 'nothing'!)
				$j("div.product:first").find("div.remove_item").hide();
			}
		});
	});
});

function get_product_form()
{
	if(formCount < maxForms)
	{
		// create a new product row based on the previous row
		$j("div.product:last").clone(true).appendTo("div.new_form_holder");
		$j("div.product:last div.remove_item").show();

		// display the new row
		$j("div.product:last").hide();
		$j("div.product:last").fadeIn();

		formCount++;
		$j("div.product:last").trigger("reset");

		if(formCount > 1)
		{
			// show the first row remove icon if there is more than one product
			$j("div.product:first").find("div.remove_item").show();
		}
	}
}

// serialize all of the form values for posting
function get_form_values()
{
	row = "";

	$j("div.product").each(function() {
		row = row + $j(this).find(":input").serialize();
		row = row + "|";
	});
	return row;
}
