//
// (C) poteryashka.spb.ru, 2008
//

var r_dogs=[
["/img/run/dog1.gif",60],
["/img/run/dog2.gif",-20],
["/img/run/dog3.gif",20],
["/img/run/dog4.gif",30],
["/img/run/dog5.gif",-15],
["/img/run/dog6.gif",40],
["/img/run/dog7.gif",20],
["/img/run/dog8.gif",20],
["/img/run/dog9.gif",-40],
["/img/run/dog10.gif",60],
["/img/run/dog11.gif",30],
["/img/run/dog12.gif",-30],
["/img/run/dog13.gif",30],
["/img/run/dog14.gif",20],
["/img/run/dog15.gif",20],
["/img/run/dog16.gif",-40],
["/img/run/dog17.gif",40],
["/img/run/dog18.gif",15],
["/img/run/cat1.gif",-40],
["/img/run/cat2.gif",40],
["/img/run/cat3.gif",15],
["/img/run/cat4.gif",-45],
["/img/run/cat5.gif",4],
//["/img/run/cat6.gif",4],
["/img/run/cat8.gif",6],
["/img/run/cat9.gif",10],
//["/img/run/cat11.gif",4],
["/img/run/cat12.gif",10],
["/img/run/cat13.gif",8],
["/img/run/cat14.gif",40]
       ];
var dog_delay=50;

function runObj(img_url, speed_x, speed_y) {
  this.delta_x=speed_x
  this.delta_y=speed_y
  var r_dog;
    r_dog = document.createElement('div');
    r_dog.setAttribute('id','r_dog');
    r_dog.innerHTML = '';
    r_dog.style.position="absolute";
    document.body.appendChild(r_dog);
  r_dog.style.visibility="hidden"
  r_dog.innerHTML="<img src='" + img_url + "' alt=''>"
  this.r_dog=r_dog
  var thisObj=this
  this.waitImgLoad_interval=window.setInterval(function(){thisObj.chkImgLoad();},500)
}

function windowH() {
  return (window.innerHeight ? window.innerHeight : document.body.offsetHeight) - 20;
}
function windowW() {
  return (window.innerWidth ? window.innerWidth : document.body.offsetWidth) - 20; // for scrollers
}

runObj.prototype.destroy = function(){
  try{ window.clearInterval(this.waitImgLoad_interval); } catch(ex) {}
  try{ window.clearInterval(this.move_interval); } catch(ex) {}
  try{ this.r_dog.innerHTML=""
  document.body.removeChild(this.r_dog)
  delete this.r_dog
  } catch(ex) {}
  var thisObj=this
  window.setTimeout(function() {delete thisObj}, 100)
}

runObj.prototype.move = function(){
  this.dog_left+=this.delta_x
  this.dog_top+=this.delta_y
  var r_dog=this.r_dog
  if( this.dog_left < 0 || this.dog_left + r_dog.offsetWidth > windowW() ) {
    this.destroy()
    return
  }
  if( this.dog_top < 0) {
    this.dog_top = 0
    this.delta_y = -this.delta_y
  }
  if( this.dog_top + r_dog.offsetHeight > windowH()) {
    this.dog_top = windowH() - r_dog.offsetHeight 
    this.delta_y = -this.delta_y
  }
  r_dog.style.left=this.dog_left
  r_dog.style.top=this.dog_top
}
runObj.prototype.chkImgLoad = function() {
  var r_dog=this.r_dog
  if(r_dog.offsetWidth < 5) return
  clearInterval(this.waitImgLoad_interval) 
  this.dog_left=(this.delta_x > 0 ? 0 : windowW()-r_dog.offsetWidth - 5)
  this.dog_top=Math.round((windowH()-r_dog.offsetHeight)*Math.random());
  r_dog.style.left=this.dog_left
  r_dog.style.top=this.dog_top
  r_dog.style.visibility="visible"
  var thisObj=this
  this.move_interval=window.setInterval(function(){ thisObj.move(); },100)
}
function next_dog() {
  var r_dog_N=Math.round((r_dogs.length-0.5)*Math.random())
  var speed_x=r_dogs[r_dog_N][1]
  var speed_y=Math.round(speed_x*(Math.random()-0.5)/3)
  new runObj(r_dogs[r_dog_N][0],speed_x,speed_y)
  window.setTimeout(next_dog,dog_delay*1000*(0.5+Math.random()))
}
window.setTimeout(next_dog,dog_delay*500*(0.5+Math.random()))