YAHOO.namespace("AMAN.carousel");  // create the AMAN carousel namespace

// create the object with initialisation, animation & properties
YAHOO.AMAN.carousel = 
{
  $D:"",  // variable to hold YAHOO.util.Dom reference
  $C:"",  // variable to hold YAHOO.AMAN.carousel reference
  $Ev:"",  // variable to hold YAHOO.util.Event reference
  $El:"",  // variable to hold YAHOO.util.Element reference
  thumbs:"",
  hero:"",
  item:0,
  atOnce:6,
  auto:true,
  timer:"",
  delay:5000,
  caption:"",
  
  hideThumbs: function() {
      $C.thumbs = $D.getElementsByClassName('carousel-thumb', 'img', 'albumrightcol'); // look for all a tags in mappanel div with class mapdot  
      $D.setStyle($C.thumbs,'display','none');
      $Ev.addListener($C.thumbs,'click',$C.showHero);
  },
  
  showThumbs: function(num) {
      var howMany = ($C.thumbs.length / $C.atOnce);
      var which = parseInt(num / $C.atOnce);
      // window.status = $C.thumbs.length + ":" + $C.atOnce +":" + howMany + ":" + which;
      for(var i=0; i< $C.atOnce; i++)
      { 
        $D.setStyle($C.thumbs[(which* $C.atOnce)+i],'display','block'); 
      }
      
      $C.showHeroImg($C.thumbs[num]);

  },

  showHero: function(e) {
    var tmp = this.getAttribute('src');
    var cap = this.getAttribute('alt');

    tmp = tmp.replace('thumbnails_64/','');
    tmp = tmp.replace('_64','_alb');
    tmp = tmp.replace('(1)','');
    hero.setAttribute('src',tmp);
    caption.innerHTML = cap;
  },

  showHeroImg: function(e) {
    var tmp = e.getAttribute('src');
    var cap = e.getAttribute('alt');
    tmp = tmp.replace('thumbnails_64/','');
    tmp = tmp.replace('_64','_alb');
    hero.setAttribute('src',tmp);
    caption.innerHTML = cap;
  },

  wireButtons: function()
  {
     $Ev.addListener('next-arrow','click',$C.nextPress);
     $Ev.addListener('prev-arrow','click',$C.prevPress);
     $Ev.addListener('pause-arrow','click',$C.pausePress);
  },
  
  nextPress: function()
  {
    $C.auto = false;
    $C.incSlide();
  },
  
  prevPress: function()
  {
    $C.auto = false;
    $C.decSlide();
  },
  
  pausePress: function()
  {
    clearInterval($C.timer);
    $C.auto = false;
  },
  
  incSlide: function()
  {
    if ($C.auto == false) { clearInterval($C.timer); }
    $C.item++;
    if ($C.item> ($C.thumbs.length - 1)) { $C.item=0; }
    $C.showHeroImg($C.thumbs[$C.item]);
    $C.hideThumbs();
    $C.showThumbs($C.item);
  },
  
  decSlide: function()
  {
    if ($C.auto == false) { clearInterval($C.timer); }
    $C.item--;
    if ($C.item <0) { $C.item=($C.thumbs.length - 1); }
    $C.showHeroImg($C.thumbs[$C.item]);
    $C.hideThumbs();
    $C.showThumbs($C.item);
  },

  init: function() {
    $C = YAHOO.AMAN.carousel;
    $D = YAHOO.util.Dom;
    $Ev = YAHOO.util.Event;
    
    // store a reference to the large image id
    hero = $D.get('album_large');
    
    // store a reference to caption text
    caption = $D.get('album_caption');
    
    $C.hideThumbs();
    $C.showThumbs($C.item);
    $C.wireButtons();
    
    if ($C.auto == true)
    {
      $C.timer = setInterval('$C.incSlide()',$C.delay);
    }
    
  }


};

YAHOO.util.Event.onAvailable('next-arrow',YAHOO.AMAN.carousel.init); // wait for the div next-arrow to appear before wiring it up


