/**
 * displayRightbarAds cycles through a master list of available
 * advertisements and chooses a specified number of those available
 * to display to the user.  This function randomizes the selection
 * of these ads to ensure a mixed result everytime the user hits
 * a page.  This function then writes the image links to the page.
 *
 */
function displayRightbarAds()
{
	var adCount = 1;  //Number of ads to display
	var master_rightbar_ads = initializeMasterAdsList();  //Master list of all available advertisements
	var output_rightbar_ads = new Array();  //Ads selected from the master list to display on the page
	var output_rightbar_keys = new Array(master_rightbar_ads.length);  //List of the master list indexes already used to prevent duplicate ads

	//Check if the desired ad count is greater than the total number of ads available
	if (adCount > master_rightbar_ads.length)
	{
		//Set length to total number of ads available
		adCount = master_rightbar_ads.length;
	}

	//Cycle through the ad selector loop until the desired number of ads is selected
	while (output_rightbar_ads.length < adCount)
	{
		//Generate a random index within the bounds of the master list length
		var randomIndex = Math.floor(Math.random()*10000) % master_rightbar_ads.length;

		//Check if this ad has already been used
		if (output_rightbar_keys[randomIndex] != true)
		{
			//Set this ad id as used
			output_rightbar_keys[randomIndex] = true;

			//Load the array with the desired master list information
			output_rightbar_ads[output_rightbar_ads.length] = master_rightbar_ads[randomIndex];
		}
	}

	//Cycle through the ads that were selected
	for(var i in output_rightbar_ads)
	{
		//Write the image links onto the page
		document.write('<div class="rightbar-box1"><a href="' + output_rightbar_ads[i].url + '"><img src="' + output_rightbar_ads[i].src + '" alt="' + output_rightbar_ads[i].alt + '"/></a></div>');
	}
}

/**
 * initializeMasterAdsList creates an array and loads the array with
 * all of the necessary properties to populate an image link to be
 * displayed in the right-hand bar of the website
 *
 * @return Array - Loaded array of all available advertisements
 */
function initializeMasterAdsList()
{
	var master_rightbar_ads = new Array();

	//Prima Guide
	master_rightbar_ads[master_rightbar_ads.length] = {
		url : "http://herald.warhammeronline.com/redirect.php?url=http%3A%2F%2Fwww.primagames.com%2Ffeatures%2Fwarhammer%2Fresults.pperl",
		src : "http://mythicmktg.fileburst.com/war/us/global/images/rightbar/right-bar_prima-guides.gif",
		alt : "Prima Guide"
	};

	//60-Day Gamecard
	master_rightbar_ads[master_rightbar_ads.length] = {
		url : "http://www.warhammeronline.com/gamecard/",
		src : "http://mythicmktg.fileburst.com/war/us/global/images/rightbar/misc-box_60daycard.jpg",
		alt : "60-Day Time Card"
	};

	//Dark Storm Gathering Novel
	master_rightbar_ads[master_rightbar_ads.length] = {
		url : "http://herald.warhammeronline.com/redirect.php?url=http%3A%2F%2Fwww.blacklibrary.com%2Fproduct.asp%3Fprod%3D60100281066%26type%3DBook",
		src : "http://mythicmktg.fileburst.com/war/us/global/images/rightbar/right-bar_DarkStormGath_01.jpg",
		alt : "DARK STORM GATHERING novel"
	};

	//Recruit a Friend
	master_rightbar_ads[master_rightbar_ads.length] = {
		url : "http://www.warhammeronline.com/recruit/index.php",
		src : "http://mythicmktg.fileburst.com/war/us/global/images/rightbar/misc-box_RaF_2.gif",
		alt : "Recruit a Friend"
	};
	
	//WAR Guides
	master_rightbar_ads[master_rightbar_ads.length] = {
		url : "http://herald.warhammeronline.com/guides/index.php",
		src : "http://mythicmktg.fileburst.com/war/us/global/images/rightbar/right-bar_war-guides.jpg",
		alt : "WAR Guides"
	};
	
	//Battleforge
	master_rightbar_ads[master_rightbar_ads.length] = {
		url : "http://www.battleforge.com/?q=warhammer",
		src : "http://mythicmktg.fileburst.com/war/us/global/images/rightbar/misc-box_battleforge.jpg",
		alt : "Battleforge"
	};

	return master_rightbar_ads;
}
