//****************************************************
//                         Global Variables
//****************************************************
var map = null;                           // GMap2 object
var tpIcon;
var piIcon;
var mode = 'add';
var pts = new Array();
var trails = new Array();
var newPoint;
var newTrail = new trail(null);
var isPublic = true;

//****************************************************
//                         Object definitions
//****************************************************

function point(p)
{
   if(p)
   {
      // These first variables correspond directly to fields in the database
      this.lat  = p.getAttribute("lat") * 1;
      this.lng  = p.getAttribute("lng") * 1;
      this.type = p.getAttribute("type");
      this.name = p.getAttribute("name");
      this.id   = p.getAttribute("id") * 1;

      this.tText = '<div class="marker"><h1>'+this.name+'</h1></div>';
      var hover = this.id + ': ' + this.name;
      this.overlay = createPMarker(new GLatLng(this.lat, this.lng), this.tText, { title:hover, icon: tpIcon, draggable: true });
   }
   else
   {
      this.lat    = 0;
      this.lng    = 0;
      this.type   = 'tp';
      this.name   = 'New Trail Point';
      this.id     = 0;
      this.tText  = '<div class="marker"><h1>'+this.name+'</h1></div>';
      this.overlay = null;
   }

   this.onMap = false;
   this.onTrail = false;
   this.dist = 0;

   this.addToMap = addToMap;
   this.removeFromMap = removeFromMap;
   this.addToSelect = addToSelect;
}

function addToMap()
{
   if(this.overlay)
   {
      if(!this.onMap)
      {
         map.addOverlay(this.overlay);
         this.onMap = true;
      }
   }

   return this.onMap;
}

function removeFromMap()
{
   if(this.overlay)
   {
      if(this.onMap)
      {
         map.removeOverlay(this.overlay);
         this.onMap = false;
      }
   }

   return this.onMap;
}

function sortByDist(a, b)
{
   return ((a.dist < b.dist) ? -1 : ((a.dist > b.dist) ? 1 : 0));
}

function sortByID(a, b)
{
   return ((a.id < b.id) ? -1 : ((a.id > b.id) ? 1 : 0));
}

//**********************************************************************************

function trail(t)
{
   this.points = new Array();
   this.onMap = false;
   this.isHilite = false;

   if(t)
   {
      this.name   = t.getAttribute("name");
      this.id     = t.getAttribute("id") * 1;
      this.length = t.getAttribute("length");

 		var p = t.getAttribute("points").split(';');
      var len = p.length;
      var linePoints = new Array();
      for(var i=0; i<len; i++)
      {
         this.points[i] = p[i] * 1;
         var ind = findIndexByID(this.points[i]);
         linePoints[i] = new GLatLng(pts[ind].lat, pts[ind].lng);
      }

      this.overlay = new GPolyline(linePoints, "#ff0000", 4, 1);
   }
   else
   {
      this.name    = 'New Trail';
      this.id      = 0;
      this.length  = 0;
      this.overlay = null;
   }

   this.addToMap = addToMap;
   this.removeFromMap = removeFromMap;
   this.addPoint = addPoint;
   this.drawTrail = drawTrail;
}

function addPoint(id)
{
   this.points.push(id * 1);
}

function drawTrail()
{
   var len = this.points.length;
   if(len > 1)
   {
      this.removeFromMap();
      var linePoints = new Array();
      for(var i=0; i<len; i++)
      {
         var ind = findIndexByID(this.points[i]);
         linePoints[i] = new GLatLng(pts[ind].lat, pts[ind].lng);
      }

      this.overlay = new GPolyline(linePoints, "#ff0000", 4, 1);
      this.addToMap();
   }
}

function addToSelect()
{
   var x = document.getElementById("aPoints");

   var y = document.createElement('option');
   y.text = this.id + ': ' + this.name;
   y.value = this.id;
   try       { x.add(y, null); }
   catch(ex) { x.add(y);       }
}

//****************************************************
//                Kennel Object Support functions
//****************************************************
function findIndexByName(name)
{
   var len = pts.length;
   for(var i=0; i<len; i++)
   {
      if(pts[i].name == name)
         return i
   }

   return -1;
}

