//Some groovy dHTML © Jim Richardson 2002.
//If you want to use it credit me, or karma will come and getcha!
//jim@djimm.co.uk
//
//Updated 2010 by Jim Richardson

//Initialise master vars

chosenLayer = 'none';

masterVis = 'visible';

masterUp = 0;
isAvail = 0;

helpState = true;

//offpointX = 0;
//offpointY = 6;

//determine which model - IE or DOM compliant - then init mousemove event handler

if(document.all){
	document.onmousemove=follow;
	}
	
if(document.getElementById && !document.all){
	window.addEventListener("mousemove",follow,false);
	}
	
//Init follow function

function follow(e){

	if(document.getElementById && !document.all){ //DOM Compliant
		if(chosenLayer != 'none'){
		
		if(isAvail && !masterUp){
			offpointX = -200;
			offpointY = -30;	
		}
		
		if(isAvail && masterUp){
			offpointX = -200;
			offpointY = 60;
		}
		
		if(!isAvail){
			offpointX = 10;
			offpointY = 10;
		}
		
			checkWidth = e.clientX + offpointX + 60 + parseFloat(getStyle(chosenLayer,'width'));
			maxHoriz = window.innerWidth - offpointX - 72 - parseFloat(getStyle(chosenLayer,'width'));
			
			if (window.innerWidth < checkWidth){
				eval('document.getElementById(chosenLayer).style.left = \'' + maxHoriz + 'px\';');
				}
			else{
				eval('document.getElementById(chosenLayer).style.left = \'' + (e.clientX + offpointX) + 'px\';');
				
				}
			if(masterUp){
				thisHeight = parseFloat(getStyle(chosenLayer,'height')) + 40;
				eval('document.getElementById(chosenLayer).style.top = \'' + (e.clientY + offpointY + window.pageYOffset - thisHeight) + 'px\';');
				}
			else{
				eval('document.getElementById(chosenLayer).style.top = \'' + (e.clientY + offpointY + window.pageYOffset) + 'px\';');
				}
			document.getElementById(chosenLayer).style.visibility = masterVis;
			}
		}
		
	if (document.all){ //IE
		if (chosenLayer != 'none'){
		
		if(isAvail && !masterUp){
			offpointX = -200;
			offpointY = -30;	
		}
		
		if(isAvail && masterUp){
			offpointX = -200;
			offpointY = 60;
		}
		
		if(!isAvail){
			offpointX = 10;
			offpointY = 10;
		}
		
			checkWidth = event.x + offpointX + parseFloat(getStyle(chosenLayer,'width')) + 40;
			maxHoriz = document.body.clientWidth - 40 - parseFloat(getStyle(chosenLayer,'width'));
			
			if (document.body.clientWidth <= checkWidth){
				document.all(chosenLayer).style.left = maxHoriz;
				}
			else{
				document.all(chosenLayer).style.left = document.body.scrollLeft + event.x + offpointX;
				}
			if(masterUp){
				thisHeight = parseFloat(getStyle(chosenLayer,'height')) + 40;
				document.all(chosenLayer).style.top = event.y + offpointY + + getScroll() - thisHeight;
				}
			else{
				document.all(chosenLayer).style.top = event.y + offpointY + getScroll();
			}
			document.all(chosenLayer).style.visibility = masterVis;
			}
		}

}

function visible(layerid,state,upward,is_avail){

	masterVis = state
	
	if(upward){
		masterUp = 1;
	}
	else{
		masterUp = 0;
	}
	
	if(is_avail){
		isAvail = 1;
	}
	else{
		isAvail = 0;
	}
	
	if (state == 'visible'){
		chosenLayer = layerid;
		}

	else{
		chosenLayer = 'none';
		}

	if (document.getElementById && !document.all){
			eval('document.getElementById(\'' + layerid + '\').style.visibility = \'hidden\'');
		}

	if (document.all){
		document.all(layerid).style.visibility = 'hidden';
		}
}

function getStyle(el,styleProp){
	var x = document.getElementById(el);
	if(x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if(window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function getInternetExplorerVersion(){

    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    	}
    return rv;
}

function getScroll(){
	thisIE = getInternetExplorerVersion();
	
	if(thisIE < 8){
		thisScroll = document.documentElement.scrollTop;
	}
	else{
		thisScroll = 0;
	}
	
	return thisScroll;
}
