/**
 * param x                  x coordinate of info window's top left corner
 * param y                  y coordinate of info window's top left corner
 */
function GenericInfoWindow(x, y) {

  this.div = document.createElement('div');
  this.header = document.createElement('div');
  this.header.className = 'generic_info_header';
  this.body = document.createElement('div');
  this.body.className = 'generic_info_body';
  
  this.div.appendChild(this.header);
  this.div.appendChild(this.body);
  
  this.div.className = 'generic_info_container';
  this.div.style.left = x + 'px';
  this.div.style.top = y + 'px';

  return this;
}

function updateOffsets() {
  var currentCenter = map.fromLatLngToDivPixel(map.getCenter());
  xOffset = initialCenter.x - currentCenter.x;
  yOffset = initialCenter.y - currentCenter.y;

  mapOffsetY = map.getContainer().offsetTop;
  mapOffsetX = map.getContainer().offsetLeft;
}

function calculateYAdjust(y, approxHeight) {
  var spaceBelow = h - (y + yOffset + mapOffsetY); 
  //var spaceBelow = map.getContainer().style.height - (y + yOffset);
  return (spaceBelow < approxHeight + 20) ? approxHeight : 0;
}

function calculateXAdjust(x, approxWidth) {
  var spaceToRight = w - (x + xOffset + mapOffsetX);
  return (spaceToRight < approxWidth + 20) ? approxWidth + 20 : 0;
}

/**
 * Estimated max height = 200px, width = 160px
 * 
 * param bp                 BoatPosition object
 * param bd                 BoatData object
 * param x                  x coordinate of info window's top left corner
 * param y                  y coordinate of info window's top left corner
 */
function PositionInfoWindow(bp, bd, x, y) {
  
  updateOffsets();
  
  /*
  //alert('yAdjust=' + yAdjust + ',spaceBelow=' + spaceBelow + ',h=' + h + ',y=' + y + ',yOffset=' + yOffset + ',mapOffsetY=' + mapOffsetY);
  */
  
  var yAdjust = calculateYAdjust(y, 200);
  var xAdjust = calculateXAdjust(x, 160);
  var yFinal = yOffset + mapOffsetY + y + 17 - yAdjust;
  var xFinal = xOffset + mapOffsetX + x + 17 - xAdjust;
  
  var iw = new GenericInfoWindow(xFinal, yFinal);
  iw.div.setAttribute('id', 'position_info_window');
  
  // header (boat symbol + name)
  var img = document.createElement('img');
  img.src = 'img/symbols/' + bd.symbol.neImage;
  iw.header.appendChild(img);
  iw.header.appendChild(document.createTextNode(unescape(bd.name)));
  
  // body (lat, lon, cog, sog, tstamp)
  
  if (bd.status != 'Active') {
    iw.body.appendChild(document.createTextNode(bd.status));
    iw.body.appendChild(document.createElement('br'));
    iw.body.appendChild(document.createElement('br'));    
  }
  
  
  //iw.body.appendChild(document.createTextNode('Lat: ' + Math.round(bp.lat * 10000) / 10000));
  iw.body.appendChild(document.createTextNode('Lat: ' + toDegMinSecPos(bp.lat)));
  iw.body.appendChild(document.createElement('br'));
  //iw.body.appendChild(document.createTextNode('Lon: ' + Math.round(bp.lon * 10000) / 10000));
  iw.body.appendChild(document.createTextNode('Lon: ' + toDegMinSecPos(bp.lon)));
  iw.body.appendChild(document.createElement('br'));
  iw.body.appendChild(document.createTextNode('Course: ' + bp.cog + ' degrees'));
  iw.body.appendChild(document.createElement('br'));
  iw.body.appendChild(document.createTextNode('Speed: ' + (Math.round(bp.sog * 0.8690 * 10) / 10) + ' knots'));
  iw.body.appendChild(document.createElement('br'));
  iw.body.appendChild(document.createElement('br'));  
  iw.body.appendChild(document.createTextNode('Updated:'));
  iw.body.appendChild(document.createElement('br'));
  iw.body.appendChild(document.createTextNode(bp.tstamp));
  
  if (!bp.current) {
    iw.body.appendChild(document.createElement('br'));
    iw.body.appendChild(document.createElement('br'));    
    iw.body.appendChild(document.createTextNode('Current position not available'));
  }
    
  return iw.div;
}

/**
 * param cp                 Checkpoint object
 * param x                  x coordinate of info window's top left corner
 * param y                  y coordinate of info window's top left corner
 */
