// inherited from GMap2 http://blog.reinpetersen.com/2008/12/work-around-for-common-inheritance.html

var map;

Function.prototype.Extends = function(base){
	 function Closure(){}
	 Closure.prototype = base.prototype;
	 this.prototype = new Closure();
	 this.prototype.constructor = this.constructor;
	 this.base = base;
}

//object declaration

function Locality()
{
    this.id = null;
    this.name = null;
    this.desc = null;
    this.status = null;
    this.position_lat = null;
    this.position_lng = null;
    this.div_show = false;
    this.div_img = "";
    this.div_content_1 = "";
    this.div_content_1_unit = "";
    this.div_content_2 = "";
    this.div_content_2_unit = "";
    this.isHidden = true;
    this.isHighlight = false;
    this.markers = new Object();
    this.divs = new Object();
	this.highlightMarker = null;
	return this;
}

function Section()
{
	this.id = null;
	this.name = null;
	this.desc = null;
	this.status = null;
	this.start_position_lat = null;
	this.start_position_lng = null;
	this.end_position_lat = null;
	this.end_position_lng = null;
    this.isHidden = true;
	this.polyline = null;
	this.highlight = null;
}

function Icons()
{
	this.icons_list = new Object();
	this.show_zoomlevel_min = null;
	this.show_zoomlevel_max = null;
	this.zoom_zoomlevel_min = null;
	this.zoom_zoomlevel_max = null;
	this.zoomlevel_min_icon_width = null;
	this.zoomlevel_max_icon_width = null;
// methods
	this.addIcons = function(googleIcons, status)
	{
		this.icons_list[status] = new Object();
		var iconWidth = googleIcons.iconSize.width;
		var iconHeight = googleIcons.iconSize.height;
		var shadowWidth = googleIcons.shadowSize.width;
		var shadowHeight = googleIcons.shadowSize.height;
		var anchorx = googleIcons.iconAnchor.x;
		var anchory = googleIcons.iconAnchor.y;
		var ax = 0;
		var ay = 0;
	    if(anchorx < iconWidth && anchorx > 0)
	    	ax = 1;
		if(anchory < iconHeight && anchory > 0)
			ay = 1;
		
		var infoAnchorx = googleIcons.infoWindowAnchor.x;
		var infoAnchory = googleIcons.infoWindowAnchor.y;
		var iax = 0;
		var iay = 0;
		if(infoAnchorx < iconWidth && infoAnchorx > 0)
	    	iax = 1;
		if(infoAnchory < iconHeight && infoAnchory > 0)
			iay = 1;
		
		var marker_size_step_width = Math.round((this.zoomlevel_max_icon_width - this.zoomlevel_min_icon_width) / (this.zoom_zoomlevel_max - this.zoom_zoomlevel_min));
		var marker_size_step_height = Math.round((iconHeight / iconWidth) * marker_size_step_width);
		for(idx = 0; idx < (this.zoom_zoomlevel_max - this.zoom_zoomlevel_min ); idx++)
		{
			var icon = new GIcon(googleIcons);
			var level_icons = new Object();
		    var icon_size_width = iconWidth - idx*marker_size_step_width;
		    var icon_size_height = iconHeight - idx*marker_size_step_height;
		    var icon_centre = Math.round(icon_size_width / 2);
		    var shadow_width = Math.round(icon_size_width * 1.5);
		    icon.iconSize = new GSize(icon_size_width, icon_size_height);
		    icon.shadowSize = new GSize(shadow_width, icon_size_height);
		    icon.iconAnchor = new GPoint(anchorx,anchory);
		    if(ay)
		    	anchory = anchory-(marker_size_step_height/2) < 0 ? 0 : anchory-(marker_size_step_height/2);
		    else
		    	anchory = anchory-(marker_size_step_height) < 0 ? 0 : anchory-(marker_size_step_height);
		    if(ax)
		    	anchorx = anchorx-(marker_size_step_width/2) < 0 ? 0 : anchorx = anchorx-(marker_size_step_width/2);
		    else
		    	anchorx = anchorx-(marker_size_step_width) < 0 ? 0 : anchorx-(marker_size_step_width);
		    
		    icon.infoWindowAnchor = new GPoint(infoAnchorx, infoAnchory);
		    if(iay)
		    	infoAnchory = infoAnchory-(marker_size_step_height/2) < 0 ? 0 : infoAnchory-(marker_size_step_height/2);
		    else
		    	infoAnchory = infoAnchory-(marker_size_step_height) < 0 ? 0 : infoAnchory-(marker_size_step_height);
		    if(iax)
		    	infoAnchorx = infoAnchorx-(marker_size_step_width/2) < 0 ? 0 : infoAnchorx-(marker_size_step_width/2);
		    else
		    	infoAnchorx = infoAnchorx-(marker_size_step_width) < 0 ? 0 : infoAnchorx-(marker_size_step_width);
			this.icons_list[status][this.zoom_zoomlevel_max - idx] = icon;
		}
	}
}

