/**
 * Wyrysowywanie i edycja Polygon-ów oraz lini na mapach google
 * @author Paweł Zawada
 */
 
featureTable_ = document.createElement("table");
    featureTable_.id = "featuretbody";

var COLORS = [["red", "#ff0000"], ["orange", "#ff8800"], ["green","#008000"],
              ["blue", "#000080"], ["purple", "#800080"]];
var options = {};
var lineCounter_ = 0;
var shapeCounter_ = 0;
var markerCounter_ = 0;
var colorIndex_ = 0;
var featureTable_;

var polygons = new Array();
var lines = new Array();
var table_points = new Array();
var itable;
var lcValues;
var shapeCounter_ = 0;

var fill_color;
var line_color;
var line_size;
var opacity;

/* Przechowuje współrzędne ostatniego kliknięcia na poligon */ 
var temp_latlng;  		

 /* Tworzenie DIVa z zawartością zakładki do dymku */
function polyTab(item) {
	var ret = document.createElement('div');
	ret.innerHTML = 'Nazwa obiektu: '+item.id+"<br>"+item.op;;
	return ret;
} 

var polygons = new Array();

function getColor(named) {
  return COLORS[(colorIndex_++) % COLORS.length][named ? 0 : 1];
}

/*
*	Funkcja odpowiada za rozpoczenicie rysowania poligonu.
*	Przyjmujac jako parametr null rysuje nowy poligon natomiast po podaniu tablicy 
*	wspolrzednych geograficznych rysuje poligon oparty na wyznaczonych wspolrzednych
*/
function startShape(points) {
//  GEvent.removeListener(listener);
	type = 'poly';
  	var color = getColor(false);
  	name = $('ofertyinwestycyjne_pl_nazwa').value;
  	
  	fill_color = $('ofertyinwestycyjne_fillcolor').value;
	line_color = $('ofertyinwestycyjne_linecolor').value;
	line_size = $('ofertyinwestycyjne_linewidth').value;
	opacity = $('ofertyinwestycyjne_opacity').value;
  	
  		if(opacity == "" || opacity == null){
  			opacity = 0.5;
			$('ofertyinwestycyjne_opacity').value = opacity; 
		}
		if(fill_color == "" || fill_color == null){
			fill_color = "#ff0000";
			$('ofertyinwestycyjne_fillcolor').value = fill_color;
		}
		if(line_color == "" || line_color == ""){
			line_color = "#ff0000";
			$('ofertyinwestycyjne_linecolor').value = line_color;
		}
		if(line_size == "" || line_size == 0 || line_size == null){
			line_size = 2;
			$('ofertyinwestycyjne_linewidth').value = "2";
		}
  	
  	if(points == null) { 
  		var	polygon = new GPolygon([], line_color, line_size, 0.8, fill_color, opacity);
  		polygons[shapeCounter_] = polygon;
  		startDrawing(polygons[shapeCounter_], shapeCounter_++,/* "Obszar " + (++shapeCounter_) */ name, function() {
    		var cell = this;
    		var area = polygon.getArea();
    
    		cell.innerHTML = (Math.round(area / 10000) / 100) + "km<sup>2</sup>";
    		preparedPoints(shapeCounter_ -1);
  	 
  		}, color);
  	} else {
  		
  		var polygon = new GPolygon(points, line_color, line_size, 0.8, fill_color, opacity);
  		
  //	 shapeCounter_ = 0;
  	 	polygons[shapeCounter_] = polygon;
  	
  		startDrawing2(polygons[shapeCounter_], shapeCounter_++ ,/* "Obszar " + (++shapeCounter_) */ name, function() {
    		var cell = this;
    		var area = polygon.getArea();
    
    		cell.innerHTML = (Math.round(area / 10000) / 100) + "km<sup>2</sup>";
  	
 			preparedPoints(shapeCounter_ -1);
  		}, color);
  	}
}

