var priorCenterItem = 1;
var imageList = [ "",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91455259TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91458651th.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91457077TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91457163TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91458261TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91459453TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91458981TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91459472TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91462422TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91462903TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91463758TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91464393TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91464949th.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91467993TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91466194TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91468005TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91469847TH.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91467154th.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91467660th.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/thumbnails/91467858th.jpg"];
var urlList = [ "",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91455259.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91458651.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91457077.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91457163.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91458261.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91459453.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91458981.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91459472.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91462422.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91462903.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91463758.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91464393.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91464949.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91467993.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91466194.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91468005.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91469847.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91467154.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91467660.jpg",
"/images/photo-gallery/ct-final-(centurion)---aus-vs-nz/91467858.jpg"];
var headList = [ "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""];
var capList = [ "",
"Peter Siddle made the early breakthrough when he removed Brendon McCullum for a duck",
"Martin Guptill gave New Zealand some momentum after a slow star",
"Aaron Redmond was stumped off Nathan Hauritz",
"Nathan Hauritz claimed key wickets to trouble New Zealand",
"Ross Taylor failed to put his best",
"Brett Lee congratulate by team mates after removing Grant Elliott",
"Neil Broom fought hard for his 37",
"Callum Ferguson needed treatment after injuring his knee",
"Shane Bond removed Tim Paine in his first over",
"Kyle Mills lifted New Zealand with the vital wicket of Ricky Ponting",
"Brendon McCullum drops a top edge off Cameron White",
"Cameron White played a vital innings after the loss of early wickets",
"Shane Watson steadied Australia's chase after two early losses",
"Shane Watson score match-winning run with a six over mid wicket",
"James Hopes and Shane Watson celebrate yet another title",
"Shane Watson named Man of The Match for his match-winning knock of 105*",
"Haroon Lorgat congratulates Ricky Ponting on the win as Shane Watson and Brett Lee wait in line",
"Australia celebrate their second consecutive Champions Trophy win",
"Ricky Ponting and Shane Watson celebrate with the Champions Trophy",
"Mitchell Johnson and Brett Lee celebrate in the changing rooms with the Champions 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 20"; 
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:               20,
    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);