function Polylines()
{
	this.colours_list = new Object();
	this.show_zoomlevel_min = null;
	this.show_zoomlevel_max = null;
	this.zoom_zoomlevel_min = null;
	this.zoom_zoomlevel_max = null;
	this.zoomlevel_min_weight = null;
	this.zoomlevel_max_weight = null;
// methods
	this.addPolylines = function(color, status)
	{
		this.colours_list[status] = color;
	}
}

function Localities(icons, onClickFunction, onMouseOverFunction, onMouseOutFunction)
{
	this.icons = icons;
	this.locality = new Object();
// methods
	this.add = function(loc){
		this.locality[loc.id] = loc;
	}
	
	this.del = function(locality_id){
		for(var i in this.locality[locality_id].markers)
			map.removeOverlay(this.locality[locality_id].markers[i]);
		delete this.locality[locality_id];
	}
	this.show = function(locality_id){
		var map_zoom = map.getZoom();	
		if(this.locality[locality_id].markers[map_zoom])
			this.locality[locality_id].markers[map_zoom].show();
		this.locality[locality_id].isHidden = false;
	}
	this.hide = function(locality_id){
		var map_zoom = map.getZoom();
		
		for(key in this.locality[locality_id].markers)
			this.locality[locality_id].markers[key].hide();
		
		this.locality[locality_id].isHidden = true;
	}
	this.showAll = function(){
		var map_zoom = map.getZoom();
		for(var i in this.locality)	
			if(this.locality[i].markers[map_zoom])
			{
				this.locality[i].markers[map_zoom].show();
				if(this.locality[i].divs[map_zoom])
					this.locality[i].divs[map_zoom].show();
				this.locality[i].isHidden = false;
			}
	}
	this.hideAll = function(){
		for(var i in this.locality)
		{
			for(var j in this.locality[i].markers)
				this.locality[i].markers[j].hide();
			this.locality[i].isHidden = true;
		}
	}
	this.highlight = function(locality_id){
		
		var mapZoom = map.getZoom();
		var markerZoom = map.getZoom();
		if(mapZoom <= this.icons.show_zoomlevel_max && mapZoom >= this.icons.show_zoomlevel_min)
		{
			if(mapZoom > this.icons.zoom_zoomlevel_max)
				markerZoom = this.icons.zoom_zoomlevel_max;
			if(mapZoom <= this.icons.zoom_zoomlevel_min)
				markerZoom = this.icons.zoom_zoomlevel_min + 1;
		    var markerPoint = this.locality[locality_id].markers[markerZoom].getPoint();
		    var polyPoints = Array();
		    var mapNormalProj = G_NORMAL_MAP.getProjection();
		    var clickedPixel = mapNormalProj.fromLatLngToPixel(markerPoint, mapZoom);
	
		    var icon = this.locality[locality_id].markers[markerZoom].getIcon();
		    var polySmallRadius = icon.iconSize.width * 0.8;
		    
		    var polyNumSides = 20;
		    var polySideLength = 18;
	
		    for (var i = 0; i<(polyNumSides+1); i++) 
		    {
		      var aRad = polySideLength*i*(Math.PI/180);
		      var polyRadius = polySmallRadius; 
		      var pixelX = clickedPixel.x + polyRadius * Math.cos(aRad);
		      var pixelY = (clickedPixel.y-2) + polyRadius * Math.sin(aRad);
		      var polyPixel = new GPoint(pixelX,pixelY);
		      var polyPoint = mapNormalProj.fromPixelToLatLng(polyPixel,mapZoom);
		      
		      polyPoints.push(polyPoint);
		    }
		
			var highlight = new GPolygon(polyPoints,"#000000",5,0.0,"#FF0000",.5);
		    this.locality[locality_id].highlightMarker = highlight;
			
	//		var mapZoom = map.getZoom();
			map.addOverlay(this.locality[locality_id].highlightMarker);
			this.locality[locality_id].isHighlight = true;
		}
	}
	this.xhighlight = function(locality_id){
		for(i in this.locality)
		{
			if(this.locality[i].highlightMarker)
			{
				map.removeOverlay(this.locality[i].highlightMarker);
				this.locality[i].highlightMarker = null;
				if(i != locality_id)
					this.locality[i].isHighlight = false;
			}
		}
		this.highlight(locality_id);
	}
	this.updateStatus = function(locality_id, status, div_content_1, div_content_2){
		var imageUrl="";
		this.locality[locality_id].status = status;
		switch(status)
		{
			case "ok":
			case "OK":
				for(var i in this.icons.icons_list["ok"])
				{
					imageUrl = this.icons.icons_list["ok"][i].image;
					break;
				}
				break;
				
			case "err":
			case "Chyba":
				for(var i in this.icons.icons_list["err"])
				{
					imageUrl = this.icons.icons_list["err"][i].image;
					break;
				}
				break;
			
			case "problem":
			case "warn":
				for(var i in this.icons.icons_list["problem"])
				{
					imageUrl = this.icons.icons_list["problem"][i].image;
					break;
				}
				break;
				
			case "cong":
			case "Kongesce":
				for(var i in this.icons.icons_list["cong"])
				{
					imageUrl = this.icons.icons_list["cong"][i].image;
					break;
				}
				break;

			case "nodata":
				for(var i in this.icons.icons_list["nodata"])
				{
					imageUrl = this.icons.icons_list["nodata"][i].image;
					break;
				}
				break;
		}
		for(i in this.locality[locality_id].markers)
			this.locality[locality_id].markers[i].setImage(imageUrl);
		for(i in this.locality[locality_id].divs)
		{
			contentt = "";
			if(div_content_1 != "")
				contentt = contentt + div_content_1 + " " + this.locality[locality_id].div_content_1_unit;
			if(div_content_2 != "" && locality_id != 'IS_NP-SA-N1')
				contentt = contentt + '<br>' + div_content_2 + " " + this.locality[locality_id].div_content_2_unit; 
			if(div_content_1 == "" && div_content_2 == "")
				contentt = contentt + "Data nejsou k dispozici.";
            
			div_01 = '<div style="background-color: #f2efe9; padding: 2px; border:1px black solid; white-space: nowrap; text-align:left;">'+contentt+'<\/div>';
			div_02 = '<div style="padding: 2px 2px 0px 0px; margin: 0px 0px 0px 3px; ">'+div_01+'<\/div>';
			div_03 = '<div style="padding: 0px 0px 15px 12px; background: url(' + localities.locality[i].div_img+') no-repeat bottom left;">'+div_02+'<\/div>';
			
			this.locality[locality_id].divs[i].setContents(div_03);
		}		
	}
	this.zoom = function(zoomlevel){
		var show_max = this.icons.show_zoomlevel_max;
		var zoom_max = this.icons.zoom_zoomlevel_max;
		var show_min = this.icons.show_zoomlevel_min;
		var zoom_min = this.icons.zoom_zoomlevel_min;
		
		for(var j in this.locality)
		{
			if(!this.locality[j].isHidden)
			{
				// max
				if(zoomlevel >= zoom_max && zoomlevel <= show_max)
				{
					maxIndex = 0;
					for(i in this.locality[j].markers)
					{
							this.locality[j].markers[i].hide();
							maxIndex = maxIndex > parseInt(i) ? maxIndex : parseInt(i);
							
					}
					this.locality[j].markers[maxIndex].show();
				}
				// min
				else if(zoomlevel <= zoom_min && zoomlevel >= show_min)
				{
					var minIndex = 100;
					for(i in this.locality[j].markers)
					{
						this.locality[j].markers[i].hide();
						minIndex = minIndex >= parseInt(i) ? minIndex : parseInt(i);
					}
					this.locality[j].markers[minIndex].show();
				}
				// middle
				else if(zoomlevel < zoom_max && zoomlevel > zoom_min)
					{
						for(i in this.locality[j].markers)
							this.locality[j].markers[i].hide();
						this.locality[j].markers[zoomlevel].show();
					}
				// other
				else
				{
					for(i in this.locality[j].markers)
						this.locality[j].markers[i].hide();
				}
			}
			if(this.locality[j].highlightMarker || this.locality[j].isHighlight)
				this.xhighlight(j);
			// divy - popisky
			//for(i in this.locality[j].divs)
			//{
			//	if(zoomlevel >= 13)
			//	{
			//		this.locality[j].divs[i].show();
			//	}
			//	else
			//	{
			//		this.locality[j].divs[i].hide();
			//	}
			//}
		}
	}
	
	this.panTo = function(locality_idx, map, outside_only)
	{
	    var locality;
	    for(var i in this.locality)
	    {
    	    if (i == locality_idx)
    	    {
	        	locality = this.locality[i];
	        	break;
    	    }
	    }
	   
	    var bounds = map.getBounds();
	    var pos = new GLatLng(locality.position_lat, locality.position_lng);
	    if(map && locality && (!outside_only || (outside_only && !bounds.containsLatLng(pos))))
	       map.panTo( pos );
	}
	
	this.getBounds = function(){}
	
	this.click = onClickFunction;
	this.mouseover = onMouseOverFunction;
	this.mouseout = onMouseOutFunction;
	return this;
}

