/* Create a slideshow based on a html list */
var SlideShow = Class.create({
  initialize: function(container, interval){
    this.current = 0;
    this.container = $(container);
    this.items= this.container.select('li');
    this.size= this.items.size();
    this.interval = interval;
    this.showFirst();
    
    // Start rotating
    var self = this;
    setInterval(function(){self.transition()}, this.interval);
  },
  showFirst: function(){
    this.items.invoke('hide');
    this.items.first().show();
  },
  transition: function() {
    Effect.Fade(this.items[this.current], { duration:1, from:1.0, to:0.0 }); 
    this.current= (this.current + 1) % this.size;
    Effect.Appear(this.items[this.current], { duration:1, from:0.0, to:1.0, delay:1.0 });
 }
});


/** Framework extensions **/
Date.millisPerDay = 86400000;

Date.prototype.stripTime = function() { return new Date(this.getFullYear(), this.getMonth(), this.getDate());};

/** Creates a new date one day in the future **/
Date.prototype.nextDay = function() {  
  //return new Date(this.getTime() + Date.millisPerDay);
  var next = new Date(this);
  next.setDate(this.getDate() + 1);
  return next;
}

/** Creates a new date one day in the future **/
Date.prototype.nextDay2 = function() {
  var next = this.stripTime();
  next.setHours(12); // Prevent daylight savings time boundaries from showing a duplicate day
  next.setDate(next.getDate() + 1);
  return next.stripTime();
}

/** Creates a new date one day in the past **/
Date.prototype.previousDay = function() {  
  //return new Date(this.getTime() - Date.millisPerDay);;
  var prev = new Date(this);
  prev.setDate(this.getDate() - 1);
  return prev;  
}

/** Prints the date out in YYYY-MM-DD form **/
Date.prototype.toISODateString = function() {
   return this.getFullYear() + '-' + (this.getMonth() + 1).toPaddedString(2) + '-' + this.getDate().toPaddedString(2);
}

/** Returns a new date given an ISO formatted dates string of YYYY-MM-DD **/
Date.fromISODateString = function(dateString) {
   values = dateString.match(/(\d\d\d\d)-(\d\d)-(\d\d)/);
   if (values.size() == 4) {
     return new Date(values[1], values[2] - 1, values[3]);
   }
}

/**
 * Class for disabling DIVs using a transparent div above them
 * (The target DIV should have relative positioning)
 */
var Disabler = Class.create({
  initialize: function(target){
    this.container = $(target);
    this.overlayClassName = 'disabler-overlay';
    this.overlay = this.container.down('.' + this.overlayClassName);
    if (!this.overlay){
      this.createOverlay(); 
    }
  },
  createOverlay: function(){
    this.overlay = new Element('div');
    this.overlay.addClassName(this.overlayClassName);

    this.overlay.setStyle({
      position: 'absolute',
      top: 0,
      left: 0,
      zIndex: 1000,
      backgroundColor: '#FFF',
    	opacity: 0.45,
      display: 'none'
    });
    this.container.setStyle({position: 'relative'});
    this.container.insert(this.overlay);
  },
  resize: function(){
    this.overlay.clonePosition(this.container, {
      setTop: false,
      setLeft: false
    });
  },
  enable: function(){    
    this.overlay.hide();
  },
  disable: function(){
    this.resize();
    this.overlay.removeClassName('loading');
    this.overlay.show();
  },
  disableLoading: function(){
    this.resize();
    this.overlay.addClassName('loading');
    this.overlay.show();
  }
});

/* On every page load */
Event.observe(window, 'load', function() {
  /* Initialize property badges */
  $$('.property-status-badge').each(function(badge){
    var image = badge.previous('img');
    if (image) {
      badge.setStyle({
        width: (image.getDimensions().width - 2) + 'px',
        left: (image.positionedOffset().left + 1) + 'px'
      });
    }
    badge.setStyle({visibility: 'visible'});
    /*clonePosition(image, {
      setTop: false,
      setHeight: false
    });*/
  });
  

  /* Emulate mouse over for hints in IE6 */
  if (Prototype.Browser.IE) {
    $$('span.help').each(function(help){
      help.observe('mouseenter', function(event){Event.element(event).addClassName('hover');});
      help.observe('mouseleave', function(event){Event.element(event).removeClassName('hover');});
    });
  }
});
