var xm = 0; // mouse position
var ym = 0;
var nx = 0; // screen position & size
var ny = 0;
var nw = 0;
var nh = 0;
var scr;

document.onselectstart = new Function("return false");

document.onmousemove = function(e)
{
  if (window.event) e = window.event;
  xm = (e.x || e.clientX);
  ym = (e.y || e.clientY);
}

function resize(){
  nx = pxLeft(scr);
  ny = pxTop(scr);
  nw = scr.offsetWidth;
  nh = scr.offsetHeight;
  make3D.resize();
}
onresize = resize;

px = function (x)
{
  return ''.concat(Math.round(x), 'px');
}
pxLeft = function(o)
{
  for(var x = -document.documentElement.scrollLeft; o != null; o = o.offsetParent)
    x += o.offsetLeft;
  return x;
}
pxTop = function(o)
{
  for(var y = -document.documentElement.scrollTop; o != null; o = o.offsetParent)
    y += o.offsetTop;
  return y;
}

make3D = {
  object : [],
  Im     : 0,
  Om     : 0,
  A      : 0,
  Y      : 0,
  temp   : 200,
  speed  : 150,
  zOOm   : .2,
  HW     : .6,
  resize : function ()
  {
    for(var i = 0; i < this.Om; i++)
    {
      var o = this.object[i];
      o.x   = .35 * nw * o.ac;
      o.z   = .35 * nw * o.as;
    }
  },

  run    : function ()
  {
    make3D.A += (xm - nw * .5 - nx) / make3D.speed;
    make3D.Y = ym - nh * .5 - ny;
    for(var i = 0; i < make3D.Om; i++)
      make3D.object[i].anim();
    setTimeout(make3D.run, 16);
  },

  picObj   : function (N)
  {
    var I = make3D.Im[N];
    var o = document.createElement("span");
    var oi = document.createElement("img");
    oi.src = I.src;
    oi.onclick = new Function("make3D.object["+N+"].clic();");
      o.appendChild(oi);
    var ot = document.createElement("div");
    ot.onmousedown=new Function("return false;");
    ot.innerHTML = "·" + I.alt + "·"
    o.appendChild(ot);
    var of = document.createElement("span");
    of.className = "chain";
    o.appendChild(of);
    scr.appendChild(o);
    this.o   = o.style;
    this.oi  = oi.style;
    this.ot  = ot.style;
    this.of  = of.style;
    this.N   = N;
    this.ys  = 0;
    this.ac  = Math.cos((360 * N / make3D.Om) * (Math.PI / 180));
    this.as  = Math.sin((360 * N / make3D.Om) * (Math.PI / 180));
    this.x   = .35 * nw * this.ac;
    this.z   = .35 * nw * this.as;
    this.Z   = .4;
    this.dZ  = false;
    this.pZ  = 0;

    this.clic = function () {
      if(!this.dZ && this.pZ == 0)
        this.dZ = true;
      else
      {
        this.dZ = false;
        this.pZ = 0;
      }
    }

    this.anim = function () {
      var s    = Math.sin(make3D.A * .01);
      var c    = Math.cos(make3D.A * .01);
      var xs   = s * this.x + c * this.z;
      var zs   = s * this.z - c * this.x;
      this.ys += (make3D.Y - this.ys) / (make3D.speed * .2);
      var D    = nw / (nw + zs);
      var w    = D * (nw * make3D.zOOm);
      var h    = w * make3D.HW;
      if(this.Z > .4 && this.dZ || this.pZ > 0)
        make3D.A -= xs / 30;
      if(this.dZ)
      {
        this.Z *= 1.01;
        this.x  = this.Z * nw * this.ac;
        this.z  = this.Z * nw * this.as;
      }
      if(this.Z > .8 && this.dZ)
      {
        this.dZ = false;
        this.pZ = make3D.temp;
      }
      if(this.dZ == false && this.Z > .4)
      {
        if(this.pZ > 0)
          this.pZ--;
        else
        {
          this.Z  *= .995;
          this.x   = this.Z * nw * this.ac;
          this.z   = this.Z * nw * this.as;
        }
      }
      this.o.width        = px(w);
      this.o.height       = px(h);
      this.o.top          = px(nh * .5 + this.ys * D - (h * .5));
      this.o.left         = px(nw * .5 + xs * D - (w * .5));
      this.o.zIndex       = Math.round(D * 1000);
      this.oi.borderWidth = px(.03 * w);
      this.ot.fontSize    = px(.06 * w);
      this.of.borderWidth = px(.01 * w);
    }
  },

  init   : function () {
    xm = nw * .6 - nx;
    ym = nw * .5 - ny;
    this.Im = document.getElementById("img").getElementsByTagName("img");
    this.Om = this.Im.length;
    for(var i = 0; i < this.Om; i++) this.object[i] = new this.picObj(i);
    this.run();
  }
}

onload = function (){
  scr = document.getElementById("screen");
  resize();
  make3D.init();
}
