/* Global variables */
var windowWidth = 0;
var windowHeight = 0;

var ball = null;
var ballInGame = false;
var ballTaken = false;
var lastX = 0;
var lastY = 0;
var thisX = 0;
var thisY = 0;


setWindowSize();


$(document).ready(function(e) {
    ball = new Gravitator($("#ball"), {
        weight: 5
    });
    
    
    coded = "Uh4LX@YMrhU.fM";
    key = "qMOvWRc5NHVgT8eSQlnsJkm1pY2KftyXwrZUPADIxa0zbGo4Eu79dCBh63ijLF";
    link = printEmail(coded, key);

    $("#contact_email").html("<a href='mailto:"+link+"'>"+link+"</a>");
    
    $('a[@rel*=lightbox]').lightBox();
});

$(window).resize(function(e) {
    ball.setMoving(false);
    oldAccelX = ball.getAccelX();
    oldAccelY = ball.getAccelY();
    oldWindowWidth = windowWidth;
    oldWindowHeight = windowHeight;
    
    setWindowSize();
    
    ball = new Gravitator($("#ball"), {
        weight: 5
    });

    ball.setAccelX(oldAccelX);
    ball.setAccelY(oldAccelY);

    if(oldWindowWidth > windowWidth) {
        newPosX = windowWidth - $("#ball").width();
        $("#ball").css("left", newPosX.toString() + "px");
    }

    if(oldWindowHeight > windowHeight) {
        newPosY = windowHeight - $("#ball").height();
        $("#ball").css("top", newPosY.toString() + "px");
    }

    ball.setMoving(true);
});

$("#ball").bind("click", function(e) {
    if(!ballInGame) {
        // Release the ball and let it move
        ballInGame = true;
        
        $("#ball").css("left", "0");
        $("#ball").css("bottom", "0");
        ball.setAccelX(Math.floor(Math.random() * 11));
        ball.setAccelY(- Math.floor(Math.random() * 11));
        
        $(document).everyTime(16, function() {
           ball.step();
        });
    }
    
    return false;
});

$("#ball").bind("mousedown", function(e) {
    ball.setTaken(true);
    
    thisX = lastX = $("#ball").offset()["left"];
    thisY = lastY = $("#ball").offset()["top"];
    
    return false;
});

$("#ball").bind("mouseup", function(e){
    ball.setTaken(false);

    var deltaX = thisX - lastX;
    var deltaY = thisY - lastY;

    ball.setAccelX(deltaX / windowWidth * 100);
    ball.setAccelY(deltaY / windowHeight * 100);
    
    return false;
});

$(document).bind("mousemove", function(e){
    if(ball.getTaken()) {
        lastX = thisX;
        lastY = thisY;

        thisX = e.pageX - $("#ball").width() / 2;
        thisY = e.pageY - $("#ball").height() / 2;

        $("#ball").css("left", thisX.toString() + "px");

        $("#ball").css("top", thisY.toString() + "px");
    }

    return false;
});

$("#background").bind("mousedown", function(e) {
    return false;
});

/* Ball stuff */
function Gravitator(obj, opts) {
    this.object = obj;
 
    this.minX = 0;
    this.minY = 0;
    
    this.posX = 0;
    this.posY = 0;
    
    this.width = obj.width();
    this.height = obj.height();
    
    this.weight = opts.weight;
    
    this.friction = 0.05;
    this.gravity = 0.05;
    
    this.accelX = 0.0;
    this.accelY = 0.0;
    this.accelCutOff = 0.5;
    
    this.moving = true;
    this.taken = false;
}

Gravitator.prototype.step = function() {
    if(this.moving) {
        this.posX = this.object.offset()["left"];
        this.posY = this.object.offset()["top"];
        
        
        
        if(this.accelY > 0) {
            // Czy juz pilka dotknela ziemi
            if(this.posY + this.height >= windowHeight) {
              // odbij
              if(this.accelY < this.accelCutOff) {
                 this.accelY = 0;
                 this.accelX = 0;
                 this.moving = false;
              } else {
                 this.accelY = - (this.accelY / 2);
              }
            } else {
              this.accelY += this.gravity;
            }
        } else if(this.accelY <= 0) {
            //if(this.posY <= this.minY) {
                //alert(this.accelY);
            //    this.accelY = - (this.accelY / 2);
           // } else {
                this.accelY += this.friction;
            //}
        }
        
        if(this.accelX > 0) {
            // prawy brzeg
            if(this.posX + this.width >= windowWidth) {
                // odbij
                //if(this.accelX < this.)
                this.accelX = - (this.accelX / 2);
            } else {
                //this.accelX -= this.friction;
            }
        } else if(this.accelX < 0) {
            if(this.posX <= this.minX) {
                this.accelX = - (this.accelX / 2);
            } else {
                //this.accelX += this.friction;
            }
        }
        
        
        newPosX = this.posX + this.weight * this.accelX;
        newPosY = this.posY + this.weight * this.accelY;
        
    
        this.object.css("left", newPosX.toString() + "px");
        this.object.css("top", newPosY.toString() + "px");
        
       
    }
}

Gravitator.prototype.setMoving = function(moving) {
    this.moving = moving;
}

Gravitator.prototype.setTaken = function(taken) {
    if(taken) {
        this.setMoving(false);
    } else {
        this.setMoving(true);
    }
    
    this.taken = taken;
}

Gravitator.prototype.getTaken = function() {
    return this.taken;
}


Gravitator.prototype.setAccelY = function(val) {
    this.accelY = val;
}

Gravitator.prototype.setAccelX = function(val) {
    this.accelX = val;
}

Gravitator.prototype.getAccelY = function() {
    return this.accelY;
}

Gravitator.prototype.getAccelX = function() {
    return this.accelX;
}

Gravitator.prototype.setX = function(val) {
    this.posX = val;
}

Gravitator.prototype.getX = function() {
    return this.posX;
}

Gravitator.prototype.setY = function(val) {
    this.posY = val;
}

Gravitator.prototype.getY = function(val) {
    return this.posY;
}











function setWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  windowWidth = myWidth;
  windowHeight = myHeight;
}

// Email obfuscator script 2.1 by Tim Williams, University of Arizona
// Random encryption key feature by Andrew Moulden, Site Engineering Ltd
// This code is freeware provided these four comment lines remain intact
// A wizard to generate this code is at http://www.jottings.com/obfuscator/
function printEmail(coded, key) { 
  shift=coded.length
  link=""
  for (i=0; i<coded.length; i++) {
    if (key.indexOf(coded.charAt(i))==-1) {
      ltr = coded.charAt(i)
      link += (ltr)
    }
    else {     
      ltr = (key.indexOf(coded.charAt(i))-shift+key.length) % key.length
      link += (key.charAt(ltr))
    }
  }
  return link;
}

