/*
    Document   : lbgm.gpx
    Created on : 19-Jul-2010, 08:03:29
    Author     : Andreas Hadjigeorgiou
    Description:
        Purpose of the stylesheet follows.
*/

lbgm.waypoint = {
    initialize: function(lat, lon, elev, name, addrdetails, desc, sym, type, cmt) {
            this.lat = lat;
            this.lon = lon;
            this.name = name;
            this.addrdetails = addrdetails;

            // Get city, streetaddr, and zip data.
            /*if( this.addrdetails ) {
                    this._initSubArea();
            }*/
            this.elev = elev;
            this.desc = desc;
            this.sym = sym;
            this.type = type;
            this.cmt = cmt;
            this.date = null;

            return this;
    },
    display: function(wpt) {
        var lonlat  = new OpenLayers.LonLat(wpt.lon,wpt.lat);
        lonlat.transform(lbgm.maplets.mapobject.map.displayProjection,lbgm.maplets.mapobject.map.getProjectionObject());

        var f = lbgm.point.featureFromLonLat(lonlat);
        f. attributes.name = wpt.name;
        f.attributes.popuptitle = "Waypoint";
        lbgm.point.arExtraPoints.push(f);

        if(!lbgm.point.pointslayerPrepared) lbgm.point.preparePointLayer();
        lbgm.point.pointslayer.addFeatures([f]);
        lbgm.point.feature = f;

        lbgm.popup.displayGenericPopup(f, wpt.name);

    }
};

lbgm.gpx = {

   init: function() {

   },

   toDocument: function(xmlString) {
        var theDoc = new ActiveXObject("Microsoft.XMLDOM");
        theDoc.async = "false";
        theDoc.loadXML( xmlString );
        return theDoc;
   },

   parseGpxWaypoint: function(waypointNode) {
            var lat  = waypointNode.getAttribute("lat");
            var lng  = waypointNode.getAttribute("lon");
            var name = this._tagValue(waypointNode,"name");
            var desc = this._tagValue(waypointNode,"desc");
            var ele  = this._tagValue(waypointNode,"ele");
            var sym  = this._tagValue(waypointNode,"sym");
            var type  = this._tagValue(waypointNode,"type");
            var cmt  = this._tagValue(waypointNode,"cmt");

            var wpt  = new lbgm.waypoint.init(lat, lng, ele, name, null, desc, sym, type, cmt);
            return wpt;
    },

    parseGpxWaypoints: function(gpxDocument) {
	var waypoints = new Array();
    	var waypointNodes = gpxDocument.getElementsByTagName("wpt");

        for( var i=0; i < waypointNodes.length; i++ ) {
                var waypointNode = waypointNodes[i];
                var wpt = this.parseGpxWaypoint(waypointNode);
                waypoints.push(wpt);
        }
        
    	this.waypoints = waypoints;
    	return waypoints;
    },

    _tagValue: function(parentNode, tagName) {
            var subNode = parentNode.getElementsByTagName(tagName);
            return subNode.length > 0 ? subNode[0].childNodes[0].nodeValue : null;
    }

    
};

