/*
 * Google Maps GArrow
 * http://redfish.pl/apps/garrow
 *
 * Copyright (c) 2009 Cezary Piekacz (cezex@redfish.pl)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Date: 2009-05-30 22:48:33 +0100 (Fri, 30 May 2009)
 */

function GArrows(arrows, alength, color, weight, opacity) {
	this.arrows = arrows;
	this.alength = alength;
	this.color = color;
	this.weight = weight;
	this.opacity = opacity;
	this.lines = new Array();
}

GArrows.prototype = new GOverlay();

GArrows.prototype.initialize = function(map) {
    this.map = map;
}

GArrows.prototype.remove = function() {
	try {
		for(var i = 0, j = this.lines; i < j; i++)
			this.map.removeOverlay(this.lines[i]); 
		this.lines = new Array();
    }
    
    catch(ex) {
    }
}

GArrows.prototype.copy = function(map) {
    return new GArrows(this.arrows, this.alength, this.color, this.weight, this.opacity);
}

GArrows.prototype.redraw = function(force) {
	// The argument force will be true if the zoom level or the pixel offset of the map view has changed, so that the pixel coordinates need to be recomputed.
	if (force == false)
		return;

	for(var i = 0, j = this.lines.length; i < j; i++)
		this.map.removeOverlay(this.lines[i]); 

	this.lines = new Array();

    this.prj = this.map.getCurrentMapType().getProjection();
    this.zoom = this.map.getZoom();

	var bounds = this.map.getBounds();
	var visibleArrows = new Array();
	
	for (var i in this.arrows)
		if (bounds.containsLatLng(this.arrows[i][0]))
			visibleArrows.push(this.arrows[i]);
	
	var arrowsNum = 15;
	var vaInc = Math.round(visibleArrows.length / arrowsNum);
	if (vaInc == 0) vaInc = 1;
	if (vaInc > arrowsNum) vaInc = arrowsNum;
	
	for(var i = 0, j = visibleArrows.length; i < j; i += vaInc)
	{
		var p1 = this.prj.fromLatLngToPixel(visibleArrows[i][0], this.zoom);
		var p2 = this.prj.fromLatLngToPixel(visibleArrows[i][1], this.zoom);
		
		var dx = p2.x - p1.x;
		var dy = p2.y - p1.y;
		var theta = Math.atan2(-dy, dx);
	
	    var t1 = theta + (Math.PI / 4) ;
	    if (t1 > Math.PI)
	        t1 -= 2 * Math.PI;
	        
	    var t2 = theta - (Math.PI / 4) ;
	    if (t2 <= (-Math.PI))
	        t2 += 2 * Math.PI;
	
	    var pts = new Array();
	    var x1 = p1.x - Math.cos(t1) * this.alength;
	    var y1 = p1.y + Math.sin(t1) * this.alength;
	    var x2 = p1.x - Math.cos(t2) * this.alength;
	    var y2 = p1.y + Math.sin(t2) * this.alength;
	    
	    var points = new Array();
	    
	    points.push(this.prj.fromPixelToLatLng(new GPoint(x1, y1), this.zoom));
	    points.push(this.prj.fromPixelToLatLng(new GPoint(p1.x, p1.y), this.zoom));    
	    points.push(this.prj.fromPixelToLatLng(new GPoint(x2, y2), this.zoom));
	    
	    var line = new GPolyline(points, this.color, this.weight, this.opacity);
	    this.lines.push(line);
	    this.map.addOverlay(line);
	
	}
}
