//Rich HTML Balloon Tooltip: http://www.dynamicdrive.com/dynamicindex5/balloontooltip.htm
//Created: September 10th, 2006
// many edits by timothy sink: http://www.squaredtechnologies.com/

var disappeardelay=2000;  //tooltip disappear delay (in miliseconds)
var verticaloffset=0; //vertical offset of tooltip from anchor link, if any
var enablearrowhead=1; //0 or 1, to disable or enable the arrow image
var arrowheadimg=["arrowdown.gif", "arrowup.gif"]; //path to down and up arrow images
var arrowheadheight=11; //height of arrow image (amount to reveal)
var iframeparser="http://www.squaredtechnologies.com/cgi-bin/wpltest.pl?plink=";
var bdebug = false;

var iX=0;
var i=0;
var lCustomInsertAdjacentHTML=false;
var lhelp = false; 
var helpChild=document.createElement("helpElementdiv");

if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
	lCustomInsertAdjacentHTML=true;
	var helpRange = document.createRange();
}
else
{
	//help MENU BUILDER FOR IE
//	for(iX=1;iX<=helpCounter;iX++)mainChild.insertAdjacentHTML("beforeEnd",helpMenu[iX]); 
}			

//////////// usefull functions

function changeImg(imgid,imgsrc,ctooltext)
{
	if(imgid!=null && imgsrc!=null)document.getElementById(imgid).src = imgsrc;

	if(ctooltext!=null)
	{
	
	}
}

function search()
{
       if(document.getElementById('ss2').checked )
               parent.MainPage.location="http://www.squaredtechnologies.com/store/TSearch.asp?txtSearchFor=" + document.getElementById('sbi').value ;
       else document.getElementById('Google').submit();

}

///////////////////create the cwindow2 floating slide window


/////////parsing functions

function help(hrefhelper)
{
	lhelp =!lhelp ;
	if(!lhelp )
	{
		while(document.getElementById('helpDiv').hasChildNodes())
		 document.getElementById('helpDiv').removeChild(document.getElementById('helpDiv').firstChild);
		lhelp =!lhelp ;
	}
	if(hrefhelper!=null)	
	{
		if(lCustomInsertAdjacentHTML) // FOR NS
		{
			helpRange.setStartBefore(document.getElementById('helpDiv'));
			helpChild = helpRange.createContextualFragment(hrefhelper);	
			document.getElementById('helpDiv').appendChild(helpChild);
		}
		else
		{
			helpChild.innerhtml=hrefhelper;
//			helpChild.insertAdjacentHTML("beforeEnd",hrefhelper);
			document.getElementById('helpDiv').appendChild(helpChild) // FOR IE
		}
	}
	
}

function include(url)  // must be a fully qualified URL, I don't know why.
{


if(bdebug)var win2=window.open("","","menubar,scrollbars,status");
if(bdebug)win2.document.write("Get: "+url+"<P>");

var xmlhttp=false;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

 } catch (e) {
  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
//@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

	if(!xmlhttp && bdebug)win2.document.write("bad xml object!<P>");
	
	xmlhttp.open( "GET", url, true );
  	xmlhttp.setRequestHeader("User-Agent", "MyCustomUser" ); //'Accept','message/x-jl-formresult');
    xmlhttp.send(null);
	xmlhttp.onreadystatechange=function() 
	{
  		if (xmlhttp.readyState==4) 
		{
		
			if(bdebug)
			{
				if (xmlhttp.status==200) win2.document.write("URL EXISTS!<P>");
				else if (xmlhttp.status==404) win2.document.write("URL doesn't exist!<P>");
				else win2.document.write("Status is "+xmlhttp.statusText+"<P>");
			}
			
			str=xmlhttp.responseText;
			if(bdebug) win2.document.write("RETURN = " +str + "<P>");
			help(str);

		}

	}
	if(bdebug) win2.document.close();

}

function parselinks()
{
// if (lhelp)help('');
// var links2=cwindow2.document.body.innerHTML;

	var links2=document.getElementById('aw0');

	if(bdebug)
	{
		var win2=window.open("","","menubar,scrollbars,status");
		if(links2!=null)
		{
			if(links2.href != "") win2.document.write(links2.href);
			else win2.document.write('EMPTY');
		}
		else win2.document.write("NULL");

		win2.document.close();
	}

	if(links2!=null)
	{
		// add &nm=4&nh=1&jca=9553
		links2.href+="&nm=4&nh=1&jca=9553";
		 document.getElementById('cwindow2').src=links2.href;

	}

	help(null);
// help(links2);
}

