
// 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
//
// Events dragstart, drag, dragend and dblclick used internally
//
function OffsetableMarker(mkrPoint, mkrOpts, opt_color, opt_width, opt_opacity, opt_dx, opt_dy) {

	this.poi = mkrPoint;//original point of interest

    //offset line style
	this.color = opt_color || "";
	this.width = opt_width || 1;
	this.opacity = opt_opacity || 1.0;

    //symbol offset distance in pixels
	this.dx = opt_dx || 0;
	this.dy = opt_dy || 0;
	
	//polyline object used to draw the offset line
	this.line = null;
	
	//ensure marker is draggable
	if(!mkrOpts) 
		mkrOpts = {};
	mkrOpts.draggable = true;
	mkrOpts.bouncy = true;
	mkrOpts.dragCrossMove = true;//? does not work in 6.5
	mkrOpts.bounceGravity = 10000.0;
	

    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
	//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,"dblclick", this.onDblclick);	
	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.onDblclick = function(){
	if(!this.map.getInfoWindow().isHidden())
		this.map.closeInfoWindow();
	this.setPoint(this.poi);//reset symbol to original location
    this.remLine();
	this.dx = 0;
	this.dy = 0;
	var mkr = this;
	window.setTimeout(function(){mkr.onDragStart.call(mkr);},100);//clear any info window poped up by double click
};

OffsetableMarker.prototype.remove = function() {
    this.remLine();
    GMarker.prototype.remove.call(this);//super class
};

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 = [];//draw new line
		pts[0] = this.poi;
		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

};








