function category_print(elem, tpl, ctype) {
	var temp = $(elem).attr('id').split('_');
	var path = temp[1].split(';');
	var id = path[path.length-1];
	var full_id = temp[1];
	var div = $(document.getElementById(ctype+'div_'+full_id));

	if (!$(div).is('*')) {
		return false;
	}
	
	var category = categories[ path[0] ];
	
	for (var i=1; i<path.length; i++) {
		if (category.childs[ path[i] ]) {
			category = category.childs[ path[i] ];
		}
	}

	var template = templates[tpl];
	var html = '';
	for (var key in category.childs) {
		var temp = template.replace(/\[NAME\]/g, category.childs[key].name);
		temp = temp.replace(/\[ID\]/g, full_id+';'+category.childs[key].id);
		html += temp.replace(/\[LINK\]/g, category.childs[key].link);
	}

	if (div.html().length == 0) {
		div.append(html);
		bind_effects(div);
	}
	
	if ($(div).is(":hidden")) {
		// $('.maingroup:visible').slideUp("fast");
		if ($(div).attr('id').indexOf('productlinediv') == 0) {
			$("div[id^='productlinediv_']:visible").slideUp("fast");
		}
		
		// alert($(div).attr('id'));
		$(div).slideDown("fast");
	} else {
		$(div).slideUp("fast");
	}
	return false;
}

function bind_effects(elem) {
	$('a', $('.productline', elem)).click(function() {	
		var content = $('td.content_td').eq(0);
		content.empty().append('<img style="margin: 14px 0 0 20px;" border="0" src="/store/static/img/ajax-loader-wheel-blue.gif" />');
		$.get($(this).attr('href'), { showOnlyContent: 1 }, function(data) {
			var content = $('td.content_td').eq(0);
			content.empty().append(data);
		});
		return category_print(this, 'maingroup', 'productline');
	});
	$('a', $('.maingroup', elem)).click(function() {
		return category_print(this, 'product', 'maingroup');
	});
}

$(document).ready(function() {
	bind_effects(this);
});
