function showHideNewObservation()
{
	var newObservationDiv = document.getElementById("newObservation")
	
	if(newObservationDiv.style.display == "none")
	{
		newObservationDiv.style.display = "";
		document.getElementById("observationsContainerDiv").style.display = "none";
	}
	else
	{
		newObservationDiv.style.display = "none";
		document.getElementById("observationsContainerDiv").style.display = "";
	}
}

function validateAddObservation()
{
	var error = 0;
	
	// Check if lan is choosen
	var lanIndex = document.getElementById("lan").selectedIndex;
	var lanValue = document.getElementById("lan").options[lanIndex].value;
	if(!checkFieldEmpty(lanValue, "lan"))
	{
		Form.errors = 1;
		Form.emptyFields = 1;
	}
	
	// Check if kommun is choosen
	var kommunIndex = document.getElementById("kommun").selectedIndex;
	var kommunValue = document.getElementById("kommun").options[kommunIndex].value;
	if(!checkFieldEmpty(kommunValue, "kommun"))
	{
		Form.errors = 1;
		Form.emptyFields = 1;
	}
	
	// If area is visible, check if kommun is choosen
	if(document.getElementById("area").style.visibility != "hidden")
	{
		var areaIndex = document.getElementById("area").selectedIndex;
		var areaValue = document.getElementById("area").options[areaIndex].value;
		if(!checkFieldEmpty(areaValue, "area"))
		{
			Form.errors = 1;
			Form.emptyFields = 1;
		}
	}
	
	// Check if headline is choosen
	if(!checkFieldEmpty(document.getElementById("headline").value, "headline"))
	{
		Form.errors = 1;
		Form.emptyFields = 1;
	}
	
	// Check if description is choosen
	if(!checkFieldEmpty(document.getElementById("description1").value, "description1"))
	{
		Form.errors = 1;
		Form.emptyFields = 1;
	}
}

// Selectedvalue = if you want a value to be selected from start. May be empty
function updateFilterKommuner(dropDown, curSelectedValue)
{

	var selectedIndex = dropDown.selectedIndex;
	var selectedValue = dropDown.options[selectedIndex].value;
	
	// HTML-objects
	var kommunDropDown = document.getElementById("filterKommunID");
	var areaDropDown = document.getElementById("filterAreaID");
	
	// Empty kommunDropDown
	removeOptions(kommunDropDown);
	
	// Empty areaDropDown
	if(areaDropDown.style.visibility == "visible")
	{
		removeOptions(areaDropDown);
	}
	
	// Hide area dropdown
	areaDropDown.style.visibility = "hidden";
	
	// Get kommuner
	var kommuner = String("" + AJAXGetKommuner(selectedValue));
	kommunerArray = kommuner.split("*");
	
	// Add "standard" text to kommun dropdown
	addOption(kommunDropDown, "Välj kommun", "");
	
	// Loop through and add kommuner to kommunDropDown
	var curSelectedIndex;
	for(var i = 0; i < kommunerArray.length - 1; i++)
	{
		var values = kommunerArray[i].split(",");
		addOption(kommunDropDown, values[1], values[0]);

		if(values[0] == curSelectedValue)
		{
			curSelectedIndex = i + 1; // + 1 because a standardvalue is shown "välj kommun"
		}
	}
	
	// If curSelectedIndex is set
	if(curSelectedIndex)
	{
		kommunDropDown.selectedIndex = curSelectedIndex;
	}
}

function updateFilterAreas(dropDown, curSelectedValue)
{
	var selectedIndex = dropDown.selectedIndex;
	var selectedValue = dropDown.options[selectedIndex].value;
	
	// HTML-objects
	var areaDropDown = document.getElementById("filterAreaID");
	
	// Empty areaDropDown
	removeOptions(areaDropDown);

	// Get areas
	var areas = AJAXGetAreas(selectedValue);
	areasArray = areas.split("*");
	
	// Set "standard" text
	addOption(areaDropDown, "Välj område", "");
	
	// Check if kommun contains areas
	if(areasArray.length > 1)
	{
		areaDropDown.style.visibility = "visible";
		
		// Loop through and add kommuner to kommunDropDown
		var curSelectedIndex;
		for(var i = 0; i < areasArray.length - 1; i++)
		{
			var values = areasArray[i].split(",");
			addOption(areaDropDown, values[1], values[0]);
			
			if(values[0] == curSelectedValue)
			{
				curSelectedIndex = i + 1; // + 1 because a standardvalue is shown "välj kommun"
			}
		}
		
		// If curSelectedIndex is set
		if(curSelectedIndex)
		{
			areaDropDown.selectedIndex = curSelectedIndex;
		}
	}
	else
	{
		areaDropDown.style.visibility = "hidden";
	}
}
var map = "";
var markers = new Array();
var newMarker = new google.maps.Marker({
     
	});
function initializeMap() {
	var lcLat =  61.5042;
	var lcLng =  17.4200;
	
	var localCenter = new google.maps.LatLng(Number(lcLat),Number(lcLng));
	
	
	var myOptions = {
	      zoom: 4,
	      center: localCenter,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
   	};
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	newMarker.setPosition(localCenter); 
    newMarker.setMap(map);
    newMarker.setTitle("Händelsens position");
	newMarker.setVisible(false);
	
	
	google.maps.event.addListener(map, 'click', function(event) {
		var myLatlng = event.latLng;
		placeNewMarker(myLatlng, 16);
	});
	markAddress();
		
}
function placeNewMarker(coords, zoom){
	var myLatlng = new google.maps.LatLng(Number(coords.lat()),Number(coords.lng()));
		newMarker.setPosition(myLatlng);
		newMarker.setVisible(true);
		map.setCenter(myLatlng);
		map.setZoom(zoom);
		var myLatLngShort = myLatlng.toUrlValue().split(",");
		$("#oLat").val(myLatLngShort[0]);
		$("#oLng").val(myLatLngShort[1]);
}

function markAddress(){
	var selected = $("#kommun option:selected");
	if(selected.val() != 0){
		   var kommun = selected.text();
	}else{
		kommun = "";
	}
	
	findCoord(kommun);
}
function findCoord(kommun){
	geocoder3 = new google.maps.Geocoder();
	geocoder3.geocode( { "address": kommun+", sverige"}, function(results, status3) {
		if (status3 == google.maps.GeocoderStatus.OK) {
			var c = results[0].geometry.location;
			placeNewMarker(c, 12);
		}else{
			geocoder3.geocode( { "address": "sverige"}, function(results, status3) {
				if (status3 == google.maps.GeocoderStatus.OK) {
					var c = results[0].geometry.location;
					placeNewMarker(c, 3);
				}else{
					alert("Error:"+status3);
				}
			});
		}
			
	});
}

function showMapLink(){
	if ($("#oLat").val() == ""){ 
		$("#confirmMapLink").show();
	}
}

$(document).ready(function() {
	$("#confirmMapLink").click(function() {
		$("#mapContainer").slideDown(function(){
			if (map==""){
				initializeMap();
			}
			
		});
	});
		
	$("#saveLink").click(function() {
		$("#mapContainer").slideUp();

	});
		
});
