function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}

var GoogleMap = new Class({
	
	Implements: Options,
	
	options: {
		'links'			: false,
		'zoom'			: 13,
		'locations'		: false,
		'types'			: false,
		'directions'	: false,
		'showSteps'		: false
	},
	
	initialize: function(placeholder,options) {
		this.setOptions(options);
   		this.placeholder	= placeholder;
   		this.geocoder 		= new GClientGeocoder();
   		this.types;
   		if (this.options.links) this.links();
	},
	
	random: function(){
		return $random(0,999999);
	},
	
	links: function(){
		this.options.links.each(function(link){
			link.addEvent('click',function(event){
				event.stop();
				this.show(link.get('html'));
			}.bind(this));
		}.bind(this));
	},
	
	locations: function() {
		this.options.locations.each(function(location,title){
			this.insert(
				title,
				location.address,
				location.postal,
				location.city,
				location.country,
				location.type,
				false
			);
		}.bind(this));
	},
	
	show: function(key){
		//load locations and types
		this.locations();
   		this.types();
   		//build map
		this.map = new GMap2(this.placeholder);
		this.map.addControl(new GLargeMapControl3D());		
		var location = this.options.locations.get(key);
		this.insert(
			key,
			location.address,
			location.postal,
			location.city,
			location.country,
			location.type,
			true
		);
	},
	
	insert: function(title,address,postal,city,country,type,first){
		this.geocoder.getLatLng(address + ' ' + postal + ' ' + city + ' ' + country,function(point){
			var marker = new GMarker(point,this.type[type].markerOptions);
			this.map.addOverlay(marker);
			if (first) { 
				this.map.setCenter(point,this.options.zoom);
				this.map.openInfoWindow(point,
										this.address(title,address,postal,city,country),
										{onOpenFn: function(){
											if (this.options.directions) {
												$('directionAddress').focus();
    	    									$('directionSubmit').addEvent('click',function(){
        											this.direction(address + ' ' + postal + ' ' + city + ' ' + country,$('directionAddress').get('value'));
        											$('directionAddress').set('value','');
        											marker.closeInfoWindow();
        										}.bind(this));
        									}
										}.bind(this)}
				);
				
			}					
        	GEvent.addListener(marker,"click", function(){
        		this.map.openInfoWindow(point,
        								this.address(title,address,postal,city,country),
										{onOpenFn: function(){
											$('directionAddress').focus();
        									$('directionSubmit').addEvent('click',function(){
        										this.direction(address + ' ' + postal + ' ' + city + ' ' + country,$('directionAddress').get('value'));
        										$('directionAddress').set('value','');
        										marker.closeInfoWindow();
        									}.bind(this));
										}.bind(this)}
				);       		
        	}.bind(this));
		}.bind(this));
	},
	
	types: function(){
		this.type = new Hash();
		this.options.types.each(function(type,title){
					
			var baseicon = new GIcon();
			baseicon.image = type.icon;
			baseicon.iconSize = new GSize(type.iconsize[0],type.iconsize[1]);
			baseicon.iconAnchor = new GPoint(type.anchor[0],type.anchor[1]);
			baseicon.infoWindowAnchor = new GPoint(type.infoAnchor[0],type.infoAnchor[1]);
						
			markerOptions = {icon:baseicon};
			
			this.type.set(title,{'markerOptions':markerOptions});
					
		}.bind(this));
	},
	
	direction: function(to,from){
		directions = new GDirections(this.map,this.options.showSteps);
  		directions.load("from: " + from + " to: " + to);
	},
	
	address: function(title,address,postal,city,country){
		var html = '<div style="color: #000000;"><strong>' + title + '</strong><br />' + 
				   address + '<br />' + postal + ' ' + city + '<br />' + country + '<br /><br />';
		if (this.options.directions) {
			html = html + '<p><strong>Routebeschrijving</strong><br /><input type="text" id="directionAddress" value="" style="border: 1px solid #999; margin:0; padding: 2px; line-height:12px;" /><button type="button" id="directionSubmit">Plan route</button></p>';
		}
		html = html + '</div>';
		return html;
	}
	
});
