$(document).ready(function(){
	// All the functions that need to be triggered as soon as the entire page loads
	// Eacht one has it's own check to see whether they need to do anything or not
	initInputs();
	initMaps();
});

var map;
var point;
var Bounds;

var smallControl;
var largeControl;
		
function initMaps(){
 if($('#googlemaps').length){
	  if (GBrowserIsCompatible()) {
		  
		smallControl = new GSmallZoomControl3D();
		largeControl = new GLargeMapControl3D();
		  
		if(mapMode == 'search'){
			// Create the smallicon
			var icon = new GIcon();
			icon.image = "/img/mapicon-small.png";
			icon.iconSize = new GSize(23, 25);
			icon.iconAnchor = new GPoint(12, 20);
			icon.infoWindowAnchor = new GPoint(25, 1);
		}else{	
			// Create the normal icon
			var icon = new GIcon();
			icon.image = "/img/mapicon.png";
			icon.iconSize = new GSize(46, 50);
			icon.iconAnchor = new GPoint(17, 45);
			icon.infoWindowAnchor = new GPoint(25, 1);
		}
				
		map = new GMap2(document.getElementById("map"),{ size:new GSize(228,197)});
		
		// Load wheel zoom functionality
		GMap2.prototype.wheelZoom = function(event) {
				if(event.cancelable) event.preventDefault();
				(event.detail || -event.wheelDelta) < 0 ? map.zoomIn():
				map.zoomOut();
				return false;
		}
		
		map.setCenter(new GLatLng(52.10313118589297,5.11962890625), 6);
		
		Bounds = new GLatLngBounds();
		
		c = getCoordinates();
		if(c.length){
			for(i=0; i<c.length; i++){
				point = new GLatLng(c[i].lat,c[i].lon);
				if(c[i].lat > 0 && c[i].lon > 0){
					var marker = new GMarker(point, icon);
					marker.bindInfoWindowHtml("<a href='"+ c[i].url +"' title='Ga naar het instituut "+ c[i].title +"'>"+ c[i].title +"</a>", {maxWidth:150});
					
					map.addOverlay(marker);					
					Bounds.extend(point);
				}
			}
		}
		
		
		
		
		if(!Bounds.isEmpty()){
			// Make sure the zoom level isn't excessive
			var zoomLevel = map.getBoundsZoomLevel(Bounds);
			
			// Set the new centre according to the bounds and new zoomlevel
			map.setCenter(Bounds.getCenter(), zoomLevel - 1);
		}
		
		GEvent.addDomListener(document.getElementById("map"),"DOMMouseScroll", map.wheelZoom);
		GEvent.addDomListener(document.getElementById("map"),"mousewheel",map.wheelZoom);
		
		map.addControl(smallControl);

		
		init();
	  }
 	}
 }

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// Initialse the click and blur actions on inputs
// If an input has a title variable set, remove the initial value when people focus on it
// If an input has nothing in it on blur, reset the value to the title tag
function initInputs(){
	if($('input:text').length){
		$('input:text').each(function(){
			t = $(this);
			if(t.attr('title') == t.val()){
				t.click(function(){
					t = $(this);
					if(t.attr('title') == t.val()){
						t.val('');
					}
				});
				t.blur(function(){
					t = $(this);
					if(t.val() == ''){
						t.val(t.attr('title'));
					}
				});
			}
		});
	}
}

function init(){
 if($('#googlemaps').length){
	 $('#googlemaps-toggle').toggle(function(){
		$('#googlemaps #map').animate({ height: "376px" }, 500);
		$('#googlemaps #map').animate({ width: "926px" }, {ducation:1000, complete:mapLarge});
		$('#googlemaps').addClass('googlemaps-large').removeClass('googlemaps-small');
		$('#googlemaps #googlemaps-toggle').html("Verklein kaart");
		
	},function(){
		$('#googlemaps #map').animate({ width: "228px" }, 500);
		$('#googlemaps #map').animate({ height: "197px" }, {ducation:1000, complete:mapSmall});
		$('#googlemaps').addClass('googlemaps-small').removeClass('googlemaps-large');
		$('#googlemaps #googlemaps-toggle').html("Vergroot kaart");
	});
  }
}


function mapSmall(){
	map.panBy(new GSize(-349,-98));
	map.removeControl(largeControl);
	map.addControl(smallControl);
}
function mapLarge(){
	map.panBy(new GSize(349,98));
	map.removeControl(smallControl);
	map.addControl(largeControl);
}

function maxLength(obj) {
	var mlength=140;
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value = obj.value.substring(0,mlength);
	$("#charsleft").html(140-obj.value.length);
}