var magComing = false;
var parentDiv = document.getElementById("parentSearchDiv");
var parentIFrameDiv = document.createElement("Div");
var openCounter = 0;

parentIFrameDiv.innerHTML = "<iframe id='ifrmHideSearch' src='about:blank' scrolling='no' frameborder='0' style='position:absolute;width:150px;height:120px;top:100;left:-3000;border:none;display:block;z-index:99999'></iframe>";
parentDiv.appendChild(parentIFrameDiv);

  function resultsPaneCounter() {
    if ( openCounter > 0 ) {
      openCounter--;
      if ( openCounter == 0 ) {
	closeSearchDiv();
      }
    }
    setTimeout("resultsPaneCounter()",1000);
  }
  setTimeout("resultsPaneCounter()",1000);

  function showMagnify( event, query ) {
    getQuery( event, query ) ;
    //		return escapeSearch( event, getQuery( query ) );
  }

  function cancelMag() {
          magComing = false;
  }

  var onSearch = false;

  function escapeSearch( event, returnXml ) {
    openCounter = 3;
   // setTimeout("closeSearchDiv()",2000);
  }

  function closeSearchDiv() {
    if ( !onSearch ) {
      var oDiv = document.getElementById("keyWord");
      if ( oDiv != null ) {
              parentDiv.removeChild( oDiv );
             parentDiv.style.display = "none"; 
      }

      var ifrm1 = document.getElementById("ifrmHideSearch");
      if ( ifrm1 != null ) {
              ifrm1.style.top = 0 + "px";
              ifrm1.style.left = -1000;
      }
    } else {
    //  setTimeout("closeSearchDiv()",2000);
    }
   }


  function getMouseCoord(e) {

            var mousex,mousey;

            if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

            if (e)
            {
                    if (e.pageX || e.pageY)
                    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
                    mousex = e.pageX;
                    mousey = e.pageY;
                    }
                    else if (e.clientX || e.clientY)
                    { // works on IE6,FF,Moz,Opera7
                    // Note: I am adding together both the "body" and "documentElement" scroll positions
                    //       this lets me cover for the quirks that happen based on the "doctype" of the html page.
                    //         (example: IE6 in compatibility mode or strict)
                    //       Based on the different ways that IE,FF,Moz,Opera use these ScrollValues for body and documentElement
                    //       it looks like they will fill EITHER ONE SCROLL VALUE OR THE OTHER, NOT BOTH
                    mousex = (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft)+"px";
                    mousey = (e.clientY + document.body.scrollTop + document.documentElement.scrollTop)+"px";
                    }
            }
            return mousex+","+mousey;
    }


    function cInclude(id, url) {
var req = false;
// For Safari, Firefox, and other non-MS browsers
if (window.XMLHttpRequest) {
        try {
        req = new XMLHttpRequest();
        } catch (e) {
        req = false;
        }
} else if (window.ActiveXObject) {
        // For Internet Explorer on Windows
        try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
        try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
                req = false;
        }
        }
}


