$(document).ready(function(){
	$("#categories").tree({
		ui:{
			theme_name: "classic"
		},
		types: {
			"default" : {
				clickable       : false,
				renameable      : false,
				deletable       : false,
				creatable       : false,
				draggable       : false
			}
		},
		data : {
			type : "json",
			async : true,
			opts : {
				method : "GET",
				url : "/ajax/categories.json"
			}
		},
		callback: {
			beforedata : function(node, treeObj) {
				return {
					root: $(node).attr("id") || "source"
				};
			}
		}
	});

	$('#categories a').livequery(function(){
		if($(this).parent().attr('id') == document.location.href.split('/')[4]) {
			$(this).css('font-weight', 'bold');
			if($(this).parent().parent().css('display') == 'none') {
				$(this).parent().parent().parent().removeClass('closed').addClass('open');
			}
		}

		$(this).click(function(){
			document.location.href = '/by-category/'+$(this).parent().attr('id')+'/';
			//sCategory = $(this).parent().attr('id');
			//filterDomains();
		});
	});

	$('#ext a').click(function(){
		sZone = $(this).attr('name');
		filterDomains();
		refreshCategories();
	});

	function filterDomains() {
		$.get('/ajax/domains.json', {'zone' : sZone, 'category' : sCategory, 'output' : 'html'}, function(data){
			$('#table').html(data);
		});
	}

	function refreshCategories() {
		$("#categories").tree({
			ui:{
				theme_name: "classic"
			},
			types: {
				"default" : {
					clickable       : false,
					renameable      : false,
					deletable       : false,
					creatable       : false,
					draggable       : false
				}
			},
			data : {
				type : "json",
				async : true,
				opts : {
					method : "GET",
					url : "/ajax/categories.json?zone="+sZone+"&category="+sCategory
				}
			},
			callback: {
				beforedata : function(node, treeObj) {
					return {
						root: $(node).attr("id") || "source"
					};
				}
			}
		});
	}
});
