$(document).ready(convertTrees);
$(document).ready(initContextMenu);

var openListClassName = 'liOpen'

function initContextMenu(){
	
	$('li').contextMenu('treeNodeMenu', {
			bindings:{
			'addPage':function(t){
				addPage(t);
			},
			'removePage':function(t){
				removePage(t);
			},   
			'editPage':function(t){
				displayPageForm(getIdFromNode(t));
			},
			'addContent':function(t){				
				displayContentForm(getIdFromNode(t));
			},
			'addBlog':function(t){
				addBlog(t);
			},
			'editBlog':function(t){
				displayBlogForm(getIdFromNode(t));
			},
			'removeBlog':function(t){
				removeBlog(t);
			},
			'addBlogEntry':function(t){
				displayBlogEntryForm(getIdFromNode(t));
			}
		}
	});
}

function addPage(t){
	var parentPageId = getIdFromNode(t);	
	
	PageManager.createPage(getIdFromNode(t), function(pageId){
		var input = document.createElement('INPUT');
		input.type = 'text';
		input.value = 'New Page';
		input.onblur = function(event){
			setPageName(event);
		};
		input.onkeypress = function(event){
			listenForReturn(event, 'page');
		}
		var li = document.createElement('LI');
	    li.appendChild(input);
		var foundUl = false;
		var ulIndex = -1;
		for(var i = 0; i < t.childNodes.length; i++){
			if(t.childNodes[i].tagName == 'UL'){
				foundUl = true;
				ulIndex = i;
			}
		}
	
		if(foundUl == true){
			//adding child to existing sublist... existing sublist needs to be an 'open node'
			li.id = pageId;
			t.childNodes[ulIndex].appendChild(li);			
		}else{
			li.id = pageId;
			//creating new sublist... parent now needs to be an 'open node', and so does 'ul'
			var ul = document.createElement('UL');			
			ul.appendChild(li);
			t.appendChild(ul);
			
		}		
		initContextMenu();		
		convertTrees();
		expandToItem('tree1', li.id);
		input.focus();
		input.select();
	});
}

function addBlog(t){
	var parentPageId = getIdFromNode(t);	
	
	BlogClient.createBlog(getIdFromNode(t), function(blogId){
		var input = document.createElement('INPUT');
		input.type = 'text';
		input.value = 'New Blog';
		input.onblur = function(event){
			setBlogName(event);
		};
		input.onkeypress = function(event){
			listenForReturn(event, 'blog');
		};
		var li = document.createElement('LI');
	    li.appendChild(input);
		var foundUl = false;
		var ulIndex = -1;
		for(var i = 0; i < t.childNodes.length; i++){
			if(t.childNodes[i].tagName == 'UL'){
				foundUl = true;
				ulIndex = i;
			}
		}
	
		if(foundUl == true){
			//adding child to existing sublist... existing sublist needs to be an 'open node'
			li.id = blogId;
			t.childNodes[ulIndex].appendChild(li);			
		}else{
			li.id = blogId;
			//creating new sublist... parent now needs to be an 'open node', and so does 'ul'
			var ul = document.createElement('UL');			
			ul.appendChild(li);
			t.appendChild(ul);
			
		}		
		initContextMenu();		
		convertTrees();
		expandToItem('tree1', li.id);
		input.focus();
		input.select();
	});
}

function removePage(t){
	var pageId = getIdFromNode(t);	
	
	PageManager.deletePage(pageId, function(t){
		var pageNode = document.getElementById(pageId);
		pageNode.parentNode.removeChild(pageNode);
	});	
}

function removeBlog(t){
	var blogId = getIdFromNode(t);	
	
	BlogClient.deleteBlog(blogId, function(t){
		var blogNode = document.getElementById(blogId);
		blogNode.parentNode.removeChild(blogNode);
	});	
}

function setPageName(element){	
	var _self = (element != null ? element : this);
	var value = _self.value;
	var parent = _self.parentNode;
	PageManager.updatePageName(parent.id, value, function(){
		var textNode = document.createTextNode(value);		
		parent.insertBefore(textNode, _self);
		parent.removeChild(_self);
		/*var addContent = confirm("Do you want to add content to this page?");
		if(addContent){
			displayContentForm(parent.id);
		}*/
	});
}

function setBlogName(element){	
	var _self = (element != null ? element : this);
	var value = _self.value;
	var parent = _self.parentNode;
	BlogClient.updateBlogName(parent.id, value, function(){
		var textNode = document.createTextNode(value);		
		parent.insertBefore(textNode, _self);
		parent.removeChild(_self);		
	});
}

function displayContentForm(pageId){
	window.location.replace('http://' + window.location.host + '/control/content?pageId=' + pageId + '&mode=Create&redirectPath=/control/page');	
}

function displayPageForm(pageId){
	window.location.replace('http://' + window.location.host + '/control/page?pageId=' + pageId + '&mode=Edit&redirectPath=/control/page');	
}

function displayBlogForm(blogId){
	window.location.replace('http://' + window.location.host + '/control/blog?blogId=' + blogId + '&mode=Edit&redirectPath=/control/page');	
}

function displayBlogEntryForm(blogId){
	window.location.replace('http://' + window.location.host + '/control/blogEntry?blogId=' + blogId + '&mode=Create&redirectPath=/control/page');	
}

function getIdFromNode(t){
	var id;

	if(t.id == null){
		id = t.parentNode.id;
	}else{
		id = t.id;
	}
	
	return id;
}


function listenForReturn(e, type){
	var keynum;

	// IE
	if(window.event){
  		keynum = window.event.keyCode;
  	}else if(e.which){
	 // Netscape/Firefox/Opera
  		keynum = e.which;
  	}

	if(keynum == 13){
		var element;
		if(window.event){
  			element = window.event.fromElement;
  		}else if(e.which){
		 // Netscape/Firefox/Opera
  			element = e.originalTarget;
  		}
		
		if(type == 'blog'){
			setBlogName(element);
		}else if(type == 'page'){
			setPageName(element);
		}
	}
}
