/*==========================================
 ImageScrollMover Scripted by kiaraumu
 2007 HiDESIGNS Inc(c) All rights reserved.
 
 Usage: ObjectElement = new ImageScrollMover();
 
 
 Options: (* は必須、 また、assignを用いてオブジェクトを view 以上登録する)
 
 * ImageScrollMover.view         : 見える画像の数
 * ImageScrollMover.moves        : 移動量ピクセル ( 左方向 < 0 > 右方向 )
   ImageScrollMover.mouseAction  : マウス移動量 1で、0〜9.999 (誤差有り)
 * ImageScrollMover.setImageSize : スクロール対象オブジェクトのサイズ設定
                                   引数1=幅(width)
                                   引数2=高さ(height)
                                  
 * ImageScrollMover.setTimer     : タイマー制御
                                   引数1=init実行から開始までの秒数
                                   引数2=スクロールタイマー間隔
                                 
 * ImageScrollMover.init         : 処理実行(引数なし)
   ImageScrollMover.assignImage   : スクロール対象オブジェクトに画像を登録する
                                   引数1=画像パス
                                   引数2=リンク先URL (空白時、リンクを生成しない)
                                   引数3=リンク先ターゲット
                                   引数4=画像タグのaltテキスト、およびリンクタグのtitle属性
                                 
   ImageScrollMover.assignObject  : スクロール対象オブジェクトを登録する
                                   引数1=document.createElement等で作成されたオブジェクト
                                 
                                   下記の設定は自動で代入される
                                   id
                                   style.width
                                   style.height
                                   style.position
                                   style.top
                                   style.left
 
   ImageScrollMover.assignHTML    : スクロール対象オブジェクト(div要素)にHTMLを登録する
                                   引数1=HTML文字列
 
==========================================*/

var ie  = (document.all)? true:false;

