function drawArrow(fromPoint, toPoint, eastColor, westColor, width, opaque)
{
	//alert('line: '+fromPoint.lat()+'/'+fromPoint.lng()+' -> '+toPoint.lat()+'/'+toPoint.lng()+' colors: '+eastColor+' / '+westColor);
	alpha=getAngle(toPoint, fromPoint);
	xa=toPoint.lng();
	ya=toPoint.lat();
	xb=fromPoint.lng();
	yb=fromPoint.lat();

	zoom=map.getZoom();
	l=widthByZoom(zoom);
	da=Math.PI/4;
	
	dxc=Math.cos(alpha-da);
	dyc=Math.sin(alpha-da)/2;
	xc=xa+l*dxc;
	yc=ya+l*dyc;
	
	dxd=Math.cos(alpha+da);
	dyd=Math.sin(alpha+da)/2;
	xd=xa+l*dxd;
	yd=ya+l*dyd;

	dxf=Math.cos(alpha-da-Math.PI/2);
	dyf=Math.sin(alpha-da-Math.PI/2)/2
	xf=xb+l*dxf;
	yf=yb+l*dyf;
	
	dxe=Math.cos(alpha+da+Math.PI/2);
	dye=Math.sin(alpha+da+Math.PI/2)/2;
	xe=xb+l*dxe;
	ye=yb+l*dye;

	//основная линия
	/*var polyline = new GPolyline([
	  toPoint,
	  fromPoint
	], "#000000", 2, 1);
	map.addOverlay(polyline);*/

	//линия на восток прямая
	
	if(eastSpeed<0)
	{
		opaque1=zoom/18;
		width1=Math.ceil(zoom/6);
	}
	else
	{
		opaque1=opaque;
		width1=width;
	}
	
	var polyline = new GPolyline([
	  new GLatLng(yd, xd),
	  new GLatLng(ye, xe)
	], speedToColor(eastSpeed), width1, opaque1);
	polyline.speed=eastSpeed>0?eastSpeed:-westSpeed; //eastSpeed;
	polyline.xa=xa;
	polyline.ya=ya;
	polyline.xb=xb;
	polyline.yb=yb;
	polyline.dxa=dxd;
	polyline.dya=dyd;
	polyline.dxb=dxe;
	polyline.dyb=dye;
	polyline.width=width;
	polyline.opaque=opaque;
	speedMap.push(polyline);
	//speedMap['line'+]=polyline;
	map.addOverlay(polyline);
	GEvent.addListener(polyline, "click", function(pos) {
		var message="<div class='baloon'>На этом участке скорость не определена. !</div>";
		if(this.speed>=0){
			message="<div class='baloon'>На этом участке скорость была "+Math.round(this.speed)+' км/ч</div>';
		}
		else
		{
			message="<div class='baloon'>На этом участке скорость была "+(Math.round(this.speed)*(-1))+' км/ч</div>';
		}
		map.openInfoWindowHtml(pos, message);
		//alert(message);
	});
	GEvent.addListener(polyline, "mouseover", function(){
	});
	
	//Линия на восток обратная
	
	if(westSpeed<0)
	{
		opaque1=zoom/18;
		width1=Math.ceil(zoom/6);
	}
	else
	{
		opaque1=opaque;
		width1=width;
	}
	
	var polyline = new GPolyline([
	  new GLatLng(yc, xc),
	  new GLatLng(yf, xf)
	], speedToColor(westSpeed), width1, opaque1);
	polyline.speed=westSpeed>0?westSpeed:-eastSpeed;
	polyline.xa=xa;
	polyline.ya=ya;
	polyline.xb=xb;
	polyline.yb=yb;
	polyline.dxa=dxc;
	polyline.dya=dyc;
	polyline.dxb=dxf;
	polyline.dyb=dyf;
	polyline.width=width;
	polyline.opaque=opaque;	
	speedMap.push(polyline);
	map.addOverlay(polyline);
	GEvent.addListener(polyline, "click", function(pos) {
		var message="<div class='baloon'>На этом участке скорость не определена.!!</div>";
		if(this.speed>=0){
			message="<div class='baloon'>На этом участке скорость была "+Math.round(this.speed)+' км/ч</div>';
		}
		else
		{
			message="<div class='baloon'>На этом участке скорость была "+(Math.round(this.speed)*(-1))+' км/ч</div>';
		}
		
		map.openInfoWindowHtml(pos, message);
		//alert(message);
	});
	GEvent.addListener(polyline, "mouseover", function(){
	});

	//рисуем стрелку прямую
	/*var polygon = new GPolygon([
		new GLatLng(ya, xa),
		new GLatLng(yc, xc),
		new GLatLng(yd, xd),
		new GLatLng(ya, xa)
	], "#ff0000", 0, 1, eastColor, 1);
	map.addOverlay(polygon);
	
	//рисуем стрелку обратную 
	var polygon = new GPolygon([
		new GLatLng(yb, xb),
		new GLatLng(ye, xe),
		new GLatLng(yf, xf),
		new GLatLng(yb, xb)
	], "#ff0000", 0, 1, westColor, 1);
	map.addOverlay(polygon);*/
}