// functions added 9/30/9
function extractlinks(element)
{
	if (document.all||document.getElementById)
	{
		if (document.all)
			var links=document.all.tags("A")
		else if (document.getElementById)
			var links=document.getElementsByTagName("A")		


		var total=links.length
		if(bdebug)
		{
			var win2=window.open("","","menubar,scrollbars,status")
			win2.document.write("<h2>Total Links="+total+"</h2><br>")
		
			for (i=0;i<total;i++)
			{
				win2.document.write('<a href="'+links[i]+'">'+links[i].innerHTML+'</a><br>')
			}
		}

		var chtml=document.getElementById(element).innerHTML;
		var mText = chtml.replace(/</g," ");
		
		if(bdebug) win2.document.write( "READS: "+ mText +'<br>');
		
		var links2 = document.getElementById(element).getElementsByTagName("IFRAME")
		total  = links2.length
		if(bdebug) win2.document.write( "<p>Iframes:<BR>");
			
		var intIndexOfMatch = -1;
		var strReplaceAll = "";
		
//               if (lhelp)help('');

		for (i=0;i<total;i++)
		{
			
			strReplaceAll = links2[i].src;
			intIndexOfMatch = strReplaceAll.indexOf("&");
			while (intIndexOfMatch != -1)
			{
				strReplaceAll = strReplaceAll.replace( "&", "~" )
  				intIndexOfMatch = strReplaceAll.indexOf( "&" );
			}			
			intIndexOfMatch = strReplaceAll.indexOf("%");
			while (intIndexOfMatch != -1)
			{
				strReplaceAll = strReplaceAll.replace( "%", "!" )
  				intIndexOfMatch = strReplaceAll.indexOf( "%" );
			}			
			
			if(bdebug) win2.document.write(strReplaceAll +"<P>");			
			document.getElementById('cwindow2').src=iframeparser+strReplaceAll
//			include(document.getElementById('cwindow2').innerText);
//			document.getElementById('helpDiv').src = iframeparser+strReplaceAll ;

		}


		setTimeout('help(cwindow2.document.body.innerHTML)',2000);
//		setTimeout('parselinks()',1000);
		
//		var links3 = document.getElementById('aw0');
//		var total3  = links3.length;
		if(bdebug)
		{
//			win2.document.write( "<p>Links to auto click:<BR>");
//			if(links3!=null) win2.document.write(links3.href);
//			for (i=0;i<total3;i++)
//			{
//				win2.document.write('<a href="'+links3[i]+'">'+links3[i].innerHTML+'</a><br>')
//			}
	
			win2.document.close()
		}
		
		
		
	}
}

/////No further editting needed
// balloon tip functions

var ie=document.all
var ns6=document.getElementById&&!document.all
verticaloffset=(enablearrowhead)? verticaloffset+arrowheadheight : verticaloffset

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function showhide(obj, e){
dropmenuobj.style.left=dropmenuobj.style.top="-500px"
if (e.type=="mouseover")
obj.visibility="visible"
//else
//obj.visibility="hidden"
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
if (whichedge=="rightedge"){
edgeoffsetx=0
var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffsetx=dropmenuobj.contentmeasure-obj.offsetWidth
return edgeoffsetx
}
else{
edgeoffsety=0
var topedge=ie && !window.opera? iecompattest().scrollTop : window.pageYOffset
var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure) //move up?
edgeoffsety=dropmenuobj.contentmeasure+obj.offsetHeight+(verticaloffset*2)
return edgeoffsety
}
}

function displayballoontip(obj, e){ //main ballooon tooltip function
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
if (typeof dropmenuobj!="undefined") //hide previous tooltip?
dropmenuobj.style.visibility="hidden"
clearhidemenu()
//obj.onmouseout=delayhidemenu
dropmenuobj=document.getElementById(obj.getAttribute("rel"))
showhide(dropmenuobj.style, e)
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")+verticaloffset
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
if (enablearrowhead)
displaytiparrow()
}

function displaytiparrow(){ //function to display optional arrow image associated with tooltip
tiparrow=document.getElementById("arrowhead")
tiparrow.src=(edgeoffsety!=0)? arrowheadimg[0] : arrowheadimg[1]
var ieshadowwidth=(dropmenuobj.filters && dropmenuobj.filters[0])? dropmenuobj.filters[0].Strength-1 : 0
//modify "left" value depending on whether there's no room on right edge of browser to display it, respectively
tiparrow.style.left=(edgeoffsetx!=0)? parseInt(dropmenuobj.style.left)+dropmenuobj.offsetWidth-tiparrow.offsetWidth-10+"px" : parseInt(dropmenuobj.style.left)+5+"px"
//modify "top" value depending on whether there's no room on right edge of browser to display it, respectively
tiparrow.style.top=(edgeoffsety!=0)? parseInt(dropmenuobj.style.top)+dropmenuobj.offsetHeight-tiparrow.offsetHeight-ieshadowwidth+arrowheadheight+"px" : parseInt(dropmenuobj.style.top)-arrowheadheight+"px"
tiparrow.style.visibility="visible"
}

function delayhidemenu(){
delayhide=setTimeout("parselinks(); dropmenuobj.style.visibility='hidden'; dropmenuobj.style.top=-500; dropmenuobj.style.left=0; if (enablearrowhead) tiparrow.style.visibility='hidden'",disappeardelay)
}

function clearhidemenu(){
if (typeof delayhide!="undefined")
{
//	extractlinks();
	dropmenuobj.style.left=0;
	dropmenuobj.style.top=-500;
	dropmenuobj.style.visibility='hidden';
	clearTimeout(delayhide);
	parselinks();
}
}

function reltoelement(linkobj){ //tests if a link has "rel" defined and it's the ID of an element on page
var relvalue=linkobj.getAttribute("rel")
return (relvalue!=null && relvalue!="" && document.getElementById(relvalue)!=null && (document.getElementById(relvalue).className=="balloonstyle" || document.getElementById(relvalue).className=="balloonwide"))? true : false

}

function initalizetooltip(){
var all_links=document.getElementsByTagName("a")
if (enablearrowhead){
tiparrow=document.createElement("img")
tiparrow.setAttribute("src", arrowheadimg[0])
tiparrow.setAttribute("id", "arrowhead")
document.body.appendChild(tiparrow)
}
for (var i=0; i<all_links.length; i++){
if (reltoelement(all_links[i])){ //if link has "rel" defined and it's the ID of an element on page
all_links[i].onmouseover=function(e){
var evtobj=window.event? window.event : e
displayballoontip(this, evtobj)

extractlinks( this.getAttribute("rel"));
}
all_links[i].onmouseout=delayhidemenu
}
}
}

if (window.addEventListener)
window.addEventListener("load", initalizetooltip, false)
else if (window.attachEvent)
window.attachEvent("onload", initalizetooltip)
else if (document.getElementById)
window.onload=initalizetooltip

