var Listed = Class.create( {
	onItemClick : null,
	onItemDblClick: null,
	container : null,
	activeItem : null,
	itemTagname: null,
	maxDepth: 0,
	options: null,

	initialize : function(container_id, options) {
		
	},
	
	__construct: function(container_id, options){
		var self = this;
		this.options = options;
		this.container = $(container_id);
		this.container.onselectstart = function(){return false};
		
		if(options.depth){
			this.maxDepth = options.depth;
		}
		
		this.setItemTagname(options.itemTagname);

		this.initItems(this.container, options, 0);
	},

	initItems : function(container, options, depth) {
		var items = container.getElementsByTagName(this.itemTagname);
		var self = this; 

		if (items.length > 0) {
			for (var i = 0; i <= items.length; i++) {
				var item = items[i];
				
				if(this.maxDepth > 0){
					var depth = this.getDepth(item);
					if(depth >= this.maxDepth){
						continue;
					}
				}
				
				if(!item)
				{
					continue;
				}
				
				if (item.tagName == this.itemTagname.toUpperCase()) {
					item.onselectstart = function(){return false};
					
					item.style.cursor = 'pointer';
					item.onclick = function(){
						if(this.className.indexOf('selected') > 0){
							if(options.onItemDblClick)
								options.onItemDblClick(this);
						}else{
							if(options.onItemClick)
							{
								options.onItemClick(this);
							}
						}
						
						self.setActive(this);
					};
					
					item.onmouseover = function(){
						this.className += ' hover';
					};
					
					item.onmouseout = function(){
						this.className = this.className.replace('hover', '');
					};
				}
			}
		}
	},

	setActive : function(item) {
		if(!item) return; 
		
		if(item.className.indexOf('selected') == -1)
		{
			if(this.options.onItemClick)
			this.options.onItemClick(item);
		}
		if (this.activeItem) {
			this.activeItem.className = this.activeItem.className.replace('selected', '');
		}
		item.className += ' selected';
		this.activeItem = item;
	},
	
	isSelected: function(item){
		if(this.activeItem){
			return this.activeItem.id == item.id;
		}else{
			return false;
		}
	},
	
	setItemTagname: function(tagname){
		this.itemTagname = tagname;
	},
	
	getDepth: function(item){
		var depth = 0;
		var parent;
		var current = item;
		
		if(!item) return depth;
		
		while(parent = current.parentNode){
			if(parent.id == this.container.id){
				return depth;
			}
			
			depth ++;
			current = parent;
		}
	}
});