
var frontSrcs = new Array();
frontSrcs[ 0] = '../images/front/competition.jpg';
frontSrcs[ 1] = '../images/front/acoustics.jpg';
frontSrcs[ 2] = '../images/front/classes.jpg';
frontSrcs[ 3] = '../images/front/whaleeducationcentre.jpg';
frontSrcs[ 4] = '../images/front/strandings.jpg';
frontSrcs[ 5] = '../images/front/behaviour.jpg';
frontSrcs[ 6] = '../images/front/satellitetagging.jpg';
frontSrcs[ 7] = '../images/front/exhibitions.jpg';
frontSrcs[ 8] = '../images/front/birthing.jpg';
frontSrcs[ 9] = '../images/front/communityactivities.jpg';
frontSrcs[10] = '../images/front/frontiersabroad.jpg';
frontSrcs[11] = '../images/front/televisiondocumentaries.jpg';
frontSrcs[12] = '../images/front/childrensart.jpg';
frontSrcs[13] = '../images/front/localschoolprojects.jpg';
frontSrcs[14] = '../images/front/populationabundance.jpg';
frontSrcs[15] = '../images/front/outerislandoutreach.jpg';
frontSrcs[16] = '../images/front/conservationinternational.jpg';
frontSrcs[17] = '../images/front/universityofauckland.jpg';
frontSrcs[18] = '../images/front/populationidentity2.jpg';
frontSrcs[19] = '../images/front/genetics.jpg';
frontSrcs[20] = '../images/front/inspiringlocalinterest.jpg';
frontSrcs[21] = '../images/front/exwhalers.jpg';
frontSrcs[22] = '../images/front/threats.jpg';
frontSrcs[23] = '../images/front/flukeidentification.jpg';
frontSrcs[24] = '../images/front/airnewzealand.jpg';
frontSrcs[25] = '../images/front/paradiseinn.jpg';

var frontImages = new Array();

/*var frontLinks = new Array();
frontLinks[0] = '../team/nan.php';
frontLinks[1] = '../ourwork/conservation.php';
frontLinks[2] = '../projects/humpback.php';
frontLinks[3] = '../support.php';*/

var rotator; //, rotatorLink;

document.write("<style type='text/css'>#rotator{visibility:hidden;}</style>");

function setOpacity(obj, opacity) {
  opacity = (opacity >= 100) ? 99.999 : opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}


function imageLoaded(img) {
   // During the onload event, IE correctly identifies any images that
   // weren’t downloaded as not complete. Others should too. Gecko-based
   // browsers act like NS4 in that they report this incorrectly.
   if (!img.complete) 
   {
      return false;
   }

   // However, they do have two very useful properties: naturalWidth and
   // naturalHeight. These give the true size of the image. If it failed
   // to load, either of these should be zero.
   if (typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0) 
   {
      return false;
   }

   // No other way of checking: assume it’s ok.
   return true;
}

function rotateImages(imgIndex, opacity, fadingin)
{
   if (fadingin)
   {
      if (opacity > 99)
      {
         opacity = 100;
         setOpacity(rotator, opacity);
         
         var nextImgIndex = imgIndex+1;
         if (nextImgIndex >= frontImages.length)
            nextImgIndex = 0;
         
         if (imageLoaded(frontImages[nextImgIndex]))
         {
            fadingin = false;
            window.setTimeout("rotateImages("+imgIndex+","+opacity+","+fadingin+")", 3000);
         }
         else
         {
            window.setTimeout("rotateImages("+imgIndex+","+opacity+","+fadingin+")", 1000);
         }
         
         //fadingin = false;
         //window.setTimeout("rotateImages("+imgIndex+","+opacity+","+fadingin+")", 3000);
      }
      else
      {
         setOpacity(rotator, opacity);
         opacity += 8;
         window.setTimeout("rotateImages("+imgIndex+","+opacity+","+fadingin+")", 100);
      }
   }
   else
   {
      if (opacity < 0)
      {
         opacity = 0;
         setOpacity(rotator, opacity);
         
         imgIndex++;
         if (imgIndex >= frontImages.length)
            imgIndex = 0;
            
         rotator.src = frontImages[imgIndex].src;
         //rotatorLink.href = frontLinks[imgIndex];
         
         fadingin = true;
         window.setTimeout("rotateImages("+imgIndex+","+opacity+","+fadingin+")", 100);
      }
      else
      {
         setOpacity(rotator, opacity);
         opacity -= 8;
         window.setTimeout("rotateImages("+imgIndex+","+opacity+","+fadingin+")", 100);
      }
   }
}



function preload()
{
   for (var i=0; i<frontSrcs.length; i++)
   {
      frontImages[i] = new Image();
      //frontImages[i].onload = function {frontReady[i] = true; }
      frontImages[i].src = frontSrcs[i];
   }
}

function waitToStartRotate()
{
   if (imageLoaded(frontImages[0]))
   {
      rotateImages(0, 0, true);
      
      rotator.src = frontImages[0].src;
      //rotatorLink.href = frontLinks[0];
      
      rotator.style.visibility = 'visible';
   }
   else
   {
      window.setTimeout("waitToStartRotate()", 1000);
   }
}

function startRotate()
{
   preload();

   rotator = document.getElementById('rotator');
   //rotatorLink = document.getElementById('rotatorLink');
   
   //rotateImages(0, 0, true);
   waitToStartRotate();
}

function confirmDelete()
{
   return confirm("Are you sure?");
}

