var minTreeviewHeight;

function mouseLoc(e){
	var mouseLocRel = /*e.clientY + */e.pageY;
	var height = $('top_menu_div').getDimensions().height + $('top_menu_div').offsetTop;
	
	//var debugMessage = ' height:' + height + ' < e.clientY:' + e.clientY + ' pageY:' + e.pageY + ' mouseRela:' + mouseLocRel + ' < offsetTop:' + $('top_menu_div').offsetTop;
	if ( mouseLocRel > height || mouseLocRel < $('top_menu_div').offsetTop ){
		//outside
		//debugMessage += ' [out]';
		new Effect.Morph('top_menu_div', {
			  style: 'height:122px', // CSS Properties
			  duration: 0.2 // Core Effect properties
			});
		closeAll();
	}
	
}

function dropEvent(e){
	var event = e || window.event;
	 if (event.stopPropagation) {
	    event.stopPropagation();
	  } else {
	    event.cancelBubble = true;
	  } 
}
function closeAll(){
	$$('.treeview').each(function(obj){
		closeTreeFolders(obj);
						});
}
function closeTreeFolders(obj){
	obj.select('.folder').each(function(obj2){
		obj2.down('ul').setStyle({ display: 'none' });
		});
}
function showSubItems(e,currentObj){
	var currentUl = currentObj.down('ul');

	currentUl.setStyle({ display: 'block' });

	//top_menu_div movement
	var minHeight = minTreeviewHeight;//122 + 60;
	var maxHeight = currentObj.up('.treeview').getDimensions().height + 60 + $('top_menu_div').offsetTop;
	if ( maxHeight > minHeight ){
		//$('top_menu_div').setStyle({ height: maxHeight + 'px' });
		//$('top_menu_div').morph('height:'+ maxHeight + 'px');
		new Effect.Morph('top_menu_div', {
			  style: 'height:'+ maxHeight + 'px', // CSS Properties
			  duration: 0.2 // Core Effect properties
			});
	}else{
		//$('top_menu_div').setStyle({ height: minHeight + 'px' });
		//$('top_menu_div').morph('height:'+ minHeight + 'px');
		new Effect.Morph('top_menu_div', {
			  style: 'height:'+ minHeight + 'px', // CSS Properties
			  duration: 0.2 // Core Effect properties
			});
	}
	
	currentObj.up('.treeview').removeClassName('closed');
	currentObj.up('.treeview').addClassName('open');
	currentObj.up('.treeview').adjacent('.treeview').each(function(obj){
		obj.removeClassName('open');
		obj.addClassName('closed');
		});

	$$('.treeview.closed').each(function(obj){closeTreeFolders(obj);});
	dropEvent(e);
}

function init(){
	
	//document.observe('mouseout',function(e){mouseLoc(e);});
	document.observe('mousemove',function(e){mouseLoc(e);});
	
	$$('.treeview .folder').each(
			function(obj){
				obj.onmouseover=function(e){
					showSubItems.delay(0.3,e,this);
				};
			});
	$$('.treeview .item').each(
			function(obj){
				obj.onclick=function(e){
					//alert( "load item:"+this.down().readAttribute('id') );
					//hoverMenu_form
					var clickable = this.down().readAttribute('strClickable');
					if ( clickable == 1 ){
						$('hovermenu_id').value = this.down().readAttribute('id');
						$('hovermenu_tpl_id').value = this.down().readAttribute('tpl_id');
						var path = '/' + this.down().readAttribute('strParentPath') + '/' + this.down().innerHTML;
						
						$('hoverMenu_form').action = 'items.php?path=' + path;
						$('hoverMenu_form').submit();
					}
					dropEvent(e);
				};
			});
	
	minTreeviewHeight = $('top_menu_div').getDimensions().height + $('top_menu_div').offsetTop;
	
	left = 100;
	$$('.treeview').each(
			function(obj){
					obj.setStyle({ left: left + 'px' });
					left+=180;
					obj.setStyle({ visibility: 'visible' });
					});
	
}

window.onload=init;