function Sections(polylines, onClickFunction, onMouseOverFunction, onMouseOutFunction)
{
	this.polylines = polylines;
	this.section =  new Object();
	var sections = this;
// methods
	this.add = function(sec){
		this.section[sec.id] = sec;
		sec.polyline.setStrokeStyle({"color":this.polylines.colours_list[sec.status], "weight":5, "opacity":0.6});
		var polyPoints = Array();
	    
		for (var i = 0; i < sec.polyline.getVertexCount(); i++)
			polyPoints.push(sec.polyline.getVertex(i));
		
		var highlight = new GPolyline(polyPoints,"#FF0000",10,.5);
		map.addOverlay(highlight);
		highlight.hide();
		sec.highlight = highlight;
		map.addOverlay(sec.polyline);
		GEvent.addListener(sec.polyline, "click", function(){	
			sections.click(sec);
			//sections.xhighlight(sec.id);
		});
		sec.polyline.hide();
	}
	
	this.del = function(section_id){
		map.removeOverlay(this.section[section_id].polyline);
		delete this.section[section_id];
	}
	this.show = function(section_id){
		this.section[section_id].polyline.show();
	}
	this.hide = function(section_id){
		this.section[section_id].polyline.hide();
	}
	this.showAll = function(){
		for(key in this.section)
			this.section[key].polyline.show();
	}
	this.hideAll = function(){
		for(key in this.section)
			this.section[key].polyline.hide();
	}
	this.highlight = function(section_id){
		if(this.section[section_id])
		{
			this.section[section_id].polyline.hide();
			this.section[section_id].highlight.show();
			this.section[section_id].polyline.show();
		}
	}
	this.xhighlight = function(section_id){
		for(key in this.section)
			this.section[key].highlight.hide();

		this.highlight(section_id);
	}
	this.updateStatus = function(section_id, status){
		this.section[section_id].status = status;
		this.section[section_id].polyline.setStrokeStyle({"color":this.polylines.colours_list[status], "weight":5, "opacity":0.6});		
	}
	
	this.panTo = function(section_idx, map)
	{
	    var section;
	    for(var i in this.section)
	    {
    	    if (i == section_idx)
    	    {
    	    	section = this.section[i];
	        	break;
    	    }
	    }
	    
	    if(map && section)
	    	map.panTo( new GLatLng(section.start_position_lat, section.start_position_lng));
	}
	this.zoom = function(zoomlevel){}
	this.getBounds = function(){}
	
	this.click = onClickFunction;
	this.mouseover = onMouseOverFunction;
	this.mouseout = onMouseOutFunction;
}

