var ad_slider = {
  current_frame: 0,
  frames: null,
  link: null,
  visible_image: null,
  new_image: null,
  is_animating: false,
  is_paused: false,
  hinterval: null,
  delay: 4000,
  init: function() {
    this.frames = $('#ad-slider .ad-slider-images a');
    if (this.frames.size() == 0) { return; }

    this.frames.hide();
    this.link = $('#ad-slider .ad-slider-link');
    
    $('#ad-slider').css({ overflow: "hidden" });
    
    $('#ad-slider .ad-slider-frames').css({ position: "absolute", left: 10, top: 143 });
    var fno = 1;
    $('#ad-slider .ad-slider-frames').append($('<a href="javascript: void(0);" title="Pause"><img src="/templates/newimage2010/images/pause-button.png" style="margin-right: 4px;" /></a>'));
    this.frames.each(function(){
      $('#ad-slider .ad-slider-frames').append($('<a href="javascript: void(0);" title="'+fno+'"><img src="/templates/newimage2010/images/'+(fno==1 ? 'current-' : '')+'frame.png" /></a>'));
      fno++;
    });
    $('#ad-slider .ad-slider-frames a').each(function() {
      var link = $(this);
      link.css({ display: "inline", position: "static" });
      link.click(function(e){
        if (this.title == "Pause" || this.title == "Resume") {
          ad_slider.is_paused = !ad_slider.is_paused;
          if (ad_slider.is_paused) {
            $('img', link).attr('src', '/templates/newimage2010/images/resume-button.png');
          } else {
            $('img', link).attr('src', '/templates/newimage2010/images/pause-button.png');
          }
        } else if (!ad_slider.is_animating) {
          ad_slider.change_frame(Number(this.title)-1);
        }
      });
    });
    
    //set up first image
    var new_frame = this.frames.eq(this.current_frame);
    this.link.attr('href', new_frame.attr('href'));
    if (new_frame.attr('rel')=='external') { ad_slider.link.attr('target', '_blank'); } else { ad_slider.link.attr('target', ''); };
    this.link.attr('title', new_frame.attr('title'));
    
    this.visible_image = $('<img />');
    $('#ad-slider .ad-slider-images').append(this.visible_image);
    this.visible_image.css({ position: "absolute", left: 0, top: 0 });
    this.visible_image.attr('src', $('img', new_frame).attr('src'));

    this.new_image = $('<img />');
    $('#ad-slider .ad-slider-images').append(this.new_image);
    this.new_image.css({ position: "absolute", left: 597, top: 0 });
    
    //set the timer going
    ad_slider.hinterval = window.setInterval(this.change_frame_tick, this.delay);
  },
  change_frame: function(fno) {
    if (!this.is_animating && fno != this.current_frame) {
    
      //do an immediate frame change
      this.is_animating=true;
      window.clearInterval(this.hinterval);
      
      var new_frame = this.frames.eq(fno);
      this.link.css({ display: 'none'});
      
      this.new_image = $('<img />');
      $('#ad-slider .ad-slider-images').append(this.new_image);
      this.new_image.css({ position: "absolute", left: 597, top: 0 });
      this.new_image.attr('src', $('img', new_frame).attr('src'));
      this.new_image.animate( { left: 0 }, function(){
        ad_slider.link.attr('href', new_frame.attr('href'));
        if (new_frame.attr('rel')=='external') { ad_slider.link.attr('target', '_blank'); } else { ad_slider.link.attr('target', ''); };
        ad_slider.link.attr('title', new_frame.attr('title'));
        ad_slider.link.css({ display: 'block'});
        
        $('#ad-slider .ad-slider-frames a:gt(0) img').attr('src', '/templates/newimage2010/images/frame.png');
        $('#ad-slider .ad-slider-frames a:eq('+(fno+1)+') img').attr('src', '/templates/newimage2010/images/current-frame.png');
        
        ad_slider.current_frame=fno;
        ad_slider.is_animating=false;

        ad_slider.hinterval = window.setInterval(ad_slider.change_frame_tick, ad_slider.delay);
      });
    }
  },
  change_frame_tick: function() {
    if (!ad_slider.is_paused) {
      var fno = ad_slider.current_frame + 1;
      
      if (fno >= ad_slider.frames.size()) { fno = 0 }
      
      ad_slider.change_frame(fno);
    }
  }
  
};
$().ready(function(){
  ad_slider.init();
});
