var currentShow = -6;
var lastShow = -6; 

function ajaxRequest() {
  var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
  if (window.ActiveXObject) { //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
    for (var i = 0; i < activexmodes.length; i++) {
       try {
         return new ActiveXObject(activexmodes[i]);
       }
       catch(e) {
         //suppress error
       }
    }
  }
  else if (window.XMLHttpRequest) { // if Mozilla, Safari etc
    return new XMLHttpRequest();
  }
  else {
    return false;
  }
}

//var showsFile = new XMLHttpRequest();
var showsFile = new ajaxRequest();
showsFile.open("GET", "shows-detail.asp?r=" + Math.floor(Math.random()*10000), true);

Array.prototype.clean = function() {
  for (var i = 0; i < this.length; i++) {
    if ((this[i] == '')||(this[i] == ' ')||(this[i] == String.fromCharCode(13))) {         
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};

showsFile.onreadystatechange = function() {
	if (showsFile.readyState === 4) {  // Makes sure the document is ready to parse.
		if (showsFile.status === 200) {  // Makes sure it's found the file.
			allText = showsFile.responseText;
			shows = allText.split("\n"); // Will separate each line into an array
			shows.clean();
			lastShow = shows.length - 6;
      fetchShow('back');
		}
	}
}
showsFile.send(null);

function showKeyCode(str) {
	for (var i=0; i<2; i++) {
		var code = str.charCodeAt(i);
		alert (code)
	}
}

function cleanStr( str ) {
	var searchFor = String.fromCharCode(65533);
	while (str.toString().indexOf(searchFor) != -1){
		str = str.toString().replace(searchFor, "'");
	}
	return str;
}

function stripTag( str ) {
  str = str.toString().replace("<!-- ", "");
  str = str.toString().replace(" -->", "");
	return str;
}

function fetchShow(direction) {
	
	if (document.getElementById('latestpodcast-link') == null) return;
	
	if ((direction == 'forward') && (currentShow > 0)) {
		currentShow = currentShow - 6;
	} else if ((direction == 'back') && (currentShow < lastShow)) {
		currentShow = currentShow + 6;
	}
	
	document.getElementById('latestpodcast-link').href = shows[currentShow];
	document.getElementById('latestpodcast-date').innerHTML = shows[(currentShow + 1)];
	document.getElementById('latestpodcast-title').innerHTML = '<a href="' + shows[currentShow] + '">' + cleanStr(shows[(currentShow + 2)]) + '</a>';
	document.getElementById('latestpodcast-teaser').innerHTML = cleanStr(shows[(currentShow + 3)]);
	document.getElementById('latestpodcast-icon').innerHTML = "<img src=\"" + shows[(currentShow + 4)] + "\" width=\"66\" height=\"50\" border=\"0\">";
	document.getElementById('latestpodcast-player').innerHTML = '<script language="JavaScript" src="http://www.forcecast.net/audio-player.js"></script><object type="application/x-shockwave-flash" data="http://www.forcecast.net/player.swf" id="audioplayer1" height="24" width="332"><param name="movie" value="http://www.forcecast.net/player.swf"><param name="FlashVars" value="playerID=1&amp;soundFile=' + stripTag(shows[(currentShow + 5)]) + '"><param name="quality" value="high"><param name="menu" value="false"><param name="wmode" value="transparent"></object>';
	//document.getElementById('latestpodcastdownload-link').href = stripTag(shows[currentShow+5]);

	if (currentShow == lastShow) {
		document.getElementById('prev-btn').style.display = 'none';
		document.getElementById('next-btn').style.display = 'block';
	} else if (currentShow == 0) {
		document.getElementById('next-btn').style.display = 'none';
		document.getElementById('prev-btn').style.display = 'block';
	} else {
		document.getElementById('prev-btn').style.display = 'block';
		document.getElementById('next-btn').style.display = 'block';
	}
}