var geocoder;

var map;  

  

var a_tab   = new Array();

var a_id    = new Array();

var a_typ   = new Array();

var a_name  = new Array();

var a_adres = new Array();

var a_gloc1 = new Array();

var a_gloc2 = new Array();

 

var typ_opis = new Array();



typ_opis['j']= 'Siedziba firmy Jadar';

typ_opis['b']= 'Brukarz';

typ_opis['d']= 'Dystrybutor';

typ_opis['db']= 'Dystrybutor, brukarz';

typ_opis['bd']= 'Brukarz, dystrybutor';



 

var markersArray = [];

  

  function initialize() {

  if (document.getElementById("map_canvas")!= null)

  {

    geocoder = new google.maps.Geocoder();

    var latlng = new google.maps.LatLng(52.2323 , 21.0084 );

    var myOptions = {

      zoom: 8,

      center: latlng,

      mapTypeId: google.maps.MapTypeId.ROADMAP

    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  }

  }





  var Base64 = {

 

	// private property

	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

 

	// public method for encoding

	encode : function (input) {

		var output = "";

		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;

		var i = 0;

 

		input = Base64._utf8_encode(input);

 

		while (i < input.length) {

 

			chr1 = input.charCodeAt(i++);

			chr2 = input.charCodeAt(i++);

			chr3 = input.charCodeAt(i++);

 

			enc1 = chr1 >> 2;

			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);

			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);

			enc4 = chr3 & 63;

 

			if (isNaN(chr2)) {

				enc3 = enc4 = 64;

			} else if (isNaN(chr3)) {

				enc4 = 64;

			}

			output = output +

			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +

			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

 

		return output;

	},

 

	// public method for decoding

	decode : function (input) {

		var output = "";

		var chr1, chr2, chr3;

		var enc1, enc2, enc3, enc4;

		var i = 0;

 

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

 

		while (i < input.length) {

 

			enc1 = this._keyStr.indexOf(input.charAt(i++));

			enc2 = this._keyStr.indexOf(input.charAt(i++));

			enc3 = this._keyStr.indexOf(input.charAt(i++));

			enc4 = this._keyStr.indexOf(input.charAt(i++));

 

			chr1 = (enc1 << 2) | (enc2 >> 4);

			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);

			chr3 = ((enc3 & 3) << 6) | enc4;

 

			output = output + String.fromCharCode(chr1);

 

			if (enc3 != 64) {

				output = output + String.fromCharCode(chr2);

			}

			if (enc4 != 64) {

				output = output + String.fromCharCode(chr3);

			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

 

	// private method for UTF-8 encoding

	_utf8_encode : function (string) {

		string = string.replace(/\r\n/g,"\n");

		var utftext = "";

 

		for (var n = 0; n < string.length; n++) {

 

			var c = string.charCodeAt(n);

 

			if (c < 128) {

				utftext += String.fromCharCode(c);

			}

			else if((c > 127) && (c < 2048)) {

				utftext += String.fromCharCode((c >> 6) | 192);

				utftext += String.fromCharCode((c & 63) | 128);

			}

			else {

				utftext += String.fromCharCode((c >> 12) | 224);

				utftext += String.fromCharCode(((c >> 6) & 63) | 128);

				utftext += String.fromCharCode((c & 63) | 128);

			}

		}

		return utftext;

	},

 

	// private method for UTF-8 decoding

	_utf8_decode : function (utftext) {

		var string = "";

		var i = 0;

		var c = c1 = c2 = 0;

 

		while ( i < utftext.length ) {

 

			c = utftext.charCodeAt(i);

 

			if (c < 128) {

				string += String.fromCharCode(c);

				i++;

			}

			else if((c > 191) && (c < 224)) {

				c2 = utftext.charCodeAt(i+1);

				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));

				i += 2;

			}

			else {

				c2 = utftext.charCodeAt(i+1);

				c3 = utftext.charCodeAt(i+2);

				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));

				i += 3;

			}

		}

		return string;

	}

}

  

  

  