if (req) {
        // Synchronous request, wait till we have it all
        req.open('GET', url, false);
        req.send(null);
	if ( req.responseText.indexOf("cannot") != -1 ) {
		return "";
	} else {
       		 return req.responseText;
	}

} else {
        element.innerHTML =
"Sorry, your browser does not support " +
        "XMLHTTPRequest objects. This page requires " +
        "Internet Explorer 5 or better for Windows, " +
        "or Firefox for any system, or Safari. Other " +
        "compatible browsers may also exist.";
}

}


   function getQuery( event, params ) {

        closeSearchDiv();

        var divContents = "<div class=searchpop >";
        divContents += "<div class=isp >";
        divContents += "<table border='0' cellpadding='0' cellspacing='0' width='220'><tr><td><table border='0' cellpadding='3' cellspacing='1' width='100%'><tr><td style='text-align:left;padding:3px;' align='left'>";
        divContents += "<H4>Loading...</H4>";
        divContents += "</td></tr></table></td></tr></table>";
        divContents += "</div></div>";

        oDiv=document.createElement("DIV");
        oDiv.id = "keyWord";
        oDiv.style.position = "absolute";

        var coord = getMouseCoord(event).split(",");
        var xLoc = parseInt(coord[0],10);
        var yLoc = parseInt(coord[1],10);
        oDiv.style.left = (xLoc + 5)+"px";
        oDiv.style.top = (yLoc)+"px";
        oDiv.style.background = '#f6fcff';
        oDiv.style.zIndex = 100000;
        oDiv.style.cursor="hand";
        oDiv.style.position = "absolute";
        oDiv.style.margin = 5;
        oDiv.style.border = "solid 1px #cccccc";

        //oDiv.onmouseleave = closeSearchDiv;
        oDiv.innerHTML = divContents;
        parentDiv.appendChild(oDiv);
       parentDiv.style.display = "block"; 
        oDiv.style.width = ( parseInt(oDiv.offsetWidth,10) - 9 ) + "px";
        oDiv.style.height = ( oDiv.offsetHeight) + "px";
        oDiv.style.border = "solid 1px #cccccc";

        var ifrm1 = document.getElementById("ifrmHideSearch");
        if ( ifrm1 != null ) {
          ifrm1.style.top = yLoc - 5;
          ifrm1.style.left = xLoc + 10;
          ifrm1.style.width = (parseInt(oDiv.offsetWidth,10) - 9 ) + "px";
          ifrm1.style.height = oDiv.offsetHeight+ "px";
        }

        event.returnValue = false;

        var scrollTop = document.body.scrollTop;
        var windowHeight = parseInt(document.body.clientHeight,10);
        var boxPosition = 350 + (yLoc-parseInt(scrollTop,10));
        boxPosition = parseInt(windowHeight,10) - boxPosition;

        if(boxPosition > 0){
            boxPosition = 0;
        }


         setTimeout("showResults('"+params+"', "+boxPosition+", "+xLoc+", "+yLoc+")",1);




    // return null;
  }

  function resetCloseCounter() {

  }

  function showResults( params, boxPosition, xLoc, yLoc) {

	closeSearchDiv();

	var innerDiv=document.createElement("DIV");
	innerDiv.id = "keyWord";
	innerDiv.style.left = (xLoc + 5)+"px";
	innerDiv.style.top = ( yLoc + boxPosition + 5)+"px";
	innerDiv.style.background = '#f6fcff';
	innerDiv.style.zIndex = 100000;
	innerDiv.style.cursor="hand";
	innerDiv.style.position = "absolute";
	innerDiv.style.margin = 5;
	innerDiv.style.border = "solid 1px #cccccc";
	
	var thisArticle = getQueryVariable( "theArt" );
	var randomnumber = Math.floor(Math.random()*1000001);
	
	var results = cInclude(null, "/Articles/SearchContents.aspx?target=" + params + '&theArt=' + thisArticle + '&' + randomnumber );
	
	if ( results.indexOf("</Record>") != -1 ) {
		var output = "";
		var resultsArray = results.split("</Record>");
		for ( var t=0; t<resultsArray.length-1; t++ ) {
			output += ( "<p><strong>" + getElement( resultsArray[t], "Rank" ) + ".  " );
			
			var title = getElement( resultsArray[t], "Title" );
			
			if ( title == "" ) {
				title = "untitled";
			}
			output += ( title );
			
			var summary = getElement( resultsArray[t], "Summary" );
			if ( summary.length > 150 ) {
				summary = summary.substring(0,150);
			}
			output += ( "</strong><br />" + summary + "..." );
			output += ( "</p>" );
		}


	        var divContents = "<div class=searchpop onmouseout='openCounter = 1;'>";
	        divContents += "<div class=isp onmouseover='openCounter = 3;'>";
	        divContents += output;
	
	        divContents += "</div></div>";
	        
	
	        innerDiv.innerHTML = divContents;
	        parentDiv.appendChild(innerDiv);
	        parentDiv.style.display = "block";
	
	        innerDiv.style.width = ( parseInt(innerDiv.offsetWidth,10) - 4 ) + "px";
	        innerDiv.style.height = ( innerDiv.offsetHeight) + "px";
	
	        var ifrm1 = document.getElementById("ifrmHideSearch");
	        if ( ifrm1 != null ) {
			ifrm1.style.top = (yLoc + boxPosition + 10) + "px";
			ifrm1.style.left = (xLoc + 10) + "px";
			ifrm1.style.width = ( parseInt(innerDiv.offsetWidth,10) ) + "px";
			ifrm1.style.height = ( parseInt(innerDiv.offsetHeight,10) - 4 ) + "px";
	        }
        }

  }

   function replacestring(str_normal,str_find,str_replace,int_case_insensitive)
    {
            if (arguments.length<3 || str_find=="" || str_normal=="" || typeof("".split)!="function")
                    return(str_normal);

            //no parm means default, "case SENSITIVE"...
            if(!(int_case_insensitive))
                    return(str_normal.split(str_find)).join(str_replace);

            str_find=str_find.toLowerCase();

            var rv="";
            var ix=str_normal.toLowerCase().indexOf(str_find);
            while(ix>-1)
            {
                    rv+=str_normal.substring(0,ix)+str_replace;
                    str_normal=str_normal.substring(ix+str_find.length);
                    ix=str_normal.toLowerCase().indexOf(str_find);
            }
            return(rv+str_normal);
    }

    function openTheWindow( href ){

		window.open( href );

    }


      function getElement( events, name ) {
	if ( events.indexOf("<"+name+">") != -1 ) {
           return events.substring( events.indexOf( "<"+name+">" ) + ( "<"+name+">" ).length, events.indexOf( "</"+name+">" ) );
	} else {
        return null;
	}
    }


    function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                    var pair = vars[i].split("=");
                    if (pair[0] == variable) {
                    return pair[1];
                    }
            }
            return null;
    }


