/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	--------------------------------------------------------------------
	
	Simple banner rotator. Version: 1.2.0
	Download, support, contact: http://www.spyka.net 
	(c) Copyright 2009 spyka Web Group
        Modified by Brent Baker, 2009, for the Boys & Girls Club of
        Corvallis. 
        I just removed some features I knew I wouldn't be using, and
        cleaned things up to suit our usage.
*/

/* 
	For full documentation:  http://www.spyka.net/docs/simple-banner-rotator
	For support:			 http://www.spyka.net/forums
*/

//								EDIT FROM HERE
///////////////////////////////////////////////////////////////////////////////////
//         						Program options


// time between refreshs of ad locations, to disable refreshs set to 0. In milliseconds, 1000 = 1 second
var refresh_time = 15000;
// maximum amount of refreshs, good to set if a user may be on a page for a long period of time.
var refresh_max = 20;

// if you do not want the same banners to display on the same page then set this to 0, else set it to 1.
// this option is only used if you have put the show_banners() javascript code more than once into a page
var duplicate_banners = 0;

// declare how many banners we're going to need. Play nice, even if Javascript doesn't require it.
var banners = new Array(5);

// notice I'm setting relative pathnames for where to find the images. Also, this
// works best (and looks best) if all of the image have the same dimensions.
banners[0] = new banner('b_0', 'banners/815_by_375/Ricky_Card_2.jpg', '815', '375');
banners[1] = new banner('b_1', 'banners/815_by_375/Sydney_Card_2.jpg', '815', '375');
banners[2] = new banner('b_2', 'banners/815_by_375/Shilpa_Card_2.jpg', '815', '375');
banners[3] = new banner('b_3', 'banners/815_by_375/Faris_Card_2.jpg', '815', '375');
banners[4] = new banner('b_4', 'banners/815_by_375/Megan_Card_2.jpg', '815', '375');
// banners[5] = new banner('b_5', 'banners/815_by_375/Matching_Card.jpg', '815', '375');

//         	There is no need to edit below here (but I did, just the same)
///////////////////////////////////////////////////////////////////////////////////

var used = 0;
var first_pass = 0;
var location_counter = 1;
var refresh_counter = 1;
var image_counter = 0;

// this funtion loads the contents, image, pathname, etc, into the array
function banner(name, image, width, height) 
{
	this.name	= name;         // FWIW
	this.image	= image;        // relative path name to the image file
    this.width  = width;        // width in pixels
    this.height = height;       // height in pixels
	this.active = 1;            // a usage flag
}

// this is the main function called by the JS interpreter
function show_banners()
{
	// I still need to figure out this next line
	var html = '<td id="adLocation-' + location_counter + '"></td>'; 
	document.write(html);
	display_banners(location_counter);
	location_counter++;
}

function display_banners(location)
{
	if(location == '' || !location || location < 0)
	{
		// no location given
		return;
	}
	
	var am	= banners.length;
	
	if((am == used) && duplicate_banners == 0) {
		// all banners have been used
		return;
	}
	// I insert my own control code here
	
	
//  var bn = banners[image_counter];
//  ++image_counter;
//  if (image_counter >= am) {
//    image_counter = 0;
//  }
  

	var rand	= Math.floor(Math.random()*am);	  // original code
	var bn 		= banners[rand];

//    var bn = banners[(location_counter-1)];
	
	// build the html tag that displays the image / banner
	var html    = '<img border="0" src="' + bn.image + '" width="' + bn.width + '" height="' + bn.height + '" >';

    if(bn.active == 1)
	{
	    // I still need to figure out what this does
		var location_element = document.getElementById('adLocation-' + location);
		
		if(location_element == null)
		{
			// ad location doesn't exist
			alert('banner rotator\nError: adLocation doesn\'t exist!');
		}
		else
		{
			location_element.innerHTML = html;		
			if(duplicate_banners == 0)
			{
				bn.active = 0;
				used++;
			}
		}
	}
	else
	{
		display_banners(location);
	}
}

function refresh_banners()
{
	if((refresh_counter == refresh_max) || refresh_time < 1)
	{
		clearInterval(banner_refresh);  
	}
	used = 0;
	for(i = 0; i < banners.length; i++)
	{
		banners[i].active = 1;
	}
	for(i = 1; i < location_counter; i++)
	{
		display_banners(i);
	}
	refresh_counter++;
}

// set the daemon (Unix term) on how often to call for a new display
var banner_refresh = window.setInterval(refresh_banners, refresh_time);
