/**code for GForest Sim capabilities.  Started in 06/2008. Author: Paul Van Deusen, NCASI
 */
var simCenter=[]; //store sim circle centers and other info as JSON objects
var simArray=[];  //used mainly to clear last rival

//call circle drawing function by double clicking on map
//gforest detects that you are on the sim tab and calls this
function draw(pnt){
  if(mySecret!=theKey){
    alert("Please login, then re-submit.");
    userLogin();
    return;
  }
  isGForestData=false; //need new data if user makes another mill
  var givenRad = 50; //user defined radius
  var percent=100; //how much the mill takes
  var givenQuality = 40;        //how many nodes in the circle, 8 is a hex
  if(simCenter.length==0){ //target
    simArray=new Array();          //make sure its cleared when starting over
    givenRad=document.getElementById("radius1").value*1;
    var q1=eval([{"Lat":pnt.lat(), "Lon":pnt.lng(),"Rad":givenRad, "Type":"T", "Take":percent }]);
    simCenter.push(q1[0]);
    //set up query array to look like a circle query to GForest
     query=new Array();
     q1=eval([{"Lat":pnt.lat(), "Lon":pnt.lng(), "Type":'r', "Num":givenRad }]);
     query.push(q1[0]);
    drawCircle(pnt, givenRad, givenQuality,"#33FF99");
    
    var simIcon = new GIcon(G_DEFAULT_ICON);
    simIcon.image = "/google/icons/mills/mill2.gif";
    simIcon.infoWindowAnchor = new GPoint(8,13); 
    var describe="Target: radius="+givenRad;
    var markerOptions = { icon:simIcon, title: describe };
    var marker=new GMarker(pnt, markerOptions)
      map.addOverlay(marker);
    simArray.push(marker);


  } else{ //rivals
    percent=document.getElementById("rivalTake").value*1;
    givenRad=document.getElementById("radius2").value*1;
    var q1=eval([{"Lat":pnt.lat(), "Lon":pnt.lng(),"Rad":givenRad, "Type":"R", "Take":percent }]);
    simCenter.push(q1[0]);
    drawCircle(pnt, givenRad, givenQuality,"#FF6633");
    var simIcon = new GIcon(G_DEFAULT_ICON);
    simIcon.image = "/google/icons/mills/mill.gif";
    var describe="Rival: radius="+givenRad+", Percent take="+percent;
    var markerOptions = { icon:simIcon, title: describe };
    var marker=new GMarker(pnt, markerOptions)

      map.addOverlay(marker);
    simArray.push(marker);
  }

}/*end of draw function*/



function drawCircle(center, radius, nodes, liColor, liWidth, liOpa, fillColor, fillOpa)
{
  // Esa 2006
  //calculating km/degree
  var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
  var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;

  //Loop 
  var points = [];
  var step = parseInt(360/nodes)||10;
  for(var i=0; i<=360; i+=step)
    {
      var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() + 
			     (radius/lngConv * Math.sin(i * Math.PI/180)));
      points.push(pint);
    }
  fillColor = fillColor||liColor||"#0055ff";
  liWidth = liWidth||2;
  var poly = new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa);
  map.addOverlay(poly);
  simArray.push(poly);
}

//get rid of the last rival that was added
function clearLastRival(){
  if(simArray.length==0)return;
  map.removeOverlay(simArray[(simArray.length-1)]); //the icon
    simArray.pop();
    map.removeOverlay(simArray[(simArray.length-1)]); //the circle
  simArray.pop();
  simCenter.pop();
}

//this gets GForest to select data from the target mills circle
//as if its a standard mapQuery from a user who wants data from a circle
//The query array was set up in the draw function
function submitSim(caller){

  if(simCenter.length==0){
    alert("Select target and rival locations first? Suggestion: Double Click the map.");
    return;
  }
 var suffix=0;
  if(!isGForestSubmitOK){
    suffix=1;
  }

  submitTab=7;//so gforest knows who submitted this even if user changes tabs
 if(!isGForestData){
   //make this look like a standard circle data query to GForest
   
   getData(caller); //caller=sim   
 }
 
 return;
}/*end function*/

//after prepGForestData call this submits the sim job via the
//gforest.submitRjob function
function simJob(){
  isGForestSubmitOK=true;
  isGForestData=true;
  submitRjob("sim");
  submitTab=7;
}

//after prepGForestData call this submits the sim email job via the
//gforest.submitRjob function
function simEmailJob(){
  isGForestSubmitOK=true;
  isGForestData=true;
  submitRjob("simEmail");
  submitTab=7;
}

//this is the onComplete function after getData
function simJobPrep(){
  isGForestSubmitOK=true;
  prepGForestData("sim");
}
//This is the onComplete function after getData for email call
function simJobEmailPrep(){
  isGForestSubmitOK=true;
  prepGForestData("simEmail");
}