function CheckpointInfoWindow(cp, x, y) {

  
  //var iw = new GenericInfoWindow(xFinal, yFinal);
  var iw = new GenericInfoWindow(0, 0);
  iw.div.setAttribute('id', 'checkpoint_info_window');
  
  // header ('Start', 'Finish' or 'Checkpoint #n.')
  var start = false;
  var finish = false;

  var approxHeight = 0;
  if (checkpoints && checkpoints.length > 0) {
    //if (cp.no == checkpoints[0].no) {
    if (cp.no == '0') {
      // start
      start = true;
      approxHeight = 100;
    //} else if (cp.no == checkpoints[checkpoints.length - 1].no) {
    } else if (cp.no == '1000') {
      // finish
      finish = true;
    }
  }
  
  var headerText = 'Checkpoint ' + cp.no + '.';
  if (start) {
    headerText = 'Start';
  } else if (finish) {
    headerText = 'Finish';
  }
  
  iw.header.appendChild(document.createTextNode(headerText));
  var courseName = document.createElement('span');
  courseName.style.font = 'normal 10px Verdana, sans-serif';
  courseName.innerHTML = '<br  />' + cp.courseName;
  iw.header.appendChild(courseName);

  // body (name, lat, lon);
  iw.body.appendChild(document.createTextNode(unescape(cp.name)));
  iw.body.appendChild(document.createElement('br'));
  iw.body.appendChild(document.createTextNode('Latitude: ' + toDegMinSecPos(cp.lat)));
  
  iw.body.appendChild(document.createElement('br'));
  iw.body.appendChild(document.createTextNode('Longitude: ' + toDegMinSecPos(cp.lon)));
  
  if (cp.no > 0) {
    iw.body.appendChild(new InfoWindowSeparator());
  
    //iw.body.appendChild(document.createTextNode(cp.passedBoats + ' teams have passed'));
  
    // show top 5
    if (cp.passedBoats > 0) {
      iw.body.appendChild(document.createTextNode(cp.passedBoats + ' teams have passed'));
      iw.body.appendChild(new InfoWindowSeparator());
      iw.body.appendChild(document.createTextNode('Top 5:'));
      iw.body.appendChild(document.createElement('br'));
 
      for (i = 0; i < 5; i++) {
        var topTxt = (i + 1) + '. ';
        if (cp.firstOnes.length >= i && cp.firstOnes[i]) {
          topTxt += unescape(cp.firstOnes[i]);
        } else {
          topTxt += '-';
        }
        iw.body.appendChild(document.createTextNode(topTxt));
        iw.body.appendChild(document.createElement('br'));  
      }
      approxHeight = 250;
    } else {
      approxHeight = 120;
    }
  }

  updateOffsets();
  var yAdjust = calculateYAdjust(y, approxHeight);
  var xAdjust = calculateXAdjust(x, 160);
  
  var xFinal = xOffset + map.getContainer().offsetLeft + x + 17 - xAdjust;
  var yFinal = yOffset + map.getContainer().offsetTop + y + 17 - yAdjust;
  
  iw.div.style.left = xFinal + 'px';
  iw.div.style.top = yFinal + 'px';
  
  return iw.div;
}

function InfoWindowSeparator() {
  var div = document.createElement('div');
  div.className = 'info_window_separator';
  div.innerHTML = '&nbsp;';
  return div;
}
/**
 * param bd					BoatData object
 * param x                  x coordinate of info window's top left corner
 * param y                  y coordinate of info window's top left corner
 */
function TeamInfoWindow(bd, x, y) {

  var finalX = x + 50;
  var finalY = y;

  var iw = new GenericInfoWindow(finalX, finalY);
  iw.div.setAttribute('id', 'team_info_window');
  
  var img = document.createElement('img');
  img.src = 'img/symbols/' + bd.symbol.neImage;
  img.style.marginRight = '10px';
  iw.header.appendChild(img);
  iw.header.appendChild(document.createTextNode(unescape(bd.name)));
  
  iw.body.appendChild(document.createTextNode(unescape(bd.skipper)));
  iw.body.appendChild(document.createElement('br'));
  iw.body.appendChild(document.createTextNode(unescape(bd.description)));
  iw.body.appendChild(document.createElement('br'));
  iw.body.appendChild(document.createElement('br'));
  
  // link to external info
  if (bd.link != '') {
    var a = document.createElement('a');
    a.href = bd.link;
    a.innerHTML = 'More info';
    iw.body.appendChild(a);
    iw.body.appendChild(document.createElement('br'));
    iw.body.appendChild(document.createElement('br'));    
  }
  
  // add to favourites
  var fav = document.createElement('div');
  fav.style.marginBottom = '5px';
  
  fav.innerHTML = '<input type="checkbox" id="fav" onchange="setFavourite(\'' +
    bd.id + '\');" /> <label for="fav">Add to favourites</label>';
  
  //iw.body.appendChild(fav);
  
  // [close]
  var span = document.createElement('span');
  span.innerHTML = '[<a href="javascript:hideTeamInfoWindow()">close</a>]';
  /*var a = document.createElement('a');
  a.className = 'info_window_link';
  a.onmouseover = function() {
    document.body.style.cursor="pointer";
  }
  a.onmouseout = function() {
    document.body.style.cursor="default";
  }
  a.onclick = function() {
    document.body.removeChild(document.getElementById("team_info_window"));
    document.body.style.cursor="default";   
  };
  a.appendChild(document.createTextNode('[close]'));
  iw.body.appendChild(a);*/
  iw.body.appendChild(span);
  
  return iw.div;
}

