$(document).ready(function(){

	var products_id = $('input[name="products_id"]').val();
	
	var freitext_max = new Array();
	$('.charCounter').each(function(){
		freitext_max[$(this).attr('id')] = $(this).html();
	});

	function fadeContainer(freitext_id, func) {
		if (freitext_id.indexOf(',') == -1) {
			// only 1 Freitext
			id = $('#' + freitext_id).attr('name');
			id = id.substr(4);
				if (func == 'fadeIn') {
					$('#container-' + id).fadeIn();
				} else {
					var counter = freitext_max['charCounter[' + freitext_id + ']'];
					$('#container-' + id).hide();
					$('#' + freitext_id).val('');																		// reset input
					$('#charCounter\\[' + freitext_id + '\\]').html(counter);				// reset counter
				}
		} else {
			// more than 1 Freitext
			ids = freitext_id.split(',');
			for (i=0; i<ids.length; i++) {
				id = $('#' + ids[i]).attr('name');
				id = id.substr(4);
				if (func == 'fadeIn') {
					$('#container-' + id).fadeIn();
				} else {
					var counter = freitext_max['charCounter[' + ids[i] + ']'];
					$('#container-' + id).hide();
					$('#' + ids[i]).val('');																		// reset input
					$('#charCounter\\[' + ids[i] + '\\]').html(counter);				// reset counter
				}
			}
		}						
	}

	/*****  Reloaded Page -> Freitext missing => scroll to products attributes  *****/
	if ($('#ftm').val() == true) {
		$.scrollTo('.productoptions', 1000, {offset: -200});

		/*****  get all Freitext fields for this product  *****/
		$.getJSON('inc/xtc_get_freitext.php?pid=' + products_id, function(freitext_array) {
			$.each(freitext_array, function(i, freitext) {
				// highlight empty Freitext inputs
				var ftm_input = $('[name="txt_' + freitext.options_values_id + '"]');
				if (ftm_input.val() == '' || ftm_input.val() == '___') {
					ftm_input.val('');
					ftm_input.addClass('redborder');
					ftm_input.parent().prepend('<p class="redfont">Bitte Personalisierung eintragen</p>');
				}
				// remove highlight onclick
				ftm_input.click(function(){
					$(this).removeClass('redborder');
					$(this).parents().find('p.redfont').hide();
				});
			});
		});	// jQuery.getJSON END
	}

	// get all optional Freitext for this product from databse
	$.getJSON('inc/xtc_get_freitext.php?action=optional&pid=' + products_id, function(optional_freitext_array) {

		// get all switch Freitext for this product from databse
		$.getJSON('inc/xtc_get_freitext.php?action=switch&pid=' + products_id, function(switch_freitext_array) {

			// iterate through all switch Freitext entries
			if (switch_freitext_array != null) {
				$.each(switch_freitext_array, function(i, switch_freitext) {	
					if ($('select[name="id[' + switch_freitext.attribute_id + ']"]').val() == switch_freitext.attribute_option) {
						// show needed Freitext fields / hide unneeded Freitext fields
						fadeContainer(switch_freitext.show_freitext, 'fadeIn');
						fadeContainer(switch_freitext.hide_freitext, 'hide');
					} else {
						// hide unneeded Freitext fields / show needed Freitext fields
						fadeContainer(switch_freitext.show_freitext, 'hide');
						fadeContainer(switch_freitext.hide_freitext, 'fadeIn');
					}
				});
			}

			// iterate through all optional Freitext entries
			if (optional_freitext_array != null) {
				$.each(optional_freitext_array, function(i, optional_freitext) {
					if ($('select[name="id[' + optional_freitext.attribute_id + ']"]').val() == optional_freitext.attribute_option) {
						// hide unneeded Freitext fields
						fadeContainer(optional_freitext.hide_freitext, 'hide');
					}
				});
			}


			// iterate through all select-boxes
			$('select.event').each(function() {
				var attribute_id = $(this).attr('name');
						attribute_id = attribute_id.substr(3, attribute_id.length-4);
				var option_id = $(this).val();
	
				// selection onchange 
				$(this).change(function() {
					option_id = $(this).val();
	
					// iterate through all optional Freitext entries
					if (optional_freitext_array != null) {
						$.each(optional_freitext_array, function(i, optional_freitext) {
							if (optional_freitext.attribute_id == attribute_id) {
								freitext_id = optional_freitext;
								if (optional_freitext.attribute_option == option_id) {
									// hide unneeded Freitext fields
									fadeContainer(freitext_id.hide_freitext, 'fadeOut');
									return false;
								}	else {
									// show needed Freitext fields
									fadeContainer(freitext_id.hide_freitext, 'fadeIn');
								}
							}
						});
					}
	
					// iterate thorugh all switch Freitext entries
					if (switch_freitext_array != null) {
						$.each(switch_freitext_array, function(i, switch_freitext) {
							if ($('select[name="id[' + switch_freitext.attribute_id + ']"]').val() == switch_freitext.attribute_option) {
								// show needed Freitext fields / hide unneeded Freitext fields
								fadeContainer(switch_freitext.show_freitext, 'fadeIn');
								fadeContainer(switch_freitext.hide_freitext, 'hide');
							} else {
								// hide unneeded Freitext fields / show needed Freitext fields
								fadeContainer(switch_freitext.show_freitext, 'hide');
								fadeContainer(switch_freitext.hide_freitext, 'fadeIn');
							}
						});
					}

				});
			});


			// CharCounter
			$('input[name^="txt_"], textarea[name^="txt_"]').each(function() {
				var counter = $('#charCounter\\[' + $(this).attr('id') + '\\]');
				var maxLength = counter.text();

				// onchange
				$(this).keyup(function(){
					$(this).removeClass('redborder');
					if ($(this).val().length > maxLength) {
						$(this).addClass('redborder');
						$(this).val($(this).val().substring(0, maxLength));
					}
					counter.html(maxLength - $(this).val().length).stop(true, true).fadeTo('fast', 0.5, function() { $(this).fadeTo('fast', 1); });
				});
			});

		});
	});

});
