/*
 SprayCan.js
*/

  function Splotch(id)
  {
    this.dynEl = new DynEl
    (
      window, 
      id, 
      "<img src='http://askthedaddy.co.uk/js/bullethole.gif' border='0' width='" + Splotch.width + "' height='" + Splotch.height + "'>", 
      -500, -500, Splotch.width
    );
    this.next = null;
    this.expire = 0;
  }
  
  Splotch.prototype.showMeAt = function(x, y)
  {
    var d = new Date();
    this.expire = d.getTime() + Splotch.duration;
    this.dynEl.moveTo(x, y);
    this.dynEl.show();
  }
  
  Splotch.splotches = 0;
  Splotch.maxSplotches = 14;
  Splotch.duration = 4000;
  Splotch.width = 20;
  Splotch.height = 20;
  Splotch.currSplotch = null;
  Splotch.splotchList = null;
  Splotch.lastX = -1000;
  Splotch.lastY = -1000;
  
  Splotch.add = function()
  {
    var n;
    var spl = new Splotch("spl" + Splotch.splotches++);
    if(Splotch.splotchList == null)
    {
      Splotch.splotchList = spl;
      Splotch.currSplotch = Splotch.splotchList;
    }
    else
    {
      for(n = Splotch.splotchList; n.next != null; n = n.next){}
      n.next = spl;
    }
  }
  
  Splotch.showNext = function(x, y)
  {
    var dx = Splotch.width * .25;
    var dy = Splotch.height * .25;
    if(((x > (Splotch.lastX + dx)) || (x < (Splotch.lastX - dx))) ||
       ((y > (Splotch.lastY + dy)) || (y < (Splotch.lastY - dy))))
    {
      Splotch.lastX = x;
      Splotch.lastY = y;
      if(Splotch.currSplotch != null)
      {
        Splotch.currSplotch.showMeAt(x, y);
        Splotch.currSplotch = Splotch.currSplotch.next;
      }
      else if(Splotch.splotchList != null)
      {
        Splotch.currSplotch = Splotch.splotchList;
        Splotch.currSplotch.showMeAt(x, y);
        Splotch.currSplotch = Splotch.currSplotch.next;
      }
    }
  }
  
  Splotch.create = function()
  {
    var i;
    for(i = 0; i < Splotch.maxSplotches; i++)
    {
      Splotch.add();
    }
  }
  
  Splotch.init = function()
  {
    var n;
    for(n = Splotch.splotchList; n != null; n = n.next)
    {
      n.dynEl.output();
      n.dynEl.hide();
    }
  }
  
  Splotch.clearAll = function()
  {
    var n;
    for(n = Splotch.splotchList; n != null; n = n.next)
    {
      n.showMeAt(-1000, -1000);
    }
  }
  
  Splotch.harvest = function()
  {
    var n;
    var d = new Date();
    for(n = Splotch.splotchList; n != null; n = n.next)
    {
      if((n.expire > 0) && (d.getTime() > n.expire))
      {
        n.showMeAt(-1000, -1000);
        n.expire = 0;
      }
    }
  }
  
  function SprayCan(id)
  {
    this.dynEl = new DynEl
    (
      window, 
      id, 
      "<img src='http://askthedaddy.co.uk/js/gun.gif' border='0' width='" + SprayCan.width + "' height='" + SprayCan.height + "'>", 
      -500, -500, SprayCan.width
    );
    this.active = true;
    //this.dynEl.output();
  }
  SprayCan.width = 72;
  SprayCan.height = 63;
  
  SprayCan.prototype.output = function()
  {
    this.dynEl.output();
  }
  
  SprayCan.prototype.stop = function()
  {
    this.active = false;
    Splotch.clearAll();
    this.dynEl.hide();
  }

  SprayCan.prototype.start = function()
  {
    this.active = true;
    Splotch.clearAll();
    this.dynEl.show();
  }

  SprayCan.prototype.updateState = function()
  {
    if(this.active)
    {
      this.dynEl.moveTo(Mouser.x + 14, Mouser.y + 2);
      if(Mouser.clicked)
      {
        Splotch.showNext(Mouser.x - 12, Mouser.y - 12);
      }
    }
  }
  
  
  
