/*!
	Copyright (c) 2009-2010 Kampeerkaart
	kampeerkaart.js: map related functions
 */

// some global variables, regrettably needed
var m_markerSelected;
var m_strLLZ = "";

// start of jquery stuff //////////////////////////////////
$(document).ready(function()
{
	var MapCenter = [46.263, 3.4936];

	// initialize the jMaps map
	$('#mapcampsites').jmap('init', {
		'mapType': 'hybrid',
		'mapCenter': MapCenter,
		'mapZoom': 6,
		'mapEnableScrollZoom': false,
		'mapShowjMapsIcon': false,
		'mapEnableType': true,
		'mapEnableScaleControl': true,
		'mapControl': 'large',
		'mapEnableSmoothZoom': false // disabled because of a reported zoom-bug at google
	},
	function(objMap, element, options)
	{
		// bind the mapdata to the map div
		$("#mapcampsites").data("objmap", objMap);

		// specifically on MoveEnd
		GEvent.addListener(objMap, "moveend", function()
		{
			populatePage();
		});

		// save position
		GEvent.addListener(objMap, "movestart", function()
		{
			savePosition();
		});

		// get current LLZ, for general purposes
		GEvent.addListener(objMap, "moveend", function()
		{
			m_strLLZ = getCurrentLLZ();
		});

		// hide infowindow
		GEvent.addListener(objMap, "movestart", function()
		{
			$("#infowin").hide();
		});
		GEvent.addListener(objMap, "zoomend", function()
		{
			$("#infowin").hide();
		});
		GEvent.addListener(objMap, "click", function()
		{
			$("#infowin").hide();
		});

	});

	// specifically for campsite details page
	$("#panoramiopanel").ready(function()
	{
		loadPanoramioPanel();
	});

	// specifically for search result page
	$("#searchresult").ready(function() // doesn't work well yet. Fix soon.
	{
		getPreferences();
	});

	// preferences
	getPreferences();

	// validate contactform
	$("#contactform").ready(function()
	{
		validateContactForm();
	});
	// validate feedbackform
	$("#feedbackform").ready(function()
	{
		validateFeedbackForm();
	});
	// validate tipform
	$("#tipform").ready(function()
	{
		validateTipForm();
	});
	// validate advertiserform
	$("#advertiserform").ready(function()
	{
		validateAdvertiserForm();
	});
	// validate tipform
	$("#newcampsiteform").ready(function()
	{
		validateNewCampsiteForm();
	});

	// characters left for you review
	$("#campsiteadmin_description").keyup(function()
	{
		$('#campsiteadmin_description_submit').removeAttr('disabled');

		var intMax = 1000;
		var intRemaining = (intMax - $("#campsiteadmin_description").val().length);

		if (intRemaining > 0)
			$("#counter_remaining").html("(You have " + intRemaining + " characters left.)");
		else
			$("#counter_remaining").html("<span style='color:red;'>(You have exceeded the maximum number of characters. Only the first " + intMax + " will be saved!)</span>");
	});

});

// ////////////////////////////////////////////////////////////////////////////////////////////////

function populatePage()
{
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		// bounding box of the map itself
		var GLatLngBounds = objMap.getBounds();
		var GZoom = objMap.getZoom();

		// get the center
		var objCenter = GLatLngBounds.getCenter();

		// get lat/lng values for Northeast and Southwest corners of the map
		var objNE = new GLatLng(GLatLngBounds.getNorthEast().y, GLatLngBounds.getNorthEast().x);
		var objSW = new GLatLng(GLatLngBounds.getSouthWest().y, GLatLngBounds.getSouthWest().x);

		// get AND trigger preferences
		getPreferences();
		getMapSize();
		getCitySearch();

		// populate the map with campsite icons
		loadCampsiteMarkers(objNE, objSW);

		// get campsites count
		loadCampsiteCount(objNE, objSW, objCenter);

		// get panoramio
		if (GZoom > 13)
			loadPanoramio(objCenter, objNE, objSW);

		// specifically for map page:
		// campsite ad
		$("#campsitead").ready(function()
		{
			getCampsiteAd();
		});
		// campsites nearby
		$("#campsitesnearby").ready(function()
		{
			if (GZoom > 13)
				getCampsitesNearby();
		});

		$("#infowin").hide();
	}
}

