var hCaptures={};
function fRunCaptures(h)
{
 var aTxt=[];
 h=String(h);
 if(hCaptures[h]==null)
 {
  if( fRunCaptures.Idx == String(h-1) )
  {
   fRunCaptures.Complete=true;
  }
  return;
 }
 fRunCaptures.Idx=h;
 hCaptures[h].fSetup();
 hCaptures[h].fDraw();
}
fRunCaptures.Idx="1";
fRunCaptures.Complete=false;
fRunCaptures.fBreakDownAll = function()
{
 fRunCaptures.Idx="1";
 fForEachCaptures(function(cap){cap.fBreakDown();});
 USGMaps.HelperPanel.fClose(USGMaps);
 hCaptures={};
}

function fForEachCaptures(func)
{
 for(var c in hCaptures)
  func(hCaptures[c]);
}
function fCancelCurrentHoleCapture()
{
 var h=parseInt(fRunCaptures.Idx);
 var hole=hCaptures[h].Hole;
 do
 {
  if(hole!=hCaptures[String(h)].Hole)
   break;
  hCaptures[String(h)].fBreakDown();
  h--;
 }
 while(h > 0);
 fRunCaptures(1);
}
function clsCapture(idx,hobject)
{
 this.Idx=idx;
 this.Marker=hobject;
 this.afBreakDowns=[];
 this.fBreakDown=function()
 {
  for(var f in this.afBreakDowns)
   this.afBreakDowns[f]();
  this.afBreakDowns=[];
 }
 this.fAddBreakDown=function(f)
 {
  this.afBreakDowns.push(f);
 }
 this.fAddMouseClickEvent=function(afuncs)
 {
  var hcapture=this;
  this.MouseClickID=USGMaps.event.addListenerOnce(USGMaps.map,"click",function(ev)
   {
    hcapture.Marker.fSetPosition(ev.latLng);
    for(var f=0;f<afuncs.length;f++)
     afuncs[f](ev);
   }
  );
  this.fAddBreakDown(function(){USGMaps.event.removeListener(hcapture.MouseClickID);hcapture.MouseClickID=null;});
 }
 this.fAddMouseMoveEvent=function(afuncs)
 {
  var hcapture=this;
  this.MouseMoveID=USGMaps.event.addListener(USGMaps.map,"mousemove",function(ev)
   {
    clsCapture.prototype.hLatLng.lat=ev.latLng.lat();
    clsCapture.prototype.hLatLng.lng=ev.latLng.lng();
    for(var f=0;f<afuncs.length;f++)
     afuncs[f](ev);
   }
  );
  this.fAddBreakDown(function(){USGMaps.event.removeListener(hcapture.MouseMoveID);hcapture.MouseMoveID=null;});
 }
 return this;
}
clsCapture.prototype.hLatLng={lat:0,lng:0};
function clsCaptureHole(type,idx,hobject)
{
 this.Capture=type;
 this.clsCapture=clsCapture;
 this.clsCapture(idx,hobject);
 this.dvMsgArea=fGD("spnCourseEditsMsg");
 this.fSetup=function()
 {
  var hcapture=this;
  USGMaps.hCourse.fActiveShot(this.Marker.Shot);
  if(this.Marker.Shot==1)
   USGMaps.hCourse.fActiveTee(this.Marker.Tee);

  if(type!="Green")
  {
   USGMaps.hCourse.ActiveHole=parseInt(hcapture.Marker.hOwner.Hole);
   this.fAddMouseClickEvent([function(ev){hcapture.fStandardMouseClickActivity()}]);
  }
  else
   this.fAddMouseClickEvent(
   [
    function(ev)
    {
     hcapture.Marker.fSetVisibility(true,USGMaps);
     hcapture.fBreakDown();
     hcapture.Marker.fBuildPolyline();
     if(hcapture.Marker.hOwner.Hole==USGMaps.hCourse.Holes)
     {
      //
      fGDs("btnCourseEditsSave").display="";
     }
     else
     {
      USGMaps.hCourse.ActiveHole=parseInt(hcapture.Marker.hOwner.Hole)+1;
      fRunCaptures(parseInt(hcapture.Idx)+1);
     }
    }
   ]);

  this.fAddMouseMoveEvent([function(ev)
  {
   hcapture.fDraw(clsCapture.prototype.hLatLng);

  }]);
  if(this.Marker.fAddListeners)
   this.Marker.fAddListeners(USGMaps);
 }
 this.fStandardMouseClickActivity=function()
 {
  this.Marker.fSetVisibility(true,USGMaps);
  this.fDraw();
  this.fBreakDown(1);
  fRunCaptures(parseInt(this.Idx)+1);
 }
 this.fLatLng=function()
 {
  return this.Marker.fLatLng();  
 }
 this.fDraw=function(hlatlng)
 {
  var aTxt=[];
  aTxt=this.Marker.fDrawCaptureMsg(aTxt,hlatlng);
  this.dvMsgArea.innerHTML=aTxt.join(" ");
  return aTxt.join(" ");
 }
 return this;
}
function fDebug(txt)
{
 if(fDebug.prototype.aDebug==null)
 {
  fDebug.prototype.aDebug=new Array();
  fDebug.prototype.OFF=false;
 } 
 if(fDebug.prototype.OFF)
  return;
 if(txt==null)
 {
  fDebug.prototype.aDebug=new Array("DEBUG WINDOW");
 }
 else 
 {
  if(typeof(txt)=="string")
   fDebug.prototype.aDebug.push(txt.replace(/\n/g,"<br>"));
  else if(typeof(txt)=="number")
   fDebug.prototype.aDebug.push("Number:"+txt);
  else if(typeof(txt)=="object")
  {
   fDebug.prototype.aDebug.push(typeof(txt));
   if(txt.length != null)
   {
    for(var t=0;t<txt.length;t++)
     fDebug.prototype.aDebug.push(txt[t]);
   }
   else
   {
    for(var t in txt)
     fDebug.prototype.aDebug.push(t+":"+txt[t]);
   }
  }
  else 
  {
   fDebug.prototype.aDebug.push("Oops:"+typeof(txt));
  }
 }
 fGD("dvDEBUG").innerHTML=fDebug.prototype.aDebug.join("<br>");
}
function fBuildCaptures(hcourse)
{
// fDebug("Starting Capture Build");
 fRunCaptures.Idx="1";
// USGMaps.map.setCenter(new USGMaps.LatLng(USGMaps.hCourse.Latitude,USGMaps.hCourse.Longitude));
 USGMaps.map.setMapTypeId(USGMaps.MapTypeId.SATELLITE);
 USGMaps.map.setZoom(16);
 var capturer=this;
 fRunCaptures.fBreakDownAll();
 USGMaps.HelperPanel.fAddListeners(USGMaps,
  function(ev,dv)
  {
   var c=hCaptures[fRunCaptures.Idx];
   var h=c.Marker.hOwner;
   c.Marker.fDrawCaptureMsg(null,clsCapture.prototype.hLatLng,dv,"<br>")
   return;
  }
 );
 USGMaps.HelperPanel._node.style.display=""; 
 fGDs("btnCourseEditsSave").display="none";
 var capture=0;
 with(USGMaps.hCourse)
 {
  var idx="";
  var hTEE;
  var hole=1;
  function fBuildHoleCaptures(capture,hole)
  {
   var hHOLE=hHoleInfo[hole];
   if(hHOLE._visible){fDebug("OOPS! the hole was already made :"+hole); return;}
   if(hHOLE.Par > 4)
   {
    for(var s=0;s<hHOLE._aShotMarkers.length;s++)
    {
     idx=String(++capture);
     hCaptures[idx]=new clsCaptureHole("Fairway",idx,hHOLE._aShotMarkers[s]);
    }
   }
   if(hHOLE.Par>3)
   {
    idx=String(++capture);
    hCaptures[idx]=new clsCaptureHole("Approach",idx,hHOLE._ApproachMarker);
   }
   idx=String(++capture);
   hCaptures[idx]=new clsCaptureHole("Green",idx,hHOLE._GreenMarker);
   return capture;
  }
  for(var hole in hHoleInfo)
  {
   var tees=Tees; // hCourse.Tees
   for(var tee in hTees)
   {
    if(tees-- < 0){fDebug("OOPS! Too many Tees :"+Tees); break;}
    hTEE=hTees[tee];
    if(hTEE._visible){fDebug("OOPS! TEE "+tee+" ALREADY MADE FOR Hole :"+hole); continue;}
    idx=String(++capture);
    hCaptures[idx]=new clsCaptureHole("Tees",idx,hTees[tee].hHoles[hole]._TeeMarker);
   }
   capture=fBuildHoleCaptures(capture,hole);
  }
 }
 fRunCaptures(1);
}

