var priorCenterItem = 1;
var imageList = [ "",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/sytrophy220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/razzaq220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/sangakkara220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/gul220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/matthews220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/shahid220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/malik220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/malinga220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/afridi220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/slteam220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/safridi220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/younis220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/younus220609_th.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/thumbnails/arazzaq220609_th.jpg"];
var urlList = [ "",
"/images/photo-gallery/wt20-final---pak-vs-sri/sytrophy220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/razzaq220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/sangakkara220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/gul220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/matthews220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/shahid220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/malik220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/malinga220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/afridi220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/slteam220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/safridi220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/younis220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/younus220609.jpg",
"/images/photo-gallery/wt20-final---pak-vs-sri/arazzaq220609.jpg"];
var headList = [ "",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""];
var capList = [ "",
"Rival captains pose on the eve of the ICC World Twenty20 finals",
"Abdul Razzaq rocked Sri Lanka's top order",
"Kumar Sangakkara held fort amid the wickets",
"Umar Gul celebrates getting rid of Chamara Silva",
"Angelo Mathews made a useful unbeaten 35",
"Shahid Afridi plays one over covers",
"Shoaib Malik supported Shahid Afridi well with his unbeaten 24",
"Lasith Malinga reacts after failing to stop the ball for a four against Pakistan",
"Shahid Afridi celebrates the moment of victory",
"The Sri Lankan team is pictured after losing the final of the ICC Twenty20 World Cup to Pakistan",
"Shahid Afridi receives the Man of the Match award",
"Pakistan are crowned World Twenty20 champions",
"Younis Khan lifts the trophy with Shahid Afridi and team mates",
"Abdul Razzaq lifts the trophy as his team mates celebrate following their victory"];
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 14"; 
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:               14,
    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);

