
var imageTotal=0;
var imageCurrent=0;
var t=null;

$(document).ready(function()
{
  // hide date
  $("#billhead").html("<p></p>");
  $("#billhead p").hide();

  // get the xml
  $.ajax({
    type: "GET",
    url: "top.xml",
    dataType: "xml",
    success: parseXml,
	error: xmlError
  });
  
});

function xmlError(xml,status,error) {
	alert("Unable to read XML file, may be malformed or contain characters that are not allowed. Be sure to convert all non-ascii characters to HTML entities. i.e. ® to &reg; .");
}

function  parseXml(xml)
{
  // set the date
  var dateNode=$(xml).find( "gamelist" );
  $("#billhead p").html( dateNode.attr( "date" ) );
  
  // show it
  $("#billhead p").show();
    
  // create the holders for all the images
  $(xml).find("gamelist").find("game").each(function()
  {
  
	// get the info
	var img = $(this).attr("image");
	var title = $(this).find("title").text();	
	var copy = $(this).find("copy").text();	
  
	var endTag = "<img id='billimg_"+imageTotal+"' width='990' height='467' src='top-img/"+img+"' class='billimg' />";
	endTag += "<div id='billbg_"+imageTotal+"' class='sideboard-bg'></div><div id='billsb_"+imageTotal+"' class='sideboard'><h2>"+title+"</h2><p>"+copy+"</p></div>";
  
	$("#billboard").append( endTag );

	$("#buttonbank").append( "<button id='link_"+imageTotal+"' class='imgLink_off' onclick='showImage("+(imageTotal)+")'>0"+(imageTotal+1)+"</button>" );
	
	$("#billboard div[id='billbg_"+imageTotal+"']").hide();
	$("#billboard img[id='billimg_"+imageTotal+"']").hide();
	$("#billboard div[id='billsb_"+imageTotal+"']").hide();
	
	imageTotal++;
  });    
  imageTotal--;
  
  //show the first one when it's loaded
  imageCurrent=0;
  $("#billboard div[id='billbg_"+0+"']").show();
  $("#billboard div[id='billsb_"+0+"']").show();
  $("#billboard img[id='billimg_"+0+"']").show();
  $("#buttonbank button[id='link_"+0+"']").removeClass('imgLink_off');
  $("#buttonbank button[id='link_"+0+"']").addClass('imgLink_on');  
  
  // set up an auto-rotator
  if(isRotating) t=setTimeout(rotateImage, imageFlipDelay);  
}

function rotateImage() {

	imageCurrent++;
	if (imageCurrent>imageTotal) imageCurrent=0;
	
	// next image
	showImage(imageCurrent);
}

function showImage(inNum) {
	if (inNum==null) inNum=0;
	imageCurrent=inNum;	
    if(isRotating) clearTimeout(t);
	
	// hide everything
	$("#billboard").find("div").each(function(){ $(this).hide() });
	$("#billboard").find("img").each(function(){ $(this).hide() });
	
	// show the good stuff
	$("#billboard div[id='billbg_"+inNum+"']").show();
	$("#billboard img[id='billimg_"+inNum+"']").fadeIn();	
	$("#billboard div[id='billsb_"+inNum+"']").show();
	
	// highlight the good link block
	$("#buttonbank").find("button").each(function(){ $(this).addClass('imgLink_off'); $(this).removeClass('imgLink_on'); });
	$("#buttonbank").find("button").each(function(){ $(this).addClass('imgLink_off'); $(this).removeClass('imgLink_on'); });
	
	$("#buttonbank button[id='link_"+inNum+"']").removeClass('imgLink_off');
	$("#buttonbank button[id='link_"+inNum+"']").addClass('imgLink_on');
	
	  if(isRotating) t=setTimeout(rotateImage, imageFlipDelay);	
}
