

var map, cluster;

function myOnLoad(kat) {
	if (GBrowserIsCompatible()) {
		map=new GMap2(document.getElementById('map'));
		map.setCenter(new GLatLng(0, 0), 0, G_NORMAL_MAP);
		
		map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7)));
		map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 28)));
		
		var marker, markersArray=[];
		
		/*	json data already loaded from marker_data_01.php by using
			<script type="text/javascript" src="marker_data_01.php"></script>
			on simple.php page
		*/
		
		for (var i=0; i<json.length; i++) {
			marker=newMarker(kat,new GLatLng(json[i].lat, json[i].lng), json[i].id, json[i].nazwa_s, json[i].nazwa, json[i].adres);
			markersArray.push(marker);
		}
		
		cluster=new ClusterMarker(map, { markers:markersArray } );
		cluster.fitMapToMarkers();
		
		map.savePosition();	//	enables the large map control centre button to return the map to initial view
		
		//	add an HtmlControl to enable toggling of the ClusterMarker cluster function
		//	see http://googlemapsapi.martinpearman.co.uk/htmlcontrol for more info on HtmlControl
		var html='<div class="htmlControl" style="padding:0px 3px 3px 3px">Grupowanie : <input type="checkbox" checked="checked" onclick="toggleClustering()" /></div>';
		var control=new HtmlControl(html);
		map.addControl(control, new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7,7)));
	}
}

function newMarker(kat, markerLocation, markerId, nazwa_s,nazwa, adres) {
	//var marker=new GMarker(markerLocation, {title:'Marker['+markerId+']'});

	var icon = new GIcon(G_DEFAULT_ICON);  
	if (kat=='as') icon.image = "img/marker.png";  else icon.image = "img/marker1.png";  
	var marker=new GMarker(markerLocation,  {icon:icon,title:nazwa_s});
	
	GEvent.addListener(marker, 'click', function() {
		//marker.openInfoWindowHtml('<p>Marker['+markerId+'] clicked.</p>');
		marker.openInfoWindowHtml('<p>'+nazwa+'<br>'+adres+'</p>');
	});
	return marker;
}

function toggleClustering() {
	cluster.clusteringEnabled=!cluster.clusteringEnabled;
	cluster.refresh(true);	//	true required to force a full update of the markers - otherwise the update would occur next time that the map is zoomed or the active markers change
}
function setCenterTown(n,e) {
if ((n>0) && (e>0)) map.setCenter(new GLatLng(n,e),14);
}
function mapCenterTown(kat) {
	var coordinates=$('#miasta').val();
	var text=$('#miasta option:selected').text();
	var pelne_zblizenie=(text.search("(1)")>0);
	//alert(pelne_zblizenie);
	var i=coordinates.search(",");
	var n=coordinates.substring(0, i);//'50.2428137';
	var e=coordinates.substring(i+1,coordinates.length);
	//alert(n);alert(e);
	if ((n>0) && (e>0)) {
		if (pelne_zblizenie) map.setCenter(new GLatLng(n,e),14); else map.setCenter(new GLatLng(n,e),12);
		}
		var idm=$('#miasta option:selected').attr("title"); 
		var woj=$('#wojewodztwa').val(); 
		showAS(kat,woj,idm);
}
function mapCenter(kat) {
	//map.setCenter(new GLatLng(50.2428137,18.8909912),10);return false;
	var wybor=$('#wojewodztwa').val(); 
			//alert(wybor);
			switch (wybor)
			{
			case 'śląskie':
			map.setCenter(new GLatLng(50.2428137,18.8909912),9);
			break;
			case 'dolnośląskie':
			map.setCenter(new GLatLng(51.065565,16.512451),8);
			break;
			case 'kujawsko-pomorskie':
			map.setCenter(new GLatLng(53.100621,18.649292),8);
			break;
			case 'lubelskie':
			map.setCenter(new GLatLng(51.194836,22.843323),8);
			break;
			case 'lubuskie':
			map.setCenter(new GLatLng(52.028838,15.369873),8);
			break;
			case 'małopolskie':
			map.setCenter(new GLatLng(49.933544,20.267029),8);
			break;
			case 'mazowieckie':
			map.setCenter(new GLatLng(52.19414,20.975647),7);
			break;
			case 'opolskie':
			map.setCenter(new GLatLng(50.677316,17.924194),8);
			break;
			case 'podkarpackie':
			map.setCenter(new GLatLng(49.910556,22.126465),8);
			break;
			case 'podlaskie':
			map.setCenter(new GLatLng(53.16324,23.164673),10);
			break;
			case 'pomorskie':
			map.setCenter(new GLatLng(54.189763,18.149414),8);
			break;
			case 'świętokrzyskie':
			map.setCenter(new GLatLng(50.868378,20.596619),8);
			break;
			case 'warmińsko-mazurskie':
			map.setCenter(new GLatLng(53.833081,20.928955),8);
			break;
			case 'wielkopolskie':
			map.setCenter(new GLatLng(52.389011,16.929932),8);
			break;
			case 'zachodniopomorskie':
			map.setCenter(new GLatLng(53.515818,15.3479),8);
			break;
			case 'łódzkie':
			map.setCenter(new GLatLng(51.638476,19.36615),8);
			break;
			case '0' : map.setCenter(new GLatLng(52.025459,19.204102),6); break;
			}
townReload(kat);			
showAS(kat,wybor,0);
}

