/*
 * Credits: unknown
 *
 * Ghost.js, 1.3, 2005-05-16
 *
 * Copyright (c) 2002-2005 Roumen Petrov.  All rights reserved.
 *
 * 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
 * - Works on:
 *   - Netscape 4.7x
 *   - Gecko 0.9.x/1.x (Mozilla, Firefox, Netscape 6x/7x, Galeon)
 *   - IE 5.x/6.x
 *   - Opera 5.x/6.x/7.x/8.x
 *   - Konqueror 2.2.x/3.x
 */

var ghosts = new Array();

var Tall;
var Wide;

if(window.innerHeight != undefined) {
  Wide = window.innerWidth/3;
  Tall = window.innerHeight/4;
  Ghost.prototype.Xpos = window.innerWidth  * 0.7;
  Ghost.prototype.Ypos = window.innerHeight * 0.3;
}
else {
  Wide = document.body.clientWidth / 4;
  Tall = document.body.clientHeight / 4;
  Ghost.prototype.Xpos = document.body.clientWidth  * 0.7;
  Ghost.prototype.Ypos = document.body.clientHeight * 0.3;
}


Ghost.prototype.step = .3;
Ghost.prototype.Next = 0;
Ghost.prototype.HomX =  72;
Ghost.prototype.HomY = 127;
Ghost.prototype.Hovering = 0;


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

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

  ghosts[_id] = this;
}


if (document.layers) {
  Ghost.prototype.isStoped = function () {
    return this.style.visibility == 'hide';
  }
} else {
  Ghost.prototype.isStoped = function () {
    return this.style.visibility == 'hidden';
  }
}


Ghost.prototype.fly = function () {
  var x = this.Xpos + Math.sin((20*Math.sin(this.Next/(30         )))+this.num*70)*Wide*(Math.sin(10+this.Next/(10+this.num))+0.2)*Math.cos((this.Next + this.num*55)/10);
  var y = this.Ypos + Math.cos((20*Math.sin(this.Next/(30+this.num)))+this.num*70)*Tall*(Math.sin(10+this.Next/(10         ))+0.2)*Math.cos((this.Next + this.num*55)/10);

  this.element.moveTo(x,y);
  this.Next += this.step;

  switch(this.Hovering) {
    case 0:  // normal fly
      setTimeout('ghosts["'+this.id+'"].fly()', 300);
      break;
    case 1: // start Hovering
      this.Hovering = 2;
      setTimeout('ghosts["'+this.id+'"].hover(' + this.Xpos + ',' + this.Ypos + ');', 80);
      break;
    case 2: //in Hovering
      break;
    case -1: //stoped
      this.element.hide();
      //alert('Ghost[' + this.id + '] stoped.');
      break;
    default:
      alert('Ghost[' + this.id + '] unknown fly state !');
  }
}


var ghost_hover_alert = 0;
Ghost.prototype.hover = function (X,Y) {
  var xx = this.HomX + ((X - this.HomX) / 1.05);
  var yy = this.HomY + ((Y - this.HomY) / 1.05);
  this.Xpos = this.element.parseInt(xx);
  this.Ypos = this.element.parseInt(yy);
  if (
    ( (this.HomX - 5) < xx && xx < (this.HomX + 5) ) &&
    ( (this.HomY - 5) < yy && yy < (this.HomY + 5) )
  ) {
    this.Hovering = 0;
    //alert('Ghost[' + this.id + '] stop hovering.');
  }
  else {
    if (ghost_hover_alert > 0) {
      ghost_hover_alert--;
      alert('Ghost[' + this.id + '] :' + xx + ','+ yy );
    }
    setTimeout('ghosts["'+this.id+'"].hover(' + xx + ',' + yy + ');', 80);
  }
  this.fly();
}


Ghost.prototype.startHovering = function () {
  this.Hovering = 1;
}


Ghost.prototype.stop = function () {
  this.Hovering = -1;
}

