$(document).ready(function() {
	$("#agenda-searchbutton").click(function() {
		window.location = "./01-" + $(".month-field").val() + "-" + $(".year-field").val();
	});
	$("#mainmenu .workshops").mouseover(function(){
		$("#lines .workshops").stop().animate({width:'135px'},600);
	}).mouseleave(function(){
		$("#lines .workshops").stop().animate({width:'0px'},600);
	});
	$("#mainmenu .spiritavonden").mouseover(function(){
		$("#lines .spiritavonden").stop().animate({width:'180px'},600);
	}).mouseleave(function(){
		$("#lines .spiritavonden").stop().animate({width:'0px'},600);
	});
	$("#mainmenu .kinderen").mouseover(function(){
		$("#lines .kinderen").stop().animate({width:'135px'},600);
	}).mouseleave(function(){
		$("#lines .kinderen").stop().animate({width:'0px'},600);
	});
	$("#mainmenu .spiritcafe").mouseover(function(){
		$("#lines .spiritcafe").stop().animate({width:'135px'},600);
	}).mouseleave(function(){
		$("#lines .spiritcafe").stop().animate({width:'0px'},600);
	});
	$("#mainmenu .divers").mouseover(function(){
		$("#lines .divers").stop().animate({width:'100px'},600);
	}).mouseleave(function(){
		$("#lines .divers").stop().animate({width:'0px'},600);
	});
	$("#generate").click(function(){
		wijsdeweg($("#adres").val() + ", " + $("#postcode").val() + " " + $("#plaats").val());
	});
	$("#print").click(function(){
		popuproute($("#adres").val() + ", " + $("#postcode").val() + " " + $("#plaats").val());
	});
	$("#printpage").click(function(){
		print_page();
	});
});
var zoeken = function(){
	window.location = 'http://www.villavijf33.nl/agendas/zoeken/'+$("#query").val();
}
var nextWeek = function (date) {
	window.location = './' + date;
}
var previousWeek = function (date) {
	window.location = './' + date;
}
var map;
var geocoder;
var directions;
var naar;
var initialize_maps = function () {
	if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        geocoder = new GClientGeocoder();
        map.setUIToDefault();
        geocoder.getLatLng("Kromme Spieringweg 533, 2141 AK Vijfhuizen", function(point) {
			if (point) {
				map.setCenter(point, 13);
				point = point;
				naar = point;
				map.addOverlay(createmarker(point, getvijf33marker()));
				if(typeof loadRoute == 'function') { 
					loadRoute(); 
				}
			}
		});
      }
}
var getvijf33marker = function () {
	var icon = new GIcon();
	icon.image = "http://www.villavijf33.nl/img/vijf33_marker.png";
	icon.iconSize = new GSize(25, 40);
	icon.iconAnchor = new GPoint(0, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	return icon;
}
var createmarker = function (point, icon) {
	return new GMarker(point, icon);
}
var set_naar = function(point) {
	naar = point;
}
var print_page = function() {
	window.print();
	return false;
}
var popuproute = function(adres){
	if(adres == ",  "){
		alert("Vul aub eerst een geldig adres in.");
	} else {
		var winl = (screen.width-700)/2;
		var wint = (screen.height-700)/2;
		var winpops=window.open("http://www.villavijf33.nl/pages/routepopup/"+adres+"/","","width=700,height=700,top="+wint+",left="+winl+",scrollbars");
	}
}
var wijsdeweg = function (van) {
	geocoder.getLatLng(van, function(point) {if (point) {wijsdeweg_vanpoint(point);}});
}
var wijsdeweg_vanpoint = function (point) {
	directions = new GDirections(map, document.getElementById('stappen'));
	GEvent.addListener(directions, "error", directionerrors);
	GEvent.addListener(directions, "load", directionload);
	map.clearOverlays();
	directions.loadFromWaypoints(new Array(point, naar));
}
var directionerrors = function(){
	if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS) alert("Het adres dat u heeft opgegeven bestaat niet of wordt niet door ons systeem herkend.");
}
var directionload = function() {
	directions.getMarker(0).getIcon().image = "http://www.villavijf33.nl/img/home_marker.png"; 
	directions.getMarker(0).getIcon().iconSize = new GSize(30, 45);
	directions.getMarker(1).getIcon().image = "http://www.villavijf33.nl/img/vijf33_marker.png"; 
	directions.getMarker(1).getIcon().iconSize = new GSize(30, 45);
}