function townReload(kat){ 
var woj=$('#wojewodztwa').val(); 
var typs=$('#typ_sprzedawcy').val(); 
var html = $.ajax({
	type: "POST",
	data: 'task=gettowns&kat='+kat+'&woj='+woj+'&typs='+typs+'&nd='+new Date().getTime(),
	url: 'includes/tasks.php',
  async: false
	 }).responseText;
	$("#towns").html(html);


}

function showAS(kat,woj,id_m){ 
var typs=$('#typ_sprzedawcy').val(); 
var html = $.ajax({
	type: "POST",
	data: 'task='+kat+'&id_m='+id_m+'&kat='+kat+'&woj='+woj+'&typs='+typs+'&nd='+new Date().getTime(),
	url: 'includes/tasks.php',
  async: false
	 }).responseText;
	$("#lista_lokalizacji").html(html);
		$("#lista_lokalizacji tbody tr:odd").addClass("odd");
	$("#lista_lokalizacji tbody tr:even").addClass("even");
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_sIR() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_pI() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_pI.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_sI() { //v3.0
  var i,j=0,x,a=MM_sI.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}


var timeoutHnd; 
var timeoutHnd1; 
var flAuto = true; 
var userAgent=navigator.userAgent, version=0;
version = parseFloat(userAgent.substring(userAgent.indexOf('MSIE')+4,userAgent.length));

function enableAutosubmit(state){ 
	flAuto = state; 
	jQuery("#submitButton").attr("disabled",state); 
	}

	
function doS(ev){ 
	if (flAuto) {
	if(timeoutHnd) clearTimeout(timeoutHnd);
	timeoutHnd = setTimeout(function(){getFirms()},1000);
	}}
function doSF(ev){ 
	if (flAuto) {
	if(timeoutHnd1) clearTimeout(timeoutHnd1);
	timeoutHnd1 = setTimeout(function(){getFirms()},500);
	}}
	

function getFirms(){ 
var nazwa_f = jQuery("#nazwa_f").val(); 
var html = $.ajax({
	type: "POST",
	data: 'nazwa='+nazwa_f+'&task=lista_firm&nd='+new Date().getTime(),
	url: 'includes/tasks.php',
  async: false
	 }).responseText;
	jQuery("#firmy").html(html);
} 
function showMenuWiz(id,nazwas){ 
var html='<a href="javascript:getwiz('+id+')">Wizytówka</a> | <a href="javascript:getmapAS('+id+')">Lokalizacja Przedstawiciela Handlowego</a> | <a href="javascript:getmapSK('+id+')">Lokalizacje składów</a>';
var html2='<span>>>&nbsp;&nbsp; '+nazwas+' &nbsp;&nbsp;<<</span>';
jQuery("#menu_wiz").html(html);
jQuery("#wiz_title").html(html2);
getwiz(id);
}
function getwiz(id){ 
jQuery("#wizytowkitekst").html('');
var html = $.ajax({
	type: "POST",
	data: 'id='+id+'&task=get_wiz&nd='+new Date().getTime(),
	url: 'includes/tasks.php',
  async: false
	 }).responseText;
	jQuery("#wizytowkitekst").hide();
	jQuery("#wizytowkitekst").html(html);
		jQuery("#wizytowkitekst").fadeIn();
} 

function getmapAS(id){ 
jQuery("#wizytowkitekst").html(' ');
var html = $.ajax({
	type: "POST",
	data: 'id='+id+'&task=get_mapAS&nd='+new Date().getTime(),
	url: 'includes/tasks.php',
  async: false
	 }).responseText;
	jQuery("#wizytowkitekst").hide();
	jQuery("#wizytowkitekst").html(html);
		jQuery("#wizytowkitekst").fadeIn();
} 

function getmapSK(id){ 
jQuery("#wizytowkitekst").html(' ');
var html = $.ajax({
	type: "POST",
	data: 'id='+id+'&task=get_mapSK&nd='+new Date().getTime(),
	url: 'includes/tasks.php',
  async: false
	 }).responseText;
	jQuery("#wizytowkitekst").hide();
	jQuery("#wizytowkitekst").html(html);
		jQuery("#wizytowkitekst").fadeIn();
} 
