/**
 * @author Edo
 */
var tabCache = new Array();
function CategoryTabSelect(event, ui)
{
	if(ui.tab.id == "delete")
	{
		var lastSelection = $(".ui-tabs-selected a");
		var lastSelectionArr = lastSelection[0].id.split("_");
		var lastId = lastSelectionArr[lastSelectionArr.length - 1];
		var username = $.jqURL.get("u");
		if(username == undefined)
			return; //location.href = "index.html";
			
		var link = "account.php?update=1&delete=" + lastId + "&u=" + username;
		
		$.getJSON(baseLink + link + callbackAttr, 
		function(JSON)
		{
			LoadCategoryTabs(3);
		});	
	}
	else
	{
		var arr = ui.tab.id.split("_");
		var type = $.jqURL.get("type");
		if(type == undefined)
			type = 2;
		LoadCategories(type, arr[arr.length - 1]);
	}
}

function GenerateCategories(type)
{
	var contentDiv = $("#idCategories");
	contentDiv.text("");
	
	//Fetch calendar contents
	var link = "";
	if(type == 0) //News
	{
		link = "categories.php?type=0";
	}
	else
	{
		var username = $.jqURL.get("u");
		if(username == undefined)
			return; //location.href = "index.html";
			
		link = "categories.php?type=1&u=" + username;
	}
	
	$.getJSON(baseLink + link + callbackAttr, 
	function(JSON)
	{
		var htmlResult = "";
		var count = -1;
		contentDiv.empty();
		
		$.each(JSON.results, 
		function(i, result)
		{
			if(count == -1)
				count = 0;
			
			if(parseInt(result.count) > 0)
			{
				htmlResult += "<h3><a href=\"#\">" + result.nameGeneral + " (" + result.count + ")</a></h3>";
				htmlResult += "<div>";
				htmlResult += "<ul>";
				
				for(var j = 0; j < result.categories.length; j++)
				{
					if(type == 0)
						htmlResult += "<li><a href=\"index.html?cat=" + result.categories[j].category + "&gcat=" + result.categoryGeneral + "\" title=\"View all posts filed under " + result.categories[j].name + "\">" + result.categories[j].name + "</a> (" + result.categories[j].count + ") </li>";
					else
						htmlResult += "<li><a href=\"blog.html?u=" + username + "&cat=" + result.categories[j].category + "&gcat=" + result.categoryGeneral + "\" title=\"View all posts filed under " + result.categories[j].name + "\">" + result.categories[j].name + "</a> (" + result.categories[j].count + ") </li>";				
				}
								
				htmlResult += "</ul>";
				htmlResult += "</div>";
			}
	  	});
		if(count == -1)
		{
			contentDiv.empty();
		}
		else
		{
			contentDiv.empty();
			contentDiv.html(htmlResult);
			contentDiv.accordion();
		}		
	});	
}

function LoadCategoryTabs(type)
{
	var edit = false;
	var oldType = type;
	if(type == 3)
		edit = true;
	if(type == 0 || type == 1)
		type = 0;
	else
		type = 1;

	var contentDiv = $("#idTabList");
	contentDiv.text("");
	
	$.getJSON(baseLink + "categories.php?type=" + type + "&post=1" + callbackAttr, 
	function(JSON)
	{
		var htmlResult = "";
		var count = -1;
		var firstCat = -1;
		contentDiv.empty();
		
		$.each(JSON.results, 
		function(i, result)
		{
			if(count == -1)
				count = 0;
				
			htmlResult += "<li><a href=\"#tabs-1\" id=\"cat_" + result.category + "\">" + result.name + "</a></li>";
			if(firstCat == -1)
				firstCat = parseInt(result.category);
	  	});
		if(count == -1)
		{
			contentDiv.empty();
		}
		else
		{
			if(edit)
			{
				htmlResult += "<li><a href=\"#tabs-2\">Add New General Category</a></li>";
				htmlResult += "<li><a href=\"#tabs-1\" id=\"delete\">Delete Selected Category</a></li>";
			}
			contentDiv.empty();
			contentDiv.html(htmlResult);
			var tabsGeneral = $("#idTabCategories");
			tabsGeneral.tabs('destroy');
			tabsGeneral.tabs();
			if(firstCat != -1)
				LoadCategories(oldType, firstCat);
			tabsGeneral.bind('tabsselect', CategoryTabSelect);				
		}		
	});	
}

function LoadCategories(type, gcat)
{
	if(type == 0 || type == 1)
		type = 0;
	else
		type = 1;

	var contentDiv = $("#idCategoryList");
	contentDiv.text("");
	
	$.getJSON(baseLink + "categories.php?type=" + type + "&post=1&gcat=" + gcat + callbackAttr, 
	function(JSON)
	{
		var htmlResult = "";
		var count = -1;
		contentDiv.empty();
		
		$.each(JSON.results, 
		function(i, result)
		{
			if(count == -1)
				count = 0;
				
			htmlResult += "<option value=\"" + result.category + "\">" + result.name + "</option>";
	  	});
		if(count == -1)
		{
			contentDiv.empty();
		}
		else
		{
			contentDiv.empty();
			contentDiv.html(htmlResult);
		}		
	});
}