function findIndexByID(id)
{
   var len = pts.length;
   for(var i=0; i<len; i++)
   {
      if(pts[i].id == id)
         return i
   }

   return -1;
}

function findIndexByLatLng(lat, lng)
{
   var len = pts.length;
   for(var i=0; i<len; i++)
   {
      if( (pts[i].lat == lat) && (pts[i].lng == lng) )
         return i
   }

   return -1;
}

function removeAllMarkers()
{
   var len = pts.length;

   for(var i=0; i<len; i++)
      pts[i].removeFromMap();
}

function removeAllTrails()
{
   var len = trails.length;

   for(var i=0; i<len; i++)
      trails[i].removeFromMap();
}

function removeTypeMarkers(t)
{
   var len = pts.length;

   for(var i=0; i<len; i++)
      if(pts[i].type == t)
         pts[i].removeFromMap();
}

function addToTPoints()
{
   var ap = document.getElementById("aPoints");
   var tp = document.getElementById("tPoints");
   var i = ap.selectedIndex;

   y = document.createElement('option');
   y.text  = ap.options[i].text;
   y.value = ap.options[i].value * 1;
   try       { tp.add(y, null); }
   catch(ex) { tp.add(y);       }

   var ind = findIndexByID(y.value);
   pts[ind].onTrail = true;
   newTrail.addPoint(y.value);
   newTrail.drawTrail();
   reorderAPoints(y.value);
}

function removeFromTPoints()
{
   var ap = document.getElementById("aPoints");
   var tp = document.getElementById("tPoints");
   var i = tp.selectedIndex;

   while(i >= 0)
   {
      y = document.createElement('option');
      y.text  = tp.options[i].text;
      y.value = tp.options[i].value;
      try       { ap.add(y, null); }
      catch(ex) { ap.add(y);       }

      tp.remove(i);
      i = tp.selectedIndex;
   }
}

function reorderAPoints(id)
{
   var len = pts.length;

   if(id)
   {
      var ind = findIndexByID(id);
      var pt = pts[ind].overlay.getPoint();

      // Calculate distance from the given point to all other points
      for(var i=0; i<len; i++)
      {
         if(pts[i].overlay)
            pts[i].dist = Math.floor(pt.distanceFrom(pts[i].overlay.getPoint()) * 3.2808399 );
         else
            pts[i].dist = 15000;
      }
      pts.sort(sortByDist);
   }

   // Remove all points from aPoints
   removeAllOptions("aPoints");

   // Add all the points back to aPoints
   len = pts.length;
   for(var i=0; i<len; i++)
   {
      if(id == 0)
         pts[i].onTrail = false;
      if(pts[i].dist > 0)
         pts[i].addToSelect();
   }
}

function movePoint()
{
   var pID = document.getElementById("pID").value * 1;
   if(pID == 0)
      newPoint.overlay.setLatLng(new GLatLng(document.getElementById("lat").value, document.getElementById("lng").value));
   else
   {
      var ind = findIndexByID(pID);
      pts[ind].lat = document.getElementById("lat").value * 1;
      pts[ind].lng = document.getElementById("lng").value * 1;
      pts[ind].overlay.setLatLng(new GLatLng(pts[ind].lat, pts[ind].lng));
   }
}

//****************************************************
//                         Other Functions
//****************************************************
function init()
{
   if(GBrowserIsCompatible())
   {
      isPublic = false;
      var pt = new GLatLng(40.4360054452,-86.911708832);
      map = new GMap2(document.getElementById("mapDIV"));
      map.setCenter(pt, 4);
      map.setUIToDefault();

      // Need to define icons here
      //ptIcon = new GIcon(G_DEFAULT_ICON, "http://www.dogsledrides.com/images/hunting.png");
      //ptIcon.shadow = "http://www.dogsledrides.com/images/shadow.png";

      loadPoints();

      newPoint = new point(null);
      newPoint.name = "A new Point";
      newPoint.overlay = createPMarker(new GLatLng(39, -105), newPoint.name, { title:"NewPoint", draggable: true });
      GEvent.addListener(map, "click", function(o, l, ol) {
         // Map clicked, find out where, move kLoc to that location, show coords in save form
         if(mode == 'add')
         {
            if(l)
            {
               newPoint.addToMap();
               newPoint.overlay.setLatLng(l);
               document.getElementById("pID").value = newPoint.id;
               document.getElementById("pName").value = newPoint.name;
            }
         }
         if(l)
         {
            document.getElementById("lat").value = l.lat();
            document.getElementById("lng").value = l.lng();
         }
      });
   }
}

