var _section = "project";
var _lat;
var _lng;
var _zoom;
var _maptype;
var _map;
var _marker;
var _baseUrl;
var iconBlue = new GIcon();

function setMapDetails(section, lat, lng, zoom, maptype)
{
    _section = section;
    _lat = lat;
    _lng = lng;
    _zoom = zoom;
    switch(maptype)
    {
        case "G_HYBRID_MAP":
            _maptype = G_HYBRID_MAP;
            break;
        case "G_SATELLITE_MAP":
            _maptype = G_SATELLITE_MAP;
            break;
        case "G_NORMAL_MAP":
            _maptype = G_NORMAL_MAP;
            break;
        default: //case "G_PHYSICAL_MAP":
            _maptype = G_PHYSICAL_MAP;
            break;
    }
}

function initMaps(baseUrl)
{
    _baseUrl = baseUrl;
    iconBlue.image = baseUrl + '/images/maps/mm_20_blue.png';
    iconBlue.shadow = baseUrl + '/images/maps/mm_20_shadow.png';
    iconBlue.iconSize = new GSize(12, 20);
    iconBlue.shadowSize = new GSize(22, 20);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    $("#wrapper_map").width($("#map_canvas").width());

    // setup default position into text and hidden input

    $("#lat").val(_lat);
    $("#lng").val(_lng);
    $("#zoom").val(_zoom);
    $("#maptype").val(_maptype);

    _map = new GMap2(document.getElementById("map_canvas"));
    _map.setMapType(_maptype);
    var center = new GLatLng(_lat, _lng);
    _map.setCenter(center, _zoom);
    _map.setUIToDefault();
    _map.disableDragging();

    var options = {
    draggable: false,
    icon: iconBlue
    };
    _marker = new GMarker(center, options);

    if(_section == "project"){
       _map.addOverlay(_marker);
    }
    
    _map.disableDoubleClickZoom();
    _map.disableScrollWheelZoom();
    _map.disableContinuousZoom();

}

function saveSpiderDiagram(ngoid, spiderid)
{
    url = _baseUrl + "/cmsngo/savespiderarms?ngoid=" + ngoid + "&spiderid=" + spiderid;
    for(i=0;i<_numSpiderArms;i++)
    {
        url += "&arm_name_" + i + "=" + urlencode(_spiderArmNames[i]);
        url += "&arm_dy_" + i + "=" + urlencode(_spiderDeltaY[i]);
        url += "&arm_dx_" + i + "=" + urlencode(_spiderDeltaX[i]);
        url += "&arm_id_" + i + "=" + urlencode(_spiderArmIds[i]);
    }
    window.location = url;
}

function addSpiderEnd()
{
    if($("#spider_arm_name").val() == "")
    {
        alert("Please enter the name of the Spider Arm");
    }
    else
    {
        _map.addOverlay(new OffsetableMarker(_map.getCenter(),50,50,$("#spider_arm_name").val(),-1));
        $("#spider_arm_name").val("")
    }
}

function addExistingArm(id, dx, dy, name)
{
    _map.addOverlay(new OffsetableMarker(_map.getCenter(),dx,dy,name,id));
}


// Create the offsetable marker
//
// GLatLng - mkrPoint the actual position to be marked
// GMarkerOptions - mkrOpts the options for the marker
// opt_color - color string for the offset line e.g. "#FF0000" or "" to use the map's default text color
// opt_width - width for the offset line in pixels
// opt_opacity - opacity for the offset line, 0.0 (transparent) to 1.0 (opaque)
// opt_dx - x pixel offset for symbol, the same at all zooms
// opt_yx - y pixel offset for symbol, the same at all zooms
//
// Bill Chadwick 2006
//
// Events dragstart, drag, dragend and dblclick used internally
//
function OffsetableMarker(mkrPoint, opt_dx, opt_dy, title, id, url) {
    url = typeof(url) != 'undefined' ? url : "";
    this.url = url;
    _spiderArmNames[_numSpiderArms] = title;
    _spiderArmIds[_numSpiderArms] = id;
    this.id = id;
    
    this.armNumber = _numSpiderArms;
    _numSpiderArms++;

	this.poi = mkrPoint;//original point of interest

    //offset line style
	this.color = "";
	this.width = 1;
	this.opacity = 1.0;

    //symbol offset distance in pixels
	this.dx = opt_dx;
	this.dy = opt_dy;

	//polyline object used to draw the offset line
	this.line = null;

	//ensure marker is draggable
    mkrOpts = {};
	mkrOpts.title = title;
    if(this.url == "")
    {
        mkrOpts.draggable = true;
        mkrOpts.dragCrossMove = true;
    }

    GMarker.call(this,mkrPoint,mkrOpts);//call super class constructor

}
OffsetableMarker.prototype = new GMarker(new GLatLng(0,0));//subclass from GMarker

