// JavaScript Document


/*

rotator.js
Generates html for AD tile management system. Requires:

1) rotator-vars.js (include before rotator.js)

2) Two divs placed *before* both js scripts:

<div id="ads"></div>
<div id="stationads"></div>

*/

var today = new Date();          // todays date

var ParrayNums = new Array();     // images to show
var Pimageholder = new String();  // text string containing html code

var SarrayNums = new Array();     // images to show
var Simageholder = new String();  // text string containing html code

var j = new Number();            // number of ADs returned
var k = new Number();            // number of Station ADs returned


for (i in PstartDate) {
	if (PstartDate[i] <= today && PendDate[i] >= today) {
		ParrayNums.push(i); 	
	}
}

fisherYates(ParrayNums);

j = ParrayNums.length;

if (j > 0) {
	iFirst = ParrayNums[0];
	imageHolder = "<a href='" + PlinkURL[iFirst] + "' title='" + PlinkTitle[iFirst] + "'><img src='" + PimageURL[iFirst] + "' alt='" + PimageTitle[iFirst] + "' border='0' width='120' /></a>";

	if (j > 1) {
		iSecond = ParrayNums[1];
		imageHolder = imageHolder + "<a href='" + PlinkURL[iSecond] + "' title='" + PlinkTitle[iSecond] + "'><img src='" + PimageURL[iSecond] + "' alt='" + PimageTitle[iSecond] + "' border='0' width='120' /></a>";
	}

	document.getElementById('ads').innerHTML = imageHolder; 
}






imageHolder = "";


for (i in SstartDate) {
	if (SstartDate[i] <= today && SendDate[i] >= today) {
		SarrayNums.push(i); 	
	}
}

fisherYates(SarrayNums);

k = SarrayNums.length;

if (k > 0) {
	iFirst = SarrayNums[0];
	imageHolder = "<a href='" + SlinkURL[iFirst] + "' title='" + SlinkTitle[iFirst] + "'><img src='" + SimageURL[iFirst] + "' alt='" + SimageTitle[iFirst] + "' border='0' width='120' /></a>";
	
		if (k > 1) {
		iSecond = SarrayNums[1];
		/*imageHolder = imageHolder + "<a href='" + SlinkURL[iSecond] + "' title='" + SlinkTitle[iSecond] + "'><img src='" + SimageURL[iSecond] + "' alt='" + SimageTitle[iSecond] + "' border='0' width='120' /></a>";*/
	}

	document.getElementById('stationads').innerHTML = imageHolder; 
}






// Function used to randomize array

function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}