function toggleHelp()
{
	// show or hide panel
	if ($("#helppanel").is(":visible"))
	{
		$("#helppanel").slideUp("normal");
		$("#helpcontent").html("");
	}
	else
	{
		// get the html
		$.ajax(
		{
			method: "get",
			url: "help.html",
			data: "&r=" + getRandom(),
			success: function(html)
			{
				$("#helpcontent").html(html);
			}
		});

		$("#helppanel").slideDown("normal");
	}
}

function submitContact()
{
	var strMessage;
	var strName;
	var strEmail;

	strMessage = $("#contact_message").val();
	strName = $("#contact_name").val();
	strEmail = $("#contact_email").val();

	$.ajax(
		{
			method: "post",
			url: "/process.aspx",
			data: "option=contactus&contact_message=" + strMessage + "&contact_name=" + strName + "&contact_email=" + strEmail + "&r=" + getRandom(),
			success: function(html)
			{
				$("#contactcontent").slideUp("normal");
				$("#contactfeedback").slideDown("normal");
				$("#contactfeedback").html(html);
			}
		});
}

function reloadContact()
{
	// re-show contactform, use only after feedback, when entered values must be preserved
	$("#contactcontent").slideDown("normal");
	$("#feedback").hide();
}

function toggleMapSave()
{
	// show or hide panel
	if ($("#savemapform").is(":visible"))
	{
		$("#savemapform").slideUp("normal");
	}
	else
	{
		$("#savemapform").slideDown("normal");

		// get the form html
		$.ajax(
		{
			method: "post",
			url: "Data.aspx",
			data: "option=getsavemapform&r=" + getRandom(),
			success: function(html)
			{
				$("#savemapform").html(html);
			}
		});
	}
}

function toggleSearch(intType)
{
	var intMove = -150;
	if (intType == 1)
		intMove = 150;

	if ( intType==1 || $('#home_search').hasClass("open") == false)
	{
		$('#home_search').animate({
			top: '+=' + intMove
		}, 500, function()
		{
			// on animation complete
			if (intType == 0)
				$('#home_search').addClass("open");
			else
				$('#home_search').removeClass("open");
		});
	}

	return false;
}

// populate the map with campsite icons from XML
function loadCampsiteMarkers(objNE, objSW)
{
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		objMap.clearOverlays();

		$.ajax(
			{
				type: "get",
				url: "data.aspx?option=campsitesformap&latnorth=" + objNE.y + "&latsouth=" + objSW.y + "&lngwest=" + objSW.x + "&lngeast=" + objNE.x + "&r=" + getRandom(),
				dataType: "xml",
				success: function(xml)
				{
					$(xml).find('campsite').each(function()
					{
						var campsiteID = $(this).find('id').text();
						var campsiteLat = $(this).find('lat').text();
						var campsiteLng = $(this).find('lng').text();
						var strCampsiteName = $(this).find('name').text();
						var intStars = $(this).find('stars').text();
						var intPitches = parseInt($(this).find('pitches').text());
						var strSaved = $(this).find('saved').text();
						var intRv = parseInt($(this).find('rv').text());

						var intWidth;
						var intHeight;
						switch (intPitches)
						{
							case 1:
								intWidth = 26;
								intHeight = 26;
								break;
							case 2:
								intWidth = 34;
								intHeight = 34;
								break;
							case 3:
								intWidth = 46;
								intHeight = 46;
								break;
						}

						// get custom icon
						var iconCampsite = getIcon("http://www.kampeerkaart.nl/img/map/c_" + intPitches + strSaved + ".png", intWidth, intHeight, 10, 10, "", 0, 0, 0, 0);

						//jQuery('#mapcampsites').jmap('AddMarker', { 'pointLatLng': [campsiteLat, campsiteLng], 'pointIcon': iconCampsite });
						var markerOptions = { icon: iconCampsite };
						var marker = new GMarker(new GLatLng(campsiteLat, campsiteLng), markerOptions);
						objMap.addOverlay(marker);

						var blnMouseOver = false;
						// catch mouseover and out, but use a delay to prevent all too nervous behaviour when hovering the mouse over the map
						GEvent.addListener(marker, "mouseover", function()
						{
							blnMouseOver = true;
							window.setTimeout(function()
							{
								if (blnMouseOver)
								{
									showInfoWin(campsiteID, objMap.fromLatLngToContainerPixel(marker.getLatLng()), objMap.getZoom(), intPitches);
								}
							}, 400);
						});
						GEvent.addListener(marker, "mouseout", function()
						{
							blnMouseOver = false;
						});

						// catch click
						GEvent.addListener(marker, "click", function()
						{
							gotoCampsiteDetails(campsiteID); //showInfoWin(campsiteID, objMap.fromLatLngToContainerPixel(marker.getLatLng()), objMap.getZoom(), intPitches);
						});

					});
				}
			});
	}
}