OffsetableMarker.prototype.initialize = function(map) {
    GMarker.prototype.initialize.call(this,map); //super class

	this.map = map;//save

    //Initial offset
    if((this.dx != 0) || (this.dy !=0)){
        var pt = map.fromLatLngToDivPixel(this.poi);
	    pt.x += this.dx;
	    pt.y += this.dy;
	    this.setPoint(map.fromDivPixelToLatLng(pt));
	}

    if(this.url != "")
    {
        // add listener
        GEvent.addListener(this, "click", function() {
            displayBubble(this, this.url, -1, this.id);
        });
    }

	//set up event handling
	var mkr = this;
	GEvent.addListener(this,"dragstart", this.onDragStart);
	GEvent.addListener(this,"drag", this.onDrag);
	GEvent.addListener(this,"dragend", this.onDrag);
	GEvent.addListener(this.map,"zoomend", function(oldL,newL){
	    //move marker after zoom
		var pt = mkr.map.fromLatLngToDivPixel(mkr.poi);
		pt.x += mkr.dx;
		pt.y += mkr.dy;
		mkr.setPoint(mkr.map.fromDivPixelToLatLng(pt));
		mkr.redraw.call(mkr);//and redraw the line
	});
}

OffsetableMarker.prototype.onDragStart = function() {
    if(!this.map.getInfoWindow().isHidden())
        this.map.closeInfoWindow();
}

OffsetableMarker.prototype.onDrag = function(){
	var pt2 = this.map.fromLatLngToDivPixel(this.getPoint());
	var pt1 = this.map.fromLatLngToDivPixel(this.poi);
	this.dx = pt2.x-pt1.x;
	this.dy = pt2.y-pt1.y;
	this.redraw();
}

OffsetableMarker.prototype.hide = function() {
    this.remLine();
    GMarker.prototype.hide.call(this);//super class
}

OffsetableMarker.prototype.remLine = function() {
	if(this.line != null){//the offset line
	    this.map.removeOverlay(this.line);
		this.line = null;
		}
}

OffsetableMarker.prototype.copy = function() {
	return new OffsetableMarker(this.poi,this.opts,this.color,this.width,this.opacity,this.dx,this.dy);
}

OffsetableMarker.prototype.redraw = function(force) {

    this.remLine();

	if((this.dx != 0) || (this.dy != 0)){
		var pts = new Array();//draw new line
		pts[0] = this.poi;
		var pt = this.map.fromLatLngToDivPixel(this.poi);
		pt.x += this.dx;
		pt.y += this.dy;
		pts[1] = this.map.fromDivPixelToLatLng(pt);
		//default color
		var lc = this.color;
		if (lc.length < 7){
			lc = this.map.getCurrentMapType().getTextColor();
			}
		this.line = new GPolyline(pts,lc,this.width,this.opacity);
		this.map.addOverlay(this.line);
	}

	GMarker.prototype.redraw.call(this,force);//super class
    _spiderDeltaY[this.armNumber] = this.dy;
    _spiderDeltaX[this.armNumber] = this.dx;
}

getObjectLoc = function(object, type){
    var objectLoc = '';
    if(type=='lat'){
        objectLoc = object.getLatLng().lat();
    }else{
        objectLoc = object.getLatLng().lng();
    }

    return objectLoc;
}