function startLine(points) {
	type = 'line';
 	name = $('ofertyinwestycyjne_pl_nazwa').value;
 	line_size = $('ofertyinwestycyjne_linewidth').value;
 	line_color = $('ofertyinwestycyjne_linecolor').value; 
 
	if(opacity == "" || opacity == null){
		opacity = 0.5;
		$('ofertyinwestycyjne_opacity').value = opacity; 
	}
	if(fill_color == "" || fill_color == null){
		fill_color = "#ff0000";
		$('ofertyinwestycyjne_fillcolor').value = fill_color;
	}
	if(line_color == "" || line_color == ""){
		line_color = "#ff0000";
		$('ofertyinwestycyjne_linecolor').value = line_color;
	}
	if(line_size == "" || line_size == 0 || line_size == null){
		line_size = 2;
		$('ofertyinwestycyjne_linewidth').value = "2";
	}
 	
  	//var color = getColor(false);
  	if(points == null){
  		var line = new GPolyline([], line_color);
  		line.weight = line_size;
  		lines[lineCounter_] = line;
  		startDrawing(line, lineCounter_++,name, function() {
    		var cell = this;
    		var len = line.getLength();
    		cell.innerHTML = (Math.round(len / 10) / 100) + "km";
    		preparedPoints(lineCounter_ -1);
  			}, line_color);
  	}
  	else {
  		var line = new GPolyline(points, line_color);
  		line.weight = line_size;
  		lines[lineCounter_] = line;
  		startDrawing2(line, lineCounter_++,name, function() {
    		var cell = this;
    		var len = line.getLength();
    		cell.innerHTML = (Math.round(len / 10) / 100) + "km";
     		preparedPoints(lineCounter_ -1);
  		}, line_color);
  	}
  
}

function addFeatureEntry(poly,number,name, color) {
  currentRow_ = document.createElement("tr");
  currentRow_.id = "poly_"+number;
  var colorCell = document.createElement("td");
  currentRow_.appendChild(colorCell);
  colorCell.style.backgroundColor = color;
  colorCell.style.width = "1em";
  colorCell.valign = "top";
  colorCell.innerHTML = "<div onclick='deletePoly("+number+")' >Usun</div>";
  
  var nameCell = document.createElement("td");
  currentRow_.appendChild(nameCell);
  nameCell.innerHTML = name;
  var descriptionCell = document.createElement("td");
  descriptionCell.valign = "top";
  currentRow_.appendChild(descriptionCell);
  featureTable_.appendChild(currentRow_);
  return {desc: descriptionCell, color: colorCell};
}

