//Toothpick Bird

function weekdaysBetween(startDate, endDate) {
   //this function was found at: http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CC30078F008
   if (startDate < endDate) {
      var s = startDate;
      var e = endDate;
   } else {
      var s = endDate;
      var e = startDate;
   }
   var diffDays = Math.floor((e - s) / 86400000);
   var weeksBetween = Math.floor(diffDays / 7);
   if (s.getDay() == e.getDay()) {
      var adjust = 0;
   } else if (s.getDay() == 0 && e.getDay() == 6) {
      var adjust = 5;
   } else if (s.getDay() == 6 && e.getDay() == 0) {
      var adjust = 0;
   } else if (e.getDay() == 6 || e.getDay() == 0) {
      var adjust = 5-s.getDay();
   } else if (s.getDay() == 0 || s.getDay() == 6) {
      var adjust = e.getDay();
   } else if (e.getDay() > s.getDay() ) {
      var adjust = e.getDay()-s.getDay();
   } else {
      var adjust = 5+e.getDay()-s.getDay();
   }
   return (weeksBetween * 5) + adjust;
}      

// This function returns a string padded with leading zeros
// found here: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions#Example:_Returning_a_formatted_number
function padZeros(num, totalLen) {
   var numStr = num.toString();             // Initialize return value as string
   var numZeros = totalLen - numStr.length; // Calculate no. of zeros
   for (var i = 1; i <= numZeros; i++) {
      numStr = "0" + numStr;
   }
   return numStr;
}

//calculate how many episodes there are (will be)      
var startDate=new Date();
startDate.setFullYear(2007,7,6); //adjust this if needed yyyy,m-1 (0-11),d
var today=new Date();  
var numDGWsince = weekdaysBetween(startDate, today) -1; //minus 1 in case the lastest episode isn't up yet 
var numDGW = 371 + numDGWsince; //adjust the number of episodes to match the startDate if needed

//animation globals      
var timerID = null;
var delay = 200;
var tpb = 0;

function animateMe(){
	var ep = document.getElementById("ep");
	ep.innerHTML = "<h3><a href='http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/DGW-282.mp3'>Choosing a random episode...</a></h3>";      	
	timerID = null;
	tpb = 0;
	cmd="changeImage()";
	timerID=window.setInterval(cmd,delay); 
}

function getRandomEp(){        
   var ranNum= Math.floor(Math.random()*numDGW) + 1;
   epNum = padZeros(ranNum, 3);
   var ep = document.getElementById("ep");
   ep.innerHTML ="<h3><a href='http://www.podtrac.com/pts/redirect.mp3/aolradio.podcast.aol.com/twit/DGW-" + epNum + ".mp3'>Listen now to episode: " + ranNum.toString() + "!</a></h3>";
}

//preload images
img_1 = new Image();
img_1.src = "tpb01.jpg";
img_2 = new Image();
img_2.src = "tpb02.jpg";
img_3 = new Image();
img_3.src = "tpb03.jpg";
img_4 = new Image();
img_4.src = "tpb04.jpg";
img_5 = new Image();
img_5.src = "tpb05.jpg";
img_6 = new Image();
img_6.src = "tpb06.jpg";
img_7 = new Image();
img_7.src = "tpb07.jpg";

var imgArray = new Array(
"tpb01.jpg",
"tpb02.jpg",
"tpb03.jpg",
"tpb04.jpg",
"tpb05.jpg",
"tpb06.jpg",
"tpb07.jpg"
);

function changeImage() { 		
   document.toothpickbird.src=imgArray[tpb];
   tpb++;     	   
   if (tpb > 6){
      tpb = 6;		
      clearInterval(timerID);
      getRandomEp();   
   }
}

