//
// config
//
var MIN_WIDTH = 700; //minimalni sirka stranky
var MIN_HEIGHT = 660; //minimalni vyska stranky

var MAX_FRAMES = 40; //maximalni pocet snimku, ktery se bude compas tocit
var INTERVAL = 30; //interval v ms, v nemz se budou snimky menit

var TIMEOUT = 1000; //doba v ms, po jejimz uplynuti se zasne kompas sam tocit
var STEP = Math.PI / 1000; //uhel v rad, o nejz se kompas otoci za jeden snimek animace



var screenWidth = getWidth();
var screenHeight = getHeight();

var padding = (screenHeight - 140 - 40 - 288 - 30 - 108) / 2;

var body = document.getElementById("body");
body.style.width = screenWidth + "px";
body.style.paddingTop = padding + "px";

var background = document.getElementById("background");
background.style.width = screenWidth + "px";
background.style.height = screenHeight + "px";

var centerX = screenWidth / 2;
var centerY = padding + 140 + 40 + 144;

var arrows = document.getElementById("arrows");

var properties = ["MozTransform", "OTransform", "WebkitTransform", "msTransform", "transform"];
var property = "";
for (var i = 0; i < properties.length; i++) {
  if (typeof arrows.style[properties[i]] == "string") {
    property = properties[i];
    break;
  }
}

window.onresize = function() {
  screenWidth = getWidth();
  screenHeight = getHeight();
  
  padding = (screenHeight - 140 - 40 - 288 - 30 - 108) / 2;
  
  body.style.width = screenWidth + "px";
  body.style.paddingTop = padding + "px";
  
  background.style.width = screenWidth + "px";
  background.style.height = screenHeight + "px";
  
  centerX = screenWidth / 2;
  centerY = padding + 140 + 40 + 144;
}

if (property != "") { //if transformace fungujou
  var SQRT_PI = Math.sqrt(Math.PI);
  var angle = 0;
  var direction = 1;
  var rotation, timeout;
  
  startRotation();
  
  var oldX = -1, oldY = -1;
  document.onmousemove = function(e) {
    if (!e) { //IE
      var e = window.event;
    }
    
    //webkit prohlizece spousti mousemove i kdyz mys stoji na miste - fix
    if (e.clientX == oldX && e.clientY == oldY) {
      return;
    }
    oldX = e.clientX;
    oldY = e.clientY;
    
    var rads = Math.atan((e.clientX - centerX) / (centerY - e.clientY));
    
    if (e.clientY > centerY) {
      rotateTo(rads + Math.PI * (e.clientX > centerX ? 1 : -1));
    } else {
      rotateTo(rads);
    }
  }
}

//
// zajistuje rotaci sledujici pohyb kurzoru
//
function rotateTo(rads) {
  clearTimeout(timeout);
  
  var difference = angle - rads;
  
  if (difference < 0.001 && difference > -0.001) { //if rozdil nepatrny -> nespoustet
    return;
  }
  
  var distance;
  if (difference > Math.PI) {
    direction = 1;
    distance = 2 * Math.PI - difference;
  } else if (difference < -Math.PI) {
    direction = -1;
    distance = 2 * Math.PI + difference;
  } else if (difference > 0) {
    direction = -1;
    distance = difference;
  } else {
    direction = 1;
    distance = Math.abs(difference);
  }
  
  var frames = Math.round((Math.sqrt(distance) * MAX_FRAMES) / SQRT_PI);
  var step = distance / frames;
  
  clearInterval(rotation);
  
  //setInterval spousti prvni sminek s timeoutem -> prvni snimek hned ted
  angle = normalize(angle + direction * step);
  arrows.style[property] = "rotate(" + angle + "rad)";
  
  var counter = 1; //odpocet snimku
  rotation = setInterval(function() {
    if (counter < frames) {
      angle = normalize(angle + direction * step);
      arrows.style[property] = "rotate(" + angle + "rad)";
    } else {
      angle = normalize(rads);
      arrows.style[property] = "rotate(" + angle + "rad)";
      clearInterval(rotation);
      startRotation();
    }
    counter++;
  }, INTERVAL);
}

//
// spousti automatickou rotaci
//
function startRotation() {
  timeout = setTimeout(function() {
    rotation = setInterval(function() {
      angle = normalize(angle + direction * STEP);
      arrows.style[property] = "rotate(" + angle + "rad)";
    }, INTERVAL);
  }, TIMEOUT);
}

//
// normalizuje radiany - prevadi je do rozmezi -pi az +pi
//
function normalize(rads) {
  var result = 0;
  
  if (rads <= Math.PI && rads >= -Math.PI) {
    result = rads;
  } else {
    result = rads - 2 * Math.PI * Math.round(rads / (2 * Math.PI));
  }
  
  return result;
}

//
// vraci sirku okna
//
function getWidth() {
  var available = 0;
  if (typeof(window.innerWidth) == 'number') {
    available = window.innerWidth;
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    available = document.documentElement.clientWidth;
  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    available = document.body.clientWidth;
  }
  
  return available < MIN_WIDTH ? MIN_WIDTH : available;
}

//
// vraci vysku okna
//
function getHeight() {
  var available = 0;
  if (typeof(window.innerHeight) == 'number') {
    available = window.innerHeight;
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    available = document.documentElement.clientHeight;
  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    available = document.body.clientHeight;
  }
  
  return available < MIN_HEIGHT ? MIN_HEIGHT : available;
}

