$(document).ready(
  function() {
	  // - - - Add CSS to Centenary news items - - - //
	  $("#newscolumn").find("div").each(function() {
			var thisLink = $(this).find("p").first().find("a").first();
			if(thisLink.attr("href").match("centenarycampaign")) {
				$(this).addClass("centicon");
				$(this).attr("title","This is a Centenary Campaign news item");
			}
	  });
  }
);


function updateHomepage() {  
  donateLinks = new Array();
  donateLinks[0] = ["building.jpg", "future/", "Building a university for the future"];
  donateLinks[1] = ["culture.jpg", "culture/", "Culture"];
  donateLinks[2] = ["medicine.jpg", "health/", "Medicine and health"];
  donateLinks[3] = ["science.jpg", "science-technology/", "Emerging Science and Technology"];
  donateLinks[4] = ["social.jpg", "social-responsibility/", "Social Responsibility"];
  donateLinks[5] = ["talent.jpg", "student-talent/", "Supporting Student Talent"];
  donateLinks[6] = ["need.jpg", "future/regular-giving.html", "Where the need is greatest"];
  //donateLinks[7] = ["centenary.jpg", "centenarycampaign", "Centenary Campaign"];
  
  // create copy of original source to repopulate the array when all items have been extracted
  donateLinksOriginal = donateLinks.slice(0);

  updateDonationBox();
  
};

// Moved to separate function so it can be included in setTimeout()
function updateDonationBox() {
  // When all items have been shown start from scratch:
  if(donateLinks.length == 0){
	donateLinks = donateLinksOriginal.slice(0);
  }
  
  // get the new link
  var rand = Math.floor(Math.random()*donateLinks.length);
  var imgURL = "url(http://www.alumni.bristol.ac.uk/publications/images/misc/homepage/" + donateLinks[rand][0] + ")";
  var linkURL = "http://www.bristol.ac.uk/centenarycampaign/strategic-imperatives/" + donateLinks[rand][1];
  var linkText = donateLinks[rand][2];	

  // fade out
  $("#donate-box-link").animate({opacity: 0}, 600,
      // fade out completed
      function() {
	    // update image, link and text whilst it's hidden
		$("#donate-box-link").css("background-image", imgURL);
		$("#donate-box-link > a").html(linkText);
		$("#donate-box-link > a").attr('href', linkURL); 
		// fade it back in
        $("#donate-box-link").animate({opacity: 1},800, 
		   // fade in completed
		   function() { 
			// Set up the next change...
			setTimeout("updateDonationBox()", 10000);
		   }
		);
      }
  );
  
  // remove the just used item from the array
  donateLinks.splice(rand,1);
};

// since utilities.js isn't loaded into the current homepage this won't work...
addLoadEvent(updateHomepage);