function CMap(Message,element)
{
    map = this;
	CMap.base.call(this, element);
	map = new GMap2(element);
	map.setMapType(G_NORMAL_MAP);
	map.addControl(new GLargeMapControl()); // map.addControl(new GSmallZoomControl3D());
	map.addControl(new GMapTypeControl());
	map.enableScrollWheelZoom();
	map.setCenter(new GLatLng(50.052225,14.411316), 11);
	
	GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {zoomMarker(oldLevel, newLevel);});

	map.localities = new Object();
	map.sections = new Object();	
	
	

	map.AddLocalities = function(localities)
	{
		var counter = 0;
		for(var key in map.localities)
			counter++;
		map.localities[counter] = localities;
		for(var i in localities.locality)
			for(idx = 0; idx < (localities.icons.zoom_zoomlevel_max - localities.icons.zoom_zoomlevel_min); idx++)
			{
					var status = "";
					switch(localities.locality[i].status)
					{
						case 'err':
						case 'Chyba':
							status = localities.icons.icons_list.err[localities.icons.zoom_zoomlevel_max - idx];
						break;
						
						case 'problem':
						case 'warn':
							status = localities.icons.icons_list.problem[localities.icons.zoom_zoomlevel_max - idx];
						break;
							
						case 'cong':
						case 'Kongesce':
							status = localities.icons.icons_list.cong[localities.icons.zoom_zoomlevel_max - idx];
						break;
							
						case 'nodata':
							status = localities.icons.icons_list.nodata[localities.icons.zoom_zoomlevel_max - idx];
						break;
							
						default :
							status = localities.icons.icons_list.ok[localities.icons.zoom_zoomlevel_max - idx];
						break;
					}
					
					localities.locality[i].markers[localities.icons.zoom_zoomlevel_max - idx] = createMarker(localities.locality[i].position_lat, localities.locality[i].position_lng, status, localities, i, localities.icons.zoom_zoomlevel_max - idx);
					map.addOverlay(localities.locality[i].markers[localities.icons.zoom_zoomlevel_max - idx]);
					localities.locality[i].markers[(localities.icons.zoom_zoomlevel_max - idx)].hide();
					
					// zobrazeni divu
					if(localities.locality[i].div_show == true)
					{
						contentt = "";
						if(localities.locality[i].div_content_1 != "")
							contentt = contentt + localities.locality[i].div_content_1 + " " + localities.locality[i].div_content_1_unit; 
						if(localities.locality[i].div_content_2 != "")
							contentt = contentt + '<br>' + localities.locality[i].div_content_2 + " " + localities.locality[i].div_content_2_unit; 
						if(localities.locality[i].div_content_1 == "" && localities.locality[i].div_content_2 == "")
							contentt = contentt + "Data nejsou k dispozici.";
				
						div_01 = ""; div_02 = ""; div_03 = "";
						div_01 = '<div style="background-color: #f2efe9; padding: 2px; border:1px black solid; white-space: nowrap; text-align:left;">'+contentt+'<\/div>';
						div_02 = '<div style="padding: 2px 2px 0px 0px; margin: 0px 0px 0px 3px; ">'+div_01+'<\/div>';
						div_03 = '<div style="padding: 0px 0px 15px 12px; background: url(' + localities.locality[i].div_img+') no-repeat bottom left;">'+div_02+'<\/div>';

						var label = new ELabel(new GLatLng(localities.locality[i].position_lat, localities.locality[i].position_lng), div_03, null, null, 75);

						localities.locality[i].divs[localities.icons.zoom_zoomlevel_max - idx] = label;
						map.addOverlay(localities.locality[i].divs[localities.icons.zoom_zoomlevel_max - idx]);
						localities.locality[i].divs[(localities.icons.zoom_zoomlevel_max - idx)].hide();
					}
			}
	}
	map.AddSections = function(sections, id)
	{
		if(id)
			map.sections[id] = sections;
		else
		{
			var counter = 0;
			for(var key in map.sections)
				counter++;
			map.sections[counter] = sections;
		}
	}
	
	return map;
}

