// This script is a combination of showhide.js and twisty.js - modified by
// Lewis Gault (2005) for Altera Corporation

// global variables
// using these to eliminate need to change Javascript code *much* to change image names
// strings: string representation of image names in page

var state = "hidden"; // default state
var path = "/support/software/nativelink/quartus2/image/"; // path to image directory - this is not the standard path, special for the EDA project
var btnshowall = "btnshowall.gif";
var btnhideall = "btnhideall.gif";
var btnshowall_hover = "btnshowall_hover.gif";
var btnhideall_hover = "btnhideall_hover.gif";
var img_expanded = "arrowdown.gif";
var img_collapsed = "arrowright.gif";

$hover = 0;

function hover() {
	if (document.images) {
		if(document.images.showall.src.indexOf(btnshowall)!=-1) {
			document.images.showall.src = path+btnshowall_hover;
		} else {
			if(document.images.showall.src.indexOf(btnshowall_hover)!=-1) {
				document.images.showall.src = path+btnshowall;
			} else {
				if(document.images.showall.src.indexOf(btnhideall)!=-1) {
						document.images.showall.src = path+btnhideall_hover;
				} else {
					if(document.images.showall.src.indexOf(btnhideall_hover)!=-1) {
						document.images.showall.src = path+btnhideall;
					}
				}	
			}
		}
	}
}



// original showhide.js script
// ***************************

/*
This script was originally written by Greg Swearingen in January 2004 when the
previous method, based on Tommy Simmons script, was found not to work in later
versions of RH.
It was then found that the new script was not compatible with RH X4 or later.
Paul Symonds of Cedar Software rewrote part of it in March 2004 to make it
work with RH X4 and has made it backwards compatible.
At the same time he has amended it so that only one javascript file is required
for topics at all folder levels.
Peter Grainge 090304 */

//arrayofDivs contains each entire div tag as an individual object
//arrayofSpans contains each entire span tag as an individual object
//arrayofLinks contains all of the href values for the a tag
//jobCheck determines whether you want to Show All or Hide All based on the button image

// lg 01/06 changed jobcheck to be independent of button image

var arrayofDivs = document.getElementsByTagName("div");
var arrayofSpans = document.getElementsByTagName("SPAN");
var arrayofLinks = document.getElementsByTagName("a");

// ****************************************

function jobCheck(imageName) 
{
	if (state=="hidden") {
		showEm();
		state = "shown";
	} else if (state=="shown") {
		hideEm();
		state = "hidden";
	}
}


// ****************************************
//PSymonds added 8/3/2004

function findRef(divId)
{
	try
	{
		for (var i=0;i<gPopupData.length;i++)
		{
			linkAttribute=gPopupData[i].popupId;
			linkAttribute = linkAttribute.substr(1, linkAttribute.length-1); 
			////alert("checking mappings: " + linkAttribute);
			
			if (linkAttribute==divId)
			{
				aId = gPopupData[i].el;
				////alert("matching id found: link id: " + aId);
				return aId;
			}
		}    
	}
	catch(e)
	{
		//Silently swallow exceptions  
		//Bad practice but allows this script to work with
		//versions < x4 that don't have a ref to 'gPopupData'
	}
}

// ****************************************

