var _CategoriesCache	=	[];
var searchFieldValue	=	null;

	
$(document).ready(function(){
	
	
	$('#topcat ul').css('display', 'none');
	$('#topcat ul.active').css('display', 'block');
	
	$('a.cat-opener.active').each(function(e) {
		
		var	id	=	this.id.split('-')[1];
		
		$('#subcat-' + id).css('display', 'block');
	});
	
	
	if ($('a.cat-opener').length > 0) {
		
		
		//_CategoriesCache[cat_ID] = $('#category-content').html();
		//_UpdateCatgegoryOpeners();
		
	}
	
	$('#q').focus(function (e) {
		
		if (!searchFieldValue) {
			searchFieldValue	=	$(this).val();
		}
		
		if ($(this).val() == searchFieldValue) {
			$(this).val('');
		}
	});
	
	$('#q').blur(function (e) {
		
		if ($(this).val() == '') {
			$(this).val(searchFieldValue);
		}
	});
	
});


function _UpdateCatgegoryOpeners ( ) {
	
	$('a.cat_opener').unbind('click');
	$('a.cat-opener').click(function (event) {
		
		
		event.stopPropagation();
		
		var id	=	this.id.split('-')[1];
		
		if ($('#subcat-' + id).css('display') == 'block') {
			$('#subcat-' + id).css('display', 'none');
			$('#subcat-' + id + ' ul').css('display', 'none');
		}
		else {
			$('#subcat-' + id).css('display', 'block');
		}
		
		if (_CategoriesCache[id]) {
			$('#category-content').html(_CategoriesCache[id]);
			Cufon.refresh();
			return false;
		}
		
		
		if ($('#overlay').length > 0) {
			$('#overlay').remove();
		}
		
		$('#category-receiver').prepend('<div id="overlay" class="category-load">Daten werden geladen ...</div>');		
		$('#overlay').css('width', $('#category-content').width());
		$('#overlay').css('height', $('#category-content').height());
		
		var url	=	serverpath + '/index.php?action[' + sockets_ID + ']=ajaxGetCategory';
		
		$.ajax({
			type:		"GET",
			dataType:	"html",
			url:		url,
			data: {
				ID:			nodes_ID,
				norewrite:	1,
				sockets_ID:	sockets_ID,				
				categories_ID:	id
			},
			success: function (html) {		
						
				$('#category-content').html( html);
				$('#overlay').remove();
				Cufon.refresh();	
				
				_CategoriesCache[id]	=	html;	
				_UpdateCatgegoryOpeners();														
			}
		});
		
			
		
		return false;
	});	
}

