
var hoverBlock=0;
var hoverTimeout=0;
var hoverFading=0;
var hoverOpacity=100;
var hoverLink=null;

function getQueryVariable(thevar)
{
	var thevar2=thevar+"=";
	var sstr="";
	sstr = location.search.substring(location.search.indexOf('?'));

	while (sstr.length>0)
	{
		sstr=sstr.substring(1);
		if(sstr.substring(0,thevar2.length)==thevar2)
		{
			sstr = sstr.substring(sstr.indexOf('=')+1);
			if(sstr.indexOf('&')>0)
			{
				sstr = sstr.substring(0,sstr.indexOf('&'));
			}
			return sstr;
		}
		sstr = sstr.substring(sstr.indexOf('&'));
	}

	return "";
}

function escapemore(e)
{
	re = new RegExp ("\\+", 'gi') ;
	e=e.replace(re, "%2b");
	return e;

}

function findValidObject(theelement)
{
	var isAnchor=false;
	if(theelement.tagName.toLowerCase()=="a")
	{
		isAnchor=true;
	}

	var par=theelement;
	var insideEditor=false;
	while(par)
	{
		if(!isAnchor && par.tagName=="A")
		{
			theelement=par;
			isAnchor=true;
		}
		if(par.tagName=="DIV" && par.contentEditable=="true")
		{
			insideEditor=true;
			break;
		}
		if(par.parentNode)
		{
			par=par.parentNode;
		}
		else
		{
			par=0;
		}
	}

	if(theelement.tagName.toLowerCase()=="a" && !insideEditor)
	{
		if(theelement.hostname && theelement.hostname.toLowerCase()=="secure.accessacs.com")
		{
			return theelement;	
		}
	}

	return false;
}

function handleExternalJump()
{
	var useNewImplementation=1;

	if(useNewImplementation)
	{
		hoverBlock.className="calloutlarge";
	}

	var theelement=hoverLink;

		if((theelement=findValidObject(theelement)))
		{
			//alert("don't go there!");
			var wl=window.location;
			var theurl=wl.protocol;
			if(wl.port)
			{
				theurl+=":"+wl.port;
			}

			theurl+= "//" + wl.hostname + wl.pathname+"?zone=" + getQueryVariable("zone") + "&ID=user.gotoacs&startUrl=" + escapemore(escape(theelement.href));

			if(document.getElementById("popupwindow"))
			{
				closePopupWindow();
			}

			if(!document.getElementById("popupwindow"))
			{
				var newiframe=document.createElement("iframe");
				if(newiframe)
				{
						newiframe.className="popup";
						newiframe.id="popup";
						newiframe.src=theurl + "&noframing=yes";
						newiframe.frameBorder="0";

						if(window.addEventListener)
						{

						}
						else
						{
							newiframe.allowTransparency=true;
						}
	

						if(!useNewImplementation)
						{
							var container=document.createElement("div");
							if(container)
							{
								container.id="popupwindow";
								container.className="popupcontainer";

								container.appendChild(newiframe);

								document.body.appendChild(container);
								window.scrollTo(0,0);
							}
						}
						else
						{
							if(hoverBlock.childNodes.length>=2)
							{
								hoverBlock.childNodes[1].innerHTML=""; // delete existing elements;
								hoverBlock.childNodes[1].appendChild(newiframe);
							}
						}
		//				if(e.preventDefault)
		//				{
		//					e.preventDefault();
		//				}
		//				if(window.event)
		//				{
		//					window.event.returnValue=false;
		//				}
					
				}
				else
				{
					theelement.href=theurl;
					theelement.target="";
				}
			}
		}

}

/*function checkOutgoingLink(e)
{
	var theelement=null;
	if(window.event)
	{
		theelement=event.srcElement;
	}
	else
	{
		theelement=e.target;
	}

	{
	}
}*/

