function Stripe (pFirst, pLast, side) {
  this.side = side;
  this.color = pFirst.color;
  // where to find the next piece in a stripe
  var orthogonalSide = (side == LEFT || side == RIGHT) ? BOTTOM: RIGHT; 
  if (pFirst == pLast) {
    this.pieces = [pFirst];
    this.size = pFirst.sideSize(DIR[side]);
  } else { 
    this.pieces = new Array();
    this.size = ZERO;
  	var pPrev = pFirst; 
    do {
      if (pPrev.connections[orthogonalSide].length === 0) {
        this._setInvalid();
        return this;
      }
      this.size = this.size.add(pPrev.sideSize(DIR[side]));
      this.pieces.push(pPrev);
      
      var pNext = null;
      if (side == LEFT) {
        pNext = pPrev.connections[BOTTOM][0];
      } else if (side == TOP) {
        pNext = pPrev.connections[RIGHT][0];
      } else if (side == RIGHT) {
        pNext = pPrev.connections[BOTTOM][pPrev.connections[BOTTOM].length-1];
      } else {
        pNext = pPrev.connections[RIGHT][pPrev.connections[RIGHT].length-1];
      }
      
      if (pNext.end[DIR[side]].bigger(pLast.end[DIR[side]])) {
        this._setInvalid();
        return this;
      }
      if (side == LEFT && !pPrev.start[HORIZONTAL].equals(pNext.start[HORIZONTAL]) ||
	      side == TOP && !pPrev.start[VERTICAL].equals(pNext.start[VERTICAL]) ||
	      side == RIGHT && !pPrev.end[HORIZONTAL].equals(pNext.end[HORIZONTAL]) ||
	      side == BOTTOM && !pPrev.end[VERTICAL].equals(pNext.end[VERTICAL]) ||      
          pNext.color != pPrev.color) {        
        this._setInvalid();
        return this;
      } 
      pPrev = pNext;
    } while (pNext != pLast);
    this.size = this.size.add(pLast.sideSize(DIR[side]));
    this.pieces.push(pLast);      
  }
}

Stripe.prototype._setInvalid = function() {
  this.size = ZERO;
}

Stripe.prototype.isValid = function() {
  return this.size.bigger(ZERO);
}

Stripe.prototype.getSize = function() {
  return this.size;
}

Stripe.prototype.getStart = function() {
  return this.pieces[0].getStart(DIR[this.side]);
}

Stripe.prototype.getEnd = function() {
  return this.pieces[0].getEnd(DIR[this.side]);
}

Stripe.prototype.getPieces = function() {
  return this.pieces;
} 

toTest.push(Stripe);


Stripe.prototype.test = function() {
  var p1 = new ConnectedPiece(ONE, ONE, ONE, ONE, BLACK);
  var p2 = new ConnectedPiece(ONE, ONE, ONE, new Rational(2, 1), BLACK);
  p1.connections[BOTTOM] = [p2];
  p2.connections[TOP] = [p1];
  var stripe = new Stripe(p1, p2, LEFT);
  assert(stripe.getSize().equals(new Rational(3, 1)));
  assert(stripe.color == BLACK);
  assert(stripe.pieces.length == 2);

  var p1 = new ConnectedPiece(ONE, ONE, ONE, ONE, BLACK);
  var stripe = new Stripe(p1, p1, LEFT);
  assert(stripe.getSize().equals(ONE));
  assert(stripe.color == BLACK);
  assert(stripe.pieces.length == 1);

}