var sponsor_icons = [
	{
		src : "images/casperson_side.JPG",
		alt : "Rappahannock Orthodontics",
		link: "http://www.drcaspersen.com/"
	},
//	{
//		src : "images/tmLawnSvc_small.jpg",
//		alt : "T &amp; M Lawn Service LLC",
//		link: "http://tandmlawns.com"
//	},
	// No link!
//	{
//		src : "images/kiki2_small.jpg",
//		alt : "Kiki's Lunch Box",
//	},
//	{
//		src : "images/carlatittle_lf_small.png",
//		alt : "Carla Tittle, Long &amp; Foster Real Estate",
//		link: "http://www.carlatittle.com/"
//	},
//	{
//		src : "images/silpada_tittle_small.png",
//		alt : "Carla Tittle, Independent Silpada Designs Representative",
//		link: "http://mysilpada.com/carla.tittle"
//	},
//	{
//		src : "images/Jacobs_ventel_small.png",
//		alt : "Sandra Jacobs, Ventel Pearls Demonstrator",
//		link: "http://www.vantelpearls.com/sandrajacobs"
//	},	
//	{
//		src : "images/sweetcaroline_small.png",
//		alt : "Sweet Caroline's Boutique",
//		link: "http://www.sweetcarolinesinc.com/"
//	},
	{
		src : "images/could_be_you.png",
		alt : "Advertise on the Marlins Website!",
		link: "javascript:navigate_function('Marlin_Sponsors.html')"
	},
	{
		src : "images/windsong_small.png",
		alt : "Windsong Portraits",
		link: "http://www.windsongportraits.com"
	},
	{
		src : "images/Family_ymca.gif",
		alt : "Rappahannock Area YMCA",
		link: "http://www.family-ymca.org"
	}
];
	function rand_indexes(count)
	{
		var result_indices = new Array();
		var working_array = new Array();

		for(i=0; i<count; i++)
		{
			working_array[i] = i;
		}
		for (i=count-1; i>=0; i--)
		{
			var index = Math.round(Math.random() * i);
			var pulled = working_array.splice(index, 1);
			result_indices.push(pulled);
			//alert("i = " + i + " index = " + index + " Pulled = " + pulled + " Working Array = " + working_array);
		}
		return result_indices;

	}
	function encode_sponsor_image(index)
	{
		var image_elem = document.createElement("img");
		image_elem.setAttribute("src", sponsor_icons[index].src);
		image_elem.setAttribute("alt", sponsor_icons[index].alt);
		if ("link" in sponsor_icons[index])
		{
			var link_elem = document.createElement("a");
			link_elem.setAttribute("href", sponsor_icons[index].link);
			link_elem.appendChild(image_elem);
			return link_elem;
		} else {
			return image_elem;
		}
	
	}
	
	function calculate_indices()
	{
		// Find count
		var count = sponsor_icons.length;
		var idx_array = rand_indexes(count);

		var div_element = document.getElementById("sponsor_results");
		for (i=0; i<count; i++)
		{
			console.log("Append");
			div_element.appendChild(encode_sponsor_image(idx_array[i]));
			div_element.appendChild(document.createElement("br"));
		}
	}
	
	
	function build_icon_table (attach_elem)
	{
		var div_element = document.getElementById(attach_elem);
		if (div_element)
		{
			var count = sponsor_icons.length;
			var idx_array = rand_indexes(count);

			var table_elem = document.createElement("table");
			table_elem.setAttribute("cellspacing", "2");
			table_elem.setAttribute("cellpadding", "2");
			table_elem.setAttribute("border", "0");
			table_elem.setAttribute("style", "text-align: left; width: 650px;");

			var tbody_elem = document.createElement("tbody");
			var cur_row = document.createElement("tr");

			for (i=0; i<count; i++)
			{			
				var cur_cell = document.createElement("td");
				cur_cell.appendChild(encode_sponsor_image(idx_array[i]));
				cur_row.appendChild(cur_cell);

				if ((i + 1) % 3)   
				{
					if (i == (count - 1))
					{
						tbody_elem.appendChild(cur_row);
					}
				} else {
					tbody_elem.appendChild(cur_row);
					cur_row = document.createElement("tr");
				}
			}

			table_elem.appendChild(tbody_elem);
			div_element.appendChild(table_elem);
		}
		
	}
	

