var priorCenterItem = 1;
var imageList = [ "",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/101784.jpg-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/101783.jpg-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/101785.jpg-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/Hkg2267315.jpg-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/Hkg2267304.jpg-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/hkg2267573-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/hkg2267602-thumb'.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/hkg2267838.jpg-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/hkg2267809.jpg-thmb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/hkg2267836.jpg-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/thumbnails/hkg2267884.jpg-thumb.jpg"];
var urlList = [ "",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/101784.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/101783.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/101785.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/hkg2267315.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/hkg2267304.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/hkg2267573.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/hkg2267602.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/hkg2267838.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/hkg2267809.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/hkg2267836.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-5/hkg2267884.jpg"];
var headList = [ "",
"REFUSING TO GIVE IN...",
"BEST KNOCK EVER",
"PART-TIMER??",
"ABSOLUTELY LIVID",
"BOWLER OF THE SERIES",
"WOULD YOU BELIEVE IT?",
"GOING THROUGH THE MOTION...",
"HELPLESSLY HOPING",
"SEAMERS IN ARMS",
"THE LOOK, SAYS IT ALL",
"MOMENT OF ECSTASY"];
var capList = [ "",
"Franklin battled for almost the whole of the first session until he was snapped by Tendulkar. Here, he plays the ball to mid-on",
"Ross Taylor celebrates his 4th Test century and easily the most significant of his brief career",
"Tendulkar bamboozled the Kiwis with leg-breaks, googlies and various other variations, leaving India on the brink of a series-win. Here, he is embraced by his team-mates for removing McCullum",
"Ross Taylor is dejected after being cleaned up by an exceptional delivery from Harbhajan Singh ",
"Harbhajan celebrates the wicket of Ross Taylor, his 16th scalp of the three-Test series",
"Tendulkar is elated after removing James Franklin! The master took two crucial wickets",
"The Indian team starts to celebrate a monumental series-win in New Zealand",
"New Zealand fast bowler Chris Martin sits and watches the outfield as the rain plays spoilsport",
"Ishant Sharma and Munaf Patel share a light moment ahead of the presentation ceremony",
"A dejected Daniel Vettori walks towards the dressing room",
"India captain Mahendra Singh Dhoni holds aloft the winner's trophy"];
var lastRan = -1;
var fmtItem = function(imgUrl, url, title, i) {
var innerHTML = '<img id="carousel-image-' + i + '" src="' + imgUrl + '" width="' + 75 + '" height="' + 75+ '"/><a id="carousel-anchor-' + i + '" href="' + url + '">' + title + '</a>';
return innerHTML;
};
var loadInitialItems = function(type, args) {
 var start = args[0];
 var last = args[1]; 
load(this, start, last);
spotlight(this);
preview(this);
};
var loadNextItems = function(type, args) {;
var li = this.getItem(priorCenterItem);;
var start = args[0];
var last = args[1];
var alreadyCached = args[2];
if(!alreadyCached) {
load(this, start, last);
 }
spotlight(this);
 preview(this);
};
var loadPrevItems = function(type, args) {
// get the last middle item and turn off spotlight
var li = this.getItem(priorCenterItem);
var start = args[0];
var last = args[1]; 
var alreadyCached = args[2];
 if(!alreadyCached) {
load(this, start, last);
}
spotlight(this);
preview(this);
};
var load = function(carousel, start, last) {
for(var i=start;i<=last;i++) {
carousel.addItem(i, fmtItem(imageList[i], urlList[i], "", i), 'non-spotlight');
 // Image click will scroll to the corresponding carousel item.
 YAHOO.util.Event.addListener('carousel-image-'+i, 'click', function(evt) {
 this.carousel.scrollTo(this.index-2);
  }, {carousel:carousel,index:i}, true);
 }
};
var getRandom = function(max, last) {
  var randomIndex;
  do {
     randomIndex = Math.floor(Math.random()*max);
   } while(randomIndex == last);
  return randomIndex;
};
var handlePrevButtonState = function(type, args) {
  var enabling = args[0];
  var leftImage = args[1];
  if(enabling) {
      leftImage.src = "/images/left-enabled.gif"; 
  } else {
     leftImage.src = "/images/left-disabled.gif";
 }
};
var handleNextButtonState = function(type, args) {
var enabling = args[0];
var rightImage = args[1];
if(enabling) {
rightImage.src = "/images/right-enabled.gif";   
} else {
rightImage.src = "/images/right-disabled.gif";
}
};
 function completeHandler(type, args) {
}
function preview(carousel) {
var firstVisible = carousel.getProperty("firstVisible");
var middle = firstVisible + 2;
var anchor = YAHOO.util.Dom.get('carousel-anchor-' + middle);
//alert(anchor.href)
YAHOO.util.Dom.get('preview').innerHTML = '<img src="' + anchor.href + '"/>';
//alert(middle);
var status = "Showing " + middle + " of 11"; 
YAHOO.util.Dom.get("indicator").innerHTML = status;
YAHOO.util.Dom.get("photo-header").innerHTML = headList[middle];
YAHOO.util.Dom.get("photo-caption").innerHTML = capList[middle];
}
function spotlight(carousel) {
var firstVisible = carousel.getProperty("firstVisible");
var start = firstVisible;
var revealAmount = carousel.getProperty("revealAmount");
var size = carousel.getProperty("size");
if(revealAmount && firstVisible > 1) {
   start = firstVisible - 1;
}
var lastVisible = firstVisible + carousel.getProperty("numVisible") - 1;
var end = lastVisible;
 if(revealAmount && lastVisible < size) {
    end = lastVisible + 1;
 }
var middle = firstVisible + 2;
for(var i=start; i<=end; i++) {
    var li = carousel.getItem(i);
   if(i == middle) {
       YAHOO.util.Dom.replaceClass(li, 'non-spotlight', 'spotlight');
       priorCenterItem = i;
   } else {
       YAHOO.util.Dom.replaceClass(li, 'spotlight', 'non-spotlight');
   }
 }
}
     var carousel; // for ease of debugging; globals generally not a good idea
var pageLoad = function() 
{
   carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
      {
    numVisible:         6,
    animationSpeed:     0.6,
    animationMethod:    YAHOO.util.Easing.backBoth,
    scrollInc:          1,
    navMargin:          40,
    scrollBeforeAmount: 2,
    firstVisible:       1,
    size:               11,
    scrollAfterAmount:  3,
    prevElement:        "prev-arrow",
    nextElement:        "next-arrow",
    loadInitHandler:    loadInitialItems,
    loadNextHandler:    loadNextItems,
    loadPrevHandler:    loadPrevItems,
    prevButtonStateHandler:   handlePrevButtonState,
    nextButtonStateHandler:   handleNextButtonState,
    animationCompleteHandler: completeHandler
   }
 );
  //carousel.loadNextHandlerEvt.subscribe(beforeNext, carousel);
};
YAHOO.util.Event.addListener(window, 'load', pageLoad);