// populate the results bar with search result
function loadCampsiteCount(objNE, objSW, objCenter)
{
	$.ajax(
		{
			method: "post",
			url: "Data.aspx",
			data: "option=count&latnorth=" + objNE.y + "&latsouth=" + objSW.y + "&lngwest=" + objSW.x + "&lngeast=" + objNE.x + "&r=" + getRandom(),
			success: function(html)
			{
				$("#mapfeedback").html(html);

				if (html.indexOf("campsiteid") >= 0)
				{
					var objMap = $("#mapcampsites").data("objmap");
					var intCampsiteID = document.getElementById("singlecampsiteid").value;
					var strLat = document.getElementById("singlecampsitelat").value;
					var strLng = document.getElementById("singlecampsitelng").value;
					var intPitches = document.getElementById("singlecampsitepitches").value;

					showInfoWin(intCampsiteID, objMap.fromLatLngToContainerPixel(new GLatLng(strLat, strLng)), objMap.getZoom(), intPitches);
				}
			}
		});
}

function showInfoWin(intCampsiteID, xy, intCurrentZoom, intPitches)
{
	var intOffsetX;
	var intOffsetY;

	switch (parseInt(intPitches))
	{
		case 1:
			intOffsetX = 0;
			intOffsetY = 29;
			break;
		case 2:
			intOffsetX = 8;
			intOffsetY = 25;
			break;
		case 3:
			intOffsetX = 20;
			intOffsetY = 19;
			break;
		default:
			intOffsetX = 0;
			intOffsetY = 0;
			break;
	}

	var X = (xy.x) + intOffsetX;
	var Y = (xy.y) - intOffsetY;

	$("#infowin").css({ left: X, top: Y });

	$.ajax(
		{
			method: "post",
			url: "Data.aspx",
			data: "option=detailsinfowin&id=" + intCampsiteID + "&currentzoom=" + intCurrentZoom + "&r=" + getRandom(),
			success: function(html)
			{
				$("#infowincontent").html(html);
			}
		});

	$("#infowin").show();

}

function showInfoWinPreview(strFilename, intPhotoID)
{
	var intTop = ($("#photo_" + intPhotoID).position().top) + 30;
	var intLeft = ($("#photo_" + intPhotoID).position().left) + 30;

	$("#infowinpreview").html("<img src='/photos/" + strFilename + "' style=\"border:2px solid #fff;\">");
	$("#infowinpreview").css("top", (intTop) + "px")
	$("#infowinpreview").css("left", (intLeft) + "px")
	$("#infowinpreview").fadeIn("fast");
}

function hideInfoWinPreview()
{
	$("#infowinpreview").hide();
}

// panoramio //////////////////////////////////////////////////////////////////////////////////////

