1 var jaws = (function(jaws) {
  2   /** 
  3    * @class Manage a parallax scroller with different layers. "Field Summary" contains options for the Parallax()-constructor.
  4    * @constructor
  5    *
  6    * @property scale     number, scale factor for all layers (2 will double everything and so on)
  7    * @property repeat_x  true|false, repeat all parallax layers horizontally
  8    * @property repeat_y  true|false, repeat all parallax layers vertically
  9    * @property camera_x  number, x-position of "camera". add to camera_x and layers will scroll left. defaults to 0
 10    * @property camera_y  number, y-position of "camera". defaults to 0
 11    *
 12    * @example
 13    * parallax = new jaws.Parallax({repeat_x: true})
 14    * parallax.addLayer({image: "parallax_1.png", damping: 100})
 15    * parallax.addLayer({image: "parallax_2.png", damping: 6})
 16    * parallax.camera_x += 1    // scroll layers horizontally
 17    * parallax.draw()
 18    *
 19    */
 20   jaws.Parallax = function Parallax(options) {
 21     if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
 22     jaws.parseOptions(this, options, this.default_options)
 23   }
 24 
 25   jaws.Parallax.prototype.default_options = {
 26     width: function() { return jaws.width },
 27     height: function() { return jaws.height },
 28     scale: 1,
 29     repeat_x: null,
 30     repeat_y: null,
 31     camera_x: 0,
 32     camera_y: 0,
 33     layers: []
 34   }
 35 
 36   /** Draw all layers in parallax scroller */
 37   jaws.Parallax.prototype.draw = function(options) {
 38     var layer, numx, numy, initx;
 39 
 40     for(var i=0; i < this.layers.length; i++) {
 41       layer = this.layers[i]
 42 
 43       if(this.repeat_x) {
 44         initx = -((this.camera_x / layer.damping) % layer.width);
 45       } 
 46       else {
 47         initx = -(this.camera_x / layer.damping)
 48       }		
 49 
 50       if (this.repeat_y) {
 51         layer.y = -((this.camera_y / layer.damping) % layer.height);
 52       }
 53       else {
 54         layer.y = -(this.camera_y / layer.damping);
 55       }
 56 
 57       layer.x = initx;
 58       while (layer.y < this.height) {
 59         while (layer.x < this.width) {
 60           if (layer.x + layer.width >= 0 && layer.y + layer.height >= 0) { //Make sure it's on screen
 61             layer.draw(); //Draw only if actually on screen, for performance reasons
 62           }
 63           layer.x = layer.x + layer.width;      
 64 
 65           if (!this.repeat_x) {
 66             break;
 67           }
 68         }
 69 
 70         layer.y = layer.y + layer.height;
 71         layer.x = initx;
 72         if (!this.repeat_y) {
 73           break;
 74         }
 75       }
 76     }
 77   }
 78   /** Add a new layer to the parallax scroller */
 79   jaws.Parallax.prototype.addLayer = function(options) {
 80     var layer = new jaws.ParallaxLayer(options)
 81     layer.scaleAll(this.scale)
 82     this.layers.push(layer)
 83   }
 84   /** Debugstring for Parallax() */
 85   jaws.Parallax.prototype.toString = function() { return "[Parallax " + this.x + ", " + this.y + ". " + this.layers.length + " layers]" }
 86 
 87   /**
 88    * @class A single layer that's contained by Parallax()
 89    *
 90    * @property damping  number, higher the number, the slower it will scroll with regards to other layers, defaults to 0
 91    * @constructor
 92    * @extends jaws.Sprite
 93    */
 94   jaws.ParallaxLayer = function ParallaxLayer(options) {
 95     if( !(this instanceof arguments.callee) ) return new arguments.callee( options );
 96 
 97     this.damping = options.damping || 0
 98     jaws.Sprite.call(this, options)
 99   }
100   jaws.ParallaxLayer.prototype = jaws.Sprite.prototype
101 
102   /** Debugstring for ParallaxLayer() */
103   // This overwrites Sprites toString, find another sollution.
104   // jaws.ParallaxLayer.prototype.toString = function() { return "[ParallaxLayer " + this.x + ", " + this.y + "]" }
105 
106   return jaws;
107 })(jaws || {});
108 
109