function showEm() 
{
	if ( arrayofDivs != null ) 
	{
		for (m = 0; m < arrayofDivs.length; m++) 
		{
			if ( arrayofDivs[m].style.display == "none") 
			{
				gergLink = arrayofDivs[m].id;
				gergLink = gergLink.substr(0, gergLink.length-4); //this is the id of the "a" link
				for (l = 0; l < arrayofLinks.length; l++) 
				{
					linkAttribute = arrayofLinks[l].getAttribute( "x-use-popup" );
					if( linkAttribute ) 
					{
						if( linkAttribute.substr(0,1) == "#" ) 
						{
							linkAttribute = linkAttribute.substr(1, linkAttribute.length-1);
						}
					}
					else
					{
						//find a ref for this div
						aId = findRef(arrayofDivs[m].id);
						
						if(arrayofLinks[l].id == aId)
						{
						 	////alert("got matching id");
						 	kadovTextPopup(arrayofLinks[l]);
						}
					}
					
					if (linkAttribute == gergLink) 
					{
						kadovTextPopup(arrayofLinks[l]); 
					}
				}
			}
		}
	}
	
	if ( arrayofSpans != null ) 
	{
		for (n = 0; n < arrayofSpans.length; n++) 
		{
			if ( arrayofSpans[n].style.display == "none") 
			{
				
				gergLink = arrayofSpans[n].id; 
				gergLink = gergLink.substr(0, gergLink.length-4); //this is the id of the "a" link
				for (k = 0; k < arrayofLinks.length; k++) 
				{
					
					linkAttribute = arrayofLinks[k].getAttribute( "x-use-popup" );
					if( linkAttribute ) 
					{
						if( linkAttribute.substr(0,1) == "#" ) 
						{
							linkAttribute = linkAttribute.substr(1, linkAttribute.length-1);
						}
					}
					else
					{
						//find a ref for this span
						aId = findRef(arrayofSpans[n].id);
					
						if(arrayofLinks[k].id == aId)
						{
						 	////alert("got matching id");
						 	kadovTextPopup(arrayofLinks[k]);
						} 
					}
					
					if (linkAttribute == gergLink) 
					{
						kadovTextPopup(arrayofLinks[k]);
					}
				}
			}
		}
	}
}

// ****************************************

function hideEm() 
{
	if ( arrayofDivs != null ) 
	{
		for (m = 0; m < arrayofDivs.length; m++) 
		{
			if ( arrayofDivs[m].style.display == "") 
			{
				gergLink = arrayofDivs[m].id; 
				gergLink = gergLink.substr(0, gergLink.length-4); //this is the id of the "a" link
				for (l = 0; l < arrayofLinks.length; l++) 
				{
					linkAttribute = arrayofLinks[l].getAttribute( "x-use-popup" );
					if( linkAttribute ) 
					{
						if( linkAttribute.substr(0,1) == "#" ) 
						{
							linkAttribute = linkAttribute.substr(1, linkAttribute.length-1);
						}
					}
					else
					{
						//find a ref for this div
						aId = findRef(arrayofDivs[m].id);
						
						if(arrayofLinks[l].id == aId)
						{
						 	////alert("got matching id");
						 	kadovTextPopup(arrayofLinks[l]);
						} 	
					}
					
					if (linkAttribute == gergLink) 
					{
						kadovTextPopup(arrayofLinks[l]);
					} 
				}
			}
		}
	}
	if ( arrayofSpans != null ) 
	{
		for (n = 0; n < arrayofSpans.length; n++) 
		{
			if ( arrayofSpans[n].style.display == "") 
			{
				gergLink = arrayofSpans[n].id;
				gergLink = gergLink.substr(0, gergLink.length-4); //this is the id of the "a" link
				for (k = 0; k < arrayofLinks.length; k++) 
				{
					 
					linkAttribute = arrayofLinks[k].getAttribute( "x-use-popup" );
					if( linkAttribute ) 
					{
						if( linkAttribute.substr(0,1) == "#" ) 
						{
							linkAttribute = linkAttribute.substr(1, linkAttribute.length-1);
						}
					}
					else
					{
						//find a ref for this span
						aId = findRef(arrayofSpans[n].id);
						
						if(arrayofLinks[k].id == aId)
						{
						 	////alert("got matching id");
						 	kadovTextPopup(arrayofLinks[k]);
						} 
					}
					
					if (linkAttribute == gergLink) 
					{
						kadovTextPopup(arrayofLinks[k]);
					} 
				}
			} 
		}
	}
}

// ****************************************



function swapImages(imageName) 
{
	if (state=="shown") {
		if($hover) {
			document.images[imageName.name].src=path+btnhideall_hover;
		} else {
			document.images[imageName.name].src=path+btnhideall;
		}
	} else if (state=="hidden") {
		if($hover) {   
			document.images[imageName.name].src=path+btnshowall_hover;
		} else {
			document.images[imageName.name].src=path+btnshowall;
		}
	}
}

