$('#{{$fieldName}}').parent().append('<div id="tree{{$fieldName}}"></div>');
$('#tree{{$fieldName}}').append('<div><input type="checkbox" id="treeroot{{$fieldName}}"/> This row will not have any parent</div>');
$('#tree{{$fieldName}}').append('<div id="actree{{$fieldName}}"></div>');
var html = $.ajax({
  url: "/system/treeview.html",
  data: "treeid={{$collection}}&eid={{$smarty.get.eid}}&frontendadmin=1",
  async: false
 }).responseText;
 
 $('#actree{{$fieldName}}').html(html);
 
 $("#actree{{$fieldName}} ul").treeview({
		persist: "location",
		collapsed: true,
		unique: true,
		toggle: function() {}
	});
	
	{{if $smarty.get.eid gt 0}}
		$("#{{$fieldName}}").removeOption("{{$smarty.get.eid}}");
	{{else}}
		$('#treeroot{{$fieldName}}').attr('checked', true);
	{{/if}}
	
	{{if $fieldValue eq ''}}$('#treeroot{{$fieldName}}').attr('checked', true);
	{{else}}
	$("#actree{{$fieldName}} ul a[itemid={{$fieldValue}}]").addClass('curentlyselected');
	{{/if}}
	
	$("#actree{{$fieldName}} ul a[itemid]").each(function(){
	
			var notselectable = $(this).attr('notselectable');
			if (notselectable == 1) {  $(this).addClass('notselectable'); }
	
			$(this).click(function(){
					var itemid 		  = $(this).attr('itemid');
					var notselectable = $(this).attr('notselectable');					
					if (notselectable == 0)
					{
						$("#actree{{$fieldName}} ul a[itemid]").removeClass('curentlyselected');
						$(this).addClass('curentlyselected');
						$('#{{$fieldName}}').selectOptions(itemid, true);
						$('#treeroot{{$fieldName}}').attr('checked', false);
					}
					else
					{
						alert('This row cannot be his own parent');
					}
			
			});
			
			$('#treeroot{{$fieldName}}').click(function(){
				$('#treeroot{{$fieldName}}').attr('checked', true);
				$("#actree{{$fieldName}} ul a[itemid]").removeClass('curentlyselected');
				$('#{{$fieldName}}').selectOptions("", true);
			});
			
			$("#{{$fieldName}}").change(function(){
			
			$("#actree{{$fieldName}} ul a[itemid]").removeClass('curentlyselected');
					var thisValue = this.value;
					
					if (thisValue == '')
					{
							$('#treeroot{{$fieldName}}').attr('checked', true);
					}
					else
					{
							$('#treeroot{{$fieldName}}').attr('checked', false);
							$("#actree{{$fieldName}} ul a[itemid="+thisValue+"]").addClass('curentlyselected');
					}
			});

			
	});