function startDrawing(poly,number,name, onUpdate, color) {
  	map.addOverlay(poly);
  	poly.enableDrawing(options);
  	poly.enableEditing({onEvent: "mouseover"});
  	poly.disableEditing({onEvent: "mouseout"});
  	GEvent.addListener(poly, "endline", function() {

	// Potrzebne do usówania w panelu administracyjnym
	polygon = poly;   
    //select("hand_b");
    var cells = addFeatureEntry(poly,number,name, color);
    GEvent.bind(poly, "lineupdated", cells.desc, onUpdate);
   	preparedPoints(number);
    GEvent.addListener(poly, "click", function(latlng, index) {
      	if (typeof index == "number") {
        	poly.deleteVertex(index);
      	} else {
       		//map.removeOverlay(poly);
     		//poly.deleteVertex(index);
      		// Przygotowanie tabeli table_points z punktami do wyslania
			preparedPoints(number);
     		var item = new Object();
     		item.id = name;
     		var poly_desc; 
     		poly_desc = document.createElement("div");
       		
       		if(type == 'poly'){ 
    			
    			var area = poly.getArea();
    			var poly_area = (Math.round(area / 10000) / 100) + "km<sup>2</sup>";
    			 fill_color = $('ofertyinwestycyjne_fillcolor').value;
  				 line_color = $('ofertyinwestycyjne_linecolor').value;
  				 line_size = $('ofertyinwestycyjne_linewidth').value;
  				 opacity = $('ofertyinwestycyjne_opacity').value;
  				poly_opa = opacity * 100
     			//poly_desc = document.createElement("div");
     			poly_desc.innerHTML = " "
			//+"<input type='button' onclick='deletePoly("+number+")' value='Usuń' \/><br/>" 
     		+"<b>Powierzchnia: </b>"+poly_area+"<br/>"
			//+"<input type='button' onclick='savePoly("+number+",\""+name+"\")' value='Zapisz' \/><br/>"
			+"<div id='colorpicker301' class='colorpicker301'></div>"
			+"<input type='button' onclick=\"showColorGrid3('input_field_1','sample_1',"+number+");\" value='Kolor obszaru'>"
			+"<input type='hidden' ID='input_field_1' size='9' value='"+fill_color+"' >&nbsp;"
			+"<input type='text' ID='sample_1' size='1' value=''  STYLE='background-color: "+fill_color+";'>"+
     	 	"<br/><b>Opacity:</b><div id='slider_target'>"
			+"<div class='form_widget_amount_slider'></div></div>"
			+"<input type='hidden' id='hue' value='"+poly_opa+"'  />"
			+"<input type='text' ID='poly_opa' value='"+opacity+"' size='2' />" 
			+"<script type='text/javascript'>form_widget_amount_slider('slider_target',$('hue'),150,0,100, 'changeObjColor("+number+")');</script>"
			+"<br/><div id='colorpicker301' class='colorpicker301'></div>"
			+"<input type='button' onclick=\"showColorGrid3('input_field_2','sample_2',"+number+");\" value='Kolor krewędzi'>"
			+"<input type='hidden' ID='input_field_2' size='9' value='"+line_color+"' >&nbsp;"
			+"<input type='text' ID='sample_2' size='1' value=''  STYLE='background-color: "+line_color+";'>"
			+"<br/><b>Grubość krawędzi:</b><div id='slider_target2'>"
			+"<div class='form_widget_amount_slider'></div></div>"
			+"<input type='text' ID='hue2' value='"+line_size+"'  size='2'/>"
			+"<script type='text/javascript'>form_widget_amount_slider('slider_target2',$('hue2'),150,1,30, 'changeObjColor("+number+")');</script>"
     
     		;
     		}
     
     		if(type == 'line'){
      			var len = poly.getLength();
     			var	linelen = (Math.round(len / 10) / 100) + "km";
     			var line_color = $('ofertyinwestycyjne_linecolor').value;
  				var line_size = $('ofertyinwestycyjne_linewidth').value;
     			//poly_desc = document.createElement("div");
     			poly_desc.innerHTML = " "
			//+"<input type='button' onclick='deletePoly("+number+")' value='Usuń' \/>" + name+"<br/>" 
     		+"Długość lini: "+linelen+"<br/>"
    	 	//+"<input type='button' onclick='savePoly("+number+",\""+name+"\")' value='Zapisz' \/>"
     		+"<br/><div id='colorpicker301' class='colorpicker301'></div>"
			+"<input type='button' onclick=\"showColorGrid3('input_field_2','sample_2',"+number+");\" value='Kolor linii'>"
			+"<input type='hidden' ID='input_field_2' size='9' value='"+line_color+"' >&nbsp;"
			+"<input type='text' ID='sample_2' size='1' value=''  STYLE='background-color: "+line_color+";'>"
     		+"<br/><b>Grubość linii:</b>"
			+"<div id='slider_target2'><div class='form_widget_amount_slider'></div></div>"
			+"<input type='text' ID='hue2' value='"+line_size+"'  size='2' onkeyup='changeObjColor("+number+")'/>"
     		+"<script type='text/javascript'>form_widget_amount_slider('slider_target2',$('hue2'),150,1,30, 'changeObjColor("+number+")');</script>";
     		} 
     
    
     		if(type == 'poly'){
     			item.type = "poly";
     		}
       
       		if(type == 'line'){
     			item.type = "line";
     		}
     		item.op = poly_desc.innerHTML;
     		var tabs = edform(item);
			var gtabs = Array();
			var n = 0;
			tabs._each(function(tab){
			    gtabs[n] = new GInfoWindowTab(tab.label,tab.content);
			    n++;
			});
			map.openInfoWindowTabs(latlng,gtabs);
			/*
			GEvent.addListener(marker, "infowindowbeforeclose", function() {
    			    alert("Zmiany nie zostaną zapisane!");
  			});
     		*/
     //map.openInfoWindowHtml(latlng,poly_desc);
       // var newColor = getColor(false);
       // cells.color.style.backgroundColor = newColor
       // poly.setStrokeStyle({color: newColor, weight: 4});
      }
    });
  });

 
}

