/*
 * Author: Vertex Logic, Inc
 * Copyright 2005, 2006, 2007 - All rights reserved
 *
 * Vertex Logic, Inc., California, USA 
 *
 * Vertex Logic grants you ("Licensee") a non-exclusive license to use
 * and modify this source and recompile it in
 * accordance with the terms of the Agreement under which this software is bought
 * and provided that (i) this copyright notice appear on all copies of the Software; 
 * (ii) Licensee does not sale the software as is or with modification without
 * the prior consent of Vertex Logic. (iii) Licensee agrees that it does not have any 
 * title or ownership of the Software.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * Vertex Logic will not be liable for any damages suffered 
 * by you as a result of using the Program. 
 * In no event will Vertex Logic be liable for any
 * special, indirect or consequential damages or lost profits even if
 * Vertex Logic have been advised of the possibility of their occurrence. 
 * Vertex Logic will not be liable for any third party claims against you.
 */
 
Af.Rectangle = Class.create();

Af.Rectangle.prototype = {
   initialize: function(x, y, w, h) {
      this.x = x;
      this.y = y;
      this.w = w;
      this.h = h;
   },
   
   intersects: function(r) {
      var tw = this.w;
      var th = this.h;
      var rw = r.w;
      var rh = r.h;
      if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
         return false;
      }
      var tx = this.x;
      var ty = this.y;
      var rx = r.x;
      var ry = r.y;
      rw += rx;
      rh += ry;
      tw += tx;
      th += ty;
      
      return ((rw < rx || rw > tx) &&
         (rh < ry || rh > ty) &&
         (tw < tx || tw > rx) &&
      (th < ty || th > ry));
      
   },
   
   contains: function(x, y) {
      var f = x  >= this.x  &&
      x  <= (this.x + this.w) &&
      y  >= this.y   &&
      y  <= (this.y + this.h);
      return f;
   },
   
   
   intersection: function(r) {
      var tx1 = this.x;
      var ty1 = this.y;
      var rx1 = r.x;
      var ry1 = r.y;
      var tx2 = tx1; tx2 += this.w;
      var ty2 = ty1; ty2 += this.h;
      var rx2 = rx1; rx2 += r.w;
      var ry2 = ry1; ry2 += r.h;
      if (tx1 < rx1) tx1 = rx1;
      if (ty1 < ry1) ty1 = ry1;
      if (tx2 > rx2) tx2 = rx2;
      if (ty2 > ry2) ty2 = ry2;
      tx2 -= tx1;
      ty2 -= ty1;
      return new Af.Rectangle(tx1, ty1, tx2, ty2);
   },
   
   print: function() {
      debugA("rectangle - " + this.x + ", " + this.y + ", " + this.w + ", " + this.h);
   }
}