function widthByZoom(zoom)
{
	switch(zoom){
		case  1:
		case  2:
		case  3:
		case  4:
		case  5:
		case  6:
		case  7:
		case  8:
		case  9:
		case 10:
		case 11:
			return 0.001;
			break;		
		case 12:
		case 13:		
			return 0.0005;
			break;
		case 14:
		case 15:
			return 0.0002;
			break;
		case 16:
		case 17:
		case 18:
		default:
			return 0.0001;
			break;
	}
}

function eraseSpeedMap()
{
	while(speedMap.length>0)
	{
		polyline=speedMap.shift();
		map.removeOverlay(polyline);
	}
}

function rezoomSpeedMap(zoom)
{
	//zoom=map.getZoom();
	l=widthByZoom(zoom);
	
	jQuery.each(speedMap, function(i, this1){
		//alert(Object.toJSON(speedMap[i]));
		newXA=this1.xa+l*this1.dxa;
		newYA=this1.ya+l*this1.dya;

		newXB=this1.xb+l*this1.dxb;
		newYB=this1.yb+l*this1.dyb;
		
		savedSpeed=this1.speed;
		width=this1.width;
		opaque=this1.opaque;
		
		if(this1.speed<0)
		{
			opaque=zoom/18;
			width=Math.ceil(zoom/6);
		}
		
		var polyline = new GPolyline([
		  new GLatLng(newYA, newXA),
		  new GLatLng(newYB, newXB)
		], speedToColor(savedSpeed), width, opaque);
		polyline.speed=savedSpeed;
		polyline.xa=this1.xa;
		polyline.ya=this1.ya;
		polyline.xb=this1.xb;
		polyline.yb=this1.yb;
		
		polyline.dxa=this1.dxa;
		polyline.dya=this1.dya;
		polyline.dxb=this1.dxb;
		polyline.dyb=this1.dyb;
		
		polyline.width=this1.width;
		polyline.opaque=this1.opaque;
		
		map.removeOverlay(this1);
		map.addOverlay(polyline);
		GEvent.addListener(polyline, "click", function(pos) {
			var message="<div class='baloon'>На этом участке скорость не определена.</div>";
			if(this.speed>=0){
				message="<div class='baloon'>На этом участке скорость была "+Math.round(this.speed)+' км/ч</div>';
			}
			else
			{
				message="<div class='baloon'>На этом участке скорость была "+(Math.round(this.speed)*(-1))+' км/ч</div>';
			}
			map.openInfoWindowHtml(pos, message);
			//alert(message);
		});
		GEvent.addListener(polyline, "mouseover", function(){
		});
		speedMap[i]=polyline;
	})
}

function getAngle(a,b)
{
	if(a.lng()!=b.lng())
	{
		alpha=Math.atan(Math.abs(a.lat()*2-b.lat()*2)/Math.abs(a.lng()-b.lng()));
	}
	else
	{
		alpha=Math.PI/2;
	}
	q=a.lng()<b.lng()?(a.lat()<b.lat()?1:4):(a.lat()<b.lat()?2:3);
	switch(q)
	{
		case 1: alpha=alpha;
				break;
		case 2: alpha=Math.PI-alpha;
				break;
		case 3: alpha=Math.PI+alpha;
				break;
		case 4: alpha=-alpha;
				break;
	}
	return alpha;
}

