﻿CategoryTree = {

	Init: function() {
	    $("div[rel=category_tree] div.level_2 a").each(CategoryTree.SetExpand);
	    $("div[rel=category_tree] a.selected").each(CategoryTree.ExpandToRoot);
	    $("div[rel=category_tree] a.selected").each(CategoryTree.Expand);
	},
	
	SetExpand: function() {
	    if ($(this).parent().next("div[rel=children]").children("div").length > 0) {
		    $(this).attr("href","javascript:;");
            $(this).click(CategoryTree.Expand);
		}
	},
	
	Expand: function() {
	    $(this).parent().siblings("div[rel=children]").hide();
	    $(this).parent().next("div[rel=children]").show();
	},
	
	ExpandToRoot: function() {
	    $(this).parents("div[rel=children]").show();
	}
};


// Attach events on document load
$(document).ready(function() {

	CategoryTree.Init();

});
