//Get the adress bar variables (seeking the episode number)
var epNumber = ( window.location.href.indexOf( "episode_" ) + 8 ) ? window.location.href.substr( window.location.href.indexOf( "episode_" ) + 8 ) : "";
epNumber = epNumber.replace(".html","");

//Remove the "0" if the number is below 10
//if (epNumber.substr(0,1).indexOf("0") != -1){ epNumber = epNumber.substr(1,1);	}

var idNumber = "";

//For a step-by-step explanation of the following code, please visit http://www.dhtmlgoodies.com/index.html?whichTutorial=ajax-basics

//This is to detect IE. IE uses XML ChildNodes indexes differently of the other Browsers. The don't count the closing tags unlike the other browsers.
//That means we have to divide by 2 the ChilNodes indexes for it to work on IE correctly
var isIE = ((navigator.appVersion.indexOf("MSIE")!= -1)&&!window.opera)? 2 : 1;
		
//Create an array of sack/Ajax objects.
var dynamicContent_ajaxObjects = new Array(); 
var jsCache = new Array();
var enableCache = false;
var XMLFile = false;


/*************************************************************************************************************
ajax_loadContent function
------------------------------------
This function is used to:
	* Populate the divs from cache if cache is enabled and this specific content is cached.
	* Create an Ajax/sack object and send a request to the server if content isn't cached or cache is disabled.
**************************************************************************************************************/
function ajax_loadContent(divId,pathToFile)
{
	//Look if the file is an XML file
	if (pathToFile.substring(pathToFile.length - 3,pathToFile.length)) {
		XMLFile = true;
	}
	
	//Look if the cache is usable (thus enabled)
	if(enableCache && jsCache[pathToFile]){
		document.getElementById(divId).innerHTML = jsCache[pathToFile];
		return; 
	}
	
	var ajaxIndex = dynamicContent_ajaxObjects.length;
	
	//Show a loading gif animiation while the AJAX loads
	document.getElementById("relPodcasts").innerHTML = '<img alt="loading animation" src="../../images/loading.gif" width="25" height="25" />';

	//Create a sack object, define the property requestFile and an onCompletion method.
	dynamicContent_ajaxObjects[ajaxIndex] = new sack();
	dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;
	
	dynamicContent_ajaxObjects[ajaxIndex].onCompletion = function(){ 
		ajax_showContent(divId,ajaxIndex,pathToFile);
	};
	
	//Execute the Ajax call
	dynamicContent_ajaxObjects[ajaxIndex].runAJAX();
} 


/*************************************************************************************************************
The ajax_showContent function
-----------------------------------------
Function that would be executed by our Ajax objects when it is finished returning content from the 
server. The purpose of this function is to put content into our divs and to update our Javascript cache array.
*************************************************************************************************************/
function ajax_showContent(divId,ajaxIndex,pathToFile)
{
	//Look if the file is an XML file
	if ((XMLFile) || (dynamicContent_ajaxObjects[ajaxIndex].responseXML != null)) {
		
		//******************************************************************************************
		//Code for populating the dynamic area of the page
		//******************************************************************************************
		var FinalHTMLcontent = "";
		
		var xmlObj = dynamicContent_ajaxObjects[ajaxIndex].responseXML;   // Assign the XML file to a var
		var rootXML = xmlObj.getElementsByTagName('channel').item(0);   // Read the first element
		
		//Look for the item in the XML file corresponding to the episode number
		var i=1;
		var itemFound = false;
		while((i<rootXML.childNodes.length) && (itemFound == false)){
			i++;
			if (rootXML.childNodes[i].tagName == "item"){
				if (rootXML.childNodes[i].childNodes[Math.floor(23/isIE)].firstChild.nodeValue.toString() == epNumber){
					itemFound = true;
					idNumber = i;
				}
			}
		}
						
		
		//Populate the Related Podcast Zone
		var techFocus = rootXML.childNodes[idNumber].childNodes[Math.floor(21/isIE)].firstChild.nodeValue;
		var nbr_relPodcasts = 5;
		var no_relPodcasts = 0;
		var FinalRelPodcastsHTML = '<p><strong>Related Podcast</strong></p>';
		for (i=1;no_relPodcasts<nbr_relPodcasts;i++)
		{
			if (i<rootXML.childNodes.length-Math.floor(2/isIE)){
				if ((rootXML.childNodes[i].tagName == "item") && (i != idNumber)){
					if (rootXML.childNodes[i].childNodes[Math.floor(21/isIE)].firstChild.nodeValue == techFocus){
						no_relPodcasts++;
						FinalRelPodcastsHTML = FinalRelPodcastsHTML + '<p class="p_relPodcast"><a href="tw_episode_' + rootXML.childNodes[i].childNodes[Math.floor(23/isIE)].firstChild.nodeValue + '.html">'+ rootXML.childNodes[i].childNodes[Math.floor(1/isIE)].firstChild.nodeValue +'</a></p>';
					}
				} //END --- if (rootXML.childNodes[i].tagName == "item")
			} else {
				//No more podcast (less then the nbr_relPodcasts) in the category, break the for loop
				break;
			}
			
		} //END --- for...
		document.getElementById("relPodcasts").innerHTML = FinalRelPodcastsHTML;
		

		//Cache the Final HTML content
		if(enableCache){
		  jsCache[pathToFile] = FinalHTMLcontent;
		}

		dynamicContent_ajaxObjects[ajaxIndex] = false;
		
	} else {
		alert("Error while loading the XML file. Can't detect the file or detected a file extension other then XML.");
	}
}