$.fn.jgm = function(args) {
	$.fn.jgm.settings = {};
	$.fn.jgm.point = Array();	
	
	try {
		if(this.length > 0 && GBrowserIsCompatible()) {
	
			if(args) {
				$.extend($.fn.jgm.settings, args);
			};
			
			$.fn.jgm.map = new GMap2(this[0], {mapTypes: [G_PHYSICAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP,G_NORMAL_MAP]});
			$.fn.jgm.map.setCenter(new GLatLng($.fn.jgm.settings.mapcenter.lat, $.fn.jgm.settings.mapcenter.lng), 11); 
			$.fn.jgm.map.addControl(new GSmallMapControl());
			
			if($.fn.jgm.settings.tourid) {
				$.fn.jgm.draw();
			}
		}		
	} catch(e) {
		//alert(e);
		return;		
	}
}

$.fn.jgm.draw = function() {
	var polypoint = Array();
	
  $.getJSON($.fn.jgm.settings.baseurl + "/ajax.php?app=googlemaps&method=get_waypoint_json&tour-id=" + $.fn.jgm.settings.tourid, function(data){
    $.each(data.marker, function(i, item) {
			$.fn.jgm.point[i] = new GLatLng(item.lat, item.lng);
			$.fn.jgm.point[i].id = item.id;
    });
		
		var polygon = new GPolygon($.fn.jgm.point, "#ff9129", 2, 1, null, null);
		$.fn.jgm.map.addOverlay(polygon);
  });
}

$.fn.jgm.pointlistener = function() {
	$.each($.fn.gm.point, function(i, point) {
		
		marker = new GMarker(point);
		$.fn.gm.map.addOverlay(marker);
		
		GEvent.addListener(marker, "click", function() {
			//console.log(point);
			//alert("x: " + point.x + "; y: " + point.y + "; id: " + point.id);
		});
	});
}

$(window).unload(function() {
	try {
		GUnload()		
	} catch(e) { }	
});
