var Promotion = Class.create({

  interval: 5,
  paused: false,
  
  markerImage: '/images/icons/promoted_spot.gif',
  textElement: '.online .promotion',

  initialize: function(mapHandler) {
    this.mapHandler = mapHandler;
    
    icon = new GIcon(G_DEFAULT_ICON);
    icon.iconSize = new GSize(69, 83);
    icon.image = this.markerImage;
    icon.printImage = this.markerImage;
    icon.shadow = null;
    icon.iconAnchor = new GPoint(35, 83);
    icon.imageMap = [0, 0, 69, 0, 69, 83, 0, 83];
    
    this.promotionMarker = new GMarker(new GLatLng(0, 0), { icon: icon });
    this.promotionMarker.hide();

    this.mapHandler.map.addOverlay(this.promotionMarker);
    
    this.periodicalExecuter = new PeriodicalExecuter(this.promoteMarker.bind(this), this.interval);
  },
  
  promoteMarker: function(marker) {
    if (this.paused) return;
  
    if (this.stashedMarker) this.stashedMarker.show();

    marker = this.mapHandler.nextPromotionMarker();
    if (marker) {
      this.stashedMarker = marker;
      marker.hide();

      GEvent.clearListeners(this.promotionMarker, 'click');
      GEvent.addListener(this.promotionMarker, 'click', function(marker, event) { GEvent.trigger(marker, 'click'); }.bind(this).curry(marker));

      this.promotionMarker.setLatLng(marker.getLatLng());
      this.promotionMarker.show();
      
      text = (marker.spotId ? '<a href="/spots/' + marker.spotId + '">' + marker.getTitle() + '</a>' : marker.getTitle());
      text += marker.promotions.map(function(promotion) {
        return ' / <a href="/spots/' + marker.spotId + '/promotions/' + promotion.id + '">' + promotion.title + '</a>';
      }).join('<br/>');
      j$(this.textElement).html(text).show();
    } else {
      this.promotionMarker.hide();
      j$(this.textElement).hide();
    }
  },
  
  pause: function() {
    this.paused = true;
  },
  
  unpause: function() {
    this.paused = false;
  }

});

