	//load external scripts if needed: problems with ajax load google maps api
	if(!google.maps) {
		//http://maps.google.com/maps/api/js?sensor=true
		throw 'Failed to load google maps API.';
	}
	$.ajaxSetup({async: false});
	if(!navigator.geolocation && !google.gears) {
		$.getScript('http://code.google.com/apis/gears/gears_init.js');
	}
	$.ajaxSetup({async: true});
	
	//create default options:
	$.GMapsDefaultOptions = {
		'markerURL'	:	'',
		'markerIcon':	'',
		'centerMarker': '',
                'centerMarkerOpened': true,
		'zoom'		:	10,
		'center'	:	'50.863178,4.487915',
		'autoCenter':	true,
		'locateIpAddr' : '',
		'centerZoom':	12,
		'mapTypeId'	:	'ROADMAP',
		'panControl'		: 	false,
		'zoomControl'		: 	true,
		'mapTypeControl'	:	false,
		'scaleControl'		: 	false,
		'streetViewControl'	: 	false,
		'theme': '',
		'scrollTo': ''
	}
	
	//create a reference for asynchronous functions:
	$.GMapsInstance = null;
	
	//create plugin:
	$.fn.extend({
	
		GMaps: function(options) {
			
			//lets start with some variables
			this.options = $.extend({}, $.GMapsDefaultOptions, options);
			this.markers = new Array();
			this.map = false;	
			$.GMapsInstance = this;	
			
			//create map:
			this.init = function() {
				var point = this.convertCoordsToPoint(this.options.center);
				
				//create map
				mapOptions = {
					'zoom'				:	this.options.zoom,
					'center'			:	point,
					'mapTypeId'			:	google.maps.MapTypeId[this.options.mapTypeId],
					'zoomControl'		:	this.options.zoomControl,
					'mapTypeControl'	:	this.options.mapTypeControl,
					'scaleControl'		:	this.options.scaleControl,
					'streetViewControl'	:	this.options.streetViewControl
				};
				this.map = new google.maps.Map($(this).get(0), mapOptions);
				
				//fix for idle errors:
				google.maps.event.addListener(this.map, 'idle', function(){
					google.maps.event.trigger(this.map, 'resize'); 
				});	
				
				//find user location if needed:
				if(this.options.autoCenter == true) {
					this.gotoUserPosition();	
				}
				
				//if a marker needs to be set in the center:
				if(this.options.centerMarker != '') {
					this.addMarker(point, this.options.centerMarker, this.options.centerMarkerOpened);
				}
				
				//scroll to another position:
				if (this.options.scrollTo != '') {
					var pos = this.options.scrollTo.split(',');
					var newCenter = new google.maps.LatLng(pos[0],pos[1]);
					this.map.setCenter(newCenter);
				}
				
				//if everything is ready: load markers:
				if(this.options.markerURL != '') {
					this.loadMarkers();	
				}
				
				//set a theme to the map:
				if (this.options.theme != '') {
					var styledMapOptions = {name: "themedmap"};
					var themedMapType = new google.maps.StyledMapType(this.options.theme, styledMapOptions);
					this.map.mapTypes.set('themedmap', themedMapType);
					this.map.setMapTypeId('themedmap');
				}
			}
			
			//get user position:
			this.gotoUserPosition = function() {
				//use webservice?
				if(this.options.locateIpAddr != '') {
					$.ajax({
					    type: "GET",
						url: 'http://api.hostip.info/?ip=' + this.options.locateIpAddr,
						dataType: "xml",
						success: function(xml) {
					 		$(xml).find('coordinates').each(function(){
 								var reverseCoords = $(this).text();
								var coords = reverseCoords.split(',');
								$.GMapsInstance.gotoPosition(coords[1],coords[0]);
							});
						}
					});
				
				// html 5 - standard functions:
				} else if(navigator.geolocation) {
					navigator.geolocation.getCurrentPosition(function(position) {
						$.GMapsInstance.gotoPosition(position.coords.latitude,position.coords.longitude);
					}, function() {
						//allready at options position
					});	
				// google gears plugin:
				} else if(google.gears) {
					var geo = google.gears.factory.create('beta.geolocation');
					geo.getCurrentPosition(function(position) {
						$.GMapsInstance.gotoPosition(position.latitude,position.longitude);
					}, function() {
						//allready at options position
					});
				//not possible to find user location:
				} else {
					//allready at options position
				}			
			}
			
			//coordinates functions
			this.convertCoordsToPoint = function(coords) {
				var point = coords.split(',');
				return new google.maps.LatLng(point[0],point[1]);
			}
			
			//goto a position
			this.gotoPosition = function(lat, long) {
				var initialLocation = new google.maps.LatLng(lat,long);
				this.map.setCenter(initialLocation);
				this.map.setZoom(this.options.centerZoom);
			}
			
			//function to add marker:
			this.addMarker = function(point, info, opened) {
				var params = {
					'map': this.map,
					'position': point
				}
				if(this.options.markerIcon != '') {
					params.icon =this.options.markerIcon;
				}

				var marker = new google.maps.Marker(params);
				var infowindow = new google.maps.InfoWindow();
								
				//infowindow on click:
				google.maps.event.addListener(marker, 'click', function() {
					  infowindow.open($.GMapsInstance.map, marker);
				});
				
				//set content:
				infowindow.setContent(info);
				if(opened) {
					infowindow.open(this.map, marker);
				}
			}
			
			//load the markers:
			this.loadMarkers = function() {
				$.ajax({
					type: "GET",
					url: this.options.markerURL,
					dataType: "json",
					success: function(json) {
						if(json) {
							$.GMapsInstance.markers = json;
							for(i=0;i<json.length;i++) {
								//add marker with right options:
								var point = new google.maps.LatLng(json[i].lat, json[i].long);
								var params = {
									'map': $.GMapsInstance.map,
									'position': point,
									'title': json[i].title
								}
								if($.GMapsInstance.options.markerIcon != '') {
									params.icon = $.GMapsInstance.options.markerIcon;
								}
								
								var marker = new google.maps.Marker(params);
								var infowindow = new google.maps.InfoWindow();
								
								//infowindow:
								google.maps.event.addListener(marker, 'click', (function(marker, i) {
									return function() {
									  infowindow.setContent($.GMapsInstance.markers[i]['infoWindow']);
									  infowindow.open($.GMapsInstance.map, marker);
									}
								})(marker, i));

							}
						}
					}
				});
			}
			
			//and finally start the logic:
			this.init();
		}	
	});
