function Collision(){
    var debug_collisions = true;
    var objectA = {};
    var objectB = {};
    var hasColllision = false;
    var intersectType = "none";
    this.intersectAt = null;
    this.imageData = null;
    
    this.check = function( a, b ){
        
        objectA = a;
        objectB = b;
                
        if( hasCollision = (this.collisionX() && this.collisionY()) ){
            
            if( debug_collisions ) debugMessage("Collision Detected");
            
            this.intersect();
        }
        else{
            if( debug_collisions ) debugMessage("");            
        }
    }
    
    this.collisionX = function(){
        var axLeft = objectA.getPosition().x;
        var axRight = objectA.getPosition().x + objectA.getWidth();
                
        var bxLeft = objectB.getPosition().x;
        var bxRight = objectB.getPosition().x + objectB.getWidth();
                     
        return ( axLeft >= bxLeft && axLeft <= bxRight ) || ( axRight >= bxLeft  && axRight <= bxRight );
    }
    
    this.collisionY = function(){
        
        var ayTop = objectA.getPosition().y;
        var ayBottom = objectA.getPosition().y + objectA.getHeight();
                
        var byTop = objectB.getPosition().y;
        var byBottom = objectB.getPosition().y + objectB.getHeight();
        
        return  ( ayTop >= byTop && ayTop <= byBottom ) || ( ayBottom >= byTop  && ayBottom <= byBottom );
    }
    
    this.intersect = function(){
        var aWidth = objectA.getWidth();
        var aHeight = objectA.getHeight();
        
        var bWidth = objectB.getWidth();
        var bHeight = objectB.getHeight();
        
        var minWidth = ( aWidth < bWidth) ? aWidth : bWidth;
        var minHeight = ( aHeight < bHeight  ) ? aHeight : bHeight ;
        
        var totalWidth = aWidth + bWidth;
        var totalHeight = aHeight + bHeight;
                 
        var aLeft   = objectA.getPosition().x;
        var aRight = objectA.getPosition().x + objectA.getWidth();
        var aTop   = objectA.getPosition().y;
        var aBottom = objectA.getPosition().y + objectA.getHeight();
        var bLeft   = objectB.getPosition().x;
        var bRight = objectB.getPosition().x + objectB.getWidth();
        var bTop   = objectB.getPosition().y;
        var bBottom = objectB.getPosition().y + objectB.getHeight();
        
        var w = 0;
        if( aLeft < bLeft ) w = totalWidth - (bRight - aLeft);        
        else{
            if( aLeft == bRight  ) w = 0;
            else if( aRight <=  bRight)
                if( aRight > bRight  ){                    
                    w = aRight - bRight;
                }
                else
                    w = minWidth;
            else if( aLeft > bRight ) w = 0;            
            else{            
                w = aRight - bRight;
            }
        }
        
        var h = 0;
        
        if( aTop < bBottom ){
            if( bBottom > aTop  ){
                if(aBottom > bBottom){
                    h = bBottom - aTop;
                }
                else if( aTop < bTop && aBottom < bBottom){
                    h = aBottom - bTop;
                }    
                else{
                    h = minHeight;  
                }
            }
            
            else{
                h = totalHeight - (bBottom - aTop);
                
            }
        }
        else{
            
            if( aTop == bBottom  ){
                h = 0;
            }
            else if( aBottom <=  bBottom){
                if( aBottom > bBottom  ){                    
                    h = aTop - bBottom;
                }
                else{
                    h = minHeight;
                }
            }
            else if( aBottom > bBottom ){
                h = aTop - bBottom;
            }
            else if( aTop > bBottom ){
                h = 0;            
            }
            else{            
                h = aBottom - bBottom;
            }
        }
        
                
        var l = t = r = b = 0;
        
        if( aLeft <= bLeft && aTop <= bTop ){            
            l = bLeft;       // LEFT
            t = bTop;       // TOP
            r = aRight;  // RIGHT
            b = aBottom; // BOTTOM
        }
        else if( aLeft <= bLeft && aTop >= bTop ){            
            l = bLeft;       // LEFT    
            t = aTop;       // TOP            
            r = aRight;  // RIGHT
            
            // This manages the corners
             if(aBottom >= bBottom ) b = bBottom;
            else b = aBottom; // BOTTOM
        }        
        else if( aLeft >= bLeft && aTop <= bTop){            
            l = aLeft;       // LEFT
            t = bTop;       // TOP
            
            if( aRight < bRight )r = aRight;  // RIGHT
            else r = bRight ;
               
            
            if(aBottom >= bBottom ) b = bBottom;
            else b = aBottom; // BOTTOM
        }
        else if( aLeft >= bLeft && aTop >= bTop ){            
            l = aLeft;       // LEFT
            t = aTop;       // TOP
            r = bRight;  // RIGHT
            
             if(aBottom >= bBottom ) b = bBottom;
            else b = aBottom; // BOTTOM
        }
        
         if( w <= 0 || h <= 0  ) intersectType = "touch";
         else if( w > 0 && h > 0 ) intersectType = "overlap";
         else if( l < objectB.getPosition.x ){ intersectType = "overlap";  }
         else intersectType = "none";
         
         if( intersectType == "none" ){
            this.intersectAt = null;
            console.log("none");
         }
        else{
            this.intersectAt = {
                top: t,
                left: l,
                width: w,
                height: h,
                areaInObjectAAffected: { x: Math.abs(objectA.getPosition().x - l), y: Math.abs(objectA.getPosition().y - t) } ,
                areaInObjectBAffected: { x: Math.abs(objectB.getPosition().x - l), y: Math.abs(objectB.getPosition().y - t) }
            };            
        }
        
         var  msg  = "Intersect Type: " + intersectType + "<br />";
                msg += "<table>";
                msg += "<thead>";
                msg += "<tr><th>&nbsp;</th><th>Left</th><th>Top</th><th>Right</th><th>Bottom</th><th>Width</th><th>Height</th></tr>";
                msg += "</thead>";
                msg += "<tbody>";
                msg += "<tr><th>A</th><td>"+objectA.getPosition().x+"</td><td>"+objectA.getPosition().y+"</td><td>"+(objectA.getPosition().x + objectA.getWidth())+"</td><td>"+(objectA.getPosition().y + objectA.getHeight())+"</td><td>"+objectA.getWidth()+"</td><td>"+objectA.getHeight()+"</td></tr>";
                msg += "<tr><th>B</th><td>"+objectB.getPosition().x+"</td><td>"+objectB.getPosition().y+"</td><td>"+(objectB.getPosition().x + objectB.getWidth())+"</td><td>"+(objectB.getPosition().y + objectB.getHeight())+"</td><td>"+objectB.getWidth()+"</td><td>"+objectB.getHeight()+"</td></tr>";
                msg += "<tr><th>intersect (C)</th><td>"+l+"</td><td>"+t+"</td><td>"+r+"</td><td>"+b+"</td><td>"+w+"</td><td>"+h+"</td></tr>";
                msg += "</tbody>";
                msg += "</table>";
        
        if( debug_collisions ) debugMessage(msg);
        
        this.intersectPixelTest();
        
        return this.intersectAt;
    }
    
    this.intersectPixelTest = function(){
        /*
        if( this.intersectAt == null  ){
            outMessage("Can't perform pixel test at intersection because intersection is not set.");
            return;
        }
        */
        
        var collision = this;        
        
        if( collision.intersectAt != null && collision.intersectAt != undefined){
             if( collision.intersectAt.width > 0 && collision.intersectAt.height > 0 ){
                 // Get the canvas image data            
                 var imageData = ctx.getImageData( collision.intersectAt.left  , collision.intersectAt.top , collision.intersectAt.width ,collision.intersectAt.height);
                 var pixelData = 0;            
                 
                 // Get the Current player image data
                 // first calculate the area of the player image the collision occurs
                 var player1CollisionArea = { x: Math.abs(collision.intersectAt.left - player1.getPosition().x), y: Math.abs(collision.intersectAt.top - player1.getPosition().y   ) };
                 bufferContext.clearRect(0,0, canvasWidth, canvasHeight); // clear canvas
                 bufferContext.drawImage( document.getElementById( player1.getBufferID() ), 0, 0 );
                 var player1ImageData = bufferContext.getImageData( collision.intersectAt.areaInObjectAAffected.x,
                                                                                                         collision.intersectAt.areaInObjectAAffected.y ,
                                                                                                         collision.intersectAt.width,
                                                                                                         collision.intersectAt.height  );
                
                 var player2CollisionArea = { x: Math.abs(collision.intersectAt.left - player2.getPosition().x), y: Math.abs(collision.intersectAt.top - player2.getPosition().y   ) };
                 bufferContext.clearRect(0,0, canvasWidth, canvasHeight); // clear canvas
                 bufferContext.drawImage( document.getElementById( player2.getBufferID() ), 0, 0 );
                 var player2ImageData = bufferContext.getImageData( collision.intersectAt.areaInObjectBAffected.x,
                                                                                                         collision.intersectAt.areaInObjectBAffected.y,
                                                                                                         collision.intersectAt.width,
                                                                                                         collision.intersectAt.height  );
                  
                  
                  // W       
                  for( var x = 0; x < player1ImageData.data.length; x++ ){
                     
                     var pixA = "";
                     var pixB = "";
                     
                     for( var y = 0; y < 4; y++){
                         if( y > 0 ){
                             pixA += ",";
                             pixB += ",";
                         }
                         pixA += player1ImageData.data[x].toString();
                         pixB += player2ImageData.data[x++].toString();
                     }
                     
                    if(pixA != "0,0,0,0" && pixB != "0,0,0,0"){
                        console.log( "Pixel Collision found at: ", pixA +  " " + pixB  );
                         x  = player1ImageData.data.length;
                    }
                     
                     /* Seems to work
                     var pixA = player1ImageData.data[x];
                     var pixB = player2ImageData.data[x];
                        
                     if( pixA != 0 && pixB != 0){
                         console.log( "Pixel Collision found at: ", pixA +  " " + pixB  );
                         x  = player1ImageData.data.length;
                         
                     }
                     */
                        
                 }             
                  /* 
                 for(var x = 0; x < imageData.data.length; x++ ){
                     if( x % 4 == 0 ) pixelData += "\n";
                     else if( x % 4 > 0  ) pixelData += ",";
                     
                     pixelData += imageData.data[x];
                 }
                  */
                 /*
                 console.log( collision.intersectAt.width + " " + collision.intersectAt.height + " : " +  imageData.data.length );
                 console.log( pixelData );
                 console.log("=============");
                 */
             }
         }
        
    }
}

