// -------------------------------------
// KNOWLEDGE RESOURCE DISPLAY CONTROLLER
// -------------------------------------
// Copyright (c) 2003 The Salamander Organization Ltd.  All Rights Reserved.
//
// THIS WORK IS SUBJECT TO U.K. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
//
// DISCLAIMER:
//
// The Salamander Organization Ltd. (hereto referred as "Salamander") makes no representations or warranties
// with respect to the contents or use of this code, and specifically disclaims any express or implied
// warranties of merchantability or fitness for any particular purpose.  Further, Salamander reserves the right
// to revise this publication and to make changes to its content, at any time, without obligation to notify any
// person or entity of such revisions or changes.
//
// Further, Salamander makes no representations or warranties with respect to any software, and specifically
// disclaims any express or implied warranties of merchantability or fitness for any particular purpose.  Salamander
// reserves the right to make changes to any and all parts of the software, at any time, without obligation to notify
// any person or entity of such changes.
//
// Inclusion of the above notice does not necessarily imply publication.



// knowledge resource array structure
function ka(id, tooltip, link, imgdesc, image, name)
{
	this.id = id;
	this.tooltip = tooltip;
	this.link = link;
	this.imgdesc = imgdesc;
	this.image = image;
	this.name = name;
}

// from onClick event from category, shows or hides resources associated with category
// is passed a category name and number of resources in that category
function ToggleShowHide(catNr, rCount)
{
	if (IsSupportedBrowser())
	{
		// start counting at 1 as rCount is a "real" number of resources
		for (cCounter=1; cCounter<=rCount; cCounter++)
		{
			catRows = new Object();
			catRows = eval('document.all.resrow'+catNr+'_'+cCounter);

			// toggle state for all resources in this category - if displayed, then hide, but if hidden, display
			if (catRows.style.display == 'none')
			{
				catRows.style.display = 'block';
			} 
			else
			{
				catRows.style.display = 'none';
			}
		}
	}
}

// from onClick event on category, toggle image from plus to minus or vice versa and toggle tooltip details to match
// also toggle background treeline.gif on/off at the same time (so the treeline doesn't appear when the category is closed)
function SwapImage(imgID)
{
	if (IsSupportedBrowser())
	{
		catImage = new Object();	
		catImage = eval('document.all.catimg'+imgID);

		if (catImage)
		{
			catImageRows = new Object();
			catImageRows = eval('document.all.catrow'+imgID);

			// check value attribute on image and toggle appropriately
			if (catImage.name == "plus")
			{
				catImage.src = "../../Images/minus.gif";
				catImage.name = "minus";
				catImageRows.title = "Hide resources for this category";
			} 
			else 
			{
				catImage.src = "../../Images/plus.gif";
				catImage.name = "plus";
				catImageRows.title = "View resources for this category";
			}

			catCells = new Object();
			catCells = eval('document.all.catcell'+imgID);

			// check value attribute on table cells and toggle appropriately
			if (catCells.name == "opened")
			{
				catCells.background = "";
				catCells.name = "closed";
			} 
			else 
			{
				catCells.background = "../../Images/treeline.gif";
				catCells.name = "opened";
			}
		}
	}
}

// write out Categories and Resources into Document
function WriteKRSection()
{
	var krSectionText = "";
	
	// loop through categories
	for (catCount = 0; catCount < catArray.length; catCount++)
	{
		// total number of resources for this category (resCount)
		var resCount = 0;
		// loop through resources in this category, counting number of resources
		// NOTE - may be able to just use tCount and drop resCount
		for (tCount = 0; tCount < kaArray.length; tCount++)
		{
			if (kaArray[tCount].id == catCount)
			{
				resCount++;
			}
		}

		// write out the category
		krSectionText += LayoutKnowledgeResourceCategory(catArray[catCount], catCount, resCount);

		krSectionText += LayoutKnowledgeResourceLinksHeader();

		// write out the resources for this category, from resource array
		var thisCount = 0;
		for (kaCount=0; kaCount < kaArray.length; kaCount++)
		{
			if (kaArray[kaCount].id == catCount)
			{
				thisCount++;
				
				var resID = "resrow" + catCount + "_" + thisCount;
				var resName = kaArray[kaCount].name;
				var resLink = kaArray[kaCount].link;
				var resImageSrc = kaArray[kaCount].image;
				var resImageDescription = kaArray[kaCount].imgdesc;
				var resTooltip = kaArray[kaCount].tooltip;
				if (thisCount == resCount)
				{
					var branchImageType = "branch";  // end 'L' branch
				}
				else
				{
					var branchImageType = "mbranch";  // middle 'T' branch
				}

				krSectionText += LayoutKnowledgeResourceLinks(resID, resName, resLink, resImageSrc, resImageDescription, resTooltip, branchImageType);
			}
		}
		
		krSectionText += LayoutKnowledgeResourceLinksFooter();
	}
	
	var krSectionOutput = LayoutKnowledgeResourceArea(krSectionText);

	//testWindow = window.open("","testWindow");
	//testWindow.document.write("<html><body>" + krSectionOutput + "</body></html>");
	
	document.write(krSectionOutput);
	
	// collapse the tree so that only categories are visible
	CollapseKRSection();
}

// Detect my browser and then, if Internet Explorer, collapse the knowledge resource tree
// Means that Netscape should see the tree even if the visibility functions aren't working
function CollapseKRSection()
{
	//check that we're in a supported browser
	if (IsSupportedBrowser())
	{
		var totalCats = catArray.length;
		for (catsCount=0; catsCount < totalCats; catsCount++)
		{
			// total number of resources for this category (resCount)
			var resInCatCount = 0;
			// loop through resources in this category, counting number of resources
			// NOTE - may be able to just use tCount and drop resCount
			for (resInCatsCount=0; resInCatsCount < kaArray.length; resInCatsCount++)
			{
				if (kaArray[resInCatsCount].id == catsCount)
				{
					resInCatCount++;
				}
			}
			
			// toggle everything off and closed
			SwapImage(catsCount);
			ToggleShowHide(catsCount, resInCatCount);
		}
	}
}

function ToggleCategory(catCount, resCount)
{
	SwapImage(catCount);
	ToggleShowHide(catCount, resCount);
}

// check current browser to see if we are going to do anything
function IsSupportedBrowser()
{
	var isSupported = false;
	
	// if browser is MSIE then continue checking
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		// find the version
		navVer = new String();
		navVer = navigator.appVersion;
		navVerArray = new Array();
		navVerArray = navVer.split(" ");
		
		// if browser version is MSIE 4 or higher, it is supported
		if (navVerArray[0] >= 4)
		{
			//alert("Supported - "+navigator.appName+" - "+navVerArray[0]);
			isSupported = true;
		} 
		else 
		{
			//alert("Not Supported - "+navigator.appName+" - "+navVerArray[0]);
			isSupported = false;
		}
	} 
	else 
	{
		//alert("Not Supported - "+navigator.appName);
		isSupported = false;
	}
	
	return isSupported;
}