var landscape;
var ball;
var basket;
var arrow = new Object();
var fixedBasket = false;
var width = 320;
var height = 356;
var isMultiTouch = false;
var settingsCookie = new Cookie("BasketballSettings");

function loaded() {
  orientationChanged();
  var text = settingsCookie.get();
  if (text) {
    try {
      var settings = eval("new Object({" + text + "})");
      $("MovingBasket").checked = settings.movingBasket;
      if (settings.gravity) {
        $("Gravity").value = settings.gravity;
      }
      if (settings.maxShots) {
        $("MaxShots").selectedIndex = settings.maxShots;
      }  
    }
    catch (e) {
      alert(e);
    }  
  }
  //setupTouch();
  //showGame(new PracticeMode());
}

function startPractice() {
  showGame(new PracticeMode());  
}

function startShots() {
  showGame(new MaxShotsMode(parseInt($("MaxShots").value)));  
}

function showGame(mode) {
  fixedBasket = !$("MovingBasket").checked;
  $("GameOver").style.display = "none";
  $("Main").style.display = "none";  
  $("Game").style.display = "block"; 
  basket = new Basket(width, height / 2);
  basket.setLeft(false);
  basket.draw();
  ball = new BBall(width, height);
  ball.basket = basket;
  ball.mode = mode;
  ball.mode.ball = ball;
  ball.setWithCeiling(false);
  arrow.y = -1;
  doShot();
}

function showGameOver() {
  $("GameOver").style.display = "block";
  ball.img.src = "eggBroken.png";
  ball.img.width = "74";
}

function closeGame() {
  showAppStore();
  /*
  ball.stop();
  ball = null;
  $("Main").style.display = "block";  
  $("Game").style.display = "none"; 
  */
  //location.reload();
}

function showAppStore() {
  $("GameOver").style.display = "none";
  $("AppStore").style.display = "block";
}  

function closeAppStore() {
  location.reload();
}  

function doLeft() {
  $("Touch").style.display = "none";
  ball.stop();
  arrow.x -= 8;
  redraw();
}

function doRight() {
  $("Touch").style.display = "none";
  ball.stop();
  arrow.x += 8;
  redraw();
}

function doUp() {
  $("Touch").style.display = "none";
  ball.stop();
  arrow.y -= 8;
  redraw();
}

function doDown() {
  $("Touch").style.display = "none";
  ball.stop();
  arrow.y += 8;
  redraw();
}

function doShot() {
  $("Touch").style.display = "none";
  $("Controls").style.display = "none";
  $("Controls2").style.display = "none";
  ball.stop();
  var canvas = $("Line");
  if (canvas.getContext) {
    var ctx = canvas.getContext('2d');
    ctx.clearRect(0, 0, 480, 480);
  }
  var gravity = parseInt($("Gravity").value);  
  ball.vx = arrow.x * 100;
  ball.vy = arrow.y * 100;
  arrow.x = 0;
  arrow.y = 0;
  ball.setGravity(gravity);
  ball.daempfung = 160;
  ball.start();   
}

function clicked(event) {
  if (ball.mode.isFinished()) {
    showGameOver();
    return;
  }
  if (ball.mode.hitted) {
    placeBall();
    ball.mode.hitted = false;
  }
  else {
    $("Controls").style.display = "block";
    $("Controls2").style.display = "block";
    arrowClick(event);
  }
}

function arrowClick(event) {
  ball.stop();
  arrow.x = event.clientX - ball.x;
  arrow.y = event.clientY - ball.y;
  redraw();
  $("Touch").style.left = event.clientX - 25 + "px";  
  $("Touch").style.top = event.clientY - 25 + "px";  
  $("Touch").style.opacity = "0.8";
  $("Touch").style.display = "block";
  window.setTimeout(function() {
    $("Touch").style.opacity = "0.6";
    window.setTimeout(function() {
      $("Touch").style.opacity = "0.3";
    }, 100);
  }, 100);  
}