function initPublic()
{
   if(GBrowserIsCompatible())
   {
      var pt = new GLatLng(40.4360054452,-86.911708832);
      map = new GMap2(document.getElementById("mapDIV"));
      map.setCenter(pt, 4);
      map.setUIToDefault();
    
      loadPoints();
   }
}

function loadPoints()
{
   var args = 'q=lp&o=xml';

	asProcess('/winterpark/admin/dsr_util_admin.php', args, 'Loading all kennels...', handleLoadPoints, "messageDIV");
}

function handleLoadPoints()
{
	if(xmlHttp.readyState == 4)
	{
		var xmlDoc = xmlHttp.responseXML;
 		var pDoc = xmlDoc.getElementsByTagName("point");
      var len = pDoc.length;
      var bounds = new GLatLngBounds();
      pts = new Array();
      for(var i=0; i<len; i++)
      {
         pts[i] = new point(pDoc[i]);
         if(!isPublic)
         {
            pts[i].addToMap();
            pts[i].addToSelect();
         }
         bounds.extend(pts[i].overlay.getLatLng());
      }

      if(!bounds.isEmpty())
      {
         map.setZoom(map.getBoundsZoomLevel(bounds));
         map.setCenter(bounds.getCenter());
      }
      loadTrails();
   }
}

function loadTrails()
{
   var args = 'q=lt&o=xml';

	asProcess('/winterpark/admin/dsr_util_admin.php', args, 'Loading all trails...', handleLoadTrails, "messageDIV");
}

function handleLoadTrails()
{
	if(xmlHttp.readyState == 4)
	{
		var xmlDoc = xmlHttp.responseXML;
 		var tDoc = xmlDoc.getElementsByTagName("trail");
      var len = tDoc.length;
      trails = new Array();
      for(var i=0; i<len; i++)
      {
         trails[i] = new trail(tDoc[i]);
         trails[i].addToMap();
      }
      showTrailList();
   }
}

function showTrailList()
{
   var l = 0;
   var lText = '';
   var out = '<ol>';
   var len = trails.length;

   for(var i=0; i<len; i++)
   {
      if(trails[i].length < 322)
      {
         l = Math.round(trails[i].length * 3.2808399);
         lText = l + " feet";
      }
      else
      {
         l = Math.round(10 * trails[i].length / 1609.344) / 10;
         lText = l + " miles";
      }
      out += '<li onclick="changeTrailColor(\'' + trails[i].id + '\')">' + trails[i].name + ', ' + lText + '</li>';
   }

   out += "</ol>";
   document.getElementById("makeTripDIV").innerHTML = out;
}

function changeTrailColor(tID)
{
   var len = trails.length;

   for(var i=0; i<len; i++)
   {
      if(trails[i].id == tID)
      {
         trails[i].isHilite = true;
         trails[i].overlay.setStrokeStyle({ color:"#ff00ff", weight: 4, opacity: 1 });
      }
      else
      {
         if(trails[i].isHilite)
         {
            trails[i].isHilite = false;
            trails[i].overlay.setStrokeStyle({ color:"#ff0000", weight: 4, opacity: 1 });
         }
      }
   }
}

function handleAddPoint()
{
	if(xmlHttp.readyState == 4)
	{
		var xmlDoc = xmlHttp.responseXML;
 		var pDoc = xmlDoc.getElementsByTagName("point");
      var l = pts.length;
      pts[l] = new point(pDoc[0]);
      pts[l].addToMap();
      pts[l].addToSelect();
      newPoint.removeFromMap();
   }
}

function saveTrail()
{
   var args = 'q=st&o=xml';
   args += '&n=' + document.getElementById("tName").value;
   args += '&l=' + newTrail.overlay.getLength();
   args += '&i=';

   var len = newTrail.points.length;
   for(var i=0; i<len; i++)
      args += newTrail.points[i] + ';';

	asProcess('/winterpark/admin/dsr_util_admin.php', args, 'Saving all trails...', handleSaveTrail, "messageDIV");
}

