//<!--
// <![CDATA[

var tries = 0;
var maxtries = 5;

function degMinSecToDecDegrees(coord) {
    //var parts = coord.match(/(-?)(\d+)&deg;\s*(\d+\.\d+)&rsquo;\s*([N|S|E|W])/);
    var parts = coord.match(/(.*). (.*). (.*)/);

    var deg = Number(parts[1]) + (Number(parts[2])/60);
    if ( ( parts[3] == "S" ) || (parts[3] == "W") ) {
        deg = -deg;
    }
   
    return deg;
}

function GMapLineSwitcher(mapel,centerlat,centerlon,zoom) {
    this.mapel = mapel;
    this.centerlat = centerlat;
    this.centerlon = centerlon;
    this.zoom = zoom;
    this.maptype = G_PHYSICAL_MAP;
    this.selectedColor = "#FFFFFF";
    this.selectedWeight = 3;
    this.selectedOpacity = 0.60;
    this.init();

    GEvent.addListener(this,'ready',function() {
        this.load();
    });
}

GMapLineSwitcher.prototype.init = function() {
    this.map = new GMap2(document.getElementById(this.mapel));
    GEvent.trigger(this,'load');
    var obj = this;
    this.center = new GLatLng(Number(this.centerlat),Number(this.centerlon));
    this.map.addControl(new GSmallZoomControl3D());
    this.map.setCenter(this.center,Number(this.zoom));
    this.map.setMapType(this.maptype);
 
    this.allpoints = [];
}


GMapLineSwitcher.prototype.addPolylinesOld = function(obj) {
    this.leginfo = obj.leginfo;
    this.selected = [];
    this.polylines = [];
    for ( var j = 0; j < obj.polylines.length; j++ ) {
        var latlngs = [];
        this.selected[j] = [];
        var line = obj.polylines[j];
        for ( var i = 0; i < line.length; i++ ) {
            latlngs[i] = new GLatLng(line[i][0],line[i][1]);
            this.allpoints.push(latlngs[i]);
        }  
        if ( typeof(obj.opts) == "undefined" ) {
            this.selected[j] = new GPolyline(latlngs,obj.color,obj.weight,obj.opacity);
            this.selected[j].setStrokeStyle({color:this.selectedColor,weight:this.selectedWeight,opacity:this.selectedOpacity})
            this.polylines[j] = this.selected[j].copy();
            this.polylines[j].defaultColor = obj.color;
            this.polylines[j].defaultWeight = obj.weight;
            this.polylines[j].defaultOpacity = obj.opacity;
        } else {
            this.selected[j] = new GPolyline(latlngs,obj.color,obj.weight,obj.opacity,obj.opts);
            this.selected[j].setStrokeStyle({color:this.selectedColor,weight:this.selectedWeight,opacity:this.selectedOpacity})
            this.polylines[j] = this.selected[j].copy();
            this.polylines[j].defaultColor = obj.color;
            this.polylines[j].defaultWeight = obj.weight;
            this.polylines[j].defaultOpacity = obj.opacity;
        }
        this.map.addOverlay(this.selected[j]);
        this.selected[j].hide();
        this.map.addOverlay(this.polylines[j]);
    }
    return this.polylines.length;
}

GMapLineSwitcher.prototype.addPolyline = function(obj) {
    this.leginfo = obj.leginfo;
    this.selected = [];

    var latlngs = [];
    var line = obj.polyline;
    var leg = 0;
    var legpoints = [];

    if ( typeof(obj.opts) == "undefined" ) {
        obj.opts = {};
    }

    for ( var i = 0; i < line.length; i++ ) {
        latlngs[i] = new GLatLng(line[i][0],line[i][1]);
        this.allpoints.push(latlngs[i]);

        if ( typeof(obj.legs[leg]) != "undefined" ) {
            if ( i >= obj.legs[leg].start && i <= obj.legs[leg].end ) {
                 legpoints.push(latlngs[i]); 
            }
            if ( i > obj.legs[leg].end ) {
                 this.selected[leg] = new GPolyline(legpoints,obj.color,obj.weight,obj.opacity,obj.opts);
                 this.selected[leg].setStrokeStyle({color:this.selectedColor,weight:this.selectedWeight,opacity:this.selectedOpacity})
                 this.map.addOverlay(this.selected[leg]);
                 this.selected[leg].hide();
                 legpoints = [];
                 leg += 1;
            }
        }

    }  
    if ( typeof(obj.legs[leg]) != "undefined" && typeof(this.selected[leg]) == "undefined" ) {
         this.selected[leg] = new GPolyline(legpoints,obj.color,obj.weight,obj.opacity,obj.opts);
         this.selected[leg].setStrokeStyle({color:this.selectedColor,weight:this.selectedWeight,opacity:this.selectedOpacity})
         this.map.addOverlay(this.selected[leg]);
         this.selected[leg].hide();
    }

    this.polyline = new GPolyline(latlngs,obj.color,obj.weight,obj.opacity,obj.opts);
    this.polyline.defaultColor = obj.color;
    this.polyline.defaultWeight = obj.weight;
    this.polyline.defaultOpacity = obj.opacity;
    this.map.addOverlay(this.polyline);

    return obj.legs.length;
}

GMapLineSwitcher.prototype.getLegInfo = function() {
    if ( typeof(this.current) == "undefined" || this.current === null ||
         typeof(this.leginfo) == "undefined" || this.leginfo === null ||
         typeof(this.leginfo[this.current]) == "undefined" ) return null;
    return this.leginfo[this.current];
}

GMapLineSwitcher.prototype.addPoint = function(obj) {
    var icon = new GIcon();
    icon.image = obj.img;
    icon.iconSize = new GSize(obj.imgSize[0],obj.imgSize[1]);
    icon.iconAnchor = new GPoint(Math.round(obj.imgSize[0]/2),Math.round(obj.imgSize[1]/2));
    icon.iconInfoWIndowAnchor = new GPoint(0,Math.round(obj.imgSize[1]/2));

    var center = new GLatLng(obj.lat,obj.lon);
    this.allpoints.push(this.center);

    if ( obj.name ) { 
        this.marker = new GMarker(center,{icon:icon,title:obj.name});
    } else {
        this.marker = new GMarker(center,{icon:icon});
    }
    this.map.addOverlay(this.marker);
    return 1;
}

GMapLineSwitcher.prototype.selectLine = function(ln) {
    this.deselectLine(this.current);
    this.current = ln;
    this.selected[ln].show();
    this.polyline.setStrokeStyle({weight:1,opacity:.90});
}

GMapLineSwitcher.prototype.deselectLine = function(ln) {
    if ( typeof(this.selected) == "undefined" ) return;
    if ( typeof(this.selected[ln]) == "undefined" ) return;

    this.selected[ln].hide();
    this.polyline.setStrokeStyle({weight:this.polyline.defaultWeight,opacity:this.polyline.defaultOpacity});
}

GMapLineSwitcher.prototype.load = function() {
    if ( this.allpoints.length < 1 ) return;
        this.bounds = new GLatLngBounds(this.allpoints[0],this.allpoints[0]);

    for ( var i = 2; i < this.allpoints.length; i++ ) {
        this.bounds.extend(this.allpoints[i]);
    }
    var zoom = this.map.getBoundsZoomLevel(this.bounds);
    this.map.setCenter(this.bounds.getCenter(),zoom);
}

// ]]>
//-->
