
$.cmfWidgetFormDoctrineNested = function (container_id, input_id, css_selector, url_action, max_depth, loader) {

	var input_src = $('#' + input_id);
	
	input_src.css('display', 'none');
	
	var _container = '<div id="' + container_id + '" class="nested_js"></div>';
	
	input_src.after(_container);
	var _main_container = $('#' + container_id);

	var _dynamic_zone = '<div class="dynamic_zone" style="float:left"></div>';
	_main_container.append(_dynamic_zone);

	var _loader_img = '<div class="loader" style="display:none;float:left;padding:3px 4px;"><img src="'+ loader + '" alt="" /></div>';

	_main_container.append(_loader_img);
	var loader_img = _main_container.children('.loader');

	var _addEventChange = function(selector, index)
	{
		selector.change(function() {
			obj = $(this);

			if(!isNaN(obj.val()) && obj.val() != '' && obj.val() != 0){
				input_src.val(obj.val());

				if(0 == max_depth || index < max_depth) {

					_killNextContainers(index);
					loader_img.css('display', 'block');

					$.post(url_action, { location_id: obj.val() },
							function(data){
						loader_img.css('display', 'none');
						data = jQuery.trim( data );
						if(data != '')
						{
							var selector = _buildSelector(index + 1);
							selector.html(data);
							selector.attr('disabled', '');
						}
					});
				}
			}else{				if(index==1)				{					input_src.val(null);				}else				{					previous = index-1;					input_src.val($("#"+input_id+"_"+previous).val());				}							}
		});
	};

	var _killNextContainers = function(start_index)
	{
		var selectors = $('#' + container_id + ' .dynamic_zone').children('select');

		for(i = selectors.length - 1; i > start_index - 1; i -- ) {
			$(selectors[i]).remove();
		}
	};

	var _buildSelector = function(index)
	{
		var id = input_id + '_' + index;
		var name = container_id + '[' + index + ']';

		var selector = '<select id="' + id +'" name="' + name + '" ></select>';

		$('#' + container_id + ' .dynamic_zone').append(selector);

		selector = $('#' + id);

		_addEventChange(selector, index);

		return selector;
	};

	var _buildInitialSelectors = function(values, last_selected_option)
	{
		var selected_option = undefined == last_selected_option ? 0 : last_selected_option + 1;

		var tree_options_id = undefined == last_selected_option ? 0 : values[last_selected_option];
		
		if((0 == max_depth || selected_option < max_depth) && values.length+1 > selected_option) {


			var index = selected_option + 1;

			var id = input_id + '_' + index;
			var name = container_id + '[' + index + ']';

			var selector = '<select id="' + id +'" name="' + name + '" ></select>';

			loader_img.css('display', 'block');

			$.post(url_action, { location_id: tree_options_id },
					function(data){
				data = jQuery.trim( data );

				if(data != '')
				{
					$('#' + container_id + ' .dynamic_zone').append(selector);

					selector = $('#' + id);

					loader_img.css('display', 'none');
					selector.html(data);
					selector.attr('disabled', '');

					_option = values[selected_option];

					selector.val(_option);
					//$('#' + id + " option[selected]").removeAttr("selected");
					$('#' + id + " option[value='"+ _option + "']").attr("selected", "selected");

					_addEventChange(selector, index);

					_buildInitialSelectors(values, selected_option);
				}
			});
		}
	};

	var _buildInitialSelector = function()
	{
		var index = 1;

		var id = input_id + '_' + index;
		var name = container_id + '[' + index + ']';

		var selector = '<select id="' + id +'" name="' + name + '" ></select>';

		loader_img.css('display', 'block');

		$.post(url_action, { location_id: 0 },
				function(data){
			data = jQuery.trim( data );

			if(data != '')
			{
				$('#' + container_id + ' .dynamic_zone').append(selector);

				selector = $('#' + id);

				loader_img.css('display', 'none');
				selector.html(data);
				selector.attr('disabled', '');

				_addEventChange(selector, index);
			}
		});

	};

	return {

		init: function(values){

		if(values.length > 0) 
			_buildInitialSelectors(values);			
		else
			_buildInitialSelector();

	}
	};
};


