
var anchoredHelpItemXYZ=null;
var lastAttemptToExpand=0;
var expandOnDisplayHelp=false;
function displayHelpInDiv(helpText){
	if(helpText=='')
		helpDivInWindow.innerHTML='';
	else{
		helpDivInWindow.innerHTML="<B>Help:</B>(Click 2 expand)<br/><br/>"+helpText;
		if(expandOnDisplayHelp){
			expandRightDiv();
		}
	}
}

var node2Help=new Object();
function addMouseOverHelp(node, withoutHighlightNorAnchor){

	if(true)
		return;

	node.onmouseover=function(){
		var now=new Date();
		node2Help[now.getTime()]=this;
		setTimeout("if(node2Help["+now.getTime()+"]){node2Help["+now.getTime()+"].displayHelp();}", 5000);
	};
	if(withoutHighlightNorAnchor+""=="true"){
		node.displayHelp=function(){
			if(anchoredHelpItemXYZ){
				anchorItem(null);
			}
			if(this.helpPage){
				var ajaxHelper=new SimpleAjaxHelper("");
				ajaxHelper.get(this.helpPage, true, new function(){
					this.onSuccess=function(req){
						displayHelpInDiv(req.responseText);
					};
				});
			}
			else if(this.helpText){
				displayHelpInDiv(this.helpText);
			}
		}
		node.onmouseout=function(){
			node2Help=new Object();
//			displayHelpInDiv("");
		}
	}
	else{
		node.displayHelp=function(){
			if(anchoredHelpItemXYZ){
				anchorItem(this);
			}
			addClass(this, "highlightedHelpableItem");
			if(this.helpPage){
				var ajaxHelper=new SimpleAjaxHelper("");
				ajaxHelper.get(this.helpPage, true, new function(){
					this.onSuccess=function(req){
						displayHelpInDiv(req.responseText);
					};
				});
			}
			else if(this.helpText){
				displayHelpInDiv(this.helpText);
			}
		}
		node.onmouseout=function(){
			node2Help=new Object();
			if(anchoredHelpItemXYZ)
				return;
			removeClass(this, "highlightedHelpableItem");
		}
		if(!node.onclick){
			node.onclick=function(){
				anchorItem(this);
			};
			
			if(!node.title){
				node.title="Click to expand help box";
				node.origTitleEmpty=true;
			}
		}
	}
}

function anchorItem(curItem){
	if(anchoredHelpItemXYZ==curItem){
		return;
	}
	if(anchoredHelpItemXYZ){
		removeClass(anchoredHelpItemXYZ, "highlightedHelpableItem");
	}
	anchoredHelpItemXYZ=curItem;
	if(anchoredHelpItemXYZ){
		expandRightDiv();
		addClass(anchoredHelpItemXYZ, "highlightedHelpableItem");
	}
}

if(document.onclick){
	document.rightHelp_old_onclick=document.onclick;
}
document.onclick=function(ev){
	if(document.rightHelp_old_onclick){
		document.rightHelp_old_onclick(ev);
	}
	if(lastAttemptToExpand){
		var timeElapsed=(new Date()).getTime()-lastAttemptToExpand;
		if(timeElapsed<800)
			return;
		anchorItem(null);
		var rightDiv=document.getElementById('rightDiv');
		if(rightDiv){
			shrinkRightDiv();
		}
	}
}

function shrinkRightDiv(){
	var rightDiv=document.getElementById('rightDiv');
	rightDiv.style.width='10%';
	rightDiv.style.height='auto';

	var rightContainer=document.getElementById('rightContainer');
	if(!rightContainer)
		return;
		
	removeClass(rightContainer, 'blackBorder');
	
	walkTree(rightDiv, function(subNode){
		if(subNode.onShrink){
			subNode.onShrink();
		}
	});
	document.getElementById('autoExpandHelpDiv').style.display='none';
	lastAttemptToExpand=null;
}

function dontShrinkYet(){
	lastAttemptToExpand=(new Date()).getTime();
}

var expandedWidth="50%";
var expandedHeight="50%";
function expandRightDiv(){
	dontShrinkYet();
	var rightDiv=document.getElementById('rightDiv');
	if(rightDiv){
		rightDiv.style.width=expandedWidth;
		rightDiv.style.height=expandedHeight;
		rightDiv.style.right='3px';
		
		var rightContainer=document.getElementById('rightContainer');
		addClass(rightContainer, 'blackBorder');
		
		rightDiv.style.zIndex=10;
		rightContainer.style.zIndex=10;
	
		walkTree(rightDiv, function(subNode){
			if(subNode.onExpand){
				subNode.onExpand();
			}
		});
		
		document.getElementById('autoExpandHelpDiv').style.display='inline';
	}
}
