<!--

//****************************************************************************
// Copyright (C) 2006 CDSM Interactive Solutions Ltd. All Rights Reserved.
//****************************************************************************

/**
 * Audio Buttons JavaScript Functions (for People & Places)
 *
 * Set of functions used for replacing the mp3 hyperlinks with a flash audio button.  This should only happen if 
 * the Flash player is installed on the client PC.  The parent script should check for the Flash Player version
 * before running this script.
 * 
 * Note: - This script depends on the Adobe/Macromedia script "AC_RunActiveContent.js" to be loaded before 
 *         this one.
 *
 *		 - Also relies on "htmlToFlashFunctions.js" to be loaded befor this one.
 * 
 * @author 	Andrew Evans
 * @version	1.0.0 - initial version (26/10/2006)
 * @version	1.0.1 - Added replaceSubMenuMp3Links, replaceInfoMp3Links and getPageContentElem functions.
 *					There is a lot of repitition in this class and a lot of it can be handled more gracefully.
 * @version	1.0.2 - 14/06/11 - Nik G - Rewrote replaceMp3LinkWithSwf() to use JQuery selector for the links instead. Covers all scenarios now. 
 * 										Rest of the class needs cleaning up now.
 *				
 */

/*
 * - Find Nadia Div
 * - Get nadia image
 * - Get nadia link
 * - Remove nadia image
 * - Insert Flash version of Nadia and pass link url as querystring into flash
 */
function replaceMp3LinkWithSwf() {
	//Get the links to be replaced
	var links = $("a.flash_audio");
	for (var i=0; i<links.length; i++) {		
		var currNode = links[i];
		
		//Get the target url for the audio player
		var aHref = currNode.getAttribute("href");
		if (aHref == null) {
			return;
		}
		//Replace link with flash audio button
		insertAudioBefore(currNode,aHref,158);
		
		//Clean up the old link
		var parent = currNode.parentNode;
		parent.removeChild(currNode);
	} 
}

/**
 * Replace nadia mp3 link
 */
function replaceNadiaMp3Link() {
	//alert("replaceNadiaMp3Link");
	// nadia div element (<div class="head_button" id="nadia">...)
	var nadia = findNadiaNode();
	if (nadia == null) {
		return;
	}
//	alert("audioElem");
	var audioElem = findAudioDiv(nadia);
	if (audioElem == null) {
		return;
	}
	// get "play_audio" 
	var audioLink = getAudioLink(audioElem);
	if (audioLink == null) {
		return;
	}
	var aHref = audioLink.getAttribute("href");
	if (aHref == null) {
		return;
	}
	// replace link with flash audio button
	insertAudioBefore(audioLink,aHref,158);
	
	audioElem.removeChild(audioLink);
	
	//alert("...end of replaceNadiaMp3Link");
	
}

/**
 * Find nadia node
 */
function findNadiaNode() {
	//alert("findNadiaNode called");
	elem = document.getElementById("nadia");	
 	return elem;
	
}

/**
 * Find nadia image
 */
function findAudioDiv(elem) {
	//alert("findAudioDiv called");
	if (elem.childNodes != null) {
		var i = 0;
		var loopLength = elem.childNodes.length;
		var currNode;
		var str = "";
		for (i; i<loopLength; i++) {
			
			currNode = elem.childNodes[i];
			
			if ( (currNode.nodeName.toLowerCase() == "div" ) ) {
				return currNode;
			}
			
		}
	}
	
}

/**
 * Return first "a" element in element (elem) argument
 */
function getAudioLink(elem) {
	//alert("getAudioLink called");
	//alert(elem);
	if (elem.childNodes != null) {
		var i = 0;
		var loopLength = elem.childNodes.length;
		var currNode;
		var str = "";
		
		for (i; i<loopLength; i++) {
			
			currNode = elem.childNodes[i];
			
			if ( (currNode.nodeName.toLowerCase() == "a" ) ) {
				//alert(currNode.innerHTML);
				return currNode;
				
			}
			
		}
	}
	
}


