var CatalogTree = {
	
	tree: null,
	active: [],
	zoomWrapper: null,
	zoomContainer: null,
	imageZoomed: false,
	scrollAllowed: true,
	
	init: function () {
		CatalogTree.tree = document.getElementById('catalog-tree');
		
		var lis = CatalogTree.tree.getElementsByTagName('li');
		for (var i = 0; i < lis.length; i++) {
			if (lis[i].className == 'active') {
				CatalogTree.active.push(lis[i]);
			}
		}
		
		CatalogTree.zoomContainer = document.createElement('div');
		CatalogTree.zoomContainer.id = 'zoomContainer';
		CatalogTree.zoomWrapper = document.createElement('div');
		CatalogTree.zoomWrapper.id = 'zoomWrapper';
		document.body.appendChild(CatalogTree.zoomContainer);
		document.body.appendChild(CatalogTree.zoomWrapper)
		
		
		Utils.addEvent(document, 'click', CatalogTree.onDocumentClick);
		
		if (window.addEventListener) {
        	window.addEventListener('DOMMouseScroll', CatalogTree.onMouseWheel, false);
		}
		window.onmousewheel = document.onmousewheel = CatalogTree.onMouseWheel;
	},
	
	onMouseWheel: function (e) {
		if (!CatalogTree.scrollAllowed) {
			if (window.addEventListener) { // FF
				e.stopPropagation();
				e.preventDefault();
			}
			
			return false;
		}
		
		return true;
	},
	
	zoomImageOn: function (e, el) {
		CatalogTree.zoomContainer.style.width = el.naturalWidth + 'px';
		CatalogTree.zoomContainer.style.marginLeft = Math.round(el.naturalWidth/(-2)) + 'px';
		
		CatalogTree.zoomContainer.innerHTML = '<img src="' + el.src + '" alt="" style="width: '+el.naturalWidth+'px; height: '+el.naturalHeight+'px;" />';
		
		CatalogTree.zoomWrapper.style.top = (document.body.scrollTop + document.documentElement.scrollTop) + 'px';
		CatalogTree.zoomContainer.style.top = (document.body.scrollTop + document.documentElement.scrollTop + 50) + 'px';
		
		CatalogTree.zoomWrapper.style.display = 'block';
		CatalogTree.zoomContainer.style.display = 'block';
		
		CatalogTree.scrollAllowed = false;
		
		setTimeout("CatalogTree.imageZoomed = true;", 200)
	},
	
	onDocumentClick: function (event) {
		event = event || window.event;
		var target = event.target || event.srcElement;
		
		if (CatalogTree.imageZoomed) {
			CatalogTree.zoomWrapper.style.display = 'none';
			CatalogTree.zoomContainer.style.display = 'none';
			CatalogTree.scrollAllowed = true;
			CatalogTree.imageZoomed = false;
		}
		
		/*if (target.tagName.toLowerCase() == 'input') {
			if (target.type == 'text') {
				target.select();
			}
		}*/
	},
	
	display: function (el) {
		var uls = el.parentNode.getElementsByTagName('ul');

		if (uls.length > 0) { // toggle tree
			if (uls[0].style.display != 'none') {
				uls[0].style.display = 'none';
			} else {
				uls[0].style.display = '';
			}
			
			return false;
		}/* else { // display info and set active
			if (el.parentNode.className != 'active') {
				CatalogTree.getData(el);
				
				CatalogTree.removeActive();
				CatalogTree.setActive(el);
			}
		}*/
		
		return true;
	},
	
	removeActive: function () {
		for (var i = 0; i < CatalogTree.active.length; i++) {
			CatalogTree.active[i].className = '';
		}
		
		CatalogTree.active = [];
	},
	
	setActive: function (el) {
		var li = el.parentNode;
		while (li.tagName.toLowerCase() == 'li') {
			li.className = 'active';
			CatalogTree.active.push(li);
			
			li = li.parentNode.parentNode;
		}
	},
	
	getData: function (el) {
		document.getElementById('catalog-data').innerHTML = '<p>'+el.innerHTML.replace(/^. /, '')+'</p>';
	}
	
}


//Utils.addEvent(window, 'load', CatalogTree.init);