function codeAddress(address,id) {

//    var address = document.getElementById("address").value;

    geocoder.geocode( { 'address': address}, function(results, status) {

      if (status == google.maps.GeocoderStatus.OK) {

        map.setCenter(results[0].geometry.location);



		var str="gmsave.php?id="+id+"&gmap="+results[0].geometry.location+"";

		document.getElementById("spy").src=str;

		// alert (id+' '+results[0].geometry.location);



        var marker = new google.maps.Marker({

            map: map, 

            position: results[0].geometry.location

        });

      } else {

        alert("Geocode was not successful for the following reason: " + status);

      }

    });

  }





  

  var image = new google.maps.MarkerImage(
    'gmap/icons/image_w.png',
  new google.maps.Size(27,32),
  new google.maps.Point(0,0),
  new google.maps.Point(14,32)
  );

  var shadow = new google.maps.MarkerImage(
    'gmap/icons/shadow_w.png',
  new google.maps.Size(47,32),
  new google.maps.Point(0,0),
  new google.maps.Point(14,32)
  );

var shape = {
  coord: [25,0,26,1,26,2,26,3,26,4,26,5,26,6,26,7,26,8,26,9,26,10,26,11,26,12,26,13,26,14,26,15,26,16,26,17,26,18,26,19,26,20,26,21,25,22,18,23,18,24,17,25,16,26,16,27,15,28,14,29,14,30,13,31,12,31,11,30,10,29,9,28,9,27,8,26,7,25,7,24,6,23,1,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,1,0,25,0],
  type: 'poly'
};

  

  
  var image_w = new google.maps.MarkerImage(
    'gmap/icons/image_w.png',
  new google.maps.Size(27,32),
  new google.maps.Point(0,0),
  new google.maps.Point(14,32)
  );

  var shadow_w = new google.maps.MarkerImage(
    'gmap/icons/shadow_w.png',
  new google.maps.Size(47,32),
  new google.maps.Point(0,0),
  new google.maps.Point(14,32)
  );



var shape_w = {
  coord: [25,0,26,1,26,2,26,3,26,4,26,5,26,6,26,7,26,8,26,9,26,10,26,11,26,12,26,13,26,14,26,15,26,16,26,17,26,18,26,19,26,20,26,21,25,22,18,23,18,24,17,25,16,26,16,27,15,28,14,29,14,30,13,31,12,31,11,30,10,29,9,28,9,27,8,26,7,25,7,24,6,23,1,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,1,0,25,0],
  type: 'poly'
};


  
  var image_a = new google.maps.MarkerImage(
    'gmap/icons/image_a.png',
  new google.maps.Size(27,32),
  new google.maps.Point(0,0),
  new google.maps.Point(14,32)
  );

  var shadow_a = new google.maps.MarkerImage(
    'gmap/icons/shadow_a.png',
  new google.maps.Size(47,32),
  new google.maps.Point(0,0),
  new google.maps.Point(14,32)
  );


var shape_a = {
  coord: [25,0,26,1,26,2,26,3,26,4,26,5,26,6,26,7,26,8,26,9,26,10,26,11,26,12,26,13,26,14,26,15,26,16,26,17,26,18,26,19,26,20,26,21,25,22,18,23,18,24,17,25,16,26,16,27,15,28,14,29,14,30,13,31,12,31,11,30,10,29,9,28,9,27,8,26,7,25,7,24,6,23,1,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,1,0,25,0],
  type: 'poly'
};




var image_ty = new google.maps.MarkerImage(

  'gmap/icons/image_ty.png',

  new google.maps.Size(20,26),
  new google.maps.Point(0,0),
  new google.maps.Point(10,26)

);



var shadow_ty = new google.maps.MarkerImage(

  'gmap/icons/shadow_ty.png',

  new google.maps.Size(36,26),
  new google.maps.Point(0,0),
  new google.maps.Point(10,26)

);



var shape_ty = {
  coord: [18,0,19,1,19,2,19,3,19,4,19,5,19,6,19,7,19,8,19,9,19,10,19,11,19,12,19,13,19,14,19,15,19,16,19,17,19,18,18,19,14,20,13,21,12,22,12,23,11,24,10,25,8,25,7,24,7,23,6,22,5,21,5,20,1,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,1,0,18,0],
  type: 'poly'
};





 

  

 