function fLoadHoleTeeYards(courseid)
{
// fGDs("dvDEBUG").display="";
 fDebug();
 fGD("dvDEBUG").style.width="450px";
 var view=USGMaps.ActiveView;
 var cid=String(courseid);
 if(cid == null)
 {
  fDebug("ERR CourseID :"+cid);
  fGDs("dvDEBUG").display="";
  return;
 }
 view.hCourseMarkers[cid].fLoadHoleTeeYards(USGMaps);
}
function fCreateCourseMarker(hsh)
{
 try
 {
  if(USGMaps.ActiveView.fCreateCourseMarker)
  {
   USGMaps.ActiveView.fCreateCourseMarker(USGMaps,hsh);
  }
 }
 catch(e)
 {
  fDebug(e.description);
 }
}
function fCreateCountMarker(hsh)
{
 try
 {
  if(USGMaps.ActiveView.fCreateCountMarker)
  {
   USGMaps.ActiveView.fCreateCountMarker(USGMaps,hsh);
  }
 }
 catch(e)
 {
  fDebug(e.description);
 }
}
function fCourseInfo(tab,cid)
{
 try
 {
  if(USGMaps.ActiveView.fCourseInfo)
  {
   USGMaps.ActiveView.fCourseInfo(tab,cid);
  }
 }
 catch(e)
 {
  fDebug(e.description);
 }
}
function fSearch(el)
{
 try
 {
  if(USGMaps.ActiveView.fSearch);
   USGMaps.ActiveView.fSearch(el);
 }
 catch(e)
 {
  fDebug(e.description);
 }
}
function fCourseListToLatLng(z,ll)
{
 var dv=fGDs("dvCourseList");
 dv.display="none";
 fGD("dvCourseListScroll").innerHTML="";
 USGMaps.map.setZoom(z);
 USGMaps.map.panTo(new USGMaps.LatLng(ll.lat,ll.lng));
}
// fAddToList is a called by clsCoursesView and clsCountMarker
function fAddToList(obj)
{
 if(obj)
  fAddToList.prototype.hLoaded[String(obj.CourseID)]=obj;
 if(obj==null)
 {
  var cls=fGD("dvCourseListScroll");
  var aTxt=[];
  for(var L in fAddToList.prototype.hLoaded)
  {
   obj=fAddToList.prototype.hLoaded[L];
   aTxt.push("<p class=bdr><a href='#' "
   +"onclick='fCourseListToLatLng(15,{lat:"+obj.Latitude+",lng:"+obj.Longitude+"});'"
   +">Zoom In</a> "
   +('<a href="#" onclick="USGMaps.fMapCourse('+obj.CourseID+','+obj.Latitude+','+obj.Longitude+');"'
   +' class="Tee-'+obj._Tee+'">&nbsp;'+(obj._Mapped>0?'Mapped':'Map')+'&nbsp;</a>')
   +"<b>"+obj.Name+"</b>"
   +"<br>Holes :<b>"+obj.Holes+"</b> Tees :<b>"+obj.Tees+"</b>"
   +" "+obj.Classification +" <b>"+obj.Phone+"</b>"
//   +(obj.hSponsor==null?"":"<br>Sponsored By:<a target='SPONSOR' href='"+obj.hSponsor.URL+"'>"+obj.hSponsor.Name
//   +"<br><img src='"+obj.hSponsor.Src+"' width='120' border=0></a>")
   +"</p>");
  }
  cls.innerHTML=aTxt.join("");
 }
}
fAddToList.prototype.iLoaded=0;
fAddToList.prototype.hLoaded={};
 
