var peaksInfo;
var rightClickX;
var rightClickY;
var requestedBearing;

function getPeaksAtBearing(){
  requestedBearing = Math.round(bearing*10)/10;
  $.post("getpeaksbybearing.php",{ bearing: bearing, latitude: plat, longitude: plong }, function(data){parsePeaksAtBearing(data);},"xml");
}

function parsePeaksAtBearing(xml){
  peaksInfo = new Array();
  var text = '<DIV id="peakselect" >Bearing: ' + requestedBearing + '<br><br><form name="peakselectbox"><p><select name="pk" size="1">';
  var optiontext;
  var i = 0;
  $('peak',xml).each(function(){
    value = $(this).find("bivouac_id").text();
    name = $(this).find("name").text();
    elev = $(this).find("elev").text();
    dist = $(this).find("dist").text();
    bearing = $(this).find("bearing").text();
    peaksInfo[i] = new peakInfo(value, name, elev, dist, bearing);
    i++;
  });
  peaksInfo.sort(sortPeakInfo);
  for(i=0; i<peaksInfo.length; i++){
    optiontext = peaksInfo[i].name + " - " + peaksInfo[i].elev + " meters - " + peaksInfo[i].dist + " km - " + peaksInfo[i].bearing + " deg";
    text = text + '<option value="' + peaksInfo[i].value + '">' + optiontext + '</option>';
  }
  text = text + '</select></p></form>';
  text = text + '<INPUT TYPE="button" NAME="addpeakbutton" VALUE="Add peak" onClick="setPanPeak();">';
  text = text + '<INPUT TYPE="button" NAME="cancelpeakbutton" VALUE="Cancel" onClick="cancelPanPeak();"></DIV>';
  $("#peakselect").replaceWith(text).css({'visibility':'visible'});
}

function peakInfo( id, name, elev, dist, bearing){ 
    this.id = id; 
    this.name = name; 
    this.elev = elev; 
    this.dist = dist; 
    this.bearing = bearing;
} 

function sortPeakInfo(a,b){
  return (parseInt(a.dist) < parseInt(b.dist))? -1 : 1;
}

function setPanPeak(){
  var peakid = peaksInfo[document.peakselectbox.pk.options.selectedIndex].id;
  var dist = peaksInfo[document.peakselectbox.pk.options.selectedIndex].dist;
  rightClickX -= (positionx + viewportx);
  if (rightClickX > imagewidth) rightClickX -= imagewidth;
  rightClickY -= (positiony + viewporty);
  rightClickX /= zoomfactor;
  rightClickY /= zoomfactor;
  $.post("setpanpeak.php",{ pan_id: panid, peak_id: peakid, x: rightClickX, y: rightClickY, dist: dist}, function(data){setPanPeakCallback(data);}, "xml");
}

function cancelPanPeak(){
  var text = '<DIV id="peakselect" ></DIV>';
  $("#peakselect").replaceWith(text).css({'visibility':'hidden', 'right':text});
}

function setPanPeakCallback(xml){
  $.post("panpeaks.php",{ panid: panid }, function(data){setpanpeaks(data);}, "xml");
  var text = '<DIV id="peakselect" ></DIV>';
  $("#peakselect").replaceWith(text).css({'visibility':'hidden', 'right':text});
}

function removePanPeak(){
  $.post("removepanpeak.php",{ panid: panid, panpeakid: panpeakid}, function(data){removePanPeakCallback(data);}, "xml");
}

function removePanPeakCallback(xml){
  $.post("panpeaks.php",{ panid: panid }, function(data){setpanpeaks(data);}, "xml");
}

function setNorthInit(){
  rightClickOffsetX = icursorx - positionx - viewportx;
  var text = '<DIV id="peakselect" ><input type="text" size="20" value = "" id="peakid">';
  text = text + '<INPUT TYPE="button" NAME="setNorth" VALUE="Set north" onClick="setNorth();">';
  text = text + '<INPUT TYPE="button" NAME="cancel" VALUE="Cancel" onClick="cancelSetNorth();"></DIV>';
  $("#peakselect").replaceWith(text).css({'visibility':'visible'});
}

function setNorth(){
  var peak = document.getElementById("peakid").value;
  rightClickX -= (positionx + viewportx);
  $.post("northcalc.php",{ panid: panid, peakid: peakid, panpeakid: peak, offset: rightClickX }, function(data){setNorthCallback(data);}, "xml");
  $("#peakselect").css({'visibility':'hidden'});
}

function cancelSetNorth(){
  $("#peakselect").css({'visibility':'hidden'});
}

function setNorthCallback(xml){
  northposition = parseInt( $("north", xml).text() );
}

function setHorizonInit(){
  rightClickOffsetX = icursorx - positionx - viewportx;
  var text = '<DIV id="peakselect" ><input type="text" size="20" value = "" id="horizon">';
  text = text + '<INPUT TYPE="button" NAME="setHorizon" VALUE="Set horizon" onClick="setHorizon();">';
  text = text + '<INPUT TYPE="button" NAME="cancel" VALUE="Cancel" onClick="cancelSetHorizon();"></DIV>';
  $("#peakselect").replaceWith(text).css({'visibility':'visible'});
}

function setHorizon(){
  var horizon = document.getElementById("horizon").value;
  $.post("sethorizon.php",{ panid: panid, horizon: horizon }, function(data){setHorizonCallback(data);}, "xml");
  $("#peakselect").css({'visibility':'hidden'});
}

function cancelSetHorizon(){
  $("#peakselect").css({'visibility':'hidden'});
}

function setHorizonCallback(xml){
  horizon = parseInt( $("horizon", xml).text() );
}

function viewA(){
  if(panpeakid == 0) return;
  $.post("panpeaklocation.php",{ panpeakid: panpeakid }, function(data){createViewA(data);}, "xml");
}

function createViewA(xml){
  var lat = parseFloat( $("lat", xml).text() );
  var long = parseFloat( $("long", xml).text() );
  var distance = parseFloat(panpeakdistance) * 1000.0 / 2.0;
  setGELookAt(lat, long, 3000.0, 88.0, distance, bearing);
}

function viewB(){
  if(panpeakid == 0) return;
  $.post("panpeaklocation.php",{ panpeakid: panpeakid }, function(data){createViewB(data);}, "xml");
}

function createViewB(xml){
  var lat = parseFloat( $("lat", xml).text() );
  var long = parseFloat( $("long", xml).text() );
  setGELookAt(lat, long, 3000.0, 88.0, 3000.0, bearing);
}

function viewC(){
  $.post("panpeaklocation.php",{ panpeakid: peakid }, function(data){createViewC(data);}, "xml");
}

function createViewC(xml){
  var lat = parseFloat( $("lat", xml).text() );
  var long = parseFloat( $("long", xml).text() );
  setGECamera(lat, long, 85.0, 3000.0, bearing);
}




