function prepareDocument(){
	// initialize scrollable
  //  $(".scrollable").scrollable();

  function toBw(element){
    var url = $(element).attr("src").replace("_ct", "_bt");
    $(element).attr("src", url);
  };

  function toCt(imageToModify){
      var url = $(imageToModify).attr("src").replace("_bt", "_ct");
      $(imageToModify).attr("src", url);
    };

  function customUpdateActivePagerLink(pager, currSlide, clsName) {
    $(pager).each(function() {
       var totalSlides = $(this).children().size();
       var $currentAnchor = $(this).children().eq(currSlide);
       var $prevAnchor;

       if (currSlide > 0) {
         //De-colorize the previous thumbnail
         $prevAnchor = $(this).children().eq(currSlide - 1);
         toBw($('img', $prevAnchor[0]));
       } else {
         // De-colorize the last thumbnail
         toBw($('img', $(this).children().eq(totalSlides-1)[0]));
       }
      //Set activeSlide class on current slide
      $(this).children().removeClass(clsName).eq(currSlide).addClass(clsName);
       //Colorize the current thumbnail
       toCt($('img', $currentAnchor[0]));
     });
  };

  $(function(){
    $('#image_wrap').cycle({
      fx: 'scrollLeft',
      speed: 500,
      timeout: 6000,
      pauseOnPagerHover: true,
      next: '.next',
      prev: '.prev',
      pager: '.scrollable',
      updateActivePagerLink: customUpdateActivePagerLink,
      pagerAnchorBuilder: function(idx, slide){
        var thumb_url;
        if (!slide.children[0].src){
        //if (slide.is("a")){
          thumb_url = slide.children[0].children[0].src.replace("_full", "_bt");
        } else {
          thumb_url = slide.children[0].src.replace("_full", "_bt");
        }
        return '<a href="#"><img src="' + thumb_url + '" class="hoverable" /></a>';
      }
    });
  });

  // Replace black and white thumbs with colored ones on hover
  $(".hoverable").live({mouseenter: function(){
    toCt(this);
  }, mouseleave: function(){
    if (!$(this).closest("a").hasClass("activeSlide")) toBw(this);
  }});
}

$(document).ready(prepareDocument);