function speedToColor(speed)
{
	if(speed>=0)
	{
		speed=Math.floor(speed/10);
		switch(speed)
		{
			case 0:
				return "#FF0000";
				break;
			case 1:
				return "#FF9900";
				break;
			case 2:
				return "#FFCC00";
				break;
			case 3:
				return "#669900";
				break;
			case 4:
				return "#009900";
				break;
			case 5:
				return "#006600";
				break;
			case 6:
				//return "#006666";
				//break;
			case 7:
				//return "#00CC00";
				//break;
			case 8:
				//return "#00CC66";
				//break;
			case 9:
				//return "#00FFCC";
				//break;
			case 10:
				//return "#00FFFF";
				//break;
			case 11:
				//return "#00CCFF";
				//break;
			case 12:
				return "#006666";
				break;
			default:
				return "#0000FF";
		}
	}
	else
	{
		return "#666666";
	}
}

function showSpeedMap()
{
	action = new MapActions(
	{
		getSpeedMap:function(segments)
		{
			//alert(Object.toJSON(segments));
			eraseSpeedMap();
			jQuery.each(segments, function() {
				if(this.edge1.fromPoint.longitude<this.edge1.toPoint.longitude)
				{
					//edge1 -> east, edge2 -> west
					fromPoint = new GLatLng(this.edge1.fromPoint.latitude,this.edge1.fromPoint.longitude);
					toPoint   = new GLatLng(this.edge1.toPoint.latitude,this.edge1.toPoint.longitude);
					if(this.edge1.isSpeedDetected)
					{
						eastSpeed = this.edge1.speed;
					}
					else
					{
						eastSpeed = -1;
					}
					if(this.edge2.isSpeedDetected)
					{
						westSpeed = this.edge2.speed;
					}
					else
					{
						westSpeed = -1;
					}
					drawArrow(fromPoint, toPoint, eastSpeed, westSpeed, 3, 1);
				}
				else
				{
					//edge2-> east, edge1 -> west
					fromPoint = new GLatLng(this.edge2.fromPoint.latitude,this.edge2.fromPoint.longitude);
					toPoint   = new GLatLng(this.edge2.toPoint.latitude,this.edge2.toPoint.longitude);
					if(this.edge2.isSpeedDetected)
					{
						eastSpeed = this.edge2.speed;
					}
					else
					{
						eastSpeed = -1;
					}
					if(this.edge1.isSpeedDetected)
					{
						westSpeed = this.edge1.speed;
					}
					else
					{
						westSpeed = -1;
					}
					drawArrow(fromPoint, toPoint, eastSpeed, westSpeed, 3, 1);
				}
			});
			jQuery("#chkbox_speed .ajax_loader img").hide();
			if(speedMapUpdateID==0)
			{
				speedMapUpdateID=setInterval(speedMap_Update, 3*60*1000);
			}
		}
	});
	jQuery("#chkbox_speed .ajax_loader img").show();
	action.getSpeedMap(mapArea);
}

function speedMap_Update()
{
	if(jQuery("#chkbox_speed div.chkbox").lenght>0)
	{
		checkBox = jQuery("#chkbox_speed div.chkbox").get(0).checked;
		if( checkBox == null || checkBox == false)
		{
			clearInterval(speedMapUpdateID);
			speedMapUpdateID=0;
		}
		else
		{
			showSpeedMap();
		}
	}
	else
	{
		showSpeedMap();
	}
}

var speedMapUpdateID=0;
var speedMap=new Array();

Ambush.events.attachObserver(Ambush.EVENT_MAP_LOADED, function(){
	jQuery("#chkbox_speed div.chkbox").click(function(){
		Ambush.Util.setCookie('speedMapChkbox', this.checked, 30);
		if(this.checked == null || this.checked == false)
		{
			eraseSpeedMap();
		}
		else
		{
			showSpeedMap();
		}
	});

	chkboxSpeed=false;
	chkboxSpeed=Ambush.Util.getCookie('speedMapChkbox');
	if(chkboxSpeed=='true' && jQuery("#chkbox_speed div.chkbox").length>0)
	{
		jQuery("#chkbox_speed div.chkbox").click();
	}
	GEvent.addListener(map, "zoomend", function(oldLevel,  newLevel) {
		//alert("Вы нажали на карту.");
		rezoomSpeedMap(newLevel);
	});
});

Ambush.events.attachObserver(Ambush.EVENT_MAP_AREA_CHANGED, function(){
	if(jQuery("#chkbox_speed div.chkbox").length>0)
	{
		checkBox = jQuery("#chkbox_speed div.chkbox").get(0).checked;
		//alert(checkBox);
		if(checkBox == true)
		{
			showSpeedMap();
		}
	}
	else
	{
		showSpeedMap();
	}
});