// new reference object
reference = new Object();
reference.links = new Array();
reference.linkPrefix = "reference";
reference.defPrefix = "def";
reference.offsetX = 6;
reference.offsetY = 0;
reference.defPosX = 0;
reference.defPosX = 0;
reference.displayTime = 0;
reference.hideTime = 200;
reference.displayTimer = '';
reference.hideTimer = '';
reference.currentDef = '';
reference.currentRefArray = new Array();
reference.defaultHeight = 160;
reference.shortHeight = 140;
reference.defaultWidth = 220;
reference.currentHeight = 0;

function startReference()
{	
	// Add all of the reference divs to the scrolling container div
	elem = document.getElementById("defContainer")
	elem.innerHTML = page.DEFS;			
	
	// For each link that has the correct ID prefix, add the appropriate methods and properties
	for( i = 0; i < document.links.length; i++){	 
		if( document.links[i].id.indexOf(reference.linkPrefix) == 0 ){	 
			var ele = document.links[i];
			ele.arrReferenceList = createReferenceList(document.links[i].id);				
			ele.onmouseover = displayReference;
		}
	}
	return true;
}

function createReferenceList(strReferenceID) {
	// Reference lists are extracted from the link's ID attribute, which takes the form "reference 1,2,3,4"  	
	var arrRefList = strReferenceID.split(' ');	
	var arrReferenceList = arrRefList[1].split(',');	
	return arrReferenceList
}

function displayReference(e)
{	
	hideDefinition();			
	/* used to capture the event in IE or mozilla */
	e = e || window.event;		
	setCurrentRefArray(this.arrReferenceList);
	setCurrentPos(e.clientX, e.clientY);		
	//reference.displayTimer = setTimeout('showDefinition()', reference.displayTime);	
	reference.displayTimer = showDefinition();	
	//howDefinition();	
	
	return true;
}

function hideReference(e)
{
	/* used to capture the event in IE or mozilla */
	e = e || window.event;
	//reference.hideTimer = setTimeout('hideDefinition()', reference.displayTime);
	reference.hideTimer = hideDefinition();
	
	
	return true;
}

function setCurrentRefArray(arrReferences) 
{	
	// Store the array containing the links references	
	reference.currentRefArray = arrReferences
	return true;
}

function setCurrentPos(x, y)
{
	
	var scrollX = setScrollX();
	var scrollY = setScrollY();
	reference.defPosX = x + reference.offsetX + scrollX;
	reference.defPosY = y + reference.offsetY + scrollY;
	return true;
}

function setScrollX()
{
	
	x = '';
	if( window.pageXOffset ){
		x = window.pageXOffset
	}else if( document.documentElement.scrollLeft ){
		x = document.documentElement.scrollLeft
	}else if( document.body.scrollLeft ){
		x = document.body.scrollLeft
	}else{
		x = 0;
	}
	return x;
}

function setScrollY()
{
	y = '';
	if( window.pageYOffset ){
		y = window.pageYOffset
	}else if( document.documentElement.scrollTop ){
		y = document.documentElement.scrollTop
	}else if( document.body.scrollTop ){
		y = document.body.scrollTop
	}else{
		y = 0;
	}
	return y;
}


function showDefinition()
{	
	if( document.getElementById){	
		
		var eleContainer = document.getElementById("referenceContainer");

		// Resize height of reference container if only 1 element exists, else use default height	
		if(reference.currentRefArray.length < 2) {
			reference.currentHeight = reference.shortHeight;
		} else {
			reference.currentHeight = reference.defaultHeight;			
		}
		
		eleContainer.style.height = String(reference.currentHeight) + 'px';
		
		// This child element must be dynamically resized for IE
		var eleContainerChild = document.getElementById("defContainer");
		eleContainerChild.style.height = String(reference.currentHeight - 17) + 'px';
		
		
		// Display each of the references divs for this link
		for(i=0; i<reference.currentRefArray.length; i++) {		
			var eleDef = document.getElementById(reference.defPrefix + reference.currentRefArray[i]);
			eleDef.style.display = 'block';
		}
		
		
		// Check if reference div will not be contained within the browser window
		
		// Get window width and height
		if (window.innerWidth != undefined) {
			// For Mozilla
			var win_w = window.innerWidth;
			var win_h = window.innerHeight;
		} else if (document.body.clientWidth != undefined) {
			// For IE  
			var win_w = (document.compatMode=="CSS1Compat")?document.documentElement.clientWidth : document.body.clientWidth; 		
			var win_h = (document.compatMode=="CSS1Compat")?document.documentElement.clientHeight : document.body.clientHeight;
		}
		
		// get vertical scroll position
		var scrollTop = document.body.scrollTop;
		if (scrollTop == 0)
		{
			if (window.pageYOffset)
				scrollTop = window.pageYOffset;
			else
				scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
		
		// check if div will be beyond window width
		var refRight = reference.defPosX + reference.defaultWidth + 5;		
		if (refRight > win_w) {			
			reference.defPosX = reference.defPosX - reference.defaultWidth - 20;
		}
		
		// check if div will be beyond window height			
		var refBot = (reference.defPosY - scrollTop) + reference.currentHeight + 5;			
		var strUserAgent = navigator.userAgent;			
		if (strUserAgent.indexOf("Safari") != -1) {
			// If userAgent is Safari, make some adjustments			
			win_h += scrollTop;
			if (refBot > win_h) {
				reference.defPosY = reference.defPosY - reference.currentHeight - scrollTop - 5;
			} else {
				reference.defPosY = reference.defPosY - scrollTop;
			}				
		} else {
			// If userAgent is NOT Safari (must be IE, or FF)
			var refBot = reference.defPosY - scrollTop + reference.currentHeight + 5;
			if (refBot > win_h) {
				reference.defPosY = reference.defPosY - reference.currentHeight  - 5;
			}	
		}
 
		// Position and show the reference container
		eleContainer.style.left = reference.defPosX+"px";
		eleContainer.style.top = reference.defPosY+"px";		
		eleContainer.style.display = 'block';
		
	}
	return true;
}

function hideDefinition()
{
	if(document.getElementById){
	
		defElem = document.getElementById("defContainer")
		scrollUp(defElem)
	
		// Hide the reference container
		elem = document.getElementById("referenceContainer")		
		elem.style.display = 'none';		
		
		// Hide each of the currently shown reference def divs  
		for(i=0; i<reference.currentRefArray.length; i++) {		
			var eleDef = document.getElementById(reference.defPrefix + reference.currentRefArray[i]);
			try{
			eleDef.style.display = 'none';
			}
			catch(e){
			
			}
		
		}	
	}
	
	// Clear the array so that no old items will show up next time
	reference.currentRefArray = new Array();
		
	return true;
}

function scrollUp(elem) 
{
    elem.scrollTop = 0;
}


/**
  This is our definitions object which will hold the HTML for the reference divs
  the rest of this javascript is generated in "controls/footer_js.ascx"
*/
page = new Object();
page.DEFS = '';