kr.co.uhsoft.framework.Util.defineClass("kr.co.uhsoft.CateManager",function(){
	this.maxTopLength=5;
 	this.maxSubLength=6;
 	this.maxDepth=5;
	this.idName="Item";
});// end of class CateManager

kr.co.uhsoft.CateManager.prototype.setIdName=function (id){
	if(id){
		this.idName=id;
	}
}

kr.co.uhsoft.CateManager.prototype.checkTopLength=function (topId){
	var size=$("#"+topId).children("li").length;
 			
	if(size>=this.maxTopLength){
 		return false;
 	}else{
 		return true;
 	}	
};

kr.co.uhsoft.CateManager.prototype.checkSubLength=function (parentId){
	var size=$("#"+parentId+" > ul").children("li").length;
 			
 	if(size>=this.maxSubLength){
 		return false;
 	}else{
 		return true;
 	}	
};
 		
kr.co.uhsoft.CateManager.prototype.checkUpDropLength=function (upId){
	var size=$("#"+upId).parent("ul").children("li").length;
 	var maxSize=this.maxSubLength;
 	if( $("#"+upId).parent("ul").attr("id") == "MenuRoot" ){
 		maxSize=this.maxTopLength;
 	}
 					
 	if(size>=maxSize){
 		return false;
 	}else{
 		return true;
 	}	
};
 		
kr.co.uhsoft.CateManager.prototype.checkSubDepth=function (dropId){
	var length=$("#"+dropId).parents("ul").length;
 	if(length>=this.maxDepth){
 		return false;
 	}else{
 		return true;
 	}
};

kr.co.uhsoft.CateManager.prototype.checkDropDepth=function(dropId,dragId,inc){
	var dropSize=$("#"+dropId).parents("ul").length;
 	var dragTotal=$("#"+dragId+" li:last-child").parents("ul").length;
 	if(dragTotal<1){
 		dragTotal=$("#"+dragId).parents("ul").length;
 	}else{
 		dragTotal -=$("#"+dragId).parents("ul").length;
 	}
 			
 	var size =dropSize +dragTotal +inc; 
 	if(size>this.maxDepth){
 		return false;
 	}else{
 		return true;
 	}
}; 	

kr.co.uhsoft.CateManager.prototype.showEditMenuForm=function(id){
	if(!id){
 		alert("id ins not defined");
 		return;
 	}
 	$('#MenuForm').resetForm();
 	$('#MenuFormParentId').val(id);
 	$('#MenuFormCmd').val("edit");
 	$('#MenuFormTitle').val($("#"+id+"_value").attr("title"));
	$('#MenuFormContainer').jqmShow();
}; 		

kr.co.uhsoft.CateManager.prototype.editMenuForm=function(){
	var id = $('#MenuFormParentId').val();
	var item=this.getEditItemHtml(id);
	$('#'+id+"_value").remove();
	$("#" + id).prepend(item);
 	this.setDrag(id);
 			
 	this.hideMenuForm();
};
		
kr.co.uhsoft.CateManager.prototype.showMenuForm=function(id){
	$('#MenuForm').resetForm();
 	if(id){
 		$('#MenuFormParentId').val(id);
 	}else{
 		$('#MenuFormParentId').val("");
 	}
 	$('#MenuFormCmd').val("add");
	$('#MenuFormContainer').jqmShow();
};
		
kr.co.uhsoft.CateManager.prototype.hideMenuForm=function(){
	$('#MenuFormContainer').jqmHide();
};

kr.co.uhsoft.CateManager.prototype.addMenuItem=function(){
	var parentId=$("#MenuFormParentId").val();
	if(parentId && ""!=parentId){
		this.addSubItem(parentId);
	}else{
		this.addTopItem();
	}
			
	this.hideMenuForm();
};		

kr.co.uhsoft.CateManager.prototype.getItemHtml=function(newId){
	var item='<li id="'+newId+'" >'+this.getEditItemHtml(newId)+'</li>';
	return item;
};