/**
 * Replace all nmp3 links in the main content area
 */
function replaceMainMp3Links(elemToSearch, elemNameToFind, className) {
	
		var page_contentElem = getPageContentElem();
		if (page_contentElem != null) {
			var homeImage = searchElement(page_contentElem, "div", "HomeImage");
			if (homeImage != null) {
				var homeElem =         searchElement(homeImage, "div", "home");
				
				if (homeElem != null) {
					var homeListElem =     searchElement(homeElem, "ul");
					//alert("homeListElem: " + homeListElem);
					if (homeListElem != null) {
						// loop through options in "homeListElem"
						handleAllOptions(homeListElem);
					}
				}
			}
		}
	
}

/**
 * Replace all nmp3 links in the sub menu template
 */
function replaceSubMenuMp3Links(elemToSearch, elemNameToFind, className) {
	
		var page_contentElem = getPageContentElem();
		//alert("page_contentElem: " + page_contentElem);
		if (page_contentElem != null) {
			var subMenu = searchElement(page_contentElem, "div", "sub_menu");
			//alert("homeImage: " + homeImage);
			if (subMenu != null) {
				var containerElem =         searchElement(subMenu, "div");
				//alert("homeElem: " + homeElem);
				if (containerElem != null) {
					var containerList =     searchElement(containerElem, "ul");
					//alert("homeListElem: " + homeListElem);
					if (containerList != null) {
						// loop through options in "homeListElem"
						handleAllOptions(containerList);
					}
				}
			}
		}
	
}

/**
 * Replace all mp3 links in the information and cmp templates
 */
function replaceInfoMp3Links() {
	
	var page_contentElem = getPageContentElem();
	//alert("page_contentElem: " + page_contentElem);
	if (page_contentElem != null) {
		var repeater = searchElement(page_contentElem, "div");
		//alert("repeater: " + repeater);
		if (repeater != null) {
			
			var i = 0;
			var loopLength = repeater.childNodes.length;
			var currNode;
			
			for (i; i<loopLength; i++) {
				
				currNode = repeater.childNodes[i];
				//alert("currNode: " + currNode);
				if (currNode != null && currNode.nodeName.toLowerCase() == "div") {
					var optionElem = searchElement(currNode, "a");
					//alert("optionElem: " + optionElem);
					
					if( optionElem != null ){
						var optionURL = optionElem.getAttribute("href");
						//alert("optionElem: " + optionElem);
						//if (optionURL != "#") {
							insertAudioBefore(optionElem,optionURL,121);
						//}
						
						currNode.removeChild(optionElem);
					}
						
				}
					
			}
		
		}
	}
}

/**
 * Return <div class="page_content"> ... </div>
 */
function getPageContentElem() {
	var contentElem =      searchElement(document.body, "div", "content");
	
	if (contentElem != null) {
		var page_contentElem = searchElement(contentElem, "div", "page_content");
		//alert("page_contentElem: " + page_contentElem);
		if (page_contentElem != null) {
			return page_contentElem;
		}
	}
}

/**
 * Replace all nmp3 links in the main content area
 */
function searchElement(elemToSearch, elemNameToFind, className) {
	//alert ("elemToSearch: " + elemToSearch + " (" + elemNameToFind + ", " + className + ")");
	if (elemToSearch.childNodes != null) {
		var i = 0;
		var loopLength = elemToSearch.childNodes.length;
		var currNode;
		var currClass;
		
		for (i; i<loopLength; i++) {
			
			currNode = elemToSearch.childNodes[i];
			
			if ( (currNode.nodeName.toLowerCase() == elemNameToFind ) ) {
				
				if (className == null) {
					
					//alert("currNode.nodeName: " + currNode.nodeName);
					return currNode;
					
				}else {
					var currClass = currNode.className;
					//alert(currClass + "/" + className);
					
						if (currClass.toLowerCase() == className.toLowerCase()) {
							
							return currNode;
							
						}else{
							
							//alert("nope");
							
						}
			
					
				}
				
			}
			
		}
		
	}else {
		return null;
	}
	
}