// populate map with thumbnails and icons from panoramio
function loadPanoramio(objCenter, objNE, objSW)
{
	// decide how many photos to show
	var intTop = 9;

	$.ajax(
		{
			method: "get",
			dataType: "jsonp",
			url: "http://www.panoramio.com/map/get_panoramas.php?order=popularity&set=public&from=0&to=" + intTop + "&minx=" + objSW.x + "&miny=" + objSW.y + "&maxx=" + objNE.x + "&maxy=" + objNE.y + "&size=square&callback?",
			success: function(data)
			{
				// photos on the map
				$.each(data.photos, function(i, photo)
				{
					// get the location of the thumbnaial file and put it in GIcon
					var iconPhoto = getIcon(photo.photo_file_url, 50, 50, 25, 25, "http://www.kampeerkaart.nl/img/map/panoramioshadow.png", 54, 54, 0, 0);

					var markerOptions = { icon: iconPhoto };
					var marker = new GMarker(new GLatLng(photo.latitude, photo.longitude), markerOptions);

					var objMap = $("#mapcampsites").data("objmap");
					objMap.addOverlay(marker);

					// show preview
					GEvent.addListener(marker, "click", function()
					{
						var strCaption = photo.photo_title;
						strCaption = strCaption.replace(/\"/g, '\'');
						strCaption = strCaption.replace(/\'/g, '\'');

						var strContent = "<img src=\"" + photo.photo_file_url.replace(/square/i, "medium") + "\">";
						strContent += "<br />" + strCaption;
						strContent += "<br /><a href=\"http://www.panoramio.com/user/" + photo.owner_id + "\" target=\"_blank\">" + photo.owner_name + "</a>";
						strContent += "<div style=\"text-align:right; margin-top:5px;\">(klik op een willekeurige plek om de foto te sluiten)</div>";

						$("#valepreview").html(strContent);
						$("#vale").show();
					});

				})

			}
		});
}

// populate picture panel with thumbnails and icons from panoramio
function loadPanoramioPanel()
{
	// get map coordinates
	var minX = $("#panoramiopanel").data("minx");
	var minY = $("#panoramiopanel").data("miny");
	var maxX = $("#panoramiopanel").data("maxx");
	var maxY = $("#panoramiopanel").data("maxy");

	// decide how many photos to show
	var intTop = 6;

	$.ajax(
	{
		method: "get",
		dataType: "jsonp",
		url: "http://www.panoramio.com/map/get_panoramas.php?order=popularity&set=public&from=0&to=" + intTop + "&minx=" + minX + "&miny=" + minY + "&maxx=" + maxX + "&maxy=" + maxY + "&size=small&callback?",
		success: function(data)
		{
			var strContent = "";
			var intPhotos = 0;

			// prepare panel
			$("#panoramiopanel").empty();

			// build content
			strContent += "<h3>Foto's van de omgeving</h3>";
			strContent += "<div>";
			$.each(data.photos, function(i, photo)
			{
				// modify the current URL-request to get to the preview
				var strPreview = photo.photo_file_url;
				if (strPreview.indexOf("thumbnail") != -1)
				{
					strPreview = strPreview.replace(/thumbnail/i, "medium");
				}

				var strTmp = "<div class=\"photo\">";
				strTmp += "<div class=\"owner\"><a href=\"http://www.panoramio.com/user/" + photo.owner_id + "\" target=\"_blank\">" + photo.owner_name + "</a></div>";
				strTmp += "<a href=\"http://www.panoramio.com/photo/" + photo.photo_id + "\" target=\"_blank\"><img src=" + photo.photo_file_url + " /></a>";
				strTmp += "</div>";

				strContent += strTmp;
				intPhotos++;
			})
			strContent += "<div style=\"clear:both\"></div>";
			strContent += "</div>";
			strContent += "<div class=\"copyright\">Photos provided by <a href=\"http://www.panoramio.com\" target=\"_blank\">Panoramio</a> are under the copyright of their owners.</p>";

			$("#panoramiopanel").append(strContent);
		//	$("#panoramiopanel").append("<h2>Er zijn geen foto's van de directe omgeving</h2>");

		}

	});
}

function hideVale()
{
	$("#vale").hide();
}

function savePosition()
{
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		objMap.savePosition();
	}
}

function returnToSavedPosition()
{
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		objMap.returnToSavedPosition();
	}
}

function openCampsiteWebsite(URL)
{
	window.open('http://' + URL, 'Camping');
}

// toggle campsite details to campsites list
function getBackToList()
{
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		$("#campsitedetails").hide();

		if (m_markerSelected != null)
			objMap.removeOverlay(m_markerSelected);
	}
}

// create custom item
function getIcon(strFile, intWidth, intHeight, intAnchorX, intAnchorY, strShadowFile, intShadowWidth, intShadowHeight, intShadowAnchorX, intShadowAnchorY)
{
	// create specific marker icon
	var baseIcon = new GIcon();

	baseIcon.image = strFile;
	baseIcon.iconSize = new GSize(intWidth, intHeight);
	baseIcon.iconAnchor = new GPoint(intAnchorX, intAnchorY);
	baseIcon.infoWindowAnchor = new GPoint(intAnchorX, intAnchorY);

	if (strShadowFile != "")
	{
		baseIcon.shadow = strShadowFile;
		baseIcon.shadowSize = new GSize(intShadowWidth, intShadowHeight);
		baseIcon.infoShadowAnchor = new GPoint(intShadowAnchorX, intShadowAnchorY);
	}

	var tmpIcon = new GIcon(baseIcon);

	return tmpIcon;
}