function startDrawing2(poly,number,name, onUpdate, color) {
  		
  	map.addOverlay(poly);
  	//poly.enableDrawing(options);
  	poly.enableEditing({onEvent: "mouseover"});
  	poly.disableEditing({onEvent: "mouseout"});
	
	// Potrzebne do usówania w panelu administracyjnym
	polygon = poly;

    var cells = addFeatureEntry(poly,number,name, color);
    GEvent.bind(poly, "lineupdated", cells.desc, onUpdate);
   	preparedPoints(number);
    GEvent.addListener(poly, "click", function(latlng, index) {
      if (typeof(index) == "number") {
        	poly.deleteVertex(index);
      } else {
      	//map.removeOverlay(poly);
     	//poly.deleteVertex(index);
      	preparedPoints(number);
     	var item = new Object();
     	item.id = name;
     	var poly_desc = document.createElement("div");
       	if(type == 'poly') {
    		var area = poly.getArea();
    			var poly_area = (Math.round(area / 10000) / 100) + "km<sup>2</sup>";
    			 fill_color = $('ofertyinwestycyjne_fillcolor').value;
  				 line_color = $('ofertyinwestycyjne_linecolor').value;
  				 line_size = $('ofertyinwestycyjne_linewidth').value;
  				 opacity = $('ofertyinwestycyjne_opacity').value * 100;
  				 poly_opa = $('ofertyinwestycyjne_opacity').value;
     			//poly_desc = document.createElement("div");
     			poly_desc.innerHTML = " "
			//+"<input type='button' onclick='deletePoly("+number+")' value='Usuń' \/><br/>" 
     		+"<b>Powierzchnia: </b>"+poly_area+"<br/>"
			//+"<input type='button' onclick='savePoly("+number+",\""+name+"\")' value='Zapisz' \/><br/>"
     		+"<div id='colorpicker301' class='colorpicker301'></div><input type='button' onclick=\"showColorGrid3('input_field_1','sample_1',"+number+");\" value='Kolor obszaru'><input type='hidden' ID='input_field_1' size='9' value='"+fill_color+"' >&nbsp;<input type='text' ID='sample_1' size='1' value=''  STYLE='background-color: "+fill_color+";'>"+
     	 	"<br/><b>Opacity:</b><div id='slider_target'><div class='form_widget_amount_slider'></div></div><input type='hidden' ID='hue' value='"+opacity+"'  /><input type='text' ID='poly_opa' value='"+poly_opa+"' size='2' readonly/>" +
     		"<script type='text/javascript'>form_widget_amount_slider('slider_target',$('hue'),150,0,100, 'changeObjColor("+number+")');</script>"+
     		"<br/><div id='colorpicker301' class='colorpicker301'><\/div><input type='button' onclick=\"showColorGrid3('input_field_2','sample_2',"+number+");\" value='Kolor krewędzi'><input type='hidden' ID='input_field_2' size='9' value='"+line_color+"' >&nbsp;<input type='text' ID='sample_2' size='1' value=''  STYLE='background-color: "+line_color+";'>"+
     		"<br/><b>Grubość krawędzi:</b><div id='slider_target2'><div class='form_widget_amount_slider'></div></div><input type='text' ID='hue2' value='"+line_size+"'  size='2'/>"+
     		"<script type='text/javascript'>form_widget_amount_slider('slider_target2',$('hue2'),150,1,30, 'changeObjColor("+number+")');</script>";
     	}
     
     	if(type == 'line') {
      		var len = poly.getLength();
     			var	linelen = (Math.round(len / 10) / 100) + "km";
     			 line_color = $('ofertyinwestycyjne_linecolor').value;
  				 line_size = $('ofertyinwestycyjne_linewidth').value;
     		poly_desc.innerHTML = "<input type='button' onclick='deletePoly("+number+")' value='Usuń' \/>" + name+"<br/>" 
     		+"Długość lini: "+linelen+"<br/>"+
    	 	"<input type='button' onclick='savePoly("+number+",\""+name+"\")' value='Zapisz' \/>"+
     		"<br/><div id='colorpicker301' class='colorpicker301'><\/div><input type='button' onclick=\"showColorGrid3('input_field_2','sample_2',"+number+");\" value='Kolor linii'><input type='hidden' ID='input_field_2' size='9' value='"+line_color+"' >&nbsp;<input type='text' ID='sample_2' size='1' value=''  STYLE='background-color: "+line_color+";'>"+
     		"<br/><b>Grubość linii:</b><div id='slider_target2'><div class='form_widget_amount_slider'></div></div><input type='text' ID='hue2' value='"+line_size+"'  size='2' onkeyup='changeObjColor("+number+")'/>"+
     		"<script type='text/javascript'>form_widget_amount_slider('slider_target2',$('hue2'),150,1,30, 'changeObjColor("+number+")');</script>"
     
    	 	;
     	} 
     
    
     	if(type == 'poly'){
     		item.type = "poly";
     	}
       	if(type == 'line'){
     		item.type = "line";
     	}
     	item.op = poly_desc.innerHTML;
     	var tabs = edform(item);
		var gtabs = Array();
		var n = 0;
		tabs._each(function(tab){
			gtabs[n] = new GInfoWindowTab(tab.label,tab.content);
			n++;
		});
		map.openInfoWindowTabs(latlng,gtabs);
		/* Stare funkcje 
		GEvent.addListener(marker, "infowindowbeforeclose", function() {
    		alert("Zmiany nie zostaną zapisane!");
  		}); */
     
     	//map.openInfoWindowHtml(latlng,poly_desc);
       	// var newColor = getColor(false);
       	// cells.color.style.backgroundColor = newColor
       	// poly.setStrokeStyle({color: newColor, weight: 4});
      }
    });
}
/**
*	Funkcja odpowiada za usuniecie obiektu z mapy po stronie panelu.
*	Po usunieciu obiektu czysci tablice wykorzystywane do przyesylania wspolrzednych.
*/
function deletePoly(poly_nr) {
alert(poly_nr);
  if(type == 'poly'){
    	map.removeOverlay(polygons[poly_nr]);
    	$('ofertyinwestycyjne_vectors').value = "";
    	map.closeInfoWindow();
    	table_points = new Array();
    	lcValues = new Array();
    }
    if(type == 'line'){
    	
    	map.removeOverlay(lines[poly_nr]);
    	$('line').value = "";
    	map.closeInfoWindow();
    	table_points = new Array();
    	lcValues = new Array();
    }
}

