(function ($){
	$(function (){
		if($("body").is(".voting")){
			var BANDS_URL = "bands.xml";
		
			// Fetch bands for each region.
			var bandsPerRegion = {
				"auckland": [],
				"hamilton": [],
				"southisland": []
			};
			
			function parseBandsXML(data, textStatus){
				$(data).find("region").each(function (){
					var region = $(this).attr("id");
					
					bandsPerRegion[region] = [];
					$("band", this).each(function (){
						bandsPerRegion[region].push({
							"id":    $.trim( $("id", this).text() ),
							"name":  $.trim( $("name", this).text() ),
							"url":  $.trim( $("url", this).text() ),
							"image": $.trim( $("image", this).text() )
						});
					});
				});
			}
			
			$.get(BANDS_URL, parseBandsXML, "xml");
			
			
			
			// Make region and band selection nicer.
			var radioLabelSelectCallback = function (){
				$(this).parent().find("label").removeClass("selected");
				$(this).addClass("selected");
				
				// Fix for IE
				$("#" + $(this).attr("for")).attr("checked", "checked");
			};

			$("body.voting #region-box label").bind("click", radioLabelSelectCallback);
			$("body.voting #band-box label").live("click", radioLabelSelectCallback);

			// Change bands depending on region.
			$("#region-box label").click(function (){
				var region = $(this).attr("for");

				// Bands for this region (ID, picture, band name);
				$("#bands-container").empty();
				for(var i = 0; i < bandsPerRegion[region].length; i++){
					var id       = bandsPerRegion[region][i].id;
					var bandName = bandsPerRegion[region][i].name;
					var bandUrl = bandsPerRegion[region][i].url;
					var image    = bandsPerRegion[region][i].image;
					
					var input = $('<input type="radio" name="bandmembers">').attr("value", id).attr("id", id);
					var label = $("<label>").attr("for", input.attr("id"));
					var img = $("<img>").attr("src", image).attr("alt", bandName);
					//var url = $("<a></a>").attr("href", bandUrl).attr("target", "_blank");
					
						
					label.append(img).append(bandName).append("&nbsp;<br>").append(bandUrl);
					
					$("#bands-container").append(input).append(label);
				}

/*				
				newBands = 
	'<input type="radio" name="band" value="band1" id="band1" /><label for="band1"><img src="img/bands/band1.jpg" alt="" />Band 1</label>'+
	'<input type="radio" name="band" value="band2" id="band2" /><label for="band2"><img src="img/bands/band2.jpg" alt="" />Band 2</label>' +
	'<input type="radio" name="band" value="band3" id="band3" /><label for="band3"><img src="img/bands/band3.jpg" alt="" />Band 3</label>' +
	'<input type="radio" name="band" value="band4" id="band4" /><label for="band4"><img src="img/bands/band1.jpg" alt="" />Band 4</label>' +
	'<input type="radio" name="band" value="band5" id="band5" /><label for="band5"><img src="img/bands/band2.jpg" alt="" />Band 5</label>' +
	'<input type="radio" name="band" value="band6" id="band6" /><label for="band6"><img src="img/bands/band3.jpg" alt="" />Band 6</label>' +
	'<input type="radio" name="band" value="band7" id="band7" /><label for="band7"><img src="img/bands/band1.jpg" alt="" />Band 7</label>' +
	'<input type="radio" name="band" value="band8" id="band8" /><label for="band8"><img src="img/bands/band2.jpg" alt="" />Band 8</label>' +
	'<input type="radio" name="band" value="band9" id="band9" /><label for="band9"><img src="img/bands/band3.jpg" alt="" />Band 9</label>';
*/



			});
		}
		
		// Switch entry FORM to 'thank you' DIV.
		/*
		$("form", "#entry-form-container").submit(function (){
			$("#entry-form-container").hide();
			$("#thankyou").show();
			return false;

		});
		*/
		
	});
})(jQuery);

function submitForm() {
	// form validation and switch #entry-form-container to visible = none and #thank-you to visible = true
}