function ImageScrollMover(id)
{
  this.id = id;
  this.image_width;
  this.image_height;
  this.screen_width;
  this.screen_height;
  this.view;
  this.moves;
  this.count;
  this.limit_left;
  this.limit_right;
  this.end_left = 0;
  this.left_image_number;
  this.right_image_number;
  this.limit_status;
  this.Images = [];
  this.Area;
  this.Screen;
  this.Doc;
  this.Timer;
  this.mouseAction = 0;
  this.mouseActionConst = 0;
  
  this.setImageSize = function(width, height)
  {
    this.image_width  = width;
    this.image_height = height;
    this.screen_width  = this.image_width * this.view;
    this.screen_height = height;
  }
  this.setTimer = function(wait, interval)
  {
    var mv = Mover[this.id]
    this.Timer = new Timer(this.id + "_image_mover", "", wait, function () { mv.scrollImages(); });
    this.Timer.interval = interval;
  }
  this.init = function(target)
  {
    if (!document.getElementById)
    {
      return;
    }
    
    if (target && document.getElementById(target))
    {
      this.Doc = document.getElementById(target);
    }
    else
    {
      this.Doc = document.getElementsByTagName('body')[0];
    }
    this.count = this.Images.length;
    this.createArea();
    this.createImages();

    if (this.count > this.view)
    {
      this.setPosition();
      this.Timer.play();
      
      this.limit_status = [0, (this.count - this.view) * this.image_width * -1];
    }
  }
  this.assignImage = function(path, url, target, alt)
  {
    this.Images[this.Images.length] =
    {
      image   : path,
      link    : url,
      target  : target,
      alt     : alt,
      element : ""
    };
  }
  this.assignObject = function(obj)
  {
    this.Images[this.Images.length] = {object: obj};
  }
  this.assignHTML = function(html)
  {
    var obj = document.createElement('div');
    obj.innerHTML = html;
    this.Images[this.Images.length] = {object: obj};
  }
  this.createArea = function(target)
  {
    var screen = document.createElement('div');
    var area   = document.createElement('div');
    screen.id = this.id + "_screen";
    screen.style.position = "relative";
    screen.style.width    = this.screen_width + "px";
    screen.style.height   = this.screen_height + "px";
    screen.style.top      = "0";
    screen.style.left     = "0";
    screen.style.overflow = "hidden";
    if (this.mouseAction != 0 && this.mouseAction > 0)
    {
      var mv = Mover[this.id];
      screen.onmousemove = function (e) { mv.setMoves(this, gmx(e), gmy(e)); };
    }
    area.id   = this.id + "_image_area";
    area.style.position = "absolute";
    area.style.top      = "0";
    area.style.left     = "0";
    area.style.width    = (this.image_width * this.count) + "px";
    area.style.height   = this.screen_height;
    
    screen.appendChild(area);
    this.Doc.appendChild(screen);
    this.Screen = document.getElementById(this.id + "_screen");
    this.Area   = document.getElementById(this.id + "_image_area");
  }
  this.createImages = function()
  {
    var target;
    for (var i = 0; i < this.count; i++)
    {
      var st     = this.Images[i];
      var parent = this.Area;
      var a   = document.createElement('a');
      var img = document.createElement('img');
      
      if (st.object)
      {
        target = st.object;
      }
      else
      {
        if (st.link)
        {
          a.href   = st.link;
          a.target = st.target;
          a.title  = st.alt;
          target   = a;
        }
        if (st.image)
        {
          img.src    = st.image;
          img.alt    = st.alt;
          img.width  = this.image_width;
          img.height = this.image_height;
          img.border = 0;
          if (!st.link)
          {
            target = img;
          }
        }
      }
      
      target.id = this.id + "_image_" + i;
      target.style.position = "absolute";
      target.style.top      = "0px";
      target.style.left     = (this.image_width * i) + "px";
      target.style.width    = this.image_width;
      target.style.height   = this.image_height;
      
      if (st.link)
      {
        a.appendChild(img);
      }
      this.Area.appendChild(target);
      this.Images[i].element = document.getElementById(this.id + "_image_" + i);
    }
  }
  this.setPosition = function()
  {
    this.Area.style.left  = "0";
    this.Area.style.top   = "0";
    this.Area.style.width = (this.image_width * this.count) + "px";
    this.end_left = 0;
    var current = 0;
    if (this.count == this.view + 1)
    {
      this.left_image_number  = 0;
      this.right_image_number = this.view;
      this.limit_left  = 0;
      this.limit_right = this.image_width * (this.view + 1);
    }
    else if (this.count >= this.view + 2)
    {
      this.left_image_number  = this.view + 1;
      this.right_image_number = this.view;
      this.limit_left  = (this.count - this.view - 1) * this.image_width * -1;
      this.limit_right = this.image_width * (this.view + 1);
    }
  
    for (var i = 0; i < this.count; i++)
    {
      if (i < this.view + 1)
      {
        current = i * this.image_width;
      }
      else
      {
        current  = this.limit_left + ((i - this.view - 1) * this.image_width);
        if (current < this.end_left)
        {
          this.end_left = current;
        }
      }
      this.Images[i].element.style.left = current + "px";
    }
  }
  this.scrollImages = function()
  {
    if (this.moves == 0)
    {
      return;
    }
    var nx = this.Area.offsetLeft;
    var to = (nx + this.moves);
    var direction = (this.moves < 0)? 1:0;
    
    this.Area.style.left = to;
    this.limit_left += this.moves;
  
    if (direction == 1 && this.limit_left <= this.limit_status[direction])
    {
      this.Images[this.left_image_number].element.style.left = this.limit_right + "px";
      this.right_image_number = this.left_image_number;
      this.left_image_number  = (this.left_image_number + 1) % this.count;
      this.limit_right += this.image_width;
      this.limit_left  += this.image_width;
      this.end_left    += this.image_width;
      if
      (
        (this.count == this.view + 1 && this.left_image_number == 0) ||
        (this.count >= this.view + 2 && this.left_image_number == this.view + 1)
      )
      {
        this.setPosition();
      }
    }
    else if (direction == 0 && this.limit_left >= this.limit_status[direction])
    {
      if
      (
        (this.count == this.view + 1 && this.right_image_number == this.view) ||
        (this.count >= this.view + 2 && this.right_image_number == this.count - 1)
      )
      {
        this.setPosition();
      }
      this.Images[this.right_image_number].element.style.left = (this.end_left - this.image_width) + "px";
      this.left_image_number   = this.right_image_number;
      this.right_image_number -= 1;
      if (this.right_image_number == -1)
      {
        this.right_image_number = this.count - 1;
      }
      this.limit_right -= this.image_width;
      this.limit_left  -= this.image_width;
      this.end_left    -= this.image_width;
    }
  }
  this.setMoves = function (scr, ex, ey)
  {
    if ((this.mouseAction == 0 || this.mouseAction < 0) || (this.mouseActionConst == 0 || this.mouseActionConst < 0))
    {
      return;
    }
    
    var ax  = ex - scr.offsetLeft - Math.floor(scr.offsetWidth  / 2);
    var ay  = ey - scr.offsetTop  - Math.floor(scr.offsetHeight / 2);
    var tn  = Math.atan2(ax, ay);
    var dis = Math.sqrt(Math.pow(Math.abs(ax), 2) + Math.pow(Math.abs(ay), 2));
    var v   = Math.sin(tn) * (Math.min(10, 10 * (dis / scr.offsetWidth) * 4));
    this.moves = -(Math.floor(v * this.mouseAction));
    
    if (this.mouseActionMode == "const")
    {
      if (this.mouseActionConst > 0)
      {
        if (this.moves < 0)
        {
          this.moves = -this.mouseActionConst;
        }
        else if (this.moves > 0)
        {
          this.moves = this.mouseActionConst;
        }
      }
    }
  }
}
function gmx(e)
{
  var doc = document.getElementsByTagName("body")[0];
  if (ie)
  {
    return doc.scrollLeft + event.clientX;
  }
  else
  {
    return e.pageX;
  }
}
function gmy(e)
{
  var doc = document.getElementsByTagName("body")[0];
  if (ie)
  {
    return doc.scrollTop + event.clientY;
  }
  else
  {
    return e.pageY;
  }
}
function Timer(name, data, wait, func)
{
  var DEFAULT_INTERVAL = 18;

  this.name     = name;
  this.data     = data;
  this.wait     = wait;
  this.index    = 0;
  this.timer    = 0;
  this.current  = -1;
  this.interval = DEFAULT_INTERVAL;
  this.exec     = func;
  
  this.play = function()
  {
    clearTimeout(this.timer);
    var tm = this;
    if (this.flag == 0 && this.current > -1)
    {
      this.exec(this);
      this.timer = setTimeout(function () { tm.play(); }, this.interval);
    }
    else if (this.current == -1)
    {
      this.current++;
      this.flag  = 0;
      this.timer = setTimeout(function () { tm.play(); }, this.wait);
    }
  }
}