/**
*	Funkcja zamieniajaca color poligonu badz lini w zaleznosci co jest
*	aktualnie edytowane.
*	parametr obj_nr oznacza numer obkiektu w tablicy objektow (polygons lub lines)
*/
function changeObjColor(obj_nr){
	if(type == 'poly'){
		poly_color = $('input_field_1').value;
		line_color = $('input_field_2').value;
		line_size = $('hue2').value;
		$('poly_opa').value = $('hue').value / 100 ;
		var opa = $('poly_opa').value;
		if(opa == null){
			opa = 0.5;
		}
	
		if(poly_color == null){
			poly_color = "#000000";
		}
		//alert(poly_color);
			polygons[obj_nr].opacity = opa;
			
			if(line_size == 0){
				map.removeOverlay(polygons[obj_nr]);
				var vertex_tmp = new Array();
				var n;
				for (n=0; n<polygons[obj_nr].getVertexCount(); n++){
					vertex_tmp[n] = polygons[obj_nr].getVertex(n);
				}
				polygons[obj_nr] = new GPolygon(vertex_tmp, line_color, line_size, 0.8, fill_color, opa);
				//polygons[obj_nr].setStrokeStyle({color: line_color, weight: line_size});
				//polygons[obj_nr].setStrokeStyle({color: line_color,});
				map.addOverlay(polygons[obj_nr]);
			}
			else {
			polygons[obj_nr].setFillStyle({color: poly_color});
				polygons[obj_nr].setStrokeStyle({color: line_color, weight: line_size});
			}
			$('ofertyinwestycyjne_opacity').value = opa;
			$('ofertyinwestycyjne_fillcolor').value = poly_color;
			$('ofertyinwestycyjne_linecolor').value = line_color;
			$('ofertyinwestycyjne_linewidth').value = line_size;
		
		
	}
	if(type == 'line'){
			var line_color = $('input_field_2').value;
			var line_size = $('hue2').value;
		    if(line_size > 30 ) {
		    	alert("Za duża wartość, podaj mniejszą !")
		    }
			if(line_size <= 0 && line_size != ""){
				alert("Niepoprawna wartość");
			}
		   if(line_size > 0 && line_size <= 30 && line_size != "")  {
		    	lines[obj_nr].setStrokeStyle({color: line_color});
		    	lines[obj_nr].weight = line_size;
		    	$('ofertyinwestycyjne_linecolor').value = line_color;
		    	$('ofertyinwestycyjne_linewidth').value = line_size;
			}
	}
	
	
}

