var _global = this;

(function () {
    
// constructor

    function Emitter ()
    {
        this._particlesPerUpdate    = 3;
        
        this._mouseVelocityX    = 0;
        this._mouseVelocityY    = 0;
        this._originX           = window.innerWidth / 2;
        this._originY           = 100;
        
        var self = this;
        
        window.onmousemove = function (event) {
            self._mouseX    = event.clientX;
            self._mouseY    = event.clientY;
        };
        
        window.onmousedown  = function (event) {
            self._originX = event.clientX;
            self._originY = event.clientY;
            
            self.start();
            event.preventDefault();
        };
        
        window.onmouseup    = function () { self.stop(); };
		
		// Test the iphone
		document.ontouchmove = function (event) {
            self._mouseX    = event.touches[0].clientX;
            self._mouseY    = event.touches[0].clientY;
			
			event.preventDefault();
        };
		
		document.ontouchstart = function (event) {
			self._originX = event.touches[0].clientX;
            self._originY = event.touches[0].clientY;
            
            self.start();
            event.preventDefault();
		};
		
		document.ontouchend    = function () { self.stop(); };
    }
    
    _global.Emitter = Emitter;
    
// public

    Emitter.prototype.start = function ()
    {
        this._active = true;
    };
    
    Emitter.prototype.stop = function ()
    {
        this._active = false;
    };
    
    Emitter.prototype.update = function ()
    {
        if (this._active) {
            for (var i = 0; i < this._particlesPerUpdate; ++i) {
                var particle = new Particle(this._originX, this._originY);

                var mVX = (this._originX - this._mouseX) * 0.4;
                var mVY = (this._originY - this._mouseY) * 0.4;

                this._originX = this._mouseX;
                this._originY = this._mouseY;

                particle.xV     = (Math.random() * 20) - 10 - mVX;
                particle.yV     = (Math.random() * 30) - 15 - mVY;
                particle.mass   = (Math.random() * 1) + 1.2;
                particle._time  = (Math.random() * 20) * particleTic;
                
                addParticle(particle);
            }
        }
    };
    
})();