function isGoogleMapSupported()
{
	return typeof GMap2!="undefined";
}

if(isGoogleMapSupported())
	CMap.Extends(GMap2);


function zoomMarker(oldLevel, newLevel)
{
	for(var key in map.localities)
	{
		map.localities[key].zoom(newLevel);
	}
	
}

function createMarker(lat, lng, status, localities, locality_id, highlight_id)
{
	var marker = new GMarker(new GLatLng(lat, lng), status);
	GEvent.addListener(marker, "click", function(){
				localities.click(localities.locality[locality_id]);
				//localities.xhighlight(locality_id);	
			});
	return marker;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Polyline with arrows
//
// Bill Chadwick May 2008
//
// Free for any use
//

// Constructor params exactly as GPolyline then 
// 1) arrow spacing in pixels, 
// 2) arrow head length in pixels
// 3) arrow colour
// 4) arrow thickness in pixels
// 5) arrow opacity

function BDCCArrowedPolyline(points, color, weight, opacity, opts, gapPx, headLength, headColor, headWeight, headOpacity) {	
    
    this.gapPx = gapPx;
    this.points = points;
    this.color = color;
    this.weight = weight;
    this.opacity = opacity;
    this.headLength = headLength;
    this.headColor = headColor;
    this.headWeight = headWeight;
    this.headOpacity = headOpacity;
    this.opts = opts;
    this.heads = new Array();
    this.line = null;
    
}

if(isGoogleMapSupported())
{
BDCCArrowedPolyline.prototype = new GOverlay();


BDCCArrowedPolyline.prototype.initialize = function(map) {

    this.map = map;   
    this.prj = map.getCurrentMapType().getProjection();
    var rdrw = GEvent.callback(this,this.recalc );
  	this.lstnMoveEnd = GEvent.addListener(map,"zoomend",function(){rdrw ();});
  	this.lstnType = GEvent.addListener(map,"maptypechanged",function(){rdrw ();});

  	this.recalc();//first draw
}

BDCCArrowedPolyline.prototype.remove = function() {

    try{
        if (this.line)
            this.map.removeOverlay(this.line);
        for(var i=0; i<this.heads.length; i++)
            this.map.removeOverlay(this.heads[i]); 
    }
    catch(ex)
    {
    }
}

BDCCArrowedPolyline.prototype.redraw = function(force) {
    return;//do nothing, the GPolyline line and heads draw themselves
}


BDCCArrowedPolyline.prototype.copy = function(map) {
    return new BDCCArrowedPolyline(this.points,this.color,this.weight,this.opacity,this.opts,this.gapPx, this.headLength, this.headColor, this.headWeight, this.headOpacity);
}


BDCCArrowedPolyline.prototype.recalc = function() {

   var zoom = this.map.getZoom();

   this.remove();

   //the main polyline
   this.line = new GPolyline(this.points,this.color,this.weight,this.opacity,this.opts);
   this.map.addOverlay(this.line);

   // the arrow heads
   this.heads = new Array();

   var p1 = this.prj.fromLatLngToPixel(this.points[0],  zoom);//first point
   var p2;//next point
   var dx;
   var dy;
   var sl;//segment length
   var theta;//segment angle
   var ta;//distance along segment for placing arrows
      
   for (var i=1; i<this.points.length; i++){
            
      p2 = this.prj.fromLatLngToPixel(this.points[i],  zoom)
      dx = p2.x-p1.x;
      dy = p2.y-p1.y;
      sl = Math.sqrt((dx*dx)+(dy*dy)); 
      theta = Math.atan2(-dy,dx);
      
      j=1;
      
	if(this.gapPx == 0){
		//just put one arrow at the end of the line
        	this.addHead(p2.x,p2.y,theta,zoom);
	}
	else if(this.gapPx == 1) {
		//just put one arrow in the middle of the line
        	var x = p1.x + ((sl/2) * Math.cos(theta)); 
        	var y = p1.y - ((sl/2) * Math.sin(theta));
        	this.addHead(x,y,theta,zoom);        
	}
	else{
      	//iterate along the line segment placing arrow markers
      	//don't put an arrow within gapPx of the beginning or end of the segment 

	      ta = this.gapPx;
      	while(ta < sl){
        	var x = p1.x + (ta * Math.cos(theta)); 
        	var y = p1.y - (ta * Math.sin(theta));
        	this.addHead(x,y,theta,zoom);
        	ta += this.gapPx;  
      	}  
      
        	//line too short, put one arrow in its middle
      	if(ta == this.gapPx){
        		var x = p1.x + ((sl/2) * Math.cos(theta)); 
        		var y = p1.y - ((sl/2) * Math.sin(theta));
        		this.addHead(x,y,theta,zoom);        
      	}
	}
      
      p1 = p2;   
   }
}

BDCCArrowedPolyline.prototype.addHead = function(x,y,theta,zoom) {

    //add an arrow head at the specified point
    var t = theta + (Math.PI/4) ;
    if(t > Math.PI)
        t -= 2*Math.PI;
    var t2 = theta - (Math.PI/4) ;
    if(t2 <= (-Math.PI))
        t2 += 2*Math.PI;
    var pts = new Array();
    var x1 = x-Math.cos(t)*this.headLength;
    var y1 = y+Math.sin(t)*this.headLength;
    var x2 = x-Math.cos(t2)*this.headLength;
    var y2 = y+Math.sin(t2)*this.headLength;
    pts.push(this.prj.fromPixelToLatLng(new GPoint(x1,y1), zoom));
    pts.push(this.prj.fromPixelToLatLng(new GPoint(x,y), zoom));    
    pts.push(this.prj.fromPixelToLatLng(new GPoint(x2,y2), zoom));
    this.heads.push(new GPolyline(pts,this.headColor,this.headWeight,this.headOpacity,this.opts));
    this.map.addOverlay(this.heads[this.heads.length-1]);
}
}