// fCourseList is called by clsCoursesView and clsCountMarker
function fCourseList(view,url,marker)
{
 var sw,overlayProjection=view.fGetProjection();
 var dv=fGD("dvCourseList");
 dv.style.display="";
 if(marker)
 {
  onclick=function()
  {
   fGD("dvCourseListScroll").innerHTML="";
   fGDs("dvCourseList").display="none";
   marker.setPosition(marker.GridBounds.getCenter());
  }
 }
 sw = overlayProjection.fromLatLngToContainerPixel(view.GMaps.map.getCenter());
 fAnchorPosition(dv,sw);
 fGD("dvCourseListScroll").innerHTML="";
 fAddToList.prototype.iLoaded=0;
 fAddToList.prototype.hLoaded={};
 fAjaxCall({async:true,url:url,fcallback:function(req,bufferstart)
  {
   if(this.LastbufferStart!=null)
   {
    eval(String(req.responseText).substr(this.LastbufferStart));
    this.LastbufferStart=null;
   }
   else
    eval(String(req.responseText).substr(bufferstart));
   fClientServerMsg();
   fAddToList();
}
  ,fcallback3:function(req,bufferstart)
{
   try
 {
    if(this.LastbufferStart!=null)
    {
     eval(String(req.responseText).substr(this.LastbufferStart));
     this.LastbufferStart=null;
 }
    else
     eval(String(req.responseText).substr(bufferstart));
   }
   catch(e)
   {
    fDebug("EH Last:"+this.LastbufferStart+", Start:"+bufferstart+", Length:"+String(req.responseText).substr(bufferstart).length);
    if(this.LastbufferStart==null)
     this.LastbufferStart=bufferstart;
//    fDebug("EH Text:"+String(req.responseText).substr(bufferstart,200))
   }
  }
 });
}
function fClientServerMsg(msg)
{
 var el=fGD("dvClientServer");
 if(msg)
 {
  el.innerHTML=msg;
  el.style.display="";
  if(fClientServerMsg.prototype.IntervalID)
   return;
  fClientServerMsg.prototype.IntervalID=window.setInterval(function(){if(el.style.display==""){el.style.display="none";}else{el.style.display="";}},220);
 }
 else
  {
  el.style.display="none";
  window.clearInterval(fClientServerMsg.prototype.IntervalID);
  fClientServerMsg.prototype.IntervalID=null;
  }
 return el;
}
fClientServerMsg.prototype.IntervalID=null;

