/**
 * OffsetableMarker v1.0
 * Copyright (c) 2006 Bill Chadwick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 *
 *
 *  Author: Bill Chadwick
 *
 *  A subclassing of GMarker allowing GMarker to be offset, on creation and by later dragging, 
 *  with its proper POI indicated by a line.
 */



// 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, mkrOpts, opt_color, opt_width, opt_opacity, opt_dx, opt_dy) {

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

    //offset line style
	this.bdcc_color = opt_color || "";
	this.bdcc_width = opt_width || 1;
	this.bdcc_opacity = opt_opacity || 1.0;

    //symbol offset distance in pixels
	this.bdcc_dx = opt_dx || 0;
	this.bdcc_dy = opt_dy || 0;
	
	//polyline object used to draw the offset line
	this.bdcc_line = null;
	
	//ensure marker is draggable
	if(!mkrOpts) 
		mkrOpts = {};
	mkrOpts.draggable = true;
	mkrOpts.bouncy = false;
	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.bdcc_map = map;//save

    //Initial offset    
    if((this.bdcc_dx != 0) || (this.bdcc_dy !=0)){
        var pt = map.fromLatLngToDivPixel(this.bdcc_poi);
	    pt.x += this.bdcc_dx;
	    pt.y += this.bdcc_dy;
	    this.setLatLng(map.fromDivPixelToLatLng(pt));
	}

	//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.bdcc_map,"zoomend", function(oldL,newL){
	    //move marker after zoom
		var pt = mkr.bdcc_map.fromLatLngToDivPixel(mkr.bdcc_poi);
		pt.x += mkr.bdcc_dx;
		pt.y += mkr.bdcc_dy;
		mkr.setLatLng(mkr.bdcc_map.fromDivPixelToLatLng(pt));
		mkr.redraw.call(mkr);//and redraw the line
	});
}

OffsetableMarker.prototype.onDragStart = function() {
    if(!this.bdcc_map.getInfoWindow().isHidden())
        this.bdcc_map.closeInfoWindow();
}

OffsetableMarker.prototype.onDrag = function(){
	var pt2 = this.bdcc_map.fromLatLngToDivPixel(this.getPoint());
	var pt1 = this.bdcc_map.fromLatLngToDivPixel(this.bdcc_poi);
	this.bdcc_dx = pt2.x-pt1.x;
	this.bdcc_dy = pt2.y-pt1.y;	
	this.redraw();
}

OffsetableMarker.prototype.onDblclick = function(){
	if(!this.bdcc_map.getInfoWindow().isHidden())
		this.bdcc_map.closeInfoWindow();
	this.setLatLng(this.bdcc_poi);//reset symbol to original location
    this.bdcc_remLine();
	this.bdcc_dx = 0;
	this.bdcc_dy = 0;
	var mkr = this;
	window.setTimeout(function(){mkr.onDragStart.call(mkr);},100);//clear any info window poped up by double click
	return false;
}

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

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

OffsetableMarker.prototype.bdcc_remLine = function() {
	if(this.bdcc_line != null){//the offset line
	    this.bdcc_map.removeOverlay(this.bdcc_line);
		this.bdcc_line = null;
		}
}

OffsetableMarker.prototype.copy = function() {
	return new OffsetableMarker(this.bdcc_poi,this.bdcc_opts,this.bdcc_color,this.bdcc_width,this.bdcc_opacity,this.bdcc_dx,this.bdcc_dy);
}  	

OffsetableMarker.prototype.redraw = function(force) {

    this.bdcc_remLine();
    		
	if((this.bdcc_dx != 0) || (this.bdcc_dy != 0)){
		var pts = new Array();//draw new line
		pts[0] = this.bdcc_poi;
		var pt = this.bdcc_map.fromLatLngToDivPixel(this.bdcc_poi);
		pt.x += this.bdcc_dx;
		pt.y += this.bdcc_dy;
		pts[1] = this.bdcc_map.fromDivPixelToLatLng(pt);
		//default color
		var lc = this.bdcc_color;
		if (lc.length < 7){
			lc = this.bdcc_map.getCurrentMapType().getTextColor();
			}
		this.bdcc_line = new GPolyline(pts,lc,this.bdcc_width,this.bdcc_opacity);
		this.bdcc_map.addOverlay(this.bdcc_line);
	}
	
	GMarker.prototype.redraw.call(this,force);//super class

}