function zoomToRegion(strLat, strLng, intZoom)
{
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		// save current view
		savePosition();

		// update map
		objMap.setCenter(new GLatLng(strLat, strLng), intZoom);
	}
}

function zoomToCampsite(strLat, strLng, intID)
{
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		// save current view
		savePosition();

		// update map
		objMap.setCenter(new GLatLng(strLat, strLng), 17);
	}
}

function findAddress(strAddress)
{
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		// save current view
		savePosition();
 
		// get the geocoder
		geocoder = new GClientGeocoder();
		if (geocoder)
		{
			var strSearch;

			if (strAddress != "")
				strSearch = strAddress + ", fr";
			else
				strSearch = $("#search_address").val() + ", fr";

			geocoder.getLatLng(
				strSearch,
				function(point)
				{
					if (point)
					{
						objMap.setCenter(point, 12);
					}
				}
			);
		}
	}
}

function enterPressed(e)
{
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	}
	else if (e) {
		keycode = e.which;
	}
	else {
		return false;
	}
	return (keycode == 13);
}

// ////////////////////////////////////////////////////////////////////////
// cookie jar
// ////////////////////////////////////////////////////////////////////////

function saveCampsite(intID)
{
	$.ajax(
		{
			method: "post",
			url: "Process.aspx?option=savecampsite&id=" + intID + "&r=" + getRandom(),
			success: function()
			{
				$("#savelink" + intID).html("<a class=\"icon_saved\" href=\"/saved.aspx\">je hebt deze camping bewaard</a>");
			}
		});
	return false;
}

function saveMapView()
{
	// first we need a map object
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		var GLatLngBounds = objMap.getBounds();
		var GZoom = objMap.getZoom();
		var objCenter = GLatLngBounds.getCenter();

		// shortened a bit, we don't need extra precise latlng-values for this
		var strLLZ = String(objCenter.y).substr(0, 6) + "|" + String(objCenter.x).substr(0, 6) + "|" + GZoom;
		var strName = $("#savemap").val();

		// set the cookie
		$.ajax(
		{
			method: "get",
			url: "Process.aspx?option=savemap&name=" + strName + "&llz=" + strLLZ + "&r=" + getRandom(),
			success: function(html)
			{
				// show feedback
				$("#savemapform").html(html);
			}
		});
	}
	return false;
}

// mapsize ////////////////////////////////////////////////////////
function setMapSize(intValue)
{
	$.ajax(
	{
		method: "post",
		url: "Process.aspx?option=setmapsize&value=" + intValue + "&r=" + getRandom(),
		success: function()
		{
			getMapSize();
		}
	});
	return false;
}

function getMapSize()
{

	$.ajax(
		{
			method: "post",
			url: "Data.aspx",
			data: "option=getmapsize&r=" + getRandom(),
			success: function(html)
			{
				// feed html to mapsize panel
				$("#mapsize").html(html);

				// set mapsize according to preference
				if (html.indexOf("MapSize1") >= 0)
				{
					$("#mapcampsites").height(450);
				}
				else if (html.indexOf("MapSize3") >= 0)
				{
					$("#mapcampsites").height(680);
				}
				else
				{
					$("#mapcampsites").height(550);
				}

				$('#mapcampsites').jmap('CheckResize');
			}
		});
}

function getCitySearch()
{
	// get the form html
	$.ajax(
	{
		method: "post",
		url: "Data.aspx",
		data: "option=getsearchcityform&r=" + getRandom(),
		success: function(html)
		{
			$("#searchcityform").html(html);
		}
	});
}

// preferences, pitches and stars /////////////////////////////////////////////////////

function getPreferences()
{
	// get the form html
	$.ajax(
		{
			method: "post",
			url: "Data.aspx",
			data: "option=getpreferences&r=" + getRandom(),
			success: function(html)
			{
				$("#preferences").html(html);
			}
		});
}

function setPreferredPitches(intValue)
{
	$.ajax(
		{
			url: "Process.aspx?option=setpitches&pitches=" + intValue + "&r=" + getRandom(),
			success: function(html)
			{
				populatePage();
			}
		});
}

function setPreferredStars(intValue)
{
	$.ajax(
	{
		url: "Process.aspx?option=setstars&stars=" + intValue + "&r=" + getRandom(),
		success: function(html)
		{
			populatePage();
		}
	});
}

