var map;
var cmPointsX1 = 0;
var cmPointsX2 = 1000;
var cmPointsY1 = 0;
var cmPointsY2 = 1000;
var polyShape = [];

function load() {
	if (GBrowserIsCompatible()) {
	   map = new GMap2(document.getElementById("Map"));
	   map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(50.789551, 19.168396), 13);
		var i = 0;
		var podl = 0;
		while($("#click"+i).attr("id")!=undefined) {
			if(!$("#click"+i).attr("checked")) {
				AddOverlay(i,'t');
				if(tabld[i][7]=='k') {
					$("#click"+i).attr("checked", true);
					podl++;
				}
				
			}
		i++;
		}
		if(i==podl) $("#CheckAll").attr("checked", true);
		zoomToComplex();
   }
}
function zoomToComplex() {
	var cmPoints = [];
	cmPoints.push(new GLatLng(cmPointsX1,cmPointsY1));
	cmPoints.push(new GLatLng(cmPointsX2,cmPointsY2));
	cmOverlay = new GPolyline(cmPoints);
	var bounds = cmOverlay.getBounds();
	map.setCenter(bounds.getCenter());
	map.setZoom(map.getBoundsZoomLevel(bounds));
}

function AddOverlay(id,c) {
	var polyPoints = [];
	var bounds = new GLatLngBounds();
	
	for(i=0;i<tabl[id].length;i++) {
		var l = tabl[id][i].split(",");
		if(i==0) {
			var bx = l[0];
			var by = l[1];
		}
		polyPoints.push(new GLatLng(l[0],l[1]));
		
		if(l[0]>cmPointsX1) cmPointsX1 = l[0];
		if(l[0]<cmPointsX2) cmPointsX2 = l[0];
		if(l[1]>cmPointsY1) cmPointsY1 = l[1];
		if(l[1]<cmPointsY2) cmPointsY2 = l[1];
		
		var pt = new GLatLng(l[0],l[1])
		bounds.extend(pt);
	}
	
	if((c!='t')||(tabld[id][7]=='k'&&c=='t')) {
		if(tabld[id][0]==1) polyShape[id] = new GPolyline(polyPoints,"#"+tabld[id][1],tabld[id][2],tabld[id][3]);
		else if(tabld[id][0]==2) {
			polyPoints.push(new GLatLng(bx,by));
			polyShape[id] = new GPolygon(polyPoints,"#"+tabld[id][1],tabld[id][2],tabld[id][3],"#"+tabld[id][4],tabld[id][5]);
		}else if(tabld[id][0]==3) {
			polyShape[id] = new GMarker(new GLatLng(bx,by));
			if(tabld[id][6]!="") {
				GEvent.addListener(polyShape[id], "click", function() {
					polyShape[id].openInfoWindowHtml(tabld[id][6]);
				})
			}
		}
		map.addOverlay(polyShape[id]);
	}
	
	if(tabogr.length==0)  DajIkone(id, bounds)
	else{
	for(var i=0;i<tabogr.length;i++) {
		if(tabogr[i]==tabp[id][0]) {
			DajIkone(id, bounds)
			//alert(tabogr[i] +" "+ id +" "+ tabp[id][0])
			break;
		}
	}
	}
}

function DajIkone(id, bounds) {

	if(tabp[id]!=undefined) {
		var baseIcon = new GIcon();
		baseIcon.shadow = "icon/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
		
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = "icon/marker" + tabp[id][0] + ".png";
		markerOptions = { icon:letteredIcon };
		var center = new GLatLng(
			(bounds.getSouthWest().lat() + bounds.getNorthEast().lat()) / 2.,
	   	(bounds.getSouthWest().lng() + bounds.getNorthEast().lng()) / 2.
	    );
		
		var num = 1000+id*1;
		polyShape[num] = new GMarker(center, markerOptions);
		map.addOverlay(polyShape[num]);
		GEvent.addListener(polyShape[num], "click", function() {
			if($("#lev").val()!="") document.location.href = "?lev="+$("#lev").val()+"&nid="+tabp[id][1];
			else document.location.href = "?nid="+tabp[id][1];
		})
		GEvent.addListener(polyShape[num], "mouseover", function() {
			polyShape[id].opacity = 0.1;
			polyShape[id].redraw(true);
		})
		GEvent.addListener(polyShape[num], "mouseout", function() {
			polyShape[id].opacity = tabld[id][5];
			//alert(id+" "+num+" "+tabld[id][2])
			polyShape[id].redraw(true);
		})
	}
	
}

function RemoveOverlay(id) {
	if(tabp[id]!=undefined) {
		var num = 1000+id*1;
		map.removeOverlay(polyShape[num]);
	}
	map.removeOverlay(polyShape[id]);
}