/*The function below can ONLY be used with the two button method.
If you require this function, delete "// " from the next three lines. */

// function printEm() {
// showEm(this);setTimeout('window.print()',50);
// }


/*
Remove "// " from the last two lines if you also want the browser Print icon
to also print all expanding and dropdown text. This will cause all hotspots to show,
print with the rest of the topic, and then hide.
Leave "// " on the last line if you want all expanding and dropdown
text to be shown after printing. This is perhaps more intuitive for the user.*/

// window.onbeforeprint = showEm;
// window.onafterprint = hideEm;


// original twisty.js script
// *************************

//This script is extracted from showhide.js from www.grainge.org
//showhide.js was originally written by Greg Swearingen in January 2004 when the
//previous method was found not to work in later versions of RH.
//It was then found that the new script was not compatible with RH X4 or later.
//Paul Symonds of Cedar Software then rewrote part of it in March 2004 to make it
//work with RH X4 and remain compatible with earlier versions (only tested in X3.
//Peter Grainge 050304


function replaceSubstring(inputString, fromString, toString) 
{
   
	// Goes through the inputString and replaces every occurrence of fromString with toString
	var temp = inputString;
	
	if (fromString == "" || toString == "") 
	{
		return inputString;
	}
	if (toString.indexOf(fromString) == -1) 
	{ 
		while (temp.indexOf(fromString) != -1) 
		{
			 var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			 var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			 temp = toTheLeft + toString + toTheRight;
		}
	}	
	// Send the updated string back to the user
	return temp; 
} 

function swapImage(imageName) 
{
	if(imageName.src.indexOf(img_expanded)!=-1) {
		document.images[imageName.name].src=replaceSubstring(imageName.src,img_expanded,img_collapsed);
	} else if (imageName.src.indexOf(img_collapsed)!=-1) {
		document.images[imageName.name].src=replaceSubstring(imageName.src,img_collapsed,img_expanded);
	}
}

// added by Lewis Gault to combine twisty.js functionality with showhide.js functionality
// only tested in X5
function showorhide (imageName)
{	
	var testStr;

	if (state == "hidden") {
		testStr = img_expanded;
	} else if (state == "shown") {
		testStr = img_collapsed;
	}
	
	for(counter=0;counter<document.images.length;counter++) {
		if (document.images[counter].src.indexOf(testStr) !=-1) {
			if (state == "hidden") {
				document.images[counter].src = replaceSubstring(document.images[counter].src, img_expanded, img_collapsed);
			} else if (state == "shown") {
				document.images[counter].src = replaceSubstring(document.images[counter].src, img_collapsed, img_expanded);
			}
		}
	}
}

// added by Lewis Gault to combine twisty.js functionality with showhide.js functionality
// only tested in X5
function showhidecheck ()
{
	isallhidden = true; // all hidden
	isallshown = true; // all shown
	
	for(counter=0;counter<document.images.length;counter++)
	{
		if(document.images[counter].src.indexOf(img_expanded)!=-1) {
			isallhidden = false; // if we find one showing (expanded), they're not all hidden
		}
		if(document.images[counter].src.indexOf(img_collapsed)!=-1) {
			isallshown = false; // if we find one hidden (collapsed), they're not all hidden
		}
	}
	if(isallhidden) {
		document.images.showall.src = path+btnshowall;
		state = "hidden";
	}
	if(isallshown) {
		document.images.showall.src = path+btnhideall;
		state = "shown";
	}
		 
}
function repopup(URL)
		{
			if(window.navigator.appName.indexOf("Microsoft")>=0)
			{
				window.showModelessDialog(URL, null,"status:false;dialogWidth:420px;dialogHeight:500px;edge: Raised; center: Yes; help: No; resizable: Yes; status: No");
			}
			else
			{
				window.open(URL, null, "status:false;dialogWidth:400px;dialogHeight:500px;edge: Raised; center: Yes; help: No; resizable: Yes; status: No")
			}
		}