function resizePopupWindow(unit)
{
	var useNewImplementation=1;

	if(!useNewImplementation)
	{
		var puw=document.getElementById("popupwindow");
		if(puw)
		{
			puw.style.height=unit + "px";
			puw.childNodes[0].style.height=unit + "px";
		}
	}
	else
	{
		if(hoverBlock && hoverBlock.childNodes.length>=2)
		{
			var puw=hoverBlock.childNodes[1];
			puw.style.height=unit + "px";
			puw.childNodes[0].style.height=unit + "px";
		}
	}
}

function closePopupWindow()
{
	var puw=document.getElementById("popupwindow");
	if(puw)
	{
		var puwparent=puw.parentNode;
		puwparent.removeChild(puw);
	}
}

function getOffset_copy(obj,type)
{
	var res=0;
	var prevObj=0;
	var positionVal='';
	while(obj.offsetParent)
	{
		if(type)
		{
			res+=obj.offsetTop;
		}
		else
		{
			res+=obj.offsetLeft;
		}
		prevObj=obj;
		obj=obj.offsetParent;
	}
	if(prevObj && prevObj.style)
	{
		if(window.getComputedStyle)
		{
			positionVal=window.getComputedStyle(prevObj,'').position;
		}
		else if(document.defaultView && document.defaultView.getComputedStyle)
		{
			positionVal=document.defaultView.getComputedStyle(prevObj,'').position;
		}
		else if(prevObj.currentStyle)
		{
			positionVal=prevObj.currentStyle.position;
		}
/*		if(positionVal=='absolute'||positionVal=='relative')
		{
			if(type)
			{
				res-=prevObj.offsetTop;
			}
			else
			{
				res-=prevObj.offsetLeft;
			}
		}*/
	}
	return res;
}

function restoreCallout()
{
	var supportsOpacity=1;

	if(hoverTimeout)
	{
		clearTimeout(hoverTimeout);
		hoverTimeout=0;
	}
	if(hoverFading)
	{
		clearTimeout(hoverFading);
		hoverFading=0;

		if(hoverBlock)
		{
			if(window.addEventListener)
			{
				if(supportsOpacity)
				{
					hoverBlock.style.opacity=100;
				}
			}
			else
			{
				hoverBlock.style.filter="";
			}
		}
	}
	
}

function hoverItemMouseOver(e)
{
	var theelement=null;
	if(window.event)
	{
		theelement=event.srcElement;
	}
	else
	{
		theelement=e.target;
	}

	restoreCallout();
}

function hoverItemMouseOut(e)
{
	var theelement=null;
	if(window.event)
	{
		theelement=event.srcElement;
	}
	else
	{
		theelement=e.target;
	}

	if(!hoverTimeout)
	{
		hoverTimeout=window.setTimeout(hideHoverEvent, 500);
	}
}

function hideCallout()
{
	var supportsOpacity=1;

	hoverOpacity-=20;

	if(hoverOpacity>0)
	{
		if(window.addEventListener)
		{
			if(supportsOpacity)
			{
				hoverBlock.style.opacity=(hoverOpacity/100);
			}
		}
		else
		{
			hoverBlock.style.filter="progid:DXImageTransform.Microsoft.Alpha( style=0,opacity=" + hoverOpacity + ");";
		}
		hoverFading=window.setTimeout(hideCallout, 50);

	}
	else
	{
		hoverBlock.style.display="none";
		if(window.addEventListener)
		{
			hoverBlock.style.opacity=100;
		}
		else
		{
			hoverBlock.style.filter="";			
		}
		hoverFading=0;
	}
}