function handleAllOptions(ulElem) {
	
	var i = 0;
	var loopLength = ulElem.childNodes.length;
	var currNode;
	
	for (i; i<loopLength; i++) {
		
		currNode = ulElem.childNodes[i];
		
		if (currNode.nodeName.toLowerCase() == "li") {
			
			handleOption(currNode);
			
		}
		
	}
	
}

function handleOption(liElem) {
	
	//alert("From handleOption: " + liElem.innerHTML);
	var childListElem = searchElement(liElem, "ul");
	var childListItemElem = searchElement(childListElem, "li");
	
	var optionElem = searchElement(childListItemElem, "a", "flash_audio");
	var optionURL = optionElem.getAttribute("href");
	//alert("optionElem: " + optionElem);
	//if (optionURL != "#") {
		insertAudioBefore(optionElem,optionURL,121);
	//}
	
	childListItemElem.removeChild(optionElem);
}

/**
 * Replace all mp3 links in  the navigation bar
 */
function replaceNavMp3Links() {
	
	var contentElem =      searchElement(document.body, "div", "content");
	if (contentElem != null) {
		var bottom_navElem = searchElement(contentElem, "div", "bottom_nav");
		if (bottom_navElem != null) {
			// left nav
			var left_navElem = searchElement(bottom_navElem, "div", "left");
			if (left_navElem != null) {
				var left_flashElem = searchElement(left_navElem, "div", "left_flash");
				if (left_flashElem != null) {
					var left_linkElem = searchElement(left_flashElem, "a", "flash_audio");
					if (left_linkElem != null) {
						//alert("left_linkElem: " + left_linkElem);
						// replace html link
						var leftURL = left_linkElem.getAttribute("href");
						insertAudioBefore(left_linkElem,leftURL,121);
						left_flashElem.removeChild(left_linkElem);
					}
				}
			}
			
			// right nav
			var right_navElem = searchElement(bottom_navElem, "div", "right");
			if (right_navElem != null) {
				var right_flashElem = searchElement(right_navElem, "div", "right_flash");
				if (right_flashElem != null) {
					var right_linkElem = searchElement(right_flashElem, "a", "flash_audio");
					if (right_linkElem != null) {
					
						// replace html link
						var rightURL = right_linkElem.getAttribute("href");
						insertAudioBefore(right_linkElem,rightURL,121);
						right_flashElem.removeChild(right_linkElem);
					}
				}
			}
		}
	}
	
	
	
	
}

/**
 * Inserts flash using MM's script
*/ 
function insertAudioBefore(target,url, width) {
	//alert("insertAudioBefore called");
	//alert("url: " + url);
	var parent = target.parentNode;
	var flashMarkup = getAudioFlashMarkup(url,width);
	var flashContainer = document.createElement("span");
		flashContainer.innerHTML = flashMarkup;
	parent.insertBefore(flashContainer,target);
	//parent.innerHTML = flashMarkup;
}



/**
 * Returns Flash markup for audio button
 */
function getAudioFlashMarkup(url,width) {
	
	var flashMarkup = AC_FL_RunContent('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0',
									   'width',width,
									   'height','25',
									   'width','98',
									   'src','/flash/play_audio_btn',
									   'quality','high',
									   'pluginspage','http://www.macromedia.com/go/getflashplayer',
									   'movie','/flash/play_audio_btn', 
									   "FlashVars", "url="+url, 
									   "name", "audio",
									   "id","audio", 
									   "wmode", "transparent",
									   "bgcolor", "#FFFFFF",
									   "title", "Play Audio Button");
	return flashMarkup;
}

-->