function savePoly(poly_nr, poly_name) {
	
	preparedPoints(poly_nr);
	var data = table_points;

//alert(table_points.toJSONString());
	
	
	itable=table_points.join(",");
	//i = table_points;
	new Ajax.Request('post_poly.php', {
	asynchronous:true,
	evalScripts:false,
	method:'post',
	onComplete:function(request, json){
		Element.hide('wait');
	},
	onLoading:function(request, json){
		Element.show('wait');
	},
	onSuccess:function(response) {
		var places = String(response.responseText);
		places = places.evalJSON()
		makeMarkers(places);
	},
	parameters:{a:itable, name:poly_name}
	});

}

/**
*
*	Funkcja przygotowuje tablice z wspolrzednymi obiektow oraz przypisuje je do
*	odpowiedniego pola w formularzu.
*
*/
function preparedPoints(poly_nr) {

	table_points = new Array();
	var points;	
	var k;

	if(type == 'poly'){
		var polygon_cur = polygons[poly_nr];
	}
	if(type == 'line'){
		var polygon_cur = lines[poly_nr];
	}
	for(k=0; k<polygon_cur.getVertexCount(); k++){
		point = polygon_cur.getVertex(k);
		table_points[k] = {'x': Number(point.y), 'y': Number(point.x)};
	};
	
	if(type == 'poly' || type == 'line'){
		$('ofertyinwestycyjne_vectors').value = table_points.toJSON();	
	}	
}

function prepareLatLngFromJson(jsonString){
	lcValues = new Array();
	k = 0;
	var json_value = eval(jsonString);

	if (json_value.size()>0) {
		json_value._each(function(item) {
			lcValues[k] =  new GLatLng(item.x,item.y);
		k++; });
		setTimeout(function(){
			//map.setZoom(actz);
			if (type == 'poly') {
				gshow('poly');
				startShape(lcValues);
			}
			if (type == 'line') {
				gshow('line');
				startLine(lcValues);
			}}, 200);
	}
}

function drawCircle(centerMarker, radius) {

    //var normalProj = map.getCurrentMapType().getProjection();
    var zoom = map.getZoom();

    var centerPt = normalProj.fromLatLngToPixel(centerMarker, zoom);
    //var radiusPt = normalProj.fromLatLngToPixel(radiusMarker, zoom);

    var circlePoints = Array();

    with (Math) {
        //var radius = floor(sqrt(pow((centerPt.x-radiusPt.x),2) + pow((centerPt.y-radiusPt.y),2)));
        for (var a = 0 ; a < 361 ; a+=5 ) {
          	var aRad = a*(PI/180);
	       	y = centerPt.y + radius * sin(aRad)
	        x = centerPt.x + radius * cos(aRad)
	        var p = new GPoint(x,y);
	        circlePoints.push(normalProj.fromPixelToLatLng(p, zoom));
        }
        circleLine2 = new GPolygon(circlePoints, "#008000", 0, 0, "#007000",0.5);
        map.addOverlay(circleLine2);
    } 
}

function makePolys (list) {
	var poly = null;
	list.polys._each(function(item) {
		var n = 0;
		var points = Array(); 
		item.points._each(function(point){
			points[n] = new GLatLng(point.x, point.y);
			n++;
		});
		var polygon = new GPolygon(points, item.line_color, item.line_size, 0.7,item.fill_color , item.opacity);
		polygon.id = item.id;
		//polygon.desc = item.desc;
		polygon.item = item;
		allpolys[polygon.id] = polygon;
		map.addOverlay(polygon, 0, 17);
		/*contentDiv = document.createElement('div');
		contentDiv.id = item.id;
		if (!item.addr) item.addr='';
		contentDiv.innerHTML = '<div style="float:left; margin:3px;">'+
			'<img src="'+basedir+'48x48/'+item.icon+'"></div>'+
			'<h1>'+item.name+'</h1>'+
			item.addr+'<br/><p>'+item.desc+'<p>';
		contentDiv.style.marginRight="10px";
		alldesc[item.id] = contentDiv; 
		polygon.style.cursor = pointer;
		polygon.bindInfoWindow(alldesc[polygon.id]); */
		//GEvent.addListener(polygon, "click", catch_pos(latlng));
	});
	if (opis_click == null) {
		opis_click = addListener(get_opis);
	}
}

function clearPolys() {
	allpolys._each(function(poly){
		if (poly) map.removeOverlay(poly);
	});
	allpolys = new Array();
}
