$(document).ready( function() {
	if( $('#mapa') ) {

		var map;
		var mapGeocoder;
		var mapMarkers = {};
		var mapItems = [];
		var mapItemsPointer = 0;
		var mapPointZoom = 15;
		var mapRegionId = 0;
		var mapLocationUrl = document.location.href;
		var re = /\/(\d+)\/(\d+)\/?/;
		if( mapLocationUrl.match( re ) ) {
			mapLocationUrl = mapLocationUrl.replace( re, '/$1/' );
		}

		function updateRegionPoints( markerRegionId, markerPointId ) {
			$.post(
				'ajax.php?get=places',
				{
					regionId: markerRegionId,
					skpId: markerPointId,
					sksId: mapServiceId
				},
				function( response ) {
					$('.adresy:first').find('div').each( function( i, obj ) {
						if( !$(obj).hasClass('default') ) {
							$(obj).remove();
						}
					});
					if( $(response).find('place').length ) {
						var places = "";
						$('.adresy:first').find('p').hide();
						$('.adresy:first').find('div.default').hide();
						$(response).find('place').each( function( i, obj ) {
							$('.adresy:first').append( $( $(obj).text() ) );
						});
					}
					else {
						$('.adresy:first').find('p').show();
						$('.adresy:first').find('div.default').show();
					}
					initAdresy();
				}
			);
		}

		function addMapItems() {
			if( mapItemsPointer < mapItems.length ) {
				mapGeocoder.getLocations( mapItems[ mapItemsPointer ][0], addMapItem );
			}
		}

		function addMapItem( response ) {
			if( response.Status.code == 620 ) {
			}
			else {
				if( response.Status.code == 200 ) {
					var place = response.Placemark[0];
					addMarker( new GLatLng( place.Point.coordinates[1], place.Point.coordinates[0] ), mapItems[ mapItemsPointer ][1], mapItems[ mapItemsPointer ][2] );
				}
				mapItemsPointer++;
			}
			setTimeout( addMapItems, 100 );
		}

		function getIcon() {
			var icon = new GIcon();
			icon.image = mapIconFile;
			icon.iconSize = new GSize( mapIconSize[0], mapIconSize[1] );
			icon.iconAnchor = new GPoint( mapIconAnchor[0], mapIconAnchor[1] );
			
			return icon;
		}

		function addMarker( point, redirectUrl, zoomToPoint ) {
			var urlParts = redirectUrl.split('/');
			if( urlParts[ urlParts.length - 1 ] != "" ) {
				urlPointer = urlParts.length - 1;
			}
			else {
				urlPointer = urlParts.length - 2;
			}
			var markerId = 'marker_' + urlParts[ urlPointer - 1 ] + '_' + urlParts[ urlPointer ];
			
			var marker = new GMarker( point, { icon: getIcon() } );
			GEvent.addListener( marker, 'click', function() {
				markerClick( markerId );
			});
			map.addOverlay( marker );

			mapMarkers[markerId] = marker;
			if( zoomToPoint ) {
				zoomMarker( marker );
			}
		}

		function markerClick( markerId ) {
			var markerIdParts = markerId.split('_');
			var markerRegionId = markerIdParts[1];
			var markerPointId = markerIdParts[2];
			if( markerRegionId != mapRegionId ) {
				updateRegionPoints( markerRegionId, markerPointId );
				mapRegionId = markerRegionId;
			}
			zoomMarker( mapMarkers[markerId] );
			activatePlace( markerId );
		}

		function zoomMarker( marker ) {
			map.setZoom( mapPointZoom );
			map.panTo( marker.getLatLng() );
		}

		function activatePlace( markerId ) {
			$('.adresy:first').find('div').each( function( i, obj ) {
				if( $(obj).attr('id') == markerId ) {
					$(obj).addClass('active');
				}
				else {
					$(obj).removeClass('active');
				}
			});
		}

		function initAdresy() {
			if( $('.adresy').length ) {
				$('.adresy:first').find('div').each( function( i, obj ) {
					$(obj).bind( 'mouseenter mouseleave', function( e ) {
						$(obj).toggleClass('over');
					});
					$(obj).bind( 'click', function( e ) {
						markerClick( $(obj).attr('id') );
					});
				});
				if( $('.adresy:first').find('div.active').length ) {
					markerClick( $('.adresy:first').find('div.active:first').attr('id') );
				}
				else {
					map.returnToSavedPosition();
				}
			}
		}

		if( $('select[name=regionId]') ) {
			$('select[name=regionId]').each( function( i, obj ) {
				$(obj).bind('change', function() {
					var urlParts = $(obj).val().split('/');
					updateRegionPoints( urlParts[ urlParts.length - 2 ], '' );
				});
			});
		}

		if( $('.menu03').length ) {
			$('.menu03').find('a').each( function( i, obj ) {
				$(obj).bind( 'click', function() {
					var urlParts = $(obj).attr('href').split('/');
					markerClick( 'marker_' + urlParts[ urlParts.length - 3 ] + '_' + urlParts[ urlParts.length - 2 ] );
					return false;
				});
			});
		}

		if( GBrowserIsCompatible() ) {
			var items = $('#mapa > span');
			
			map = new GMap2( document.getElementById('mapa') );
			map.clearOverlays();
			map.setCenter( new GLatLng( 52.054724, 18.90564 ), 6 );
			map.addControl( new GSmallMapControl() );
			map.enableScrollWheelZoom();
			map.savePosition();

			mapGeocoder = new GClientGeocoder();
			mapGeocoder.setCache( null );
			
			if( items.length ) {
				items.each( function( i, obj ) {
					var url = $(obj).find('a:first').attr('href');
					var selected = ( $(obj).attr('class') && $(obj).attr('class') == "selected" );
					if( $(obj).find('input[name=x]').length ) {
						var x = $( $(obj).find('input[name=x]:first') ).val();
					}
					if( $(obj).find('input[name=y]').length ) {
						var y = $( $(obj).find('input[name=y]:first') ).val();
					}
					var adr = $( $(obj).find('input[name=address]:first') ).val();
					if( x && y ) {
						addMarker( new GLatLng( x, y ), url, selected );
					}
					else {
						mapItems[ mapItems.length ] = [ adr, url, selected ];
					}
				});
				if( mapItems.length > 0 ) {
					addMapItems();
				}
			}
		}
	}
	
	initAdresy();
});