dojo.require("dijit.Tree");
dojo.require("dojo.parser");
dojo.provide("treeModel");
dojo.declare(
	"treeModel",
	null,
{

	constructor: function(args){
	},

	destroy: function(){
	},

	// =======================================================================
	// Methods for traversing hierarchy

	getRoot: function(onItem, onError){
		onItem({type: "root", root: true, name: "Katalog"});
	},

	mayHaveChildren: function(/*dojo.data.Item*/ item){
		// summary:
		//		Tells if an item has or may have children.
		// [ALI049]
		return item.root || item.mayHaveChildren == "True";
	},

	getChildren: function(/*dojo.data.Item*/ item, /*function(items)*/ onComplete, /*function*/ onError) {
        var d;
		switch(item.root ?  "top" : item.type){
			case "top":
				d = dojo.xhrGet({url : "/shop/directory/root", handleAs : "json"});
                d.addCallback(onComplete);
				break;
			case "tag":
				d = dojo.xhrGet({url : "/shop/directory/" + item.pk, handleAs : "json"});
                d.addCallback(onComplete);
                break;
		}
	},

	// =======================================================================
	// Inspecting items

	getIdentity: function(/* item */ item){
		return item.root ? "top" : item.type;
	},

	getLabel: function(/*dojo.data.Item*/ item){
        names = item.name.split(';')
		return names[names.length - 1];
	},

	// =======================================================================
	// Write interface (unimplemented)

	newItem: function(/* Object? */ args, /*Item*/ parent){
	},

	pasteItem: function(/*Item*/ childItem, /*Item*/ oldParentItem, /*Item*/ newParentItem, /*Boolean*/ bCopy){
	},

	// =======================================================================
	// Callbacks (unimplemented)
	
	onChange: function(/*dojo.data.Item*/ item){
	},

	onChildrenChange: function(/*dojo.data.Item*/ parent, /*dojo.data.Item[]*/ newChildrenList){
	},

	onDelete: function(/*dojo.data.Item*/ parent, /*dojo.data.Item[]*/ newChildrenList){
	}
});

