/*
 * Course object file
 */
function Landmark() {
     this.itemId = null
     this.latitude = null;
     this.longitude = null;
     this.name = null; // tee, green
     this.customDesc = null;
          
     this.updateProps = function(marker, name) {
         if (marker != null) {
             this.latitude = marker.getLatLng().lat();
             this.longitude = marker.getLatLng().lng();
         }
         
         if (name != null)
             this.name = name;
     }
       
     this.getNodeInfo = function(lmNode) {
         this.latitude = XMLFindNodeText(lmNode, "lat");
         this.longitude = XMLFindNodeText(lmNode, "lng");
         this.name = XMLFindNodeText(lmNode, "name");
     }
     
     this.addXML = function(doc) {
        doc.pushNewNode("lm");
        doc.addTextNode("lat", this.latitude);
        doc.addTextNode("lng", this.longitude);
        doc.addTextNode("name", this.name);
        doc.popNode();
     }
     
     this.toString = function() {
         var s = "";
         s += "landmark lat = " + this.latitude +
              ", long = " + this.longitude + ", name = " + this.name + "\n";
         return s;
     }
}

function Hole(holeNumber) {
     this.holeNumber = holeNumber;
     this.par = 0;
     this.handicap = "";
     this.comment = "";
     this.landmarks = new Array();
     
     this.addLandmark = function() {
         var lm = new Landmark();
         this.landmarks.push(lm);
     }
     
     this.removeLandmark = function() {
         this.landmarks.pop();
     }
     
     this.updateLandmark = function(index, marker, name) {
         var lm = this.landmarks[index];
         lm.updateProps(marker, name);
     }
     
     this.getNodeInfo = function(holeNode) {
         this.holeNumber = XMLFindNodeText(holeNode, "holenum");
         this.par = XMLFindNodeText(holeNode, "par");
         this.handicap = XMLFindNodeText(holeNode, "handicap");
         this.comment = XMLFindNodeText(holeNode, "comment");
             
         var lmNodes = XMLGetNodeList(holeNode, "lm");
         this.landmarks = new Array();
         for (var i = 0; i < lmNodes.length; i++) {
             var lm = new Landmark();
             this.landmarks[i] = lm;
             lm.getNodeInfo(lmNodes[i]);
         }
     }
     
     this.addXML = function(doc) {
         doc.pushNewNode("hole");
         doc.addTextNode("holenum", this.holeNumber);
         doc.addTextNode("par", this.par);
         if (this.handicap.length > 0)
             doc.addTextNode("handicap", this.handicap);
         if (this.comment.length > 0)
             doc.addTextNode("comment", this.comment);
         doc.pushNewNode("lms");
         for (var i = 0; i < this.landmarks.length; i++)
             this.landmarks[i].addXML(doc);
         doc.popNode(); // markers
         doc.popNode(); // hole
     }

     this.toString = function() {
         var s = "";
         s += "hole #" + this.holeNumber + ", par " + this.par;
         for (var i = 0; i < this.landmarks.length; i++)
             s += this.landmarks[i].toString() + "\n";
         return s;
     }
}
 
function Course() {
     this.courseName = "";
     this.city = "";
     this.country = "United States";
     this.zip = "";
     this.holeCount = 18;
     this.holes = new Array(this.holeCount);
     this.currentHole = 0;
     this.lmcnt = -1;
     this.courseid = -1;
     this.updateTS = null;
     this.author = null;
     this.userIsAuthor = false;
     this.comment = "";

     logMsg("got course init");
     //logObj(this);
     
     for (var i = 0; i < this.holeCount; i++)
         this.holes[i] = new Hole(i + 1);
         
     this.getHole = function(holeNumber) {
         return this.holes[holeNumber];
     }
     
     this.getCurrentHole = function() {
     	 return this.holes[this.currentHole - 1];
     }
          
     this.getNextHole = function() {
         if (this.currentHole < this.holeCount)
             this.currentHole++;
     	 return this.holes[this.currentHole - 1];
     }
     
     this.getPrevHole = function() {
         if (this.currentHole > 1)
             this.currentHole--;
     	 return this.holes[this.currentHole - 1];
     }
     
     this.addXML = function(doc) {
         doc.addTextNode("courseName", this.courseName);
         doc.addTextNode("city", this.city);
         doc.addTextNode("state", this.state);
         doc.addTextNode("country", this.country);
         if (this.comment.length > 0)
             doc.addTextNode("comment", this.comment);
         doc.addTextNode("numholes", this.holeCount);
         doc.pushNewNode("holes");
         for (var i = 0; i < this.holes.length; i++) 
             this.holes[i].addXML(doc);
         doc.popNode(); // holes
     }    
     
     this.getNodeInfo = function(courseNode, getSubnodes) {
         this.city = XMLFindNodeText(courseNode, "city");
         this.state = XMLFindNodeText(courseNode, "state");
         this.country = XMLFindNodeText(courseNode, "country");
         this.courseName = XMLFindNodeText(courseNode, "courseName");
         this.lmcnt = XMLFindNodeText(courseNode, "lmcnt");
         this.courseid = XMLFindNodeText(courseNode, "courseid");
         this.updateTS = XMLFindNodeText(courseNode, "updateTS");
         this.author = XMLFindNodeText(courseNode, "author");
         this.userIsAuthor = XMLFindNodeText(courseNode, "userIsAuthor");
         this.comment = XMLFindNodeText(courseNode, "comment");
         this.holeCount = XMLFindNodeText(courseNode, "numholes");
         if (this.holeCount == "")
             this.holeCount = "18";

         if (!getSubnodes)
             return;
              
         var holeNodes = XMLGetNodeList(courseNode, "hole");
         for (var i = 0; i < holeNodes.length; i++) {
             var hole = new Hole(i + 1);
             this.holes[i] = hole;
             hole.getNodeInfo(holeNodes[i]);
         }
     }    
     
     this.toXML = function(pretty) {
         var doc = new XMLDoc();
         doc.parse("<course/>");
         
         this.addXML(doc);

         return doc.toXML(pretty);
     }
          
     this.toString = function() {
         var s = "";
         s += "course: " + this.courseName;
         s += ", city: " + this.city;
         s += "\n";
         for (var hole in this.holes) {
             if (hole.par == 0)
                 continue;
             s += hole.toString();
         }
     }
}

function getCourseList(xmlstr) {
     doc = new XMLDoc();
     doc.parse(xmlstr);
     
     logMsg("courses parsed to....");
     logMsg(doc.toXML());
     
     var courseList = new Array();
     
     var nodes = XMLGetNodeList(doc.root, "course");
     for (var i = 0; i < nodes.length; i++) {
         var course = new Course();
         course.getNodeInfo(nodes[i], false);
         courseList.push(course);
     }    
     
     return courseList;
}

function getCourseMap(xmlstr) {
     doc = new XMLDoc();
     doc.parse(xmlstr);
     
     logMsg("course map parsed to....");
     logMsg(doc.toXML());
     
     var course = new Course();
     course.getNodeInfo(doc.root, true);
     
     return course;
}
         
