var pchp = {};

pchp.PromoInfo = {
  container_id:     'homepromo-container',
  arrow_class:      'arrowlink',
  left_arrow_id:    'homepromolink-left',
  right_arrow_id:   'homepromolink-right',
  slot_class:       'homepromoslot',
  slot_image_class: 'homepromoslot-img',
  slot_ids:         ['homepromoslot1', 'homepromoslot2', 'homepromoslot3']
};

pchp.makeImage = function(link) {
  var img = new Image();
  img.src = link.src;
  return img;
};

pchp.getExistingPromoLinks = function(slots) {
  var m = MochiKit.Base, md = MochiKit.DOM;
  var links = [];
  
  for(var i=0; i<slots.length; i++) {
    var slot = slots[i];
    var slot_image = md.getElement(slot.id+'-img');
    links.push({
      href: slot.href,
      src: slot_image.src,
      title: slot_image.alt || ""
    });
  }
  return links;
};

pchp.compareHrefs = function(a, b) {
  return (a.href == b.href);
};

// This takes a liberal charge by presuming the names of the promo slot
// ids and classnames. See `homepage.css` and pchp.PromoInfo for definitions.
pchp.PromoManager = function(links) {
  var m = MochiKit.Base, md = MochiKit.DOM, P = pchp.PromoInfo;
  links = links || [];
  
  this.container = md.getElement(P.container_id);
  this.left_arrow = md.getElement(P.left_arrow_id);
  this.right_arrow = md.getElement(P.right_arrow_id);
  this.slots = m.map(md.getElement, P.slot_ids);
  
  // Find the links already in the page, and then add them to the end of
  // the passed in list.
  var existing_links = pchp.getExistingPromoLinks(this.slots);
  var existing_hrefs = m.map(m.itemgetter('href'), existing_links);
  var promolinks = m.filter(
    function(v) { return (m.findValue(existing_hrefs, v.href) == -1); }, 
    links
  );
  this.preloadCache = m.map(pchp.makeImage, promolinks);
  
  m.extend(promolinks, existing_links);
  
  this.promolinks = promolinks;
  this.promolist = linklist.Double(promolinks, {circular: true});
  this.window_start = this.promolist.find(m.partial(pchp.compareHrefs, this.slots[0]));
  this.window_end = this.promolist.find(m.partial(pchp.compareHrefs, this.slots[this.slots.length-1]));
  
  m.bindMethods(this);
  this.connectEvents();
};

MochiKit.Base.update(pchp.PromoManager.prototype, {
  __repr__: function() {
    return "[pchp.PromoManager]";
  },
  toString: function() { return this.__repr__(); },
  
  connectEvents: function() {
    var connect = MochiKit.Signal.connect;
    connect(this.left_arrow, 'onclick', this, this.onLeftClick);
    connect(this.right_arrow, 'onclick', this, this.onRightClick);
    connect(window, 'onunload', this, this.onWindowUnload);
  },
  
  onLeftClick: function(evt) {
    var first, second, third;
    first = this.window_start.next;
    second = first.next;
    third = second.next;
    
    this.updateSlots([first, second, third]);
    
    evt.stop();
  },
  
  onRightClick: function(evt) {
    var first, second, third;
    third = this.window_end.previous;
    second = third.previous;
    first = second.previous;
    
    this.updateSlots([first, second, third]);
    
    evt.stop();
  },
  
  updateSlots: function(newslots) {
    var m = MochiKit.Base, md = MochiKit.DOM;
    for(var i=0; i<newslots.length; i++) {
      var slot = this.slots[i], newslot = newslots[i].value;
      var slot_image = md.getElement(slot.id+'-img');
      
      slot.href = newslot.href;
      slot_image.src = newslot.src;
      slot_image.alt = newslot.title;
    }
    this.window_start = newslots[0];
    this.window_end = newslots[newslots.length-1];
  },
  
  // cleanup;
  onWindowUnload: function(evt) {
    this.preloadCache = null;
  }
});
