//Get the adress bar variables (seeking the id number)
var idNumber = ( window.location.href.indexOf( "?id=" ) + 4 ) ? window.location.href.substr( window.location.href.indexOf( "?id=" ) + 4 ) : "";
//alert(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("itemTitle").innerHTML = '<img alt="loading animation" src="images/loading.gif" width="25" height="25" />';
	document.getElementById("top_description").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 such as the Podcast/Broadcast Title, it's lenght, etc.
		//******************************************************************************************
		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
		
		if (rootXML.childNodes[idNumber].tagName == "item"){
			//Populate the HTML tags
			document.getElementById("itemTitle").innerHTML = rootXML.childNodes[idNumber].childNodes[Math.floor(1/isIE)].firstChild.nodeValue;
			document.getElementById("top_description").innerHTML = '<strong>TechWiseTV episode ' + rootXML.childNodes[idNumber].childNodes[Math.floor(19/isIE)].firstChild.nodeValue + ' (' + rootXML.childNodes[idNumber].childNodes[Math.floor(13/isIE)].firstChild.nodeValue + ' min)</strong><br />' + rootXML.childNodes[idNumber].childNodes[Math.floor(5/isIE)].firstChild.nodeValue;
			document.getElementById("top_techFocus").innerHTML = 'Technology Focus: ' + rootXML.childNodes[idNumber].childNodes[Math.floor(17/isIE)].firstChild.nodeValue;
			//document.getElementById("top_registerBtn").innerHTML = 'Register to watch full episode<br />' + '<input type="button" value="Register" onClick="javascript:openLargePopup(\'' + rootXML.childNodes[idNumber].childNodes[Math.floor(23/isIE)].firstChild.nodeValue + '\',\'pdf_window\');" />';
			document.getElementById("top_registerBtn").innerHTML = 'Register to watch full episode<br />' + '<input type="image" id="btn_register" name="btn_register" src="images/btn_register-up.jpg" border="0" onMouseOver="javascript:document.getElementById(\'btn_register\').src = \'images/btn_register-over.jpg\'" onMouseOut="javascript:document.getElementById(\'btn_register\').src = \'images/btn_register-up.jpg\'" onClick="javascript:openLargePopup(\'' + rootXML.childNodes[idNumber].childNodes[Math.floor(23/isIE)].firstChild.nodeValue + '\',\'pdf_window\');" />';
			document.getElementById("bottom_registerBtn").innerHTML = 'Register to watch full episode<br />' + '<input type="image" id="btn_register" name="btn_register" src="images/btn_register-up.jpg" border="0" onMouseOver="javascript:document.getElementById(\'btn_register\').src = \'images/btn_register-over.jpg\'" onMouseOut="javascript:document.getElementById(\'btn_register\').src = \'images/btn_register-up.jpg\'" onClick="javascript:openLargePopup(\'' + rootXML.childNodes[idNumber].childNodes[Math.floor(23/isIE)].firstChild.nodeValue + '\',\'pdf_window\');" />';
			document.getElementById("rating").innerHTML = rootXML.childNodes[idNumber].childNodes[Math.floor(27/isIE)].firstChild.nodeValue;
			
			//Add the special offer if existing
			if (rootXML.childNodes[idNumber].childNodes.length > Math.floor(29/isIE)){
				if (rootXML.childNodes[idNumber].childNodes[Math.floor(29/isIE)].hasChildNodes()){
					document.getElementById("specialOffer").innerHTML = rootXML.childNodes[idNumber].childNodes[Math.floor(29/isIE)].firstChild.nodeValue;
					document.getElementById("specialOffer").style.display = "block";
				}
			}
			
			//Change the "small.jpg" last characters from the image name and replace it with "medium.jpg" so that the image show on the video player is a larger one
			var videoImage = rootXML.childNodes[idNumber].childNodes[Math.floor(21/isIE)].firstChild.nodeValue;
			videoImage = videoImage.substr(0,videoImage.length-9);
			videoImage = videoImage + "medium.jpg"
			
			//Add the video
			addVideo(rootXML.childNodes[idNumber].childNodes[Math.floor(11/isIE)].firstChild.nodeValue,videoImage);
		}
		

		//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.");
	}
}