
if (!window.My) { My = {}}
if (!My.Sidebar) { My.Sidebar = {}}

My.Sidebar.Control = AW.UI.List.subclass();
My.Sidebar.Control.create = function(){

	var obj = this.prototype;
	obj.setClass("sidebar","box") ;

	obj.defineTemplate("group", new My.Sidebar.Group);
	obj.setItemTemplate(new My.Sidebar.Item);
	obj.setScrollTemplate(function(){return this.getGroupTemplate(0)});
	
	obj.setController("selection", function() {}) ;

	obj.getContentTemplate().mapTemplate("item", function(i){
		return this.$owner.getGroupTemplate(i);
	});

	obj.defineViewProperty("expanded", true);
	obj.defineViewProperty("textpanel", -1 ) ;
	
	var model = { item : -1}
	obj.defineModel("textpanel" , model) ;

	obj.onSidebarSignClicked = function(src, i){
		if (this.getViewIndices(i)) {
			this.setViewExpanded(!this.getViewExpanded(i), i);
		}
	};

	obj.onViewExpandedChanged = function(e, i){
		this.getGroupTemplate(i).refresh();
	};

};

// ----

My.Sidebar.Group = AW.System.Template.subclass();
My.Sidebar.Group.create = function(){

	var obj = this.prototype;

	obj.setTag("span");
	obj.setClass("sidebar", "view");

	obj.setContent("start", function(){
		return this.$0 ? this.getItemTemplate() : "";
	});

	obj.setContent("items", function(){

		if (this.$0 && !this.getViewProperty("expanded")){
			return "";
		}
		else {
			return this.getContentTemplate();
		}

	});

	obj.setContent("end", "");

};

// ----

My.Sidebar.Item = AW.Templates.ImageText.subclass();
My.Sidebar.Item.create = function(){

	var obj = this.prototype;

	obj.setClass("sidebar", function(){

		return this.getViewProperty("count") ?  "folder" : "leaf";
	});

	obj.setClass("expanded", function(){
		return this.getViewProperty("expanded") ? "true" : "false";
	});

	obj.setClass("textonly", function(){
		var x= this.getViewProperty("textpanel");
		if (x == this.$0) return "true" ;
		return "false";
	});

	var sign = new AW.HTML.SPAN;
	sign.setClass("sidebar", "sign");

	sign.setEvent("onclick", function(){
		this.raiseEvent("onSidebarSignClicked");
	});

	obj.setContent("box/sign", sign);
};


