function loadDiv(sender, callback) {
	
	var url = sender["url"];
	var target = sender["target"];
	
	$j.ajax({ url : url,  success : function (response) {
		$j("#"+target).html(response);
		callback();
	}});	
}

function listItemClick(sender) {
	
	var list = $j(sender).parent();
	
	var listItems = $j("li", list);
	
	listItems.each( function (i) {
		if (this == sender)
			this.className = "active";
		else 
			if (this.className == "active") this.className = "";
	});
}


function initEditor() {
	
	var textEditors = $j("textarea", "#editor-list");
	
	textEditors.each( function(i) {
		if (i == 0) {
			 $j(this).cleditor({
				  width: 650,
				  height: 500
			 }).focus();
		}
		else {
			$j(this).cleditor({
				  width: 650,
				  height: 500
			 });	
		}
	});	
}

function deleteProject(projectId) {	
	
	$j.ajax({ type : 'POST', 
		  url : '/project/delete',
		  data : { projectId : projectId },
		  success : function (response) {
			  if (response.length > 0)
				  $j("#formMessage").html(response);
			  else				  
				  window.location.reload();
		  }
	});
	
}

function saveContent(sender, contentId) {

	var parent = $j(sender).parent();
	var btnSave = $j(sender).parent().html();		
	$j(parent).html("<div class='box-5'><img src='/images/ajax-loader.gif' /></div>");
	
	var content = $j("#contentEditor"+contentId).val();
	
	$j.ajax({ type : 'POST', 
			  url : '/project/savecontent',
			  data : { contentId : contentId, content: content },
			  success : function () { 
				  $j(parent).html(btnSave);			  
			  }
	});
}


function deleteContent(contentId) {	
	
	$j.ajax({ type : 'POST', 
		  url : '/project/deletecontent',
		  data : { contentId : contentId },
		  success : function (response) {
			  if (response.length > 0)
				  $j("#formMessage").html(response);
			  else				  
				  window.location.reload();
		  }
	});
	
}

function addTags(contentId, projectId) {
	var tag = $j("#tagName").val();
	tag = $j.trim(tag);
	if (tag == "") {
		$j("#formMessage").html("Tag name cannot be an empty string.");
		return;
	}
	
	$j.ajax({ type : 'POST', 
		  url : '/project/addtags',
		  data : { contentId : contentId, projectId : projectId, tag : tag },
		  success : function (response) { 
			 
			  if (response.length > 0)
				  $j("#formMessage").html(response);
			  else
				  $j("#taglink"+contentId).trigger('click');
		  }
	});	
}

function deleteTag(sender, tagId, contentId) {
	
	$j(sender).parent().hide();
	
	$j.ajax({ type : 'POST', 
		  url : '/project/deletetag',
		  data : { tagId : tagId, contentId : contentId },
		  success : function (response) { 
			  if (response == 0)
				  $j(sender).parent().show();			  
		  }
	});
	
}


//updates list item name in left nav menu after ajax update from jeditable
function updateListName(value, settings) {

	var listItem = $j("#list-item"+this.id, $j("#content-list"));
	var listItemName = $j("#name", listItem);	
	
	if (value.length > 20)
		value = value.substring(0, 20) + '...';
	
	listItemName.html(value);
}





