function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_jumpMenuGo(selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	var value = new String(selObj.options[selObj.selectedIndex].value);
	if (value.substr(value.length - 3) == "pdf") {
		targ = "pdf";
		window.open(value, targ);
	}
	else {
		eval(targ + ".location='" + value + "'");
	}
}

function jumpto(x){

  if (document.form.jumpmenu.value != "null") {
	  window.open(x);
  }
}



//Seattle.gov - Javascript
function testBrowser(){
  if(document.all){
  document.write('<form style="MARGIN:0PX;PADDING:0PX;" name="searchBOX" method="get" action="http://search.cityofseattle.net/search/">')
  document.write(' SEARCH: ')
  document.write('<input type="hidden" name="sp_a" value="sp10016c6f">')
//  document.write('<input type="hidden" id="spt" name="sp_t" value="seattle.tpl">')
  document.write('<input type="hidden" id="spk" name="sp_k" value="">')
  document.write('<input onclick="this.value=\'\'" style="font-size:10px;width:145px" value="Seattle.Gov" name="sp_q"> ')
  document.write('<input style="font-size:10px" type="submit" value="Go">')
  document.write('</form>')
  }else{
  document.write('<form name="searchBOX" method="get" action="http://search.cityofseattle.net/search/">')
  document.write(' SEARCH: ')
  document.write('<input style="position: absolute; visibility: hidden;" type="hidden" name="sp_a" value="sp10016c6f">')
//  document.write('<input type="hidden" id="spt" name="sp_t" value="Seattle">')
  document.write('<input style="position: absolute; visibility: hidden;" type="hidden" id="spk" name="sp_k" value="">')
  document.write('<input style="font-size:12px;" size="10" name="sp_q"> ')
  document.write('<input style="font-size:12px" type="submit" value="Go">')
    }
}

var offsetXPoint=0 // Customize x offset of tooltip
var offsetYPoint=0 // Customize y offset of tooltip
var thediv;
var tooltipDelay = 250;  // Tooltip disappear speed onMouseout (in miliseconds)
var ie = document.all;
var ns6 = document.getElementById && !document.all;
var tipPositionLeft = "";
var tipPositionTop = "";

function titleManager() {
	var args = titleManager.arguments;
	switch (args[0])
	{
		case "make_div":
			thediv = document.all? document.all["tooltipLayer"] : document.getElementById? document.getElementById("tooltipLayer") : ""
			thediv.onmouseover = setBlock;
			thediv.onmouseout = setNone;
			break;
		case "attach_events":
			if ( args[ 1 ].addEventListener )
			{
			args[ 1 ].addEventListener( args[ 2 ], args[ 3 ], args[ 4 ] );
			return true;
			}
			else  ( args[ 1 ].attachEvent )
			{
			return args[ 1 ].attachEvent( "on" + args[ 2 ], args[ 3 ] );
			}
			break;
		case "detach_events":
			if (args[1].removeEventListener) {
				return args[1].removeEventListener(args[ 2 ],args[ 3 ],args[ 4 ]);
			}
			else {
				(args[1].detachEvent)
			}
			return args[1].detachEvent("on"+args[ 2 ],args[ 3 ]);
			break;
	}
}

function positionTip(e) {
	var curX=(ns6)? e.pageX : event.clientX + getIEBody().scrollLeft;
	var curY=(ns6)? e.pageY : event.clientY + getIEBody().scrollTop;

	//Find out how close the mouse is to the corner of the window
	var rightedge=ie&&!window.opera? getIEBody().clientWidth-event.clientX-offsetXPoint : window.innerWidth-e.clientX-offsetXPoint-20
	var bottomedge=ie&&!window.opera? getIEBody().clientHeight-event.clientY-offsetYPoint : window.innerHeight-e.clientY-offsetYPoint-20

	var leftedge=(offsetXPoint<0)? offsetXPoint*(-1) : -1000

	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge<thediv.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		tipPositionLeft = ie? getIEBody().scrollLeft+event.clientX-thediv.offsetWidth+"px" : window.pageXOffset+e.clientX-thediv.offsetWidth+"px"
	else if (curX<leftedge)
		tipPositionLeft = "5px"
	else
		//position the horizontal position of the menu where the mouse is positioned
		tipPositionLeft = curX+offsetXPoint+"px"

	//same concept with the vertical position
	if (bottomedge<thediv.offsetHeight)
		tipPositionTop = ie? getIEBody().scrollTop+event.clientY-thediv.offsetHeight-offsetYPoint+"px" : window.pageYOffset+e.clientY-thediv.offsetHeight-offsetYPoint+"px"
	else
		tipPositionTop = curY+offsetYPoint+"px"


}

function getIEBody() {
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function tooltipStart(obj) {
	thediv = document.all? document.all["tooltipLayer"] : document.getElementById? document.getElementById("tooltipLayer") : "";

	// Only link anchor tags
	var gettags = document.getElementsByTagName("a");
	for ( var j = 0, thelength2 = gettags.length; j < thelength2; j ++ )
	{
		var el = gettags[j];
		var thetitle = el.getAttribute("title");
		var href = el.getAttribute("href");
		if (!thetitle)
		{
			continue;
		}
		// Only link hrefs with "#tooltip".
		if(String(href).indexOf("#tooltip") >= 0) {
			el.setAttribute("alttitle", thetitle);
			el.setAttribute("title", "");
			titleManager('detach_events', el, 'mouseover', showTip, false);
			titleManager('detach_events', el, 'mouseout', hideTip, false);
			titleManager('attach_events', el, 'mouseover', showTip, false);
			titleManager('attach_events', el, 'mouseout', hideTip, false);
		}
	}

	document.onmousemove = positionTip;
}


function showTip(evt) {
	if(ie || ns6) {
		if (window.event) event.cancelBubble=true
		else if (evt.stopPropagation) evt.stopPropagation()
		clearHideTip();
		var el = evt.currentTarget || evt.srcElement;
		var title = el.getAttribute('title');
		content = el.getAttribute('alttitle');
		titleManager('make_div');
		thediv.innerHTML = autoLink(content);
		thediv.style.display = "block";
		thediv.style.top = tipPositionTop;
		thediv.style.left = tipPositionLeft;

		// Leave visible if user on tooltip
		if (thediv) {
			thediv.onmouseout = tipOutCheck;
			thediv.onmouseover = clearHideTip;
		}
	}
}

function setBlock() {
	thediv.style.display = "block";
}

function hideTip() {
	if(ie||ns6) {
		delayhide = setTimeout("setNone()", tooltipDelay);
	}
}

function clearHideTip() {
	if(typeof delayhide != "undefined") {
		clearTimeout(delayhide);
	}
}

function contained(oNode, oCont) {
  if (!oNode) return; // in case alt-tab away while hovering (prevent error)
  while ( oNode = oNode.parentNode ) if ( oNode == oCont ) return true;
  return false;
}


function getDOM(e) {
    e = e ? e: window.event;
    e.tgt = e.srcElement? e.srcElement: e.target;

    if (!e.preventDefault) e.preventDefault = function () { return false; }
    if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }

    return e;
}

function tipOutCheck(e) {
	e = getDOM(e);
	var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
	if ( this != toEl && !contained(toEl, this) ) hideTip();
}

function setNone() {
	thediv.style.display = "none";
}

function autoLink(str) {

	var regLink = /((mailto\:|(news|(ht|f)tp(s?))\:\/\/){1}\S+)/g;

	// Replace links
	return str.replace(regLink,"<a href=\"$1\" target=\"_new\">$1</a>");
}

window.onload = tooltipStart;
