// -*- c++ -*-
// Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.
// This is free software with ABSOLUTELY NO WARRANTY.
// You can redistribute it and/or modify it under the terms of the GNU GPL2.

function History(xp) {
  this.xp = xp;
  this.windowWidth = this.getWidth();
  this.divlist = this.xp.getDivListByClass("era");
  //this.moveDivs();
  this.indicator = this.xp.getById("indicator");
  this.eraWidth = this.calcEraWidth();
  this.lastdiv = null;
  this.lines = null;
  //this.displayMode = true;
  this.displayMode = false;
  this.drawRuler();
}

History.prototype.moveDivs = function() {
  var o = this.xp.getById("eratable");
  //g_xp.showAllProperty(o);
  var left = o.style.offsetLeft;
  var top  = o.style.offsetTop;
  alert("pos "+left+top);
  for (var i=0; i < this.divlist.length; i++) {
    var o = this.divlist[i];
    o.style.left = left;
    o.style.top  = top;
  }
}

History.prototype.getWidth = function() {
  var w = 500; // adhoc
  var cursorsize = 200;
  var minsize = 100;
  if (document.body.clientWidth) w = document.body.clientWidth; // IE
  else if (window.innerWidth)    w = window.innerWidth; // GK
  w = w - cursorsize;
  if (w < minsize) w = minsize;
  return w;
}
History.prototype.calcEraWidth = function() {
  return Math.floor(this.windowWidth / this.divlist.length);
}
History.prototype.drawRuler = function() {
  var ar = [];
  var divlines = this.xp.getById("lines");
  for (var i=0; i < this.divlist.length; i++) {
    var o = document.createElement("v:line");
    o.id = "vl"+this.divlist[i].id;
    ar.push(o);
    divlines.appendChild(o);

    var x = this.eraWidth * i;
    o.style.visibility = "visible";
    o.setAttribute("strokeweight", "1");
    o.setAttribute("strokecolor",   "#009");
    o.style.position = "absolute";
    o.style.left = "0px";
    o.style.top  = "0px";
    o.setAttribute("from", ""+x+","+40);
    o.setAttribute("to",   ""+x+","+48);
  }
  this.lines = ar;
}
History.prototype.onMouseMove = function(p) {
  var i = Math.floor(p.x / this.eraWidth);
  var div = this.divlist[i];
  if (div == null) return;
  if (this.lastdiv) {
    if (this.lastdiv == div) return; // do nothing
    if (this.displayMode) this.lastdiv.style.display = "none";
    else this.lastdiv.style.visibility = "hidden";
  }
  this.lastdiv = div;

  if (this.displayMode) this.lastdiv.style.display = "block"; // show it
  else this.lastdiv.style.visibility = "visible";

  var date = new Date((div.id-0)*1000);
  this.indicator.innerHTML = date.toLocaleString();
}

function qwik_onmousemove(p) { g_history.onMouseMove(p); }

// global
var g_history = new History(g_xp);
