// homepage image swapper code.
function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}

function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
var bikky = document.cookie;
var today = new Date();
var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days

  
function SetCookie(name, value) { // use: setCookie("name", value);
    if (value != null && value != "")
      document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
    bikky= document.cookie; // update bikky
}


function rotateHomePageImage() {
  // rotates an image on the homepage depending upon which images have already been seen by the user.
  // set up: 
  // 1. make sure your image has a matching name attr e.g. <img name="homepageimage" src="img/home_image1.jpg" width="557" height="223">
  // 2. make sure imageToRotate is the same thing that you call the image e.g. "homepageimage"
  // 3. define you image array
  // 4. add the function call rotateHomePageImage() in script tags directly after the image to rotate in the HTML
  // note: you can also do the function call in the body onload event, but it will not look as nice, because you will have to wait for 
  // the entire page to load before seeing the image.
  
  var lastSeen = 0;
  var imagesToRotate = ['/images/image1.jpg','/images/image2.jpg', '/images/image3.jpg','/images/image4.jpg','/images/image5.jpg','/images/image6.jpg'];

  var imageToRotate = "homepageimage";
  lastSeen = GetCookie("lastSeenImage");

  if (lastSeen) {
    //   alert("the last seen cookie was " + lastSeen + " adding 1");
    SetCookie("lastSeenImage",1 + parseInt(lastSeen));
    // alert("cookie is now" + GetCookie("lastSeenImage") + "image is now " + imagesToRotate[0]);
  } else {
    SetCookie("lastSeenImage","1");
    lastSeen = 0;
    // alert("set cookie to 1");
  }
  var imageToUse;
  if (lastSeen >= imagesToRotate.length) {
    var remainder = lastSeen % imagesToRotate.length;
    imageToUse = imagesToRotate[remainder];
    // alert("use the remainder " + remainder );
  } else {
    imageToUse = imagesToRotate[lastSeen];
    //alert("use last seen " + lastSeen);
  }
   // alert("use the image " + imageToUse);
   MM_swapImage(imageToRotate,'',imageToUse,1);
}