function flipBasket() {
  basket.setLeft(!basket.left);
  if (!basket.left) {
    basket.setXY(ball.width - basket.getBasketWidth(), basket.y);
  }
  else {
    basket.setXY(0, basket.y);
  }
}

function placeBall() {
  var playheight = ball.height;
  if (!fixedBasket) {
    flipBasket();
    var y = playheight / 4 + parseInt(Math.random() * playheight / 2);
    basket.setXY(basket.x, y);
  }
  ball.y = playheight - ball.radius;
  ball.x = parseInt(Math.random() * (ball.width - basket.getBasketWidth()) / 2 + (ball.width - basket.getBasketWidth()) / 2);
  if (ball.x < ball.radius) {
    ball.x = ball.radius;
  }
  if (ball.x > ball.width - basket.getBasketWidth() - 2 * ball.radius) {
    ball.x = ball.width - basket.getBasketWidth() - 2 * ball.radius;
  }
  if (basket.left) {
    ball.x += basket.getBasketWidth();
  }
  ball.draw();
}


function redraw() {
  var canvas = $("Line");
  if (canvas.getContext) {
    var ctx = canvas.getContext('2d');
    ctx.clearRect(0, 0, 480, 480);
    ctx.strokeStyle = 'rgba(0,0,0,0.3)';
    ctx.lineWidth = 2;
    ctx.beginPath();
    ctx.moveTo(ball.x, ball.y + 2);
    ctx.lineTo(ball.x + arrow.x, ball.y + arrow.y + 2);
    ctx.stroke();

    ctx.strokeStyle = 'rgba(255,255,255,1)';
    ctx.lineWidth = 2;
    ctx.beginPath();
    ctx.moveTo(ball.x, ball.y);
    ctx.lineTo(ball.x + arrow.x, ball.y + arrow.y);
    ctx.stroke();
  }  
}

function setupTouch() {
  $("Line").ontouchstart = function(event) {
    isMultiTouch = true;
    event.preventDefault();
  }  
}

function showSettings() {
  $("Settings").style.display = "block";
  $("SettingsButton").style.display = "none";
}

function hideSettings() {
  $("Settings").style.display = "none";
  $("SettingsButton").style.display = "block";
}

function saveSettings() {
  var text = "";
  text += "movingBasket:" + $("MovingBasket").checked;
  text += ",gravity:" + $("Gravity").value;
  text += ",maxShots:" + $("MaxShots").selectedIndex;
  text += "";
  settingsCookie.store(text);
}

function help() {
  $("Help").style.display = "block";
}

function tellFriend() {
  var body = "Hi,<br><br>I just stumbled upon this Easterball iPhone application:" +
      "<br><br>http://easterball.speedymarks.com<br><br>" +
      "Play it!" +
      "<br><br>Best regards";
  window.open("mailto:?subject=Easterball on the iPhone&body=" + body, "_self");  
}

function orientationChanged() {
  if (window.orientation != undefined) {
    landscape = window.orientation != 0 && window.orientation != 180;
  }
  else {
    landscape = window.innerWidth > window.innerHeight;
  }  
  if (landscape) {
    width = 480;
    height = 270;
    $("LandscapeCSS").href = "landscape.css";
    if (window.isAppStore) {
      height = 300;
      $("Game").style.height = "300px";
    }
  }
  else {
    width = 320;
    height = 356;    
    $("LandscapeCSS").href = "portrait.css";
    if (window.isAppStore) {
      height = 460;
      $("Game").style.height = "460px";
    }
  }
  $("Line").width = width;
  $("Line").height = height;
  if (ball) {
    ball.stop();
    showGame(ball.mode);
  }
  setTimeout(function() {window.scrollTo(0,1)}, 1);
}

function $(id) {
  return document.getElementById(id);
}

function debug(msg) {
  console.log(msg);
}      