﻿
	$().ready(function(){


		initTree();
	});

	

	

	

	function initTree()

	{
		$('a.hide').each(function(){
			$(this).unbind("click");
			$(this).click(function(){
				hideItem(this);
				return false;
			});
		});
		
		
		$('a.show').each(function(){
			$(this).unbind("click");
			$(this).click(function(){
				showItem(this); 
				return false;
			});
		});
		
		
		
		
		$('a.trash').each(function(){
			$(this).unbind("click");
			$(this).click(function(){
				trashItem(this);
				return false;
			});
		});
		
		
		$('a.restore').each(function(){
			$(this).unbind("click");
			$(this).click(function(){
				restoreItem(this); 
				return false;
			});
		});
		
		
		
		
		$('a.showTrash').each(function(){
			$(this).unbind("click");
			$(this).click(function(){
				showTrash();
				return false;
			});
		});
		
		$('a.hideTrash').each(function(){
			$(this).unbind("click");
			$(this).click(function(){
				showTrash(0);
				return false;
			});
		});
		
		
		
		
		$('a.delete').each(function(){
			$(this).unbind("click");
			$(this).click(function(){
				deleteItem(this);
				return false;
			});
		});
		
		
		





		$('a.sub').each(function(){ 

		

			$(this).unbind("click");
			$(this).click(function(){ 
				addSubItem(this); 
				return false;
			});
		});


		$('a.pre').each(function(){ 

		

			$(this).unbind("click");
			$(this).click(function(){ 
				addItemBefore(this); 
				return false;
			});
		});

		$('a.move').each(function(){ 

		

			$(this).unbind("click");
			$(this).html("[move]");
			$(this).click(function(){ 
				moveItem(this); 
				return false;
			});
		});





		$('a.pre, a.sub').each(function(){ 
			$(this).html("[+]");
		});

		

		

		$('.movingItem').each(function(){ 
			$(this).removeClass("movingItem");
		});

		

		

	}

	

	

	

	
function showTrash(show)
{
	if (show==0)
	{
		$("#tree").removeClass("trashVisible");
	}
	else
	{
		$("#tree").addClass("trashVisible");
	
	}
}
	

	
	function moveItem(item){
		var id = $(item).parent().attr("id");

		

		

		$(item).parent().addClass("movingItem");

		//alert("move : "+id);

		
		$(item).html("[dont_move]");

		

		

		$(item).unbind("click");

		$(item).click(function(){ 

				//alert("dontmove : "+id);

				

				

				initTree();
				return false;
			});

			

			

		

		$('a.pre, a.sub').each(function(){ 
			$(this).unbind("click");
			$(this).html("[move_here]");
		});

		

		
		$('a.pre').click(function(){ 
			var toId = $(this).parent().attr("id");

			moveClick(id, toId, "pre");
			return false;
		});

		

		
		$('a.sub').click(function(){ 

		
			var toId = $(this).parent().attr("id");

			moveClick(id, toId, "sub");
			return false;
		});

		
	}

	

	

	function moveClick(id, toId, mode)

	{	

		//alert("move  "+id+" , "+mode+" , to "+toId);

		

		
		$.ajax({ 
			url:'/aj/tree/', 
			type: 'get', 
			data:{'func':'moveItem','id':id, 'mode':mode, 'toId':toId}, 
			success:function(msg){ 

				$("#tree").html(msg);

				initTree();
				return false;
			}
		});
		return false;

	}

	

	

	

	
	function addItemBefore(item){
		var id = $(item).parent().attr("id");

		

		//alert("addItemBefore id : "+id);

	

		if (id)

		{
			$.ajax({ 
				url:'/aj/tree/', 
				type: 'get', 
				data:{'func':'addItemBefore','id':id}, 
				success:function(msg){ 

					//alert("msg: "+msg); 

					

					$("#tree").html(msg);

					initTree();
					return false;
				}
			});

		}

		else

		{

		

		}

		

	
	}

	

	
	function addSubItem(item){
		var id = $(item).parent().attr("id");

		

		//alert("addSubItem id : "+id);

	
			//alert("addSubItem: "+id); 

		if (id)

		{
		
			$.ajax({ 
				url:'/aj/tree/', 
				type: 'get', 
				data:{'func':'addSubItem','id':id}, 
				success:function(msg){ 

					//alert("msg: "+msg); 

					$("#tree").html(msg);

					initTree();
					return false;
				}
			});

		}

		else

		{

		

		}

	
	}

	
	function trashItem(item){
		var id = $(item).parent().attr("id");
		alert("trashItem: "+id); 


		if (id)
		{
			$.ajax({ 
				url:'/aj/tree/', 
				type: 'get', 
				data:{'func':'trashItem','id':id}, 
				success:function(msg){ 
					$(item).parent().remove();
					$("#tree").html(msg);
					initTree();
					return false;
				}
			});

		}
	}



	function restoreItem(item){
		var id = $(item).parent().attr("id");

		if (id)
		{
			$.ajax({ 
				url:'/aj/tree/', 
				type: 'get', 
				data:{'func':'restoreItem','id':id}, 
				success:function(msg){
					$("#tree").html(msg);
					initTree();
					return false;
				}
			});

		}
	}
	
	
	
	
	
	
	function hideItem(item){
		var id = $(item).parent().attr("id");
		

		if (id)
		{
			$.ajax({ 
				url:'/aj/tree/', 
				type: 'get', 
				data:{'func':'hideItem','id':id}, 
				success:function(msg){ 
					$(item).parent().remove();
					$("#tree").html(msg);
					initTree();
					return false;
				}
			});

		}
	}

	function showItem(item){
		var id = $(item).parent().attr("id");

		if (id)
		{
			$.ajax({ 
				url:'/aj/tree/', 
				type: 'get', 
				data:{'func':'showItem','id':id}, 
				success:function(msg){
					$("#tree").html(msg);
					initTree();
					return false;
				}
			});

		}
	}









	
	function deleteItem(item){
		var id = $(item).parent().attr("id");

		if (id)
		{
			$.ajax({ 
				url:'/aj/tree/', 
				type: 'get', 
				data:{'func':'deleteItem','id':id}, 
				success:function(msg){ 
					$(item).parent().remove();
					//$("#tree").html(msg);
					initTree();
					return false;
				}
			});

		}
	}

	
