/*
 * Fly.js, 1.1, 2011-08-25
 *
 * Copyright (c) 2011 Roumen Petrov.
 *
 * Roumen Petrov grants you a royalty free license to use, modify or
 * distribute this software provided that this copyright notice appears
 * on all copies.  This software is provided "AS IS," without a
 * warranty of any kind.
 *
 * Notes:
 * - Do not put script in HTML head section - NS4 and frames problem
 */

var fly = new Array();
Fly.prototype.tmOut1 = 50;
Fly.prototype.tmOut2 = 70;
Fly.prototype.step1 = 0;
Fly.prototype.step2 = 7;

Fly.prototype.visible = new Object();
if (window.pageYOffset != undefined) {
  Fly.prototype.visible.left = window.pageXOffset;
  Fly.prototype.visible.top  = window.pageYOffset;
} else {
  Fly.prototype.visible.left = document.body.scrollLeft;
  Fly.prototype.visible.top  = document.body.scrollTop ;
}


function Fly(_id, _num, _ex, _ey) {
  this.element = new xElement(_id)
  if (this.element == undefined) {
    return;
  }

  this.id    = _id;
  this.num   = _num;
  this.style = this.element.style

  this.factor = 1;
  this.endX = 250;
  this.endY = 150;
  this.W = this.element.width();
  this.H = this.element.height();

  this.element.left(-this.element.width());
  this.element.top (0);

  fly[_id] = this;
}


Fly.prototype.fly = function () {
  this.element.show();
  setTimeout('fly["'+this.id+'"].fly1()', this.tmOut1);
}


var fly_fly1_alert = 0;
Fly.prototype.fly1 = function () {
  clearInterval(this.timer);

  if (this.step1 <= 450) {
    this.step1 += 10;
    //this.step2 = - this.step2;

    var omega = this.step1 / 360. * Math.PI * 2;
    this.element.moveTo (
      this.endX - this.endX * Math.exp(-.3 * omega) * Math.cos(omega) * this.factor ,
      this.step1 * this.endY / 450.
    );

    setTimeout('fly["'+this.id+'"].fly1()', this.tmOut1);
  } else {
    if (fly_fly1_alert > 0) {
      fly_fly1_alert--;
      alert('Fly[' + this.id + ']  end:' + this.element.style.left + ','+ this.element.style.top);
    }
    setTimeout('fly["'+this.id+'"].fly2()', this.tmOut2);
  }
}


Fly.prototype.fly2 = function () {
  clearInterval(this.timer);
  if (Math.abs (this.step2) > 1) {
    this.step2 *= -.9;
    this.element.top(this.endY + this.step2);
    setTimeout('fly["'+this.id+'"].fly2()', this.tmOut2);
  }
}

