
//var attribute_blocks = new Array();
var matches = new Array();
var validators = new Array();

var error_elements = new Array();


$(document).ready(function() {

	// Auswahl um Textfelder erweitern
	// aber nur, wenn es auch eine Auswahl gibt
	if ($(".attr_selector .attr_wrap").length)
	{

		$(".attr_selector").append('<div class="clearBoth customoption_header">Wünschen Sie Muster von Linien, die hier nicht aufgeführt sind, tragen Sie die Namen der Linien bitte hier einzeln ein:</div>');

		for (var i = 0; i < 3; i++)
		{
			$(".attr_selector").append('<div class="attr_wrap customoption customoption_' + i + '"><input type="checkbox" value="" class="checkbox" id="customoption_' + i + '" name="tx_com_multitool_attrselect[Designlinien][]"> <input class="input" type="text" value="" /></div>');
		}

		var padd = 0;
		$('.customoption').each(function(index, element) {
			$('.input', element).keyup(function() {

				var value = $(this).val();
				var checkbox = $('.checkbox', element);

				if (value != '')
				{
					checkbox.attr('checked', true);
					checkbox.val(value);
				}
				else
				{
					checkbox.attr('checked', false);
				}

				validate_attribute();
			});
		});

		$(".attr_selector").append('<div class="clearBoth"></div>');
	}




	// Formelemente finden
	$("input").each(function(i)
	{

		if (this.name.indexOf('com_validator_') > -1)
		{
			// Validator-Input-Element gefunden!

			var $params = this.value.split(' ');

			validators.push($params);
		}
		else if (this.name.indexOf('tx_com_multitool_attrselect') > -1)
		{

			var found = false;
			//for (var i in matches)
			for (var i = 0; i < matches.length; i++)
			{
				if (matches[i].name == this.name)
				{
					found = true;
					break;
				}
			}
			if (!found)
			{
				matches.push(this);
			}
		}
	});

	validate_attribute();

	$('input :submit').click(function() {
		validate_attribute();
	});


	$('.attr_wrap input:checkbox').click(function() {
		validate_attribute();
	});


	$("form").submit(function () {
		return validate_attribute();
	});

	function validate_attribute()
	{
		var hits = new Object();



		$('input').each(function(i)
		{
			if (this.checked)
			{
				if (hits[this.name])
				{
					hits[this.name] ++;
				}
				else
				{
					hits[this.name] = 1;
				}
			}
		});

		//for (var i in matches)
		for (var i = 0; i < matches.length; i++)
		{
			if (matches[i].name.indexOf('tx_com_multitool_attrselect') > -1)
			{
				//alert(i.name + " " + hits[i.name]);

				// pr�fen, ob Wert mit dem des Validators �bereinstimmt

				var attr_title = matches[i].name.substr(28);
				attr_title = attr_title.substring(0, attr_title.indexOf(']'));

				//for (var v in validators)
				for (var j = 0; j < validators.length; j++)
				{

					var v = validators[j];
					//alert("_" + v[0] + "_  _" + attr_title + "_; " + (v[0] == attr_title));

					if (v[0] == attr_title)
					{
						//alert(hits[matches[i].name]);

						if (v[2] != hits[matches[i].name])
						{
							//$('.attr_selector_count_error').fadeIn("slow");

							showErrorMessage(v);

							$('.item-info input').removeAttr("disabled");

							return false;
						}
						else
						{
							hideErrorMessage(v);

							$('input:checkbox').attr("disabled", "disabled");
							$('input:checked').removeAttr("disabled");

							$('.customoption').each(function() {
								if (!$('.checkbox', this).attr('checked'))
								{
									$('.input', this).attr("disabled", "disabled");
								}

							});


							/*
							$('.attr_selector_count_error').fadeOut("slow", function()
									{
										$(this).hide();
									}
							);*/
							return true;
						}
					}
					else
					{
						alert("unbekannt");
					}
				}

			}

		};

		return true;
	}

	function showErrorMessage($validator)
	{

		var found = false;
		for (var i = 0; i < error_elements.length; i++)
		{
			if (error_elements[i] == $validator[0])
			{
				found = true;
				break;
			}
		}

		if (!found)
		{
			var anchor = "[name='com_validator_" + $validator[0] + "']";
			//alert($(anchor).data);
			var proto = $('.attr_selector_validator_prototype');

			//alert(proto.data);
			var message = proto.clone(true).insertAfter(anchor).addClass('validator_errormsg_' + $validator[0]).removeClass('attr_selector_validator_prototype');


			// Elementeigenschaften setzen
			$('.attr_selector_validator_title', message).html($validator[0]);
			$('.attr_selector_validator_count', message).html($validator[2]);


			error_elements.push($validator[0]);

		}
		/*
		else
		{
			alert('gibts schon');
		}
		*/

		var id = ".validator_errormsg_" + $validator[0];
		$(id).fadeIn("slow");
	}

	function hideErrorMessage($validator)
	{
		var found = false;
		for (var i = 0; i < error_elements.length; i++)
		{
			if (error_elements[i] == $validator[0])
			{
				found = true;
				break;
			}
		}

		if (found)
		{
			var id = ".validator_errormsg_" + $validator[0];
			$(id).fadeOut("slow", function()
				{
					$(this).hide();
				});
		}
	}


	var inputqty = $('.com-input-qty').val();
	if (!inputqty || trim(inputqty) == '')
	{
		$('.com-input-qty').val('1');
	}

	function trim(str, chars) {
		return ltrim(rtrim(str, chars), chars);
	}

	function ltrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	}

	function rtrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}
});

