/*
 * Copyright Piet Jonas, all rights reserved
 * 
 * Should use formula from http://en.wikipedia.org/wiki/Solar_azimuth_angle
 */

  var settingsCookie = new Cookie;
  var current = null;
  var landscape = true;
  var mapShown = false;
  var firstStart = true;
  var settings = {
    lat: 45,
    lng: -getTimezoneOffset(new Date()) / 12 / 60 * 180,
    origin: -1
  }
  var timer = null;
  var ticker = null;
  var roseImg = new Image();
  var sunImg = new Image();
  var roseAngle = 0;
  var currentSunAngle = 0;
	    
  function setup() {
    landscape = window.innerWidth < window.innerHeight;    
    setTimeout(function() {window.scrollTo(0,1)}, 1);
    settingsCookie = new Cookie("CompassSettings");
    if (settingsCookie.get() != null) {
      try {
        var newSettings = eval("new Object({" + settingsCookie.get() + "})");
        settings = newSettings;
        $("Origin").value = settings.origin;
        firstStart = false;
      }
      catch (e) {
        console.message(e);
      }  
    }
    setupMap();
    roseImg.onload = function() {
      sunImg.onload = function() {
        $("Locator").style.visibility = "visible";  
        $("Loading").style.display = "none";
      }  
      sunImg.src = "sun64.png";
    }  
    roseImg.src = 'rose320.png';
  }
  
  function setupMap() {
    if (GBrowserIsCompatible()) {
       map = new GMap2(document.getElementById("Map"), {
         mapTypes : G_DEFAULT_MAP_TYPES
       });
       map.setCenter(new GLatLng(settings.lat, settings.lng), 2);
       map.addControl(new GSmallMapControl());
       map.addControl(new GMapTypeControl());
       //map.addControl(new GOverviewMapControl());
       var opts = new Object();
       opts.title = "Current Location";
       marker = new GMarker(new GLatLng(settings.lat, settings.lng), opts);
       map.addOverlay(marker);
       geocoder = new GClientGeocoder();
       
       GEvent.addListener(map, "click", function(ignored, point) {
         settings.lat = point.lat();
         settings.lng = point.lng();
         map.panTo(new GLatLng(settings.lat, settings.lng));
         marker.setPoint(point);
         tick();
         saveSettings();
       });       
    }  
  }
  
  function saveSettings() {
    var text = "lat:" + settings.lat;
    text += ",lng:" + settings.lng;
    text += ",origin:" + settings.origin;
    settingsCookie.store(text);
  }
  
  function originChanged() {
    var angle = parseInt($("Origin").value);
    if (angle == -1) {
      angle = -currentSunAngle;
    }
    if (angle < roseAngle) {
      roseAngle -= 360;
    }
    rotateTo(roseAngle, angle, currentSunAngle);
  }
  
  function tick() {
    var sunAngle = calcSunAngle(settings.lat, settings.lng);
    var angle = roseAngle;
    if ($("Origin").value == "-1") {
      angle = -sunAngle;
    }
    drawRose(angle, sunAngle);
  }
  
  function rotateTo(startAngle, endAngle, sunAngle) {
    startAngle += 5;
    if (startAngle > endAngle) {
      startAngle = endAngle;
    }
    drawRose(startAngle, sunAngle);
    if (startAngle == endAngle) {
      timer = null;
      return;
    }
    timer = window.setTimeout(function() {
      rotateTo(startAngle, endAngle, sunAngle);
    }, 50);
  }
  
  function drawRose(angle, sunAngle) {
    currentSunAngle = sunAngle;
    roseAngle = angle;
    var canvas = document.getElementById("Rose");
    var ctx = canvas.getContext("2d");
    
    ctx.clearRect (0, 0, 320, 320);
    ctx.save();
    ctx.translate(160, 160);
    ctx.rotate(-angle * Math.PI / 180);
    ctx.drawImage(roseImg, -160, -160);
    sunAngle = sunAngle * Math.PI / 180;

    var radius = 150;
    ctx.globalAlpha = 0.0 + 0.5 * (new Date().getSeconds() % 2);
    ctx.strokeStyle = "rgb(0, 0, 0)";
    ctx.lineWidth = 4;
    ctx.lineCap = "round";
    ctx.moveTo(0, 0);
    ctx.lineTo(-radius * Math.sin(sunAngle), -radius * Math.cos(sunAngle));
    ctx.stroke();

    ctx.globalAlpha = 0.0 + 0.8 * (new Date().getSeconds() % 2);
    ctx.strokeStyle = "rgb(255, 0, 0)";
    ctx.lineWidth = 2;
    ctx.lineCap = "round";
    ctx.moveTo(0, 0);
    ctx.lineTo(-radius * Math.sin(sunAngle), -radius * Math.cos(sunAngle));
    ctx.stroke();

    ctx.globalAlpha = 0.8;
    ctx.drawImage(sunImg, -110 * Math.sin(sunAngle) - 32, -110 * Math.cos(sunAngle)- 32);

    ctx.restore();    
  }
  
  function calcSunAngle(lat, lng) {
    var date = new Date();
    /*
    var test = parseInt($("TestInput").value);
    if (!isNaN(test)) {
      date.setHours(test + 1);
      date.setMinutes(0);
    }
    */  
    var hours = date.getUTCHours() - 12 + date.getUTCMinutes() / 60.0;
    hours = (hours + 12 * lng / 180);
    if (hours < - 12) {
      hours = 24 + hours;
    }
    if (hours > 12) {
      hours = hours - 24;
    }
    var angle = -Math.atan(Math.sin(lat*Math.PI/180) * Math.tan((15 * hours)*Math.PI/180));
    angle = angle * 180 / Math.PI;
    if (Math.abs(hours) >= 6) {
      angle = angle
    }
    else {
      angle = angle - 180;
    }
    if (lat < 0) {
      angle = angle + 180;
    }  
    $("Result").innerHTML = hours + " " + lat;
    return angle;
  }
  
  function calcSun(day, zone, lat, lng) {
    // Formula from http://mathforum.org/library/drmath/view/56478.html
    var test = 0.39795 * Math.cos(0.2163108 + 2*Math.atan(0.9671396 * Math.tan(0.00860 * (day-186))));
    var p = Math.asin(test);
    
    var nom = Math.sin(0.83333*Math.PI/180) + Math.sin(lat*Math.PI/180)*Math.sin(p);
    var denom = Math.cos(lat*Math.PI/180)* Math.cos(p);
  
    test = nom/denom;
    
    if (Math.abs(test) > 1) {
      if (test > 0) {
        this.sunrise = -60;
        this.sunset = 25 * 60; //never
      }
      else {
        this.sunrise = 25 * 60;
        this.sunset = -60; //never      
      }  
      return;
    }
  
    var d = 24 - (24/Math.PI) * Math.acos(test);
    $("SunTotalHours").innerHTML = Math.floor(d);
    $("SunTotalMinutes").innerHTML = Math.floor((d - Math.floor(d)) * 60);

    var shift = (- lng) / 360.0 * 24 + 12;
    //alert((shift - d / 2) + " " + (shift + d / 2));
    sunrise = (shift - d / 2) * 60;
    sunset = (shift + d / 2) * 60;
    if (sunrise < 0) {
      $("SunriseGMT").innerHTML = "";
      $("SunsetGMT").innerHTML = "No Sunset";
    }
    else if (sunset < 0) {
      $("SunriseGMT").innerHTML = "No Sunrise";
      $("SunsetGMT").innerHTML = "";
    }
    else {
      $("SunriseGMT").innerHTML = "\u2191 " + this.formatTime2(parseInt(this.sunrise / 60), 
                                     parseInt(this.sunrise % 60), 
                                     parseInt((this.sunrise - Math.floor(this.sunrise)) * 60)); 
      $("SunsetGMT").innerHTML = "\u2193 " + this.formatTime2(parseInt(this.sunset / 60), 
                                     parseInt(this.sunset % 60), 
                                     parseInt((this.sunset - Math.floor(this.sunset)) * 60)); 
    }                                 
    
    var shift = (zone*15 - lng) / 360.0 * 24 + 12;
    //alert((shift - d / 2) + " " + (shift + d / 2));
    sunrise = (shift - d / 2) * 60;
    sunset = (shift + d / 2) * 60;
    if (this.sunrise < 0) {
      $("SunriseLocal").innerHTML = "";
      $("SunsetLocal").innerHTML = "No Sunset";
    }
    else if (this.sunset < 0) {
      $("SunriseLocal").innerHTML = "No Sunrise";
      $("SunsetLocal").innerHTML = "";
    }
    else {
      $("SunriseLocal").innerHTML = "\u2191 " + this.formatTime2(parseInt(this.sunrise / 60), 
                                     parseInt(this.sunrise % 60), 
                                     parseInt((this.sunrise - Math.floor(this.sunrise)) * 60)); 
      $("SunsetLocal").innerHTML = "\u2193 " + this.formatTime2(parseInt(this.sunset / 60), 
                                     parseInt(this.sunset % 60), 
                                     parseInt((this.sunset - Math.floor(this.sunset)) * 60)); 
    }                                 
  }

  function formatTime(date) {
    var hours = date.getHours();
    var minutes = date.getMinutes() + "";
    var seconds = lastSeconds % 60 + "";
    return this.formatTime2(date.getHours(), date.getMinutes(), parseInt(date.getTime() / 1000 % 60));
  }

  function formatTime2(hours, minutes, seconds) {
    var isPM = hours >= 12;
    if (this.show12Clock) {
      hours = hours % 12;
      if (hours == 0) {
        hours = 12;
      }
    }
    hours = hours + "";
    if (hours.length < 2 && !this.show12Clock) {
      hours = "0" + hours;
    }    
    minutes = minutes + "";
    if (minutes.length < 2) {
      minutes = "0" + minutes;
    }
    seconds = seconds + "";
    if (seconds.length < 2) {
      seconds = "0" + seconds;
    }
    var result = hours + ":" + minutes + ":" + seconds;
    if (this.show12Clock) {
      if (isPM) {
        result += "<span class='timeExt'>PM</span>";
      }
      else {
        result += "<span class='timeExt'>AM</span>";
      }
    }
    return result;
  }
  
  function getTimezoneOffset(date) {
    var offset = date.getTimezoneOffset();
    var diff = -(date.getHours() - date.getUTCHours()) * 60;
    if (Math.abs(diff) != Math.abs(offset)) {
      if (diff > 0) {
        diff -= 24 * 60;
      }
      else {
        diff += 24 * 60;
      }  
    }
    if (diff != offset) {
      offset = -offset;
    }
    return offset;
  }
  
  function start() {
    ticker = window.setInterval(tick, 1000);      
  }
  
  function stop() {
    window.clearInterval(ticker);
  }
  
  function showCompass() {
    $("Compass").style.display = "block";
    $("Content").style.display = "none";
    window.scrollTo(0,1)
    originChanged();
    start();
  }
  
  function hideCompass() {
    $("Compass").style.display = "none";
    $("Content").style.display = "block";
    window.scrollTo(0,1)
    stop();
  }
  
	function orientationChanged() {
    landscape = window.innerWidth < window.innerHeight;
    setTimeout(function() {window.scrollTo(0,1)}, 1);
  }
	  
  function tellFriend() {
    var body = "Hi,<br><br>I just stumbled upon this iPhone Compass application:" +
        "<br><br>http://compass.speedymarks.com<br><br>" +
        "Adds the missing compass to the iPhone. You just have to " +
        "see the sun and know the time to determine the north direction." +
        "<br><br>Best regards";
    window.open("mailto:?subject=Compass on the iPhone&body=" + body, "_self");  
  }
  
  function $(id) {
    return document.getElementById(id);
  } 
   
  function debug(msg) {
    var e = document.getElementById("Debug");
    e.innerHTML += msg + "<br>";
  }
