var priorCenterItem = 1;
var imageList = [ "",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/thumbnails/Hkg2264487-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/thumbnails/Hkg2264510-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/thumbnails/Hkg2264518-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/thumbnails/000_Hkg2264614-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/thumbnails/Hkg2264543-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/thumbnails/Hkg2264605-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/thumbnails/Hkg2264510-thumb.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/thumbnails/Hkg2264692-thumb.jpg"];
var urlList = [ "",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/hkg2264487.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/hkg2264510.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/hkg2264518.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/000_hkg2264614.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/hkg2264543.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/hkg2264605.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/hkg2264650.jpg",
"/images/photo-gallery/wellington-test-(nz-v-ind)---day-4/hkg2264692.jpg"];
var headList = [ "",
"STEPPING IT UP...",
"DELICATELY PLAYED",
"STEAMING IN...",
"PROUD MOMENT",
"MASTER'S ADVICE",
"WARM REGARDS...",
"CASTLED!",
"NEW ZEALAND'S ONLY HOPE"];
var capList = [ "",
"Dhoni runs a quick single during his brisk half-century on Monday morning",
"Yuvraj steers the ball to  mid-wicket and takes off for a couple of runs",
"New Zealand's Chris Martin was relentless with a fiery spell in the morning session",
"World Record holder for catches, Rahul Dravid gives thumbs up to the cameras as India go into for the lunch interval",
"Tendulkar tells Ishant and Zaheer a thing or two before the start of New Zealand's second innings",
"India captain MS Dhoni congrats Dravid on setting the new world record for catches",
"Daniel Flynn's poor run in the series continued as he dragged a Zaheer Khan delivery back onto his stumps",
"Ross Taylor rocks back and plays a beautiful pull shot off Ishant. The aggressive batsman played his strokes despite the fall of wickets"];
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 8"; 
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:               8,
    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);