kr.co.uhsoft.CateManager.prototype.getEditItemHtml=function(newId){
	var title=$("#MenuFormTitle").val();	
	var linkType=$("#MenuFormLinkType").val();
	var link=$("#MenuFormLink").val();
	var appid=$("#MenuFormAppid").val();
	if(!appid || ""==appid){
		appid=newId;
	}
	
	var icon=$("#MenuFormIcon").val();
	var needed="false";
	if(document.getElementById("MenuFormNeeded").checked){
		needed=$("#MenuFormNeeded").val();
	}
			
	var item='<div id="'+newId+'_value" ' 
		+'title="'+title + '" '
		+'linkType="'+linkType + '" '
		+'link="'+link + '" '
		+'appid="'+appid + '" '
		+'icon="'+icon + '" '
		+'needed="'+needed + '" '
		+' >' 
		+'<span>'
		+'<span id="'+newId+'_title" class="menuDrag menuDrop" parentId="'+newId+'">'
		+title+'('+newId+')</span>'
		+'<span id="'+newId+'_up" class="menuDrop" parentId="'+newId+'">[UPSIDE]</span>'
		+'<input type="button" value="Add Sub" onclick="cateManager.showMenuForm(\''+newId+'\')" />'
		+'<input type="button" value="Edit" onclick="cateManager.showEditMenuForm(\''+newId+'\')" />'
		+'<input type="button" value="Remove" onclick="cateManager.removeItem(\''+newId+'\')" />'
		+'</span>'
		+'</div>';
	
	return item;
};	

kr.co.uhsoft.CateManager.prototype.getSpanHtml=function(newId,title){
			
	var item='<span>'
		+'<span id="'+newId+'_title" class="menuDrag menuDrop" parentId="'+newId+'">'
		+title+'('+newId+')</span>'
		+'<span id="'+newId+'_up" class="menuDrop" parentId="'+newId+'">[UPSIDE]</span>'
		+'<input type="button" value="Add Sub" onclick="cateManager.showMenuForm(\''+newId+'\')" />'
		+'<input type="button" value="Edit" onclick="cateManager.showEditMenuForm(\''+newId+'\')" />'
		+'<input type="button" value="Remove" onclick="cateManager.removeItem(\''+newId+'\')" />'
		+'</span>';
	
	return item;
};		

kr.co.uhsoft.CateManager.prototype.getIdName=function(){
	var id = document.getElementById("MenuFormId");
	if( id && "" != $("#MenuFormId").val() ){
		return $("#MenuFormId").val();
	} 
	
	var number=$("#MenuRoot").attr("lastNum");
	var nextNum=parseInt(number)+1;
	var newId=this.idName+"_"+nextNum;
	$("#MenuRoot").attr("lastNum",nextNum);
	return newId;
}


kr.co.uhsoft.CateManager.prototype.addTopItem=function(){
	if(!document.getElementById("MenuRoot")){
		$("#CateMenuEdit").append('<ul id="MenuRoot" lastnum="0"></ul>');
	}
	
	if(!this.checkTopLength("MenuRoot")){
		alert(" Top Item is Full ");
		return;
	}
			
	var newId=this.getIdName();
			
	var item=this.getItemHtml(newId);
	$("#MenuRoot").append(item);
	
	this.setDrag(newId);
};

kr.co.uhsoft.CateManager.prototype.addSubItem=function(parentId){
	if(!this.checkSubDepth(parentId)){
		alert(" SubItem is Full ");
		return;
	}
			
	if(!this.checkSubLength(parentId)){
		alert(" SubItem is Full ");
		return;
	}
			
	var newId=this.getIdName();
	var size=$("#" + parentId + " > ul").size();
	if(size>0){
		var item=this.getItemHtml(newId);
		$("#" + parentId + " > ul").append(item);
	}else{
		var item='<ul>'+this.getItemHtml(newId)+'</ul>';
		$("#" + parentId).append(item);
	}
			
	this.setDrag(newId);
};
		
kr.co.uhsoft.CateManager.prototype.menuDragDrop=function(dropElem,dragElem){
	var dropId=$(dropElem).attr("id");
	var size=$("#" + dropId + " > ul").size();
	if(size<1){
		var item='<ul></ul>';
		$("#" + dropId).append(item);
	}
			
	$("#" + dropId + " > ul").append(dragElem);
};
		
