var map;
var geocoder;
var stepDisplay;
var bounds;
var markerCollection;

$(document).ready(function(){
	
	if ($("#wojewodztwoSB").size() > 0)
	$("#wojewodztwoSB").change(function(){
		$.get("/dodaj/cities/wojid/"+$(this).attr("value")+"/", function(data){
			$("#citySB").html(data);
			$("#citySB").attr("disabled", "");
		});
	});
	
	$("#header_menu ul li a").hover(
		function(){
			var img = $(this).children("img");
			var src = img.attr("src").replace(".gif","_over.gif");
			
			$(this).children("img").attr("src", src);
		},
		function() {
			var img = $(this).children("img");
			var src = img.attr("src").replace("_over.gif",".gif");
			
			$(this).children("img").attr("src", src);
		}
	);
	
	load();
	
	if ($("#szukaj_na_mapie").length > 0) {
		$("#szukaj_na_mapie").click(function(){
			codeAddress();
		});
	}
});

function codeAddress() {
	var miasto = $("#citySB option:selected").text();
	var adres = $("#address").val();
    var address = miasto+", "+adres;
	
	if (miasto != '' && $("#citySB:disabled").length == 0) {
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var point = results[0].geometry.location;
				addMarker(point);
			} else {
				message = "";
				if (status == "ZERO_RESULTS") {
					message = "nie znaleziono takiej lokalizacji";
				} else {
					message = status;
				}
				alert("Szukanie nie powiodło się ponieważ : " + message);
			}
		});
	} else {
		alert("Proszę wybrać miasto");
	}
}

function load() {
    if (GBrowserIsCompatible()) 
    {
		var latlng = new google.maps.LatLng(51.919438,19.145136);

		var myOptions = {
		  zoom: 6,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		}

		stepDisplay = new google.maps.InfoWindow(); 
		bounds = new google.maps.LatLngBounds(); 
		geocoder = new google.maps.Geocoder();
		markerCollection = new Array();
      	map = new google.maps.Map(document.getElementById("map"), myOptions);
		
		if ($("div.loadall").size() > 0)
			$.get('/hotspots/all/', processLocations );
		
		if ($("div.loadOne").size() > 0) { 
			processLocationsOne();
		}
		
		if ($("div.loadGroup").size() > 0)
		{
			var id = $("#place_id").attr("value");
			var type = $("#place_type").attr("value");
			$.get('/hotspots/group/param/'+id+","+type+"/", processLocations );
		}
			
		if ($(".addHotspot").size() > 0) {
			$("form.addHotspot").submit(function (){
				marker = markerCollection.pop();
				position = marker.getPosition();
				
				$("#latitude").attr("value", position.lat());
				$("#longitude").attr("value", position.lng());
				
				return true;
			});
		}
    }
}

function processLocations(content) {
    eval("locations = "+content);
    locations.forEach(function(element, index, array) {
		var markerLatLng = new google.maps.LatLng(element.latitude, element.longitude); 
        var marker = new google.maps.Marker({
            map: map, 
			title: element.name,
			clickable: true,
			flat: true,
            position: markerLatLng
        });
		
		bounds.extend(markerLatLng); 
		markerCollection.push(marker);
		
        google.maps.event.addListener(marker, 'click', function() {
			stepDisplay.setContent('<div class="chmurka"><strong>'+element.place+'</strong><br />'+element.code+' '+element.miasto+'<br />'+element.address+'<br /><p class="right"><a href="http://www.wifi4free.pl/hotspot/wifi/param/'+element.wifi_id+','+element.miasto_link+'/">Pokaz szczegoly</a></p></div>');
			stepDisplay.open(map, marker);
			$(".chmurka").parent().css("overflow-y","visible");
        });
    });
    
    zoomShowAll();
}

function processLocationsOne() {
	var latitude = $("#latitude").attr("value");
	var longitude = $("#longitude").attr("value");
	
	var markerLatLng = new google.maps.LatLng(latitude, longitude); 
	
	var marker = new google.maps.Marker({
		map: map, 
		flat: true,
		clickable: false,
		position: markerLatLng
	});
	
	bounds.extend(markerLatLng); 
	
	zoomShowAll();
	map.setZoom(14); 
}

function addMarker(point) {
	removeAllMarks();
	var marker = new google.maps.Marker({
		map: map, 
		flat: true,
		clickable: true,
		draggable: true,
		position: point
	});
	
	markerCollection.push(marker);
	bounds.extend(point); 
	
	zoomShowAll();
	map.setZoom(16);
}
function removeAllMarks() {
	while(m = markerCollection.pop()) {
		m.setMap(null);
	}
	bounds = new google.maps.LatLngBounds(); 
}
function zoomShowAll() {
	map.fitBounds(bounds);
}
