(function($){
	$.fn.gmaps = function(options) 
	{  
	   	var defaults = 
	   	{  
	   		canvas : 'gmap',
	   		errors: 'gmapError',
	   		key: '',
	   		icon: Array(),
	   		current_icon: "normal",
	   		mapPoints: Array,
	   		mapCenter: Array
	 	};  
	
	   	var reasons=new Array();
	   	var icons = new Array();
	   	var map = null;
	 
	    var options = $.extend(defaults, options);
	
	    return this.each(function() {
		
	    	 if (GBrowserIsCompatible() && options.canvas != '') {
	            map = new GMap2(document.getElementById(options.canvas));
		         
	            
	            /*TO DO SOMTING TO DEFINE OUTSIDE INTANALISATIE*/
	            seticon('http://preview.popolsku.eu/gfx/default/gmap.png', '', {
	    			icon_x: 25,
	    			icon_y: 33,
	    			shadow_x: 0,
	    			shadow_y: 0,
	    			anchor_x: 12,
	    			anchor_y: 33,
	    			info_x: 20,
	    			info_y: 0
	    		}, 'normal');
	   
	            $.each(options.mapPoints, function(index, point) { 
	            	if(point !== null && point !== undefined){
	              		addPoint(point.addres, point.html, options.current_icon);
	            	}
	            });

	            if(typeof(options.mapCenter.addres) === undefined) {
	            	options.mapCenter.addres = 'Netherlands';
	            }
	            if(typeof(options.mapCenter.zoom) === undefined) {
	            	options.mapCenter.zoom = 5;
	            }
	            
				setcenter(options.mapCenter.addres, options.mapCenter.zoom);
	          }
	    });
	    
	   
	    function addPoint(address, $html, pointIcon){
	    	geocoder = new GClientGeocoder();
			geocoder.getLatLng(
				address,
				function(point) {
					var marker = new GMarker(point, icons[pointIcon]);
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml($html);
					});
					map.addOverlay(marker);
				}
			);	
	    }
	    
		function setcenter (address, zoomlevel) {
			if(typeof(address)=='object'&&(address instanceof Array)) {
				var point = new GLatLng(address[0], address[1]);
				map.setCenter(point, zoomlevel);
			} else {
				geocoder = new GClientGeocoder();
				geocoder.getLatLng(
					address,
					function(point) {
						if (point) {
							map.setCenter(point, zoomlevel);
							map.setUIToDefault();
						}else{
							console.log('didnot find point');
						}
					}
				);
			}
		}
		
		function seticon(image, shadow, sizes, name) {
			if(typeof(name) == "undefined") {
				var name = "normal";
			}
			icons[name] = new GIcon();
			icons[name].image = image;
			icons[name].shadow = shadow;
			icons[name].iconSize = new GSize(sizes["icon_x"], sizes["icon_y"]);
			icons[name].shadowSize = new GSize(sizes["shadow_x"], sizes["shadow_y"]);
			icons[name].iconAnchor = new GPoint(sizes["anchor_x"], sizes["anchor_y"]);
			icons[name].infoWindowAnchor = new GPoint(sizes["info_x"], sizes["info_y"]);
		}
	}
})(jQuery);