(function($){
 $.fn.gmapsv3 = function(options) {

	var defaultGmapsOptions = {
		zoom: 14,
		center: new google.maps.LatLng(0, 0),
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true,
		mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
			position: google.maps.ControlPosition.TOP_RIGHT
		},
		panControl: true,
		panControlOptions: {
			position: google.maps.ControlPosition.TOP_LEFT
		},
		zoomControl: true,
		zoomControlOptions: {
			style: google.maps.ZoomControlStyle.LARGE,
			position: google.maps.ControlPosition.TOP_LEFT
		},
		scaleControl: true,
		scaleControlOptions: {
			position: google.maps.ControlPosition.BOTTOM_LEFT
		},
		streetViewControl: true,
		streetViewControlOptions: {
			position: google.maps.ControlPosition.TOP_LEFT
		},
		scrollwheel : false
	};

	var _markerImage = new google.maps.MarkerImage(HwReg.get('staticImageURI') + "hw_icon.png",
		new google.maps.Size(26, 29),
		new google.maps.Point(0,0),
		new google.maps.Point(0, 29)
	);
	var _markerImageRegion = new google.maps.MarkerImage(HwReg.get('staticImageURI') + "hw_icon_purple.png",
		new google.maps.Size(26, 29),
		new google.maps.Point(0,0),
		new google.maps.Point(0, 29)
	);
	var _markerShadow = new google.maps.MarkerImage(HwReg.get('staticImageURI') + "hw_icon_shadow.png",
		new google.maps.Size(59, 19),
		new google.maps.Point(0,0),
		new google.maps.Point(0,29)
	);


	if (options.center) {
		defaultGmapsOptions.center = new google.maps.LatLng(options.center[0], options.center[1]);
	}
	var gmapsOptions = $.extend(defaultGmapsOptions, options.options);
    return this.each(function() {
		var map = new google.maps.Map(this, gmapsOptions);
		// Check if we have markers
		if (options.markers != undefined)
		{
			var _openedInfoWindowIndex = null;
			var _infoWindows = new Array();
			$.each(options.markers, function(index, element) {
			    //dont show the zero coordinate markers
			    if (element.location[0] != 0.0000000 && element.location[1] != 0.0000000) {
				var markerLoc = new google.maps.LatLng(element.location[0], element.location[1]);
				var markerShadow = _markerShadow;
				var markerImage;
				if (element.type == 'region')
				{
					 markerImage = _markerImageRegion;
				}
				else
				{
				 	markerImage = _markerImage;
				}
				//check if we displaying the numbered markers
				if (options.options != undefined && options.options.useNumberedMarkers != undefined)
				{
					var pinImgPath = HwReg.get('staticImageURI') + "mappins/marker" + index + ".png";
					markerImage = new google.maps.MarkerImage(pinImgPath,
						new google.maps.Size(20,34),
						new google.maps.Point(0,0),
						new google.maps.Point(0, 34)
					);
					markerShadow = new google.maps.MarkerImage(HwReg.get('staticImageURI') + "mappins/markershadow.png",
						new google.maps.Size(37, 34),
						new google.maps.Point(0,0),
						new google.maps.Point(0,34)
					);
				}
				else if (element.info.type != undefined)
				{
					markerImage = new google.maps.MarkerImage(HwReg.get('staticImageURI') + 'mappins/' + element.info.type.toString().toLowerCase() + '_marker.png',
						new google.maps.Size(19,17),
						new google.maps.Point(0,0),
						new google.maps.Point(0,17)
					);
					markerShadow = new google.maps.MarkerImage(HwReg.get('staticImageURI') + 'mappins/marker_shadow.png',
						new google.maps.Size(19,17),
						new google.maps.Point(0,0),
						new google.maps.Point(0,17)
					);
				}


				var marker = new google.maps.Marker({
					position: markerLoc,
					map: map,
					shadow: markerShadow,
					icon: markerImage,
					title: element.info.title,
					zIndex: parseInt(1+index)
				});
				_infoWindows[index] = new google.maps.InfoWindow({
					content: element.info.content
				});
				google.maps.event.addListener(marker, 'click', function() {
					if (_openedInfoWindowIndex) {
						_infoWindows[_openedInfoWindowIndex].close();
					}
				  _infoWindows[index].open(map, marker);
				  _openedInfoWindowIndex = index;
				});
			    }
			});
			google.maps.event.addListener(map, 'click', function() {
				for (i in _infoWindows) {
					_infoWindows[i].close();
				}
			});
		}
    });
};
})(jQuery);

