(function() {
  Neaux.Namespace('Neaux.Api');

  //
  // Altos is an "abstract class" from which AltosChart and AltosStats "classes" are derived
  // Define the constructors in the Neaux.Api namespace. The members will be defined below.
  //
  Neaux.Merge( Neaux.Api, {
    Altos: function( aID) {
      if ( aID) this.dflt = { pai: aID, ra: 'a', q: 'a', rt: 'sf' };
    },
    AltosChart: function( aID) {
      if ( !aID) throw new Error( 'Altos: An Altos Customer ID must be specified as the constructor argument');
      Neaux.Api.Altos.apply( this, arguments);
      Neaux.Merge( this.dflt, { service: 'chart', sz: 'm', ts: 'e', sd: 'f', endDate: ''});
      this.Reset();
    },
    AltosStats: function( aID) {
      if ( !aID) throw new Error( 'Altos: An Altos Customer ID must be specified as the constructor argument');
      Neaux.Api.Altos.apply( this, arguments);
      //this.dflt.service = 'statXml';
      this.dflt.service = 'stat';
      this.dflt.lnk = 'f';
      this.Reset();
    }
  });

  // Define Some Constants
  Neaux.Api.Altos.FIELDS = {  // FIELDS
    FLD_ACCOUNT_ID: 'pai', FLD_SERVICE: 'service', FLD_STATISTIC: 's', FLD_CITY: 'c',
    FLD_STATE: 'st', FLD_ZIP: 'z', FLD_ROLLING_AVERAGE: 'ra', FLD_QUARTILE: 'q',
    FLD_RESIDENCE_TYPE: 'rt', FLD_TIMESPAN: 'ts', FLD_CHART_SIZE: 'sz', FLD_SHOW_DATE: 'sd',
    FLD_END_DATE: 'endDate', FLD_CSS: 'css' 
  }
  Neaux.Api.Altos.PARAMS = {  // FIELD VALUES
    AX_LEFT: 'l', AX_RIGHT: 'r',  // Axis
    RA_SEVEN_DAY: 'a', RA_NINETY_DAY: 'c', // Rolling Average
    QU_TOP: 't', QU_UPPER_MIDDLE: 'u', QU_LOWER_MIDDLE: 'l', QU_BOTTOM: 'b', QU_ALL: 'a',  // Quartile
    RT_SINGLE: 'sf', RT_CONDO: 'mf',       // Residence Type
    SZ_TINY: 't', SZ_SMALL: 's', SZ_BLOG: 'b', SZ_INT: 'i', SZ_MEDIUM: 'm', SZ_LARGE: 'l', // Chart Size
    TS_30: 'a', TS_60: 'b', TS_90: 'c', TS_180: 'd', TS_360: 'e', TS_UNLIMITED: 'z'        // Time Span
  }
  Neaux.Api.Altos.STATS = [   //  Stats, expressed as array, TODO Objects w/ description?
    'mean', 'median', 'inventory', 'median_new_listings', 'median_listings_sold',
    'median_sqft', 'median_per_sqft', 'median_age', 'median_dom', 'market_heat', 
    'median_percent_relisted', 'median_percent_price_decreased', 'median_percent_price_increased',
    'median_beds', 'median_baths', 'mean_lot'
  ];


  // Altos class definition - Defines the base functionality common to both Altos Charts and Stats
  //
  Neaux.Api.Altos.prototype = {
    data: null,       // Working storage for Altos parameters
    dflt: null,       // Saved state - Restore to data when Reset

    SetField: function( aFld, aVal) {
      this.data[aFld] = ( typeof aVal == 'object' && aVal instanceof Array) ? aVal.join(',') : aVal; 
    },
    GetField: function( aFld) { return this.data ? this.data[aFld] : null },
    RemoveField: function( aFld) { if ( this.data[aFld]) delete this.data[aFld] },

    Reset: function() { this.data = Neaux.Object.Clone( this.dflt) },
    Save: function() { this.dflt = Neaux.Object.Clone( this.data) },
    toString: function() { return '[Neaux.Api.Altos]' }
  }

  Neaux.Api.AltosChart.prototype = new Neaux.Api.Altos;
  Neaux.Merge( Neaux.Api.AltosChart.prototype, {
    GetUri: function() { return 'http://charts.altosresearch.com/altos/app?' + Neaux.Object.EncodeQueryString( this.data, false) },
    Image: function() {
      var theImage = document.createElement('img');
      theImage.src = this.GetUri();
      theImage.alt = 'Real Estate Market Chart by Altos Research www.altosresearch.com';
      return theImage;
    },
    toString: function() { return '[Neaux.Api.AltosChart]' }
  });

  Neaux.Api.AltosStats.prototype = new Neaux.Api.Altos;
  Neaux.Merge( Neaux.Api.AltosStats.prototype, {
    GetUri: function() { return 'http://www.altosresearch.com/altos/app?' + Neaux.Object.EncodeQueryString( this.data, false) },
    GetStats: function( aStats) {
      var theStats = aStats || Neaux.Api.Altos.STATS;
      var retVal = {};
      for ( var i=0; i < theStats.length; ++i)
        retVal[theStats[i]] = this.Stat( theStats[i]);
      return retVal;
    },
    Stat: function( aStat) {
      this.SetField( Neaux.Api.Altos.FIELDS.FLD_STATISTIC, aStat);
      var theFrame = document.createElement('iframe');
      theFrame.src = this.GetUri();
      theFrame.scrolling = 'no';
      theFrame.frameborder = '0';
      theFrame.allowTransparency = "true";
      theFrame.style.height = '1.33em';
      theFrame.style.width = '80px';
      theFrame.style.border = 'none';
      theFrame.style.backgroundColor = '#d9e0e8';
      theFrame.style.verticalAlign = 'baseline';
      //theFrame.onload = function() { alert('yo') }
//alert(theFrame.src);
      return theFrame;
    },
    toString: function() { return '[Neaux.Api.AltosStats]' }
  });

})();