function handleSaveTrail()
{
	if(xmlHttp.readyState == 4)
	{
      // Remove all points from tPoints
      removeAllOptions("tPoints");
      pts.sort(sortByID);
      reorderAPoints(0);
      newTrail = new trail(null);
   }
}

function reloadPoints()
{
   removeAllMarkers();
   removeAllTrails();
   loadPoints();
}

function changeAction()
{
   mode = getSelectValue("action");
   document.getElementById("actionButt").innerHTML = mode + ' Point';
}

function makeTrailMode()
{
   mode = 'trail';
   document.getElementById("actionButt").innerHTML = 'Make Trail';
}

function doAction()
{
   switch(mode)
   {
      case 'add':
         var pID = document.getElementById("pID").value;
         if(confirm("Are you sure you want to add this point?"))
         {
            var args = 'q=ap&o=xml';
            args += '&lat='+document.getElementById("lat").value;
            args += '&lng='+document.getElementById("lng").value;
            args += '&n='+document.getElementById("pName").value;
            args += '&t='+document.getElementById("pType").value;
         	asProcess('/winterpark/admin/dsr_util_admin.php', args, 'Editing trail point...', handleAddPoint, "messageDIV");
         }
         break;
      case 'edit':
         var pID = document.getElementById("pID").value;
         var ind = document.getElementById("ind").value;
         if(confirm("Are you sure you want to edit point '"+pID+"'"))
         {
            var args = 'q=ep&id='+pID;
            args += '&lat='+document.getElementById("lat").value;
            args += '&lng='+document.getElementById("lng").value;
            args += '&n='+document.getElementById("pName").value;
            args += '&t='+document.getElementById("pType").value;
         	asProcess('/winterpark/admin/dsr_util_admin.php', args, 'Editing trail point...', handleASProcess, "messageDIV");
         }
         break;
      case 'delete':
         var pID = document.getElementById("pID").value;
         var ind = document.getElementById("ind").value;
         if(confirm("Are you sure you want to delete point '"+pID+"'"))
         {
            var args = 'q=dp&id='+pID;
         	asProcess('/winterpark/admin/dsr_util_admin.php', args, 'Deleting trail point...', handleASProcess, "messageDIV");
            pts[ind].removeFromMap();
            pts.splice(ind, 1);
            document.getElementById("lat").value = '';
            document.getElementById("lng").value = '';
            document.getElementById("ind").value = '';
            document.getElementById("pID").value = '';
            document.getElementById("pName").value = '';
            document.getElementById("pType").value = '';
         }
         break;
   }
}

function toggleMarkers(type)
{
   var len = pts.length;

   for(var i=0; i<len; i++)
   {
      if(pts[i].onMap)
         pts[i].removeFromMap();
      else
         pts[i].addToMap();
   }
}

function toggleTrails()
{
   var len = trails.length;

   for(var i=0; i<len; i++)
   {
      if(trails[i].onMap)
         trails[i].removeFromMap();
      else
         trails[i].addToMap();
   }
}

function createPMarker(point, html, opts)
{
   var marker = new GMarker(point, opts);
   GEvent.addListener(marker, "click", function(l)
   {
      document.getElementById("lat").value = l.lat();
      document.getElementById("lng").value = l.lng();
      var ind = findIndexByLatLng(l.lat(), l.lng());
      document.getElementById("ind").value = ind;
      document.getElementById("pID").value = pts[ind].id;
      document.getElementById("pName").value = pts[ind].name;
      document.getElementById("pType").value = pts[ind].type;
//      marker.openInfoWindowHtml(html);
   });

   GEvent.addListener(marker, "dragend", function(l) {
      var pID = document.getElementById("pID").value * 1;
      if(pID == 0)
      {
         newPoint.lat = l.lat();
         newPoint.lng = l.lng();
      }
      else
      {
         var ind = findIndexByID(pID);
         pts[ind].lat = l.lat();
         pts[ind].lng = l.lng();
      }
      document.getElementById("lat").value = l.lat();
      document.getElementById("lng").value = l.lng();
   });

   return marker;
}
