//  ----------------------------------------------------------------------------
// Title: DateTimeAxis
// Description: Konstruieren von Datums-Achsen
// Copyright: Copyright (c) 2010
// Company: SiG Software Integration GmbH</p>
// Author Dr. Horst Walther
//  ----------------------------------------------------------------------------

function DateTimeAxis (_p) {   // extends Axis
  this.id = 'DateTimeAxis';
  this.zeroTime = new Date().setZero();
  this.dOld = new Date().setZero();
  var wordy = false;
  this.p = _p;
  this.axisType = 'dateTime';
  if (wordy) {alert (this.id+' created');}
}

DateTimeAxis.prototype = new Axis ();

//  ----------------------------------------------------------------------------
DateTimeAxis.prototype.setScaler = function (min, max, viewLen) {
//  alert (this.id+'.setScaler ('+min+', '+max+', '+viewLen+')');
  var scaler = new DateTimeScaler(min, max, viewLen);
  return scaler;
};

//  ----------------------------------------------------------------------------
DateTimeAxis.prototype.getLevel = function () {
  return this.scaler.getLevel();
};

//  ----------------------------------------------------------------------------
DateTimeAxis.prototype.toString = function(t) {
  return ((t ? t : this.id+": " )+ "o: (" + this.o.toString() + "), min: " + new Date(this.min).toString() + ", max: " + new Date(this.max).toString() + ', viewLen: ' + this.viewLen + ', isXaxis: ' + this.isXaxis + ', text: ' + this.text + ', color: ' + this.color);
};

//  ----------------------------------------------------------------------------
DateTimeAxis.prototype.scaleValue = function(value) {
  var v = isDate(value) ? value.getTime() : (isString(value) ? new Date().fromString(value).getTime() : value);
  return (this.isXaxis ? this.o.x : this.o.y) + this.getShift() + this.scaler.scaleValue(v);
};

//  --------------------------------------------------------------------------->
DateTimeAxis.prototype.calcMeanAndMax = function(val) {
  switch (this.getLevel()) {
    case this.zeroTime.SECONDS:  // seconds
      this.jMax = 10;
      this.jMean = 5;
      break;
    case this.zeroTime.MINUTES:  // minutes
      this.jMax = 60;
      this.jMean = 30;
      break;
    case this.zeroTime.HOURS:   // hours
      this.jMax = 60;
      this.jMean = 30;
      break;
    case this.zeroTime.DAYS:   // days
      this.jMax = 24;
      this.jMean = 12;
      break;
    case this.zeroTime.MONTHS:  // months
      this.jMax = new Date(val).daysInMonth();
      this.jMean = Math.floor(this.jMax / 2);
      break;
    case this.zeroTime.YEARS:  // years
      if (this.scaler.getYearInc() > 1) {
        this.jMax = 10;
        this.jMean = 5;
      } else {
        this.jMax = 12;
        this.jMean = 6;
      }
      break;
    default:    
      alert ('###  '+this.id+'.calcMeanAndMax: no valid value of "level": '+this.getLevel());
      break;
  }
};

//  ----------------------------------------------------------------------------
DateTimeAxis.prototype.adjustPositionFormat = function (format, date) {
  if (!isDate(date)) {
    alert(this.id+'.adjustPositionFormat: '+date+' is not a Date');
    date = new Date().fromString(''+date);
  }
  format[0] = Math.min(date.minSignificance(), format[0]);
  return format;
};

//  ----------------------------------------------------------------------------
DateTimeAxis.prototype.makeTickText = function (v, inc) {
  var d = new Date();  d.setTime(v);
  var format = this.findPositionFormat ([this.dOld, d]);
//  alert (this.id+'.makeTickText: dOld='+this.dOld+' --> value='+d+', format='+format+', --> '+this.formatPosition (d, format));
  this.dOld = d;
  return this.formatPosition (d, format); 
};

//  ----------------------------------------------------------------------------
DateTimeAxis.prototype.makePositionText = function (position, format) {
//  alert (this.id+'.makePositionText: '+format+' --> '+this.adjustPositionFormat (format, position));
  return this.formatPosition (position, this.adjustPositionFormat (format, position)); 
};

//  ----------------------------------------------------------------------------
DateTimeAxis.prototype.formatPosition = function(position, format) {
  return position.toString (format);
};

//  ----------------------------------------------------------------------------
DateTimeAxis.prototype.findPositionFormat = function(positions) {
//  alert ('DateTimeAxisTest.findPositionFormat: '+new Date(positions.min())+', '+new Date(positions.max()));
  return new Date(positions.min()).getChangeBounds (new Date(positions.max()));
};

//  ---------------------------------------------------------------------------->
DateTimeAxis.prototype.rebuildValue = function (pixelPos) {
//  alert (this.id+'.rebuildValue('+pixelPos+')'+(new Date(this.toValue(pixelPos)).toString()));
  return new Date(this.toValue(pixelPos)).toString(); 
};

//  --------------------------------------------------------------------------->
DateTimeAxis.prototype.calcValue = function (i) {
  return this.scaler.calcValue(i);
};

  //  --------------------------------------------------------------------------->
DateTimeAxis.prototype.calcMicroValue = function (val, i, jMax) {
  var level = this.getLevel()-1;
  while (level < 0) {
    i /=10;
    level += 1;
  }
  if (this.scaler.getYearInc() > 1)
    {level = this.zeroTime.YEARS;}
  switch (level) {
    case this.zeroTime.SECONDS:  // seconds
      return new Date (val).addSeconds(i).getTime();
    case this.zeroTime.MINUTES:  // minutes
      return new Date (val).addMinutes(i).getTime();
    case this.zeroTime.HOURS:   // hours
      return new Date (val).addHours(i).getTime();
    case this.zeroTime.DAYS:  // days
      return new Date (val).addDays(i).getTime();
    case this.zeroTime.MONTHS:  // months
      return new Date (val).addMonths(i).getTime();
    case this.zeroTime.YEARS:  // years
      return new Date (val).addYears(i * (this.scaler.getYearInc()/10)).getTime();
    default:  
      alert ('###  '+this.id+'.calcValue: no valid value of "level": '+this.getLevel());
  }
};

//  ----------------------------------------------------------------------------
