var XmlHttpObj;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj() {
	
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try {
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(oc) {
			XmlHttpObj = null;
		}
	}
	
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") {
		XmlHttpObj = new XMLHttpRequest();
	}
}

function loadPodcast(podcastID) {
    
    // url of page that will send xml data back to client browser
	
	var requestUrl = "http://www.smoothfmlive.com/loadPodcast.php?podCast=" + encodeURIComponent(podcastID) ;
	    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj) {
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);
		document.getElementById("podcastText").innerHTML = '<div style="float:left; width:24px; height:24px; margin:0 187px;"><img src="http://www.smoothfmlive.com/images/waiting.gif" width="24" height="24" alt="Please Wait - Loading Podcast Data" /></div>'
	}
}

function StateChangeHandler() {
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4) {
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200) {
			showPodcast(XmlHttpObj.responseXML.documentElement);
		} else {
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

function showPodcast(xmlResponse) {
	document.getElementById("podcastPlayer").style.display = 'block';
	document.getElementById("podcastTitle").innerHTML = xmlResponse.getElementsByTagName("podcastTitle")[0].childNodes[0].nodeValue;
	document.getElementById("podcastText").innerHTML = xmlResponse.getElementsByTagName("podcastText")[0].childNodes[0].nodeValue;
	
	var fileName = xmlResponse.getElementsByTagName("FileName")[0].childNodes[0].nodeValue;
	loadMediaPlayer(fileName);
	
}
function loadMediaPlayer(fileToLoad) {
	document.getElementById("mediaPlayer").innerHTML = '<object id="MediaPlayer1" width=475 height=60 classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Loading Microsoft? Windows? Media Player components..."  type="application/x-oleobject" align="middle">	<param name="FileName" value="' + fileToLoad + '" />	<param name="AutoStart" value="false" />	<param name="ShowStatusBar" value="True" />	<param name="balance" value="0" />	<param name="volume" value="100" />	<param name="mute" value="0" />	<param name="ShowPositionControls" value="0" />	<param name="ShowTracker" value="0" />	<!-- BEGIN PLUG-IN HTML FOR FIREFOX-->	<embed type="application/x-mplayer2" pluginspage = "http://www.microsoft.com/Windows/MediaPlayer/" src="http://www.smoothfmlive.com/' + fileToLoad + '" align="middle" width="475" height="60" autostart="0"></embed>	<!-- END PLUG-IN HTML FOR FIREFOX--></object>';
}
