// setup sponsors array to hold all possible sponsors
var sponsors=[
["images/sponsors/atapco1.gif","Atapco Properties"]
,["images/sponsors/esi1.gif","ESI Enviro Solutions"]
,["images/sponsors/Havtech1.jpg","Havtech"]
,["images/sponsors/johnson-controls1.gif","Johnson Controls"]
,["images/sponsors/merritt1.gif","Merritt Properties, LLC"]
,["images/sponsors/wt1.gif","Whiting-Turner"]
,["images/sponsors/ffis1.gif","Fairfield Inn and Suites"]
,["images/sponsors/SJP.jpg", "St, John Properties"]
];

// how fast should logos rotate -- 1000 equals 1 second
timer = 3000;

// how many sponsors do we want to show on the page?
// be careful to never put a number in here that is greater than the number of available logos above, or you could inadvertantly create a black hole which could destroy the world. if you hear a big sucking sound, it's too late.
var sponsorAmount = 6

// create an array to hold the sponsors we intend to display
var sponsorset = new Array();

// make "indexOf" available to IE
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

// gets a random logo index number and verifies it is unique before adding it to the sponsorset array
function sponsorsetCheck()
{
	// pull random logo index number from sponsors array
	var rand_no = (Math.ceil(sponsors.length*Math.random()) -1);
	
	// if number doesn't yet exist in sponsorset array, add it
	if(sponsorset.indexOf(rand_no)==-1)
	{
		//document.write(" | " + rand_no + "added to array");
		sponsorset.push(rand_no);
	}
	// if it already exists in sponsorset array, run this function again so that we never duplicate a logo in the group
	else
	{
		//document.write(" | " + rand_no + "NOT added to array");
		sponsorsetCheck();
	}
}

// find unique logo index numbers and fill sponsorset array
var i=0;
for (i=0;i<=(sponsorAmount-1);i++)
{
	sponsorsetCheck();
}


// write initial sponsor list to the page
document.write('<ul id="sponsors1">');

// loop through sponsorset array to pull the unique random index numbers
var i=0;
for (i=0;i<=(sponsorAmount-1);i++)
{
	// get the index number of a logo
	sponsorIndex = sponsorset[i];
	// write the list item containing the logo
	document.write('<li id="sponsor' + i +'"><img src="' + sponsors[sponsorIndex][0] + '" alt="' + sponsors[sponsorIndex][1] + '" /></li>');
}

document.write('</ul>');



// start loop to rewrite the sponsor list items with new logos
setInterval("rewrite()", timer);

// rewrites each <li> with new logo info
function rewrite()
{
	
	// clear out the sponsorset array so we can refill it with new index numbers
	sponsorset.length = 0;
	
	// find unique logo index numbers and fill sponsorset array
	var i=0;
	for (i=0;i<=(sponsorAmount-1);i++)
	{
		sponsorsetCheck();
	}
	
	// loop through sponsorset array to pull the unique random index numbers
	var i=0;
	for (i=0;i<=(sponsorAmount-1);i++)
	{
		// get the index number of a logo
		sponsorIndex = sponsorset[i];
		// rewrite the list item with new logo info
		document.getElementById("sponsor"+i).innerHTML = '<img src="' + sponsors[sponsorIndex][0] + '" alt="' + sponsors[sponsorIndex][1] + '" />';
	}
	
}