var GoogleMapsSearch = {
	config: {
		init: {
			mapZoom: 6,
			mapCenter: new google.maps.LatLng(51.9194380, 19.1451360),
			strokeColor: "#000000",
			style: null,
			minValue: 1,
			maxValue: 19,
			sliderValue: 10
		},
		calculateRadius: {
			radius: [5916575, 2958287, 1479143, 739571, 369785, 184892, 92446, 46223, 23111, 11555, 5777, 2888, 1444, 722, 361, 180, 90, 45, 22, 11]
		},
		search: {
			url: null,
			pageId: null,
			dataSelectors: {},
			dataHtmlSelector: null,
			backButton: null
		},
		placeCircleMarker: {
			icon: null
		},
		placeMarker: {
			icon: null
		}
	},
	map: null,
	marker: null,
	circle: null,
	markers: [],
	markerClusterer: null,
	
	init: function() {
		var mapZoom = parseInt($("input#map_zoom").val());
		
		if (isNaN(mapZoom)) {
			mapZoom = this.config.init.mapZoom;
		}
		
		$("input#map_zoom").val(mapZoom);
		
		var mapCenter = this.config.init.mapCenter;
		
		var mapCenterLat = parseFloat($("input#map_center_lat").val());
		var mapCenterLng = parseFloat($("input#map_center_lng").val());
		
		if (!isNaN(mapCenterLat) && !isNaN(mapCenterLng)) {
			mapCenter = new google.maps.LatLng(mapCenterLat, mapCenterLng);
		}
		
		mapCenterLat = mapCenter.lat();
		mapCenterLng = mapCenter.lng();
		
		$("input#map_center_lat").val(mapCenterLat);
		$("input#map_center_lng").val(mapCenterLng);
		
		this.map = new google.maps.Map(document.getElementById("map"), {
			zoom: mapZoom,
			center: mapCenter,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		});
		
		google.maps.event.addListener(this.map, "zoom_changed", function() {
			var mapZoom = GoogleMapsSearch.map.getZoom();
			
			$("input#map_zoom").val(mapZoom);
			
			if (mapZoom < 0) {
				mapZoom = 0;
			}
			
			if (mapZoom > 19) {
				mapZoom = 19;
			}
			
			var radius = parseInt($("input#radius").val());
			
			if (radius > 0) {
				var sliderValue = parseInt($("input#slider_value").val());
				
				radius = GoogleMapsSearch.calculateRadius(sliderValue, mapZoom);
				
				GoogleMapsSearch.circle.setRadius(radius);
				
				$("input#radius").val(radius);
				
				GoogleMapsSearch.search();
			}
		});
		
		google.maps.event.addListener(this.map, "idle", function() {
			$("input#map_center_lat").val(GoogleMapsSearch.map.getCenter().lat());
			$("input#map_center_lng").val(GoogleMapsSearch.map.getCenter().lng());
		});
		
		google.maps.event.addListener(this.map, "mousedown", function(event) {
			GoogleMapsSearch.placeCircleMarker(event.latLng);
			GoogleMapsSearch.search();
		});
		
		var sliderValue = parseInt($("input#slider_value").val());
		
		if (isNaN(sliderValue)) {
			sliderValue = this.config.init.sliderValue;
		}
		
		var options = {
			min: this.config.init.minValue,
			max: this.config.init.maxValue,
			value: sliderValue,
			step: 1,
			orientation: 'horizontal',
			slide: function(e, ui) {
				if (GoogleMapsSearch.circle.getMap()) {
					var zoom = GoogleMapsSearch.map.getZoom();
					var radius = GoogleMapsSearch.calculateRadius(ui.value, zoom);
					GoogleMapsSearch.circle.setRadius(radius);
					$("input#slider_value").val(ui.value);
					$("input#radius").val(radius);
				} else {
					$("input#slider_value").val('');
					$("input#radius").val('');
				}
			},
			change: this.search
		};
		
		$("input#slider_value").val(sliderValue);
		
		var radius = parseInt($("input#radius").val());
		
		if (radius > 0) {
			options.startValue = $("input#slider_value").val();
			
			$(".slider").slider(options);
			
			var lat = parseFloat($("input#lat").val());
			var lng = parseFloat($("input#lng").val());
  			var latLng = new google.maps.LatLng(lat, lng);
			
			this.placeCircleMarker(latLng);
		} else {
			$(".slider").slider(options);
			$(".slider").slider("disable");
		}
		
		this.markerClusterer = new MarkerClusterer(this.map, [], {
			styles: this.config.init.styles
		});
        
        
        google.maps.event.addDomListener(this.markerClusterer, "clusterclick", function(et) {
            GoogleMapsSearch.placeCircleMarker(et[0].getCenter());
            GoogleMapsSearch.search();
        });
        
        
	},
	
	calculateRadius: function(sliderValue, mapZoom) {
        jQuery( '#description_ext_info_all' ).hide();
        
		return Math.round(sliderValue * GoogleMapsSearch.config.calculateRadius.radius[mapZoom] / 5);
	},
	
	placeCircleMarker: function(latLng) {
        jQuery( '#description_ext_info_all' ).hide();
        
		$(".slider").slider("enable");
		
		var radius = parseInt($("input#radius").val());
		
		if (isNaN(radius)) {
			var sliderValue = $(".slider").slider("value");
			var mapZoom = GoogleMapsSearch.map.getZoom();
			
			radius = GoogleMapsSearch.calculateRadius(sliderValue, mapZoom);
		}
		
		$("input#radius").val(radius);
		
		if (this.marker && this.circle) {
			this.marker.setMap(this.map);
			this.marker.setPosition(latLng);
			this.circle.setMap(this.map);
			this.circle.setRadius(radius);
		} else {
			var options = {
				map: this.map,
				position: latLng,
				draggable: true
			}
			
			if (this.config.placeCircleMarker.icon != null) {
				var image = new google.maps.MarkerImage(
					this.config.placeCircleMarker.icon,
					new google.maps.Size(64, 64),
					new google.maps.Point(0, 0),
					new google.maps.Point(32, 32)
				);
				
				options.icon = image;
				
				var shape = {
					coord: [1, 32, 18, 28, 28, 18, 32, 1, 36, 16, 46, 28, 61, 32, 46, 36, 36, 46, 31, 61, 28, 46, 18, 36, 1, 32],
					type: 'poly'
				};
				
				options.shape = shape;
			}
			
			this.marker = new google.maps.Marker(options);
			
			google.maps.event.addListener(this.marker, "dragend", function() {
				GoogleMapsSearch.updateCircleMarkerPosition(GoogleMapsSearch.circle.getCenter());
				GoogleMapsSearch.search();
			});
			
			google.maps.event.addListener(this.marker, "click", function() {
				GoogleMapsSearch.marker.setMap(null);
				GoogleMapsSearch.circle.setMap(null);
				GoogleMapsSearch.updateCircleMarkerPosition(null);
				
				$("input#radius").val('');
				$(".slider").slider("disable");
				
				GoogleMapsSearch.search();
			});
			
			this.circle = new google.maps.Circle({
				map: this.map,
				radius: radius,
				fillColor: "#ffffff",
				fillOpacity: 0.0,
				strokeColor: this.config.init.strokeColor,
				strokeOpacity: 1,
				strokeWeight: 2
			});
			
			this.circle.bindTo("center", this.marker, "position");
		}
		
		this.map.setCenter(latLng);
		this.updateCircleMarkerPosition(latLng);
	},
	
	updateCircleMarkerPosition: function(latLng) {
        jQuery( '#description_ext_info_all' ).hide();
        jQuery( 'div.marker, .pager' ).show();
        
		if (latLng) {
			$("input#lat").val(latLng.lat());
			$("input#lng").val(latLng.lng());
		} else {
			$("input#lat").val('');
			$("input#lng").val('');
		}
	},
	
	placeMarker: function(markerData) {
		var latLng = new google.maps.LatLng(markerData["lat"], markerData["lng"]);
		
		var contentString = '<h1>' + markerData["name"] + '<\/h1><div style="padding: 5px;"><div>' + markerData['postal_code'] + ' ' + markerData['place'] + '</div><div>' + markerData['street'] + '</div><div>' + '</div><div> ' + markerData['phone'] + '</div>'+ markerData['description']+'<\/div>';
		
		var options = {
			position: latLng,
			title: markerData["name"],
            id: markerData["id"]
		};
		
		if (this.config.placeMarker.icon != null) {
			var image = new google.maps.MarkerImage(
				this.config.placeMarker.icon,
                new google.maps.Size(13, 37),
                new google.maps.Point(0, 0),
                new google.maps.Point(0, 37)
			);
			
			options.icon = image;
			/*
			var shape = {
				coord: [9, 0, 6, 1, 4, 2, 2, 4, 0, 8, 0, 12, 1, 14, 2, 16, 5, 19, 7, 23, 8, 26, 9, 30, 9, 34, 11, 34, 11, 30, 12, 26, 13, 24, 14, 21, 16, 18, 18, 16, 20, 12, 20, 8, 18, 4, 16, 2, 15, 1, 13, 0],
				type: 'poly'
			};
			
			options.shape = shape;*/
		}
		
		var marker = new google.maps.Marker(options);
		
		var infoWindow = new google.maps.InfoWindow({
			content: contentString
		});
		
		google.maps.event.addListener( marker, "click", function(){
            infoWindow.close();
            var data = {};
            
            prevZoom        = $("#map_zoom").val();
            prevLatitude    = $("#map_center_lat").val();
            prevLongitude   = $("#map_center_lng").val();
            prevPageId      = $("#page_id").val();
            
            $.ajax({
                complete: function(){
                    if( jQuery('#punkt' + marker.id ).html() != null ) ht = jQuery('#punkt' + marker.id ).html().valueOf();
                        else
                            ht = '';

                    $("#description_ext_info_all").html( ht + '<br/><br/><a href="javascript:backToList()">Powrót</a>' ).show();

                    infoWindow.open( GoogleMapsSearch.map, marker );
                },
                type: "POST",
                url: GoogleMapsSearch.config.search.url + "&markerId=" + marker.id,
                dataType: "json",
                success: function(data){
					GoogleMapsSearch.config.search.pageId = 0;
					GoogleMapsSearch.markers = new Array();

					$(GoogleMapsSearch.config.search.dataHtmlSelector).html( data.html );
					
					GoogleMapsSearch.markerClusterer.clearMarkers();
					
					for (i in data.markers) {
						GoogleMapsSearch.placeMarker(data.markers[i]);
					}
                }
            });
		});
		
		this.markerClusterer.addMarker(marker);
		
		GoogleMapsSearch.markers[i] = marker;
	},
	
	search: function() {
		var data = {};
		
		/*if(GoogleMapsSearch.config.search.backButton) {
			$(GoogleMapsSearch.config.search.backButton).hide();
		}*/
		
		for (i in GoogleMapsSearch.config.search.dataSelectors) {
		
			data[i] = $(GoogleMapsSearch.config.search.dataSelectors[i]).val()
		}
		
        if( jQuery( '#marker_c' ).val() ){
            markID = "&markerId=" + jQuery( '#marker_c' ).val();
            jQuery( '#marker_c' ).val('');
        }
        else
            markID = '';

		if (GoogleMapsSearch.config.search.url) { 
			$.post(
                
				GoogleMapsSearch.config.search.url + "&page=" + GoogleMapsSearch.config.search.pageId + markID,
				data,
				function(data, textStatus, XMLHttpRequest) {
					
					GoogleMapsSearch.config.search.pageId = 0;
					GoogleMapsSearch.markers = new Array();

					$(GoogleMapsSearch.config.search.dataHtmlSelector).html( data.html );
					
					GoogleMapsSearch.markerClusterer.clearMarkers();
					
					for (i in data.markers) {
						GoogleMapsSearch.placeMarker(data.markers[i]);
					}
				},
				"json"
			);
		}
	}
}

GoogleMapsz = function(mapId, latSelector, lngSelector) {
	this.mapId = mapId;
	this.latSelector = latSelector;
	this.lngSelector = lngSelector;
	
	this.config = {
		init: {
			mapZoom: 6,
			mapCenter: new google.maps.LatLng(51.9194380, 19.1451360)
		},
		placeMarker: {
			icon: null
		},
		geocodeAddress: {
			selectors: []
		}
	}
	
	this.geocoder = null;
	this.map = null;
	this.marker = null;
	
	this.updateMarkerPosition = function(latLng) {
		if (latLng) {
			$(this.latSelector).val(latLng.lat());
			$(this.lngSelector).val(latLng.lng());
		} else {
			$(this.latSelector).val('');
			$(this.lngSelector).val('');
		}
	}
	
	this.init = function() {
		this.geocoder = new google.maps.Geocoder();
		
		var mapZoom = this.config.init.mapZoom;
		var latLng = this.config.init.mapCenter;
		var lat = parseFloat($(this.latSelector).val());
		var lng = parseFloat($(this.lngSelector).val());
		
		var placeMarker = false;
		
		if (!isNaN(lat) && !isNaN(lng)) {
			placeMarker = true;
			latLng = new google.maps.LatLng(lat, lng);
			mapZoom = 16;
		}
		
		this.map = new google.maps.Map(document.getElementById(this.mapId), {
			zoom: mapZoom,
			center: latLng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		});
		
		if (placeMarker) {
			this.placeMarker(latLng);
		} else {
			this.marker = null;
		}
		
		google.maps.event.addListener(this.map, "click", 
		(
			function(GoogleMapsPlaceMarker)
			{ 
				return function(event) 
				{
					GoogleMapsPlaceMarker.placeMarker(event.latLng);
				}		
			}
		)
		(this)
		);
	}
	
	this.placeMarker = function(latLng) {
		if (this.marker) {
			this.marker.setMap(this.map);
			this.marker.setPosition(latLng);
		} else {
			var options = {
				position: latLng,
				map: this.map,
				draggable: true
			};
			
			if (this.config.placeMarker.icon != null) {
				var image = new google.maps.MarkerImage(
                    this.config.placeMarker.icon,
                    new google.maps.Size(13, 37),
                    new google.maps.Point(0, 0),
                    new google.maps.Point(0, 37)
				);
				
				options.icon = image;
				/*
				var shape = {
					coord: [9, 0, 6, 1, 4, 2, 2, 4, 0, 8, 0, 12, 1, 14, 2, 16, 5, 19, 7, 23, 8, 26, 9, 30, 9, 34, 11, 34, 11, 30, 12, 26, 13, 24, 14, 21, 16, 18, 18, 16, 20, 12, 20, 8, 18, 4, 16, 2, 15, 1, 13, 0],
					type: 'poly'
				};
				
				options.shape = shape;*/
			}
			
			this.marker = new google.maps.Marker(options);
			
			google.maps.event.addListener(this.marker, "dragend", (function(GoogleMapsPlaceMarker) {return function() {
				GoogleMapsPlaceMarker.updateMarkerPosition(
					this.getPosition()
				);
			}})(this));
			
			google.maps.event.addListener(this.marker, "click", (function(GoogleMapsPlaceMarker) {return function() {
				GoogleMapsPlaceMarker.marker.setMap(null);
				GoogleMapsPlaceMarker.updateMarkerPosition(null);
			}})(this));
		}
		
		this.map.setCenter(latLng);
		this.updateMarkerPosition(latLng);
	}
	
	this.geocodeAddress = function() {
		if (this.geocoder) {
			var address = [];
			
			for (var i = 0; i < this.config.geocodeAddress.selectors.length; i++) {
				address[i] = $(this.config.geocodeAddress.selectors[i]).val();
			}
			
			this.geocoder.geocode({'address': address.join(' ')}, (function(GoogleMapsPlaceMarker) {return function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					GoogleMapsPlaceMarker.map.setCenter(results[0].geometry.location);
				}
			}})(this));
		}
	}
}

jQuery.fn.extend( {
    showAjaxIndicator: function() {
    	var div = $('<div class="jq_progress2"></div>');
        this.before(div);

        div.css('width', this.get(0).offsetWidth + 'px');
        div.css('height', this.get(0).offsetHeight + 'px');
        div.css('top', this.get(0).offsetTop + 'px');
        div.css('left', this.get(0).offsetLeft + 'px');
    }
});

jQuery.fn.extend( {
    hideAjaxIndicator: function() {
        $(this).prev('div.jq_progress2').remove();
    }
});