function checkOutgoingLinkMouseOver(e)
{
	var theelement=null;
	if(window.event)
	{
		theelement=event.srcElement;
	}
	else
	{
		theelement=e.target;
	}

	var modified=false;

	if((theelement=findValidObject(theelement)))
	{
		restoreCallout();
			
		if(!hoverBlock)
		{
			hoverBlock=document.createElement("div");
			if(hoverBlock)
			{
				modified=true;

				var hoverTop=document.createElement("div");
				var hoverMid=document.createElement("div");
				var hoverBottom=document.createElement("div");

				hoverTop.className="callouttop";
//				hoverTop.style.backgroundImage="url(images/callout/callout_top.gif)";
//				hoverTop.style.backgroundRepeat="no-repeat";
//				hoverTop.style.position="relative";
//				hoverTop.style.width="292px";
//				hoverTop.style.height="25px";

				hoverMid.className="calloutmiddle";
//				hoverMid.style.backgroundImage="url(images/callout/callout_middle.gif)";
//				hoverMid.style.backgroundRepeat="repeat-y";
//				hoverMid.style.paddingLeft="5px";
//				hoverMid.style.width="282px";
//				hoverMid.style.paddingRight="5px";

				hoverBottom.className="calloutbottom";
//				hoverBottom.style.backgroundImage="url(images/callout/callout_bottom.gif)";
//				hoverBottom.style.backgroundRepeat="no-repeat";
//				hoverBottom.style.position="relative";
//				hoverBottom.style.width="292px";
//				hoverBottom.style.height="10px";

				hoverBlock.appendChild(hoverTop);
				hoverBlock.appendChild(hoverMid);
				hoverBlock.appendChild(hoverBottom);

				if(window.addEventListener)
				{
					hoverBlock.addEventListener('mouseover',hoverItemMouseOver,false);
					hoverBlock.addEventListener('mouseout',hoverItemMouseOut,false);
				}
				else
				{
					hoverBlock.attachEvent('onmouseover',hoverItemMouseOver);
					hoverBlock.attachEvent('onmouseout',hoverItemMouseOut);
				}

//				hoverBlock.style.width="292px";
//				hoverBlock.style.zIndex=2000;
//				hoverBlock.style.position="absolute";
	
				hoverBlock.className="calloutsmall";

				document.body.appendChild(hoverBlock);
			}
		}

		// handle hovering over the link should not cause the callout to reset! if the link is the same link that made the callout grow

		var xcoor=getOffset_copy(theelement,0);
		var ycoor=getOffset_copy(theelement,1);

		if(modified || hoverBlock.style.display=="none" || hoverLink!=theelement)
		{
			// reset contents
			hoverBlock.className="calloutsmall";
			if(hoverBlock.childNodes.length>=2)
			{
				hoverBlock.childNodes[1].innerHTML="<div style='text-align:right;font-weight:bold;color:#aaaaaa;'>GOTO ACCESS ACS</div><a href='javascript:handleExternalJump();' style='font-weight:bold;'>Sign in</a> with website ID and password.";
				hoverBlock.childNodes[1].style.height="auto";
			}
		}

		hoverBlock.style.left=xcoor + "px";
		hoverBlock.style.top=(ycoor + theelement.offsetHeight) + "px";
		hoverBlock.style.display="";

		hoverLink=theelement;
	}
}

function hideHoverCallout(force)
{
	if(!force && hoverBlock.className=="calloutlarge")
	{	/** If callout is large, do not hide */
		return;
	}
	hoverTimeout=0;
	//hoverBlock.style.display='none';
	hoverOpacity=100;
	hoverFading=window.setTimeout(hideCallout, 100);
}

function hideHoverEvent()
{
	hideHoverCallout(0);
}

function closeCallout()
{
	hideHoverCallout(1);
}

function checkOutgoingLinkMouseOut(e)
{
	var theelement=null;
	if(window.event)
	{
		theelement=event.srcElement;
	}
	else
	{
		theelement=e.target;
	}

	if((theelement=findValidObject(theelement)))
	{
		if(hoverBlock && hoverBlock.style.display!='none')
		{
//			hoverBlock.style.display='none';
			if(!hoverTimeout)
			{
					hoverTimeout=window.setTimeout(hideHoverEvent, 500);
			}
		}
	}
}

if(window.addEventListener)
{
//	document.addEventListener('click',checkOutgoingLink,false);
	document.addEventListener('mouseover',checkOutgoingLinkMouseOver,false);
	document.addEventListener('mouseout',checkOutgoingLinkMouseOut,false);
}
else
{
//	document.attachEvent('onclick',checkOutgoingLink);
	document.attachEvent('onmouseover',checkOutgoingLinkMouseOver);
	document.attachEvent('onmouseout',checkOutgoingLinkMouseOut);
}