function g_set_marker (lat_lng,opis,typ) {



my_image  = image;

my_shadow = shadow;

my_shape  = shape;



if (typ=='w')

{

my_image  = image_w;

my_shadow = shadow_w;

my_shape  = shape_w;

}



if (typ=='a')
{
my_image  = image_a;
my_shadow = shadow_a;
my_shape  = shape_a;
}



if (typ=='db' || typ=='bd')

{

my_image  = image_db;

my_shadow = shadow_db;

my_shape  = shape_db;

}



if (typ=='j')

{

my_image  = image_j;

my_shadow = shadow_j;

my_shape  = shape_j;

}





if (typ=='ty')
{
my_image  = image_ty;
my_shadow = shadow_ty;
my_shape  = shape_ty;
}



// var myLatLng = new google.maps.LatLng(gloc1,gloc2);

          var marker = new google.maps.Marker({

		 	icon: my_image,

			shadow: my_shadow,

			shape: my_shape,

            map: map, 

            position: lat_lng

        }); 

		

		var infowindow = new google.maps.InfoWindow({  

		content: opis  

		});  

		

		google.maps.event.addListener(marker, 'click', function() {  

		infowindow.open(map, marker);  

		}); 



markersArray.push(marker);

}





function clearOverlays() {

  if (markersArray) {

    for (i in markersArray) {

      markersArray[i].setMap(null);

    }

  }

}



// Shows any overlays currently in the array

function showOverlays() {

  if (markersArray) {

    for (i in markersArray) {

      markersArray[i].setMap(map);

    }

  }

}



// Deletes all markers in the array by removing references to them

function deleteOverlays() {

  if (markersArray) {

    for (i in markersArray) {

      markersArray[i].setMap(null);

    }

    markersArray.length = 0;

  }

}





 

function find_nearest () 
{
deleteOverlays();
typ='';
typ = typ + '';
if (document.getElementById("ch_a").checked) typ = typ+'a';
if (document.getElementById("ch_w").checked) typ = typ+'w';

// alert (typ);

var address = document.getElementById("address").value;

if (false)
{
var tojadar=0;
var exp = /jadar/i;
xxx = address.search(exp)
if (xxx>=0) {typ='j'; tojadar=1; address="Radom, "}
}




    geocoder.geocode( { 'address': address+ ' Polska'}, function(results, status) {

      if (status == google.maps.GeocoderStatus.OK ) {

		var bounds = new google.maps.LatLngBounds();

		turl= "gm_nearest.php?typ="+typ+"&gmap="+results[0].geometry.location

		//alert (turl);

var html = $.ajax({

  url: turl,

   dataType: "html",

  async: false

 }).responseText;



 

/////////////////////////////////////// 

// ZAPISZ DO BAZY

///////////////////////////////////////



	var str="gmap/gmsave.php?address="+address+"&typ="+typ+"&gmap="+results[0].geometry.location+"";

	// alert(str);

	document.getElementById("spy").src=str; 

 

///////////////////////////////////////

// MARKER NAJBLIŻSZEGO PUNKTU

///////////////////////////////////////

arr_opisy=html.split('#');



for (i=0;i<arr_opisy.length-1;i++)

{

// alert (arr_opisy[i]); 

		a_opis=arr_opisy[i].split('|');

		cel_id		=a_opis[0];

		cel_nazwa	=a_opis[1];

		cel_typ		=a_opis[2];

		cel_adres	=a_opis[3];

		cel_telefon		=a_opis[4];

		cel_lat		=a_opis[5];

		cel_lng		=a_opis[6];

		cel_distance	=a_opis[7];

	

		opis = "Odległość: "+cel_distance+" km<br /><b>"+cel_nazwa+"</b><br />"+cel_adres+"<br />tel."+cel_telefon;

lat_lng = new google.maps.LatLng(cel_lat,cel_lng);

g_set_marker (lat_lng,opis,cel_typ)

		

		bounds.extend(lat_lng);



}



///////////////////////////////////////

// MARKER PUNKTU POBYTU

///////////////////////////////////////



g_set_marker (results[0].geometry.location,'Tu jesteś!','ty')

bounds.extend(results[0].geometry.location);





/////////////////////////////////////





        map.fitBounds(bounds);





      } else {

        alert("Nie znaleźliśmy lokalizacji. Podaj dokładny adres twojej lokalizacji: miejscowość, kod pocztowy, ulicę i numer budynku!");

      }

    });

}







function show_points()

{

var bounds = new google.maps.LatLngBounds();

for (i=0;i<a_id.length;i++){  

g_set_marker(a_gloc1[i],a_gloc2[i],a_id[i],a_typ[i]);

bounds.extend(new google.maps.LatLng(a_gloc1[i],a_gloc2[i]));

}

map.fitBounds(bounds);

}


