/*

Author: Johnc
Proj No: 50181
Proj Name: Flash Construct 2.0
Usage:	Contains methods for creating menus and breadcrumbs.

Date: 10/02/06

*/


// Dynamically creates a menu based on the information in the course structure javascript.
function _createMenu()
{
	if (this.hasMenu)
	{
		var menuString = "";
		if (this.childObjsAssociative)
		{
			var odd = true;
			for (currChildID in this.childObjsAssociative)
			{
				if (odd)
				{
					odd = false
					var currClass = "oddRowClass";
				}
				else
				{
					odd = true
					var currClass = "evenRowClass";
				}
				var currChildObj = this.childObjsAssociative[currChildID]
				menuString += "<div id='menuLinkFor" + currChildObj.fullIDString + "ID' class='" + currClass + "'><table cellspacing='0' cellpadding='0' border='0'><tr><td>" + currChildObj.createTrackingStateIcon() +"</td><td valign='bottom' >"+ currChildObj.createMenuLink()
				if (currChildObj.timeAllowed!=0)
				{
					menuString += "<span class='topicItemTimeStateSpan'>(" + currChildObj.timeAllowed + " mins)"
				}

				var mediaIcons = "";
				// Check to see if there are any media types specified and add pointless icons
				// if they have.
				if(currChildObj.objType.toLowerCase()!="expert")
				{
					if (currChildObj.mediaTypes)
					{
						//alert("this.mediaTypes!")
						if (currChildObj.mediaTypes["audio"])
						{
							mediaIcons += " <img src='../../images/topic_type_audio.gif' alt='Topic contains audio.' border='0' class='mediaIcon'/> "
						}
						if (currChildObj.mediaTypes["video"])
						{
							mediaIcons += " <img src='../../images/topic_type_media.gif' alt='Topic contains video or animation.' border='0' class='mediaIcon'/> "
						}
					}
				}
				menuString += mediaIcons + "</span>"

				menuString += "</td></tr></table></span></div>\n";

			}
		}
		else
		{
			menuString += "<p>No children where found for this menu item, despite the course structure setting this object's .hasMenu property to true. Please check the setting's in the course structure file.</p>"
		}
		return menuString;
	}
	else
	{
		alert("Failed to create menu HTML as object is not set to have a menu in the course structure.\n\nthis.objID: " + this.objID + "\n\n_createMenu(), course_object_menu_methods.js")
		return false;
	}
}

// In the first instance it creates static title, then recurse's up the object chain until it
// finds an object with no parent, adding the menu links along the way.
function _createBreadcrumb(whatBreadCrumbString)
{
	// If there is a breadcrumb string, then we are in the recursing phase.
	if (whatBreadCrumbString)
	{
		whatBreadCrumbString = this.createBreadcrumbLink() + "<img src='" + this.rootPath + "images/breadcrumb_delimiter.png' alt=''> " + whatBreadCrumbString;
	}
	else
	{
		// Make the first part of the breadcrumb
		whatBreadCrumbString = this.objTitle + " - " + this.objID
	}
	// If there is a parent, carry on with the recursive chain, otherwise simply return the
	// current value.
	if (this.parentObj)
	{
		return "<h2 class='screenReaderVisibleOnly'>Breadcrumb</h2>" + this.parentObj.createBreadcrumb(whatBreadCrumbString);
	}
	else
	{
		return whatBreadCrumbString;
	}
}


