function CategoryContent()
{
	this.oAjaxThreads = new Array();
	this.loadingThreads = 0;
	this.temp = new Array();
	this.currentId = '';
	this.loadingId = new Array();
}

CategoryContent.prototype.getNextThread = function()
{
	var i = 0;
	
	if (this.oAjaxThreads.length == 0)
	{
		var oAjax = new AJAX();
		this.oAjaxThreads.push(oAjax);		
	}

	while (this.oAjaxThreads[i] && this.oAjaxThreads[i].working && i < this.oAjaxThreads.length )
	{
		i++;
	}
	
	if (!this.oAjaxThreads[i])
	{
		var oAjax = new AJAX();
		this.oAjaxThreads.push(oAjax);		
	}
	
	return this.oAjaxThreads[i];
}

CategoryContent.prototype.runLoad = function(id, category, mode, controller, view, screenName, andCategoryId, rootCategoryId)
{
	switch(category)
	{
		case 'basic':
			var overlayId = 'overlay_'+id;
			document.getElementById(overlayId).style.display = '';
		break;
		case 'help':
			var overlayId = 'overlay_'+id;
			document.getElementById(overlayId).style.display = '';
		break;
		case 'zspace_preferences_zcommpages':
			var overlayId = 'overlay_'+id;
			document.getElementById(overlayId).style.display = '';
		break;
		default :
			var controller = controller.toLowerCase();
			var oAjax = this.getNextThread();
			oAjax.url = '/'+controller+'/loadCategoryContent/';
			oAjax.data = 'mode='+mode+'&id='+id+'&name='+category+'&view='+view+'&screenName='+screenName+'&andCategoryId='+andCategoryId+'&rootCategoryId='+rootCategoryId;
			oAjax.method = 'post';
			oAjax.addRequestListener(this, this.loaded, false);
			this.currentId = id;
			oAjax.open();
		break;
	}
}

CategoryContent.prototype.loaded = function(xmlDocument, text)
{
	var p = document.getElementById('content_'+this.currentId);
	p.innerHTML = text;
	
	if(this.quickEdit)
	{
		$('#contentCol_1').sortable("destroy");
		oQuickEdit.allContentSortable('contentCol_1');
		
		if($('#contentCol_2'))
		{
			$('#contentCol_2').sortable("destroy");
			oQuickEdit.allContentSortable('contentCol_2');
		}
		
		if($('#content_'+this.currentId))
		{
			$('#content_'+this.currentId).sortable("destroy");
			oQuickEdit.allContentSortable('content_'+this.currentId);
		}
	}
}

CategoryContent.prototype.showAllContent = function(categoryId)
{
	if(document.getElementById('content'+categoryId))
	{
		document.getElementById('showContent').innerHTML = document.getElementById('content'+categoryId).innerHTML;
	}
}

CategoryContent.prototype.add = function(event,categoryId)
{
	oUtils.setDivEventPos(event, "catLinkQuickEdit");
	$("#catLinkQuickEdit").css("display", "");
	$("#catLinkCategoryIdQuickEdit").val(categoryId);
}

CategoryContent.prototype.save = function()
{
	this.loadingRequestAjax();
	
	$.ajax({
	   type: "POST",
	   url: "/quickEdit/categoryLinkSave",
	   data: $("#formCatLinkQuickEdit").serialize(),
	   success: function(topNavContent) {
			oCategoryContent.refreshAjax(topNavContent, "Link successfully saved");	
	   }
	});
}

CategoryContent.prototype.closeQuickEditDiv = function()
{
	$("#catLinkQuickEdit").css("display", "none");
}

CategoryContent.prototype.remove = function(categoryLinkId)
{
	if(confirm("Are you sure ?"))
	{
		$.ajax({
		   type: "POST",
		   url: "/quickEdit/categoryLinkRemove",
		   data: "categoryLinkId="+categoryLinkId,
		   success: function(topNavContent) {
		   		oCategoryContent.refreshAjax(topNavContent, "Link successfully removed");
		   }
		});
	}
}

CategoryContent.prototype.refreshAjax = function(newContent, message)
{
	$("#categoryLinksContent").html(newContent);
	
	$("#formCatLinkQuickEdit")[0].reset();
	
	$("#catLinkButtonSave").attr("disabled", false);
	$("#catLinkButtonSave").val("Save");
	
	$("#catLinkQuickEdit").css("display","none");
	
	if(message)
	{
		alert(message);
	}
}

CategoryContent.prototype.loadingRequestAjax = function()
{
	$("#catLinkButtonSave").attr("disabled", true);
	$("#catLinkButtonSave").val("Loading...");
}

CategoryContent.prototype.edit = function(event, linkId)
{
	$.post("/quickEdit/loadCategoryLink", { "linkId": linkId },
	  function(data)
	  {
	    $("#catLinkDescriptionQuickEdit").val(data.description);
	    $("#catLinkCategoryIdQuickEdit").val(data.categoryId);
	    $("#catLinkLinkQuickEdit").val(data.link);
	  }, "json"
	);
	
	oUtils.setDivEventPos(event, "catLinkQuickEdit");
	
	$("#catLinkQuickEdit").css("display", "");
	$("#catLinkIdQuickEdit").val(linkId);
}

var oCategoryContent = new CategoryContent();