//////////////////////////////////////////////////////////////////////////

function getCurrentLLZ()
{
	var strLLZ = "";

	// we need a map object
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		var GLatLngBounds = objMap.getBounds();
		var GZoom = objMap.getZoom();
		var objCenter = GLatLngBounds.getCenter();

		strLLZ = String(objCenter.y).substr(0, 6) + "," + String(objCenter.x).substr(0, 6) + "," + GZoom;
	}

	return strLLZ;
}

function gotoCampsiteDetails(intCampsiteID)
{
	document.location.href = 'camping.aspx?id=' + intCampsiteID + '&llz=' + m_strLLZ;
}

function getLinkToThisMap()
{
	var strLink = 'http://www.kampeerkaart.nl/map.aspx?llz=' + m_strLLZ;

	if (m_strLLZ != "")
	{
		if ($("#linktomapcontainer").is(":visible"))
		{
			$("#linktomapcontainer").slideUp("normal");
		}
		else
		{
			$("#linktomapcontainer").val(strLink);
			$("#linktomapcontainer").slideDown("normal");
		}
	}
}

function hideInfoWin()
{
	$("#infowin").fadeOut("fast")
	return false;
}

// feedback ////////////////////////////////////////////////////////////////////////

function validateContactForm()
{
	$.validator.setDefaults({
		submitHandler: function()
		{
			submitContact();
			return false;
		}
	});
	$("#contactform").validate({
		rules: {
			contact_message: "required",
			contact_name: "required",
			contact_email: {
				required: true,
				email: true
			}
		},
		messages: {
			contact_message: "Je hebt nog geen bericht ingevuld.",
			contact_name: "Je hebt je naam nog niet ingevuld.",
			contact_email: {
				email: "Vul een geldig e-mailadres in.",
				required: "Je hebt nog geen e-mailadres ingevuld."
			}
		}
	});
}

function validateFeedbackForm()
{
	$.validator.setDefaults({
		submitHandler: function()
		{
			submitFeedback();
			return false;
		}
	});
	$("#feedbackform").validate({
		rules: {
			feedback_rating: "required",
			feedback_name: "required",
			feedback_email: {
				required: true,
				email: true
			},
			feedback_month: "required",
			feedback_year: "required"
		},
		messages: {
			feedback_rating: "Je hebt nog geen waardering ingevuld.",
			feedback_name: "Je hebt je naam nog niet ingevuld.",
			feedback_email: {
				email: "Je hebt geen geldig e-mailadres ingevuld.",
				required: "Je hebt nog geen e-mailadres ingevuld."
			},
			feedback_month: "De maand ontbreekt.",
			feedback_year: "Het jaar ontbreekt."
		}
	});
}

function validateNewCampsiteForm()
{
	$.validator.setDefaults({
		submitHandler: function()
		{
			submitNewCampsite();
			return false;
		}
	});
	$("#newcampsiteform").validate({
	rules: {
			newcampsite_campsitename: "required",
			newcampsite_city: "required",
			newcampsite_name: "required",
			newcampsite_email: {
				required: true,
				email: true
			},
			newcampsite_message: "required",
			newcampsite_pitches: "required",
			newcampsite_stars: "required"
		},
		messages: {
			newcampsite_city: "Je hebt de plaatsnaam nog niet ingevuld.",
			newcampsite_campsitename: "Je hebt de naam van de camping nog niet ingevuld.",
			newcampsite_name: "Je hebt je naam nog niet ingevuld.",
			newcampsite_email: {
				email: "Je hebt geen geldig e-mailadres ingevuld.",
				required: "Je hebt nog geen e-mailadres ingevuld."
			},
			newcampsite_message: "Je hebt nog geen informatie gegeven.",
			newcampsite_pitches: "Je hebt het aantal plaatsen nog niet ingevuld.",
			newcampsite_stars: "Je hebt het aantal sterren nog niet ingevuld."
		}
	});
}

function validateTipForm()
{
	$.validator.setDefaults({
		submitHandler: function()
		{
			submitTip();
			return false;
		}
	});
	$("#tipform").validate({
		rules: {
			tip_name: "required",
			tip_email: {
				required: true,
				email: true
			},
			tip_message: "required"
		},
		messages: {
			tip_name: "Je hebt je naam nog niet ingevuld.",
			tip_email: {
				email: "Je hebt geen geldig e-mailadres ingevuld.",
				required: "Je hebt nog geen e-mailadres ingevuld."
			},
			tip_message: "Je hebt nog geen bericht geschreven."
		}
	});
}

function validateAdvertiserForm()
{
	$.validator.setDefaults({
		submitHandler: function()
		{
			submitAdvertiser();
			return false;
		}
	});
	$("#advertiserform").validate({
		rules: {
			advertiser_name: "required",
			advertiser_email: {
				required: true,
				email: true
			}
		},
		messages: {
			advertiser_name: "U heeft uw naam nog niet ingevuld.",
			advertiser_email: {
				email: "U heeft geen geldig e-mailadres ingevuld.",
				required: "U heeft nog geen e-mailadres ingevuld."
			}
		}
	});
}

function submitFeedback()
{
	var intID;
	var strRating;
	var strComments;
	var strName;
	var strEmail;
	var strMonth;
	var strYear;

	intID = $("#feedback_id").val();
	strRating = $("#feedback_rating").val();
	strComments = $("#feedback_message").val();
	strName = $("#feedback_name").val();
	strEmail = $("#feedback_email").val();
	strMonth = $("#feedback_month").val();
	strYear = $("#feedback_year").val();

	strComments = strComments.replace("'"," ");
	strComments = strComments.replace("\"", " ");

	$.ajax(
		{
			method: "post",
			url: "/process.aspx",
			data: "option=feedback&campsiteid=" + intID + "&feedback_rating=" + strRating + "&feedback_comments=" + strComments + "&feedback_name=" + strName + "&feedback_email=" + strEmail + "&feedback_month=" + strMonth + "&feedback_year=" + strYear + "&r=" + getRandom(),
			success: function(html)
			{
				$("#feedbackpanel").html(html);
			}
		});
}

function submitTip()
{
	var intID;
	var strMessage;
	var strName;
	var strEmail;

	intID = $("#tip_id").val();
	strMessage = $("#tip_message").val();
	strName = $("#tip_name").val();
	strEmail = $("#tip_email").val();

	$.ajax(
		{
			method: "post",
			url: "/process.aspx",
			data: "option=tip&campsiteid=" + intID + "&tip_message=" + strMessage + "&tip_name=" + strName + "&tip_email=" + strEmail + "&r=" + getRandom(),
			success: function(html)
			{
				$("#tippanel").html(html);
			}
		});
}

function submitAdvertiser()
{
	var strMessage;
	var strName;
	var strEmail;

	strMessage = $("#advertiser_message").val();
	strName = $("#advertiser_name").val();
	strEmail = $("#advertiser_email").val();

	$.ajax(
		{
			method: "post",
			url: "/process.aspx",
			data: "option=advertiser&advertiser_message=" + strMessage + "&advertiser_name=" + strName + "&advertiser_email=" + strEmail + "&r=" + getRandom(),
			success: function(html)
			{
				$("#advertiserpanel").html(html);
			}
		});
}

function submitNewCampsite()
{
	var strCampsiteName;
	var strCity;
	var strMessage;
	var strName;
	var strWebsite;
	var strEmail;
	var intPitches;
	var intStars;
	var intOwner;

	strCampsiteName = $("#newcampsite_campsitename").val();
	strCity = $("#newcampsite_city").val();
	strMessage = $("#newcampsite_message").val();
	strName = $("#newcampsite_name").val();
	strWebsite = $("#newcampsite_website").val();
	strEmail = $("#newcampsite_email").val();
	intPitches = $("#newcampsite_pitches").val();
	intStars = $("#newcampsite_stars").val();
	intOwner = $("#newcampsite_owner").val();

	$.ajax(
		{
			method: "post",
			url: "/process.aspx",
			data: "option=newcampsite&newcampsite_campsitename=" + strCampsiteName + "&newcampsite_city=" + strCity + "&newcampsite_message=" + strMessage + "&newcampsite_name=" + strName + "&newcampsite_website=" + strWebsite + "&newcampsite_email=" + strEmail + "&newcampsite_pitches=" + intPitches + "&newcampsite_stars=" + intStars + "&newcampsite_owner=" + intOwner + "&r=" + getRandom(),
			success: function(html)
			{
				$("#newcampsitepanel").html(html);
			}
		});
}

function toggleMailSavedCampsites()
{
	if ($("#mailsavedcampsites").is(":visible"))
		$("#mailsavedcampsites").slideUp("normal");
	else
		$("#mailsavedcampsites").slideDown("normal");
}

function submitSavedCampsites()
{
	var strEmail;

	strEmail = $("#savedcampsites_email").val();

	// clear field
	$("#savedcampsites_email").val("");

	$.ajax(
		{
			method: "post",
			url: "/process.aspx",
			data: "option=mailsavedcampsites&savedcampsites_email=" + strEmail + "&r=" + getRandom(),
			success: function(html)
			{
				$("#mailsavedcampsitesfeedback").show();
				$("#mailsavedcampsitesfeedback").html(html);
			}
	});
}

function togglePrintSavedCampsites()
{
	if ($("#printsavedcampsitescontainer").is(":visible"))
		$("#printsavedcampsitescontainer").hide();
	else
	{
		$("#printsavedcampsitescontainer").show();
		$("#printsavedcampsites").html("<div class=\"options_right\" style=\"margin-right:10px;\"><a class=\"icon_close\" href=\"#\" onclick=\"togglePrintSavedCampsites();\">sluit</a></div><iframe src=\"/data.aspx?option=getprintsavedcampsites&r=" + getRandom() + "\" style=\"width:99%; height:95%;\" frameborder=\"0\"></iframe>");
	}
}

// location-specific campsite ad
function getCampsiteAd()
{
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		// bounding box of the map itself
		var GLatLngBounds = objMap.getBounds();
		//var GZoom = objMap.getZoom();

		// get the center
		var objCenter = GLatLngBounds.getCenter();

		// get lat/lng values for Northeast and Southwest corners of the map
		var objNE = new GLatLng(GLatLngBounds.getNorthEast().y, GLatLngBounds.getNorthEast().x);
		var objSW = new GLatLng(GLatLngBounds.getSouthWest().y, GLatLngBounds.getSouthWest().x);

		$.ajax(
			{
				method: "post",
				url: "Data.aspx",
				data: "option=getcampsitead&latnorth=" + objNE.y + "&latsouth=" + objSW.y + "&lngwest=" + objSW.x + "&lngeast=" + objNE.x + "&r=" + getRandom(),
				success: function(html)
				{
					$("#campsitead").html(html);
				}
			});

	}
}

// campsites nearby
function getCampsitesNearby()
{
	var objMap = $("#mapcampsites").data("objmap");
	if (objMap)
	{
		// bounding box of the map itself
		var GLatLngBounds = objMap.getBounds();

		// get the center
		var objCenter = GLatLngBounds.getCenter();

		$.ajax(
			{
				method: "post",
				url: "Data.aspx",
				data: "option=getcampsitesnearby&lat=" + objCenter.y + "&lng=" + objCenter.x + "&r=" + getRandom(),
				success: function(html)
				{
					$("#campsitesnearby").html(html);
				}
			});

	}
}

function initMap(intID, Lat, Lng, intPitches, strContext)
{
	var objMap = new GMap2(document.getElementById("map_" + strContext + "_" + intID));

	var intZoom;
	if (strContext == "searchresult")
	{
		objMap.setMapType(G_NORMAL_MAP);
		intZoom = 6;
	}
	else if (strContext == "village")
	{
		objMap.setMapType(G_NORMAL_MAP);
		intZoom = 12;
	}
	else
	{
		objMap.setMapType(G_HYBRID_MAP);
		
		var customUI = objMap.getDefaultUI();
		customUI.controls.scalecontrol = false;
		customUI.zoom.scrollwheel = false;
		objMap.setUI(customUI);
		
		intZoom = 14;
	}

	var center = new GLatLng(Lat, Lng);
	objMap.setCenter(center, intZoom);

	var intWidth;
	var intHeight;

	switch (intPitches)
	{
		case 1:
			intWidth = 26;
			intHeight = 26;
			break;
		case 2:
			intWidth = 34;
			intHeight = 34;
			break;
		case 3:
			intWidth = 46;
			intHeight = 46;
			break;
	}

	var iconCampsite = getIcon("http://www.kampeerkaart.nl/img/map/c_" + intPitches + ".png", intWidth, intHeight, 10, 10, "", 0, 0, 0, 0);
	var markerOptions = { icon: iconCampsite };
	var marker = new GMarker(new GLatLng(Lat, Lng), markerOptions);
	objMap.addOverlay(marker);
}