/* 
function AddOverlay(id,c) {
	var polyPoints = [];
	for(i=0;i<tabl[id].length;i++) {
		var l = tabl[id][i].split(",");
		if(i==0) {
			var bx = l[0];
			var by = l[1];
		}
		polyPoints.push(new GLatLng(l[0],l[1]));
		
		if(l[0]>cmPointsX1) cmPointsX1 = l[0];
		if(l[0]<cmPointsX2) cmPointsX2 = l[0];
		if(l[1]>cmPointsY1) cmPointsY1 = l[1];
		if(l[1]<cmPointsY2) cmPointsY2 = l[1];
		
	}
	if((c!='t')||(tabld[id][7]=='k'&&c=='t')) {
		if(tabld[id][0]==1) polyShape[id] = new GPolyline(polyPoints,"#"+tabld[id][1],tabld[id][2],tabld[id][3]);
		else if(tabld[id][0]==2) {
			polyPoints.push(new GLatLng(bx,by));
			polyShape[id] = new GPolygon(polyPoints,"#"+tabld[id][1],tabld[id][2],tabld[id][3],"#"+tabld[id][4],tabld[id][5]);
		}else if(tabld[id][0]==3) {
			polyShape[id] = new GMarker(new GLatLng(bx,by));
			GEvent.addListener(polyShape[id], "click", function() {
				polyShape[id].openInfoWindowHtml(tabld[id][6]);
			})
		}
		map.addOverlay(polyShape[id]);
	}
}

function RemoveOverlay(id) {
	map.removeOverlay(polyShape[id]);
}
*/
function CadConvert(l) {
	twu = new Array(931164.258,255360.159,927244.841,251524.272);
	twd = new Array(50.811250,19.216869,50.776134,19.162366);
	var bx = (l[0]-twu[3]) / (twu[1]-twu[3]);
	var lx = twd[3] + (twd[1]-twd[3]) * bx;
	var by = (l[1]-twu[2]) / (twu[0]-twu[2]);
	var ly = twd[2] + (twd[0]-twd[2]) * by;
	return new Array(ly,lx);
}

$(document).ready(function() { 
	$(".Polygon").click(function() {
		var id = $(this).attr("id");
		var lid = id.substr(5,id.length);
		if($(this).attr("checked")) {
			AddOverlay(lid);
		}else{
			RemoveOverlay(lid);
		}
	})
	$("#CheckAll").click(function() {
		var ch = $(this).attr("checked");
		var i = 0;
		while($("#click"+i).attr("id")!=undefined) {
			if(ch) {
				if(!$("#click"+i).attr("checked")) {
					AddOverlay(i,'n');
					$("#click"+i).attr("checked", true);
				}
			}else{
				if($("#click"+i).attr("checked")) {
					RemoveOverlay(i);
					$("#click"+i).attr("checked", false);
				}
			}
		i++;
		}
	})
});


$(document).ready(function(){
   $(".lightbox").lightbox();

  $("#tresc_lewa h2").click(function(){
    $(this).parent().next().animate({height: 'toggle', opacity: 'toggle'}, 'slow', null);
    $(this).parent().next().next().animate({height: 'toggle', opacity: 'toggle'}, 'slow', null);
  });

  $("#tresc_lewa a.wiecej").click(function(){
    $(this).parent().parent().prev().prev().animate({height: 'toggle', opacity: 'toggle'}, 'slow', null);
    $(this).parent().parent().prev().animate({height: 'toggle', opacity: 'toggle'}, 'slow', null);
  });
  
  $("#akt_zaw").fadeIn("slow");
  $("#akt_zaw img").fadeIn("slow");
});

function Sprawdz() {
	
	var regname = /^[A-Za-z êÊóÓ&#353;&#317;¶¦³£¿¯&#317;¬æÆñÑ]{3,100}$/;
	var regphone = /^[0-9 -+]{7,30}$/;
	var regemail = /^[a-z0-9][\w\.-]*\w@([\w-]+\.)*[a-z]{2,4}$/;
	//RegExp("[^@]{1,}[@]{1}[^@.]{1,}[.]{1}[^@]{1,}","gi");
	var regtxt = /^[A-Za-z êÊóÓ&#353;&#317;¶¦³£¿¯&#317;¬æÆñÑ 0-9 -=.,+]{1,1000}$/;
	
	var firma = document.getElementById("firma").value;
        var iname = document.getElementById("name").value;
	var phone = document.getElementById("phone").value;
	var mail = document.getElementById("mail").value;
	var txt = document.getElementById("txt").value;

        //alert(mail);
        if(!regtxt.test(firma)) return ShowMessage("Podaj nazwê firmy");
	if(!regname.test(iname)) return ShowMessage("Podaj swoje imiê");
	if(!regphone.test(phone)) return ShowMessage("Podaj prawid³owy numer telefonu");
	if(!regemail.test(mail)) return ShowMessage("Podaj prawid³owy adres email");
	if(txt == '') return ShowMessage("Wpisz tre¶æ wiadomo¶ci");
	return true;
}

function ShowMessage(txt) {
	document.getElementById("Message").innerHTML = '<b><font color="#800000">'+txt+'</font></b>';
	return false;
}