kr.co.uhsoft.CateManager.prototype.checkDragDrop=function(drop,drag){
	var flag=false;
	var dropParentId=$(drop).attr("parentId");
	var dropElem=document.getElementById(dropParentId);
						
	var dragParentId=$(drag).attr("parentId");
	var dragElem=document.getElementById(dragParentId);
						
	if(dropElem == dragElem){ 
		return false;
	}
						
	$("#"+dropParentId).parents("li").each(function(){
   		if(this == dragElem){
   			flag=true;
   			return;
   		}						
 	});
						
	if(flag){
		return false;
	}
	
	return true;
};
		
kr.co.uhsoft.CateManager.prototype.removeItem=function(id){
	var size=$("#" + id + " > ul > li").size();
	if(size>0){
		alert(" Not Delete Because subItem is exist ");
	}else{
		var size=$("#" + id).siblings().length;
		if(size<1 && "MenuRoot" != $("#" + id).parent("ul").attr("id") ){
			$("#" + id).parent("ul").remove();
		}else{
			$("#" + id).remove();
		}
	}
};	

kr.co.uhsoft.CateManager.prototype.setDrag=function(id){
	$('#'+id+"_title").Draggable({
		zIndex: 	1000,
		ghosting:	true,
		revert:  	true,
		opacity: 	0.7
	});
			
	$('#'+id+"_title").Droppable({
		accept : 'menuDrag', 
		hoverclass:	'menuDropHover',
		ondrop:	function (drag) {
			try{
				var dropParentId=$(this).attr("parentId");
				var dropElem=document.getElementById(dropParentId);
						
				var dragParentId=$(drag).attr("parentId");
				var dragElem=document.getElementById(dragParentId);
						
				if(!cateManager.checkDropDepth(dropParentId,dragParentId,0)){
					alert(" SubItem is Full");
					return;
				}
						
				if(!cateManager.checkSubLength( dropParentId)){
					alert(" SubItem is Full");
					return;
				}
						
				if(cateManager.checkDragDrop(this,drag)  ){
					cateManager.menuDragDrop(dropElem,dragElem);
				}
						
			}catch(error){
				alert("Error:"+error);
			}
		},
		fit: true
	});
			
	$('#'+id+"_up").Droppable({
		accept : 'menuDrag', 
		hoverclass:	'menuDropHover',
		ondrop:	function (drag) {
			try{
				var dropParentId=$(this).attr("parentId");
				var dropElem=document.getElementById(dropParentId);
						
				var dragParentId=$(drag).attr("parentId");
				var dragElem=document.getElementById(dragParentId);	
						
				if(!cateManager.checkDropDepth(dropParentId,dragParentId,-1)){
					alert(" SubItem is Full");
					return;
				}
					
				if(!cateManager.checkUpDropLength( dropParentId)){
					alert(" SubItem is Full");
					return;
				}
						
				if(cateManager.checkDragDrop(this,drag)){
					$(dropElem).before(dragElem);
				}
			}catch(error){
				alert("Error:"+error);
			}
		},
		fit: true
	});
			
};

kr.co.uhsoft.CateManager.prototype.stringToEditItem=function(menuHtml){
	$("#CateMenuEdit").append(menuHtml);
	$('li',"#CateMenuEdit" ).each(function (){
 		var id = $(this).attr("id");
 		var valueId=id+"_value";
 		var title=$("#"+valueId).attr("title");
 		var item=cateManager.getSpanHtml(id,title);
		$("#"+valueId).prepend(item);
		cateManager.setDrag(id);
 	});
	
};

kr.co.uhsoft.CateManager.prototype.getCateMenu=function(){
	var menu=$('#CateMenuEdit').clone();
	$('span',menu ).remove();
	return $(menu).html();
};

// end of CateManager class 

//Singleton Method
function getCateManager(){
	if(!window.cateManager){
		window.cateManager=new kr.co.uhsoft.CateManager();
	}
	
	return window.cateManager;
}

//create commandFactory
getCateManager();	