// Creates a link to either a menu or a topic depending on the level of the object.
function _createMenuLink()
{

	// If it is a document, add the bookmark icon in the menu as you can't bookmark
	// in the normal manner from within the "topic". Also check to see if that item
	// has already been bookmarked, in which case put the greyed out icon instead.
	var bookMarkLink = "";
	if(this.objType=="pdf"||this.objType=="ppt"||this.objType=="doc")
	{
		linkName = this.objID.split(':')[1]+"."+this.objID.split(':')[0];
		if (!this.checkBookmarkStatus())
		{
			var bookMarkID = "bookMarkIcon_" + this.objID + "_ID"
			var bookMarkLink = "<a href=\"javascript:addBookmark('"+linkName+"','"+this.objType+"','"+this.objTitle.replace("'", "&quot;")+"')\" onmouseover='rollover(\"" + bookMarkID + "\")' onmouseout='rollout(\"" + bookMarkID + "\")'>&nbsp;&nbsp;&nbsp;<img id='" + bookMarkID + "' src='../../images/bookmark_icon_n.gif' alt='Add bookmark' border='0' align='top'></a>";
		}
		else
		{
			var bookMarkLink = "&nbsp;&nbsp;&nbsp;<img src='../../images/bookmark_icon_g.gif' alt='Item already bookmarked' border='0' align='top'>";
		}
	}

	var linkText = this.objTitle;
	var linkTitleText = linkText + ", " + this.getTrackingStateInEnglish();

	switch (this.objType)
			{
				case "topic":
					iconAlt="Topic"
				break;
				case "scenario":
					iconAlt="Scenario"
				break;
				case "ppt":
					iconAlt="Powerpoint"
				break;
				case "pdf":
					iconAlt="Pdf file"
				break;
				case "doc":
					iconAlt="Word document"
				break;
				case "assessment":
					iconAlt="Assessment"
				break;
				case "revision":
					iconAlt="revision"
				break;
				default:
					iconAlt=""
				break;

			}

	var iconImage = "<img src='../../images/topic_type_" + this.objType +".gif' alt='" + iconAlt + "' border='0' align='middle'>&nbsp;";

	// Check if the link should start a topic or navigate to a menu.
	if (this.hasMenu)
	{
		var menuString =  iconImage + "</td><td><a href='javascript:goSubMenu(\"" + this.objID + "\")' title='" + linkTitleText + "'>" + linkText + "</a></td>\n";
	}
	else
	{
		var menuString = "<td>" + iconImage + "<span id='div"+this.objID+"' class='menuItemWrapper'><a id='link"+this.objID+"' href='javascript:goPreTopic(\"" + this.objID + "\")' >" + linkText + "</a>" + bookMarkLink + "</span></td>";

	}
	return menuString;
}

// Checks against the bookmark array in the index file and returns true if it finds the
// objects ID in there.
function _checkBookmarkStatus()
{
	var count = 0;
	while (count < bookmarks.length)
	{

		if(bookmarks[count][0]!=undefined)
		{
		var bookmarkRef = bookmarks[count][0].slice(0, bookmarks[count][0].lastIndexOf("."))
		var objID = this.objID.split(":")[1]
		//alert("bookmarkRef: " + bookmarkRef + "\n\nobjID: " + objID)

		if (bookmarkRef == objID)
			return true
		}
		count++;
	}
	return false;
}


// Creates a link to a menu for the current object.
function _createBreadcrumbLink()
{
	var linkText = this.objTitle + " - " + this.objID
	// The higher the depth, the earlier in the tabindex the item should appear. The Breadcrumb has
	// 30-39 as a current range.
	var tabindexText = "tabindex='" + (39 - this.getDepth()) + "'"
	if (this.hasMenu)
	{
		var menuString = "<a href='javascript:goMenu(\"" + this.fullIDString + "\")' title='" + linkText + " menu' " + tabindexText + ">" + linkText + "</a>\n"
	}
	else
	{
		alert("_createBreadcrumbLink() called for object: " + this.objID + " however that object does not have a menu.")
	}
	return menuString
}

// Returns the HTML for the relivant image with alt text for the objects current tracking state.
function _createTrackingStateIcon()
{
	var imageText = "<img";
	imageText += " src='" + this.rootPath + "images/tracking_state_" + this.trackingState + ".gif'";
	imageText += " alt='" + this.getTrackingStateInEnglish() + "' hspace='5'";
	imageText += "/> ";
	this.debug(imageText,1,false,false)
	return imageText;
}
