 SimpleGMap = function(id, lat, lng, zoom)
{
	if(!id) return; else this.id = id;
	(!!lat)?this.lat=lat:this.lat=35.687168;
	(!!lng)?this.lng=lng:this.lng=139.757412;
	(!!zoom)?this.zoom=zoom:this.zoom=10;
	
	this.map;
	this.mapControl = new GLargeMapControl();
	this.mapScale = true;
	this.mapType = true;
	this.mapOverview = true;
	this.markers = [];
	//this.marker;
	
	//this.onLoad(this, "createMap");
}
//
// public function
//
SimpleGMap.prototype.addMarker = function(lat, lng, label)
{
	lat = roundEx(lat, 6);
	lng = roundEx(lng, 6);
	//console.log(lat+","+lng);
	//if(this.marker) return;
	var m = { lat:   lat,
				lng:   lng,
				label: label
				}
	this.markers.push(m);
	//this.marker = obj;
	var marker = this.createMarker(m.lat, m.lng, m.label);
	this.update(marker);
}
SimpleGMap.prototype.addMapControl = function(type)
{
	if(type=="L") this.mapControl = new GLargeMapControl();
	else if(type=="S") this.mapControl = new GSmallMapControl();
	else this.mapControl = false;
}
SimpleGMap.prototype.addScale = function(b)
{
	this.mapScale = b;
}
SimpleGMap.prototype.addType = function(b)
{
	this.mapType = b;
}
SimpleGMap.prototype.addOverview = function(b)
{
	this.mapOverview = b;
}
//
// private function
//
SimpleGMap.prototype.onLoad = function(scope, func)
{
	if(window.addEventListener) window.addEventListener('load', function(e){ scope[func](e); }, false);
	else if(window.attachEvent) window.attachEvent('onload', function(e){ scope[func](e); });
}
SimpleGMap.prototype.createMap = function()
{
	if(GBrowserIsCompatible())
	{
		this.map = new GMap2(document.getElementById(this.id));
		if(this.mapControl) this.map.addControl(this.mapControl);
		if(this.mapScale) this.map.addControl(new GScaleControl());
		if(this.mapType) this.map.addControl(new GMapTypeControl());
		if(this.mapOverview) this.map.addControl(new GOverviewMapControl());
		this.map.setCenter(new GLatLng(this.lat, this.lng), this.zoom); 
	}
	if(len>1) this.map.setCenter(bounds.getCenter(), this.map.getZoom() );
}
SimpleGMap.prototype.update = function(marker)
{
	this.map.addOverlay(marker);
	var len = this.markers.length;
	var bounds = new GLatLngBounds();
	for(var i=0; i<len; i++)
	{
		var m = this.markers[i];
		bounds.extend(new GLatLng(m.lat, m.lng));
	}
	if(len>1) this.map.setCenter(bounds.getCenter(), this.map.getBoundsZoomLevel(bounds));
	//if(len>1) this.drawLine();
}
SimpleGMap.prototype.createMarker = function(lat, lng, label)
{
	var marker = new GMarker(new GLatLng(lat, lng));
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(label);
	});
	return marker;
}
SimpleGMap.prototype.drawPolyline = function()
{
	var len = this.markers.length;
	var latlngs = [];
	for(var i=0; i<len; i++)
	{
		console.log(i);
		var latlng = new GLatLng(this.markers[i].lat, this.markers[i].lng)
		//var latlng = this.markers[i].getLatLng();
		latlngs.push(latlng);
	}
	var polyline = new GPolyline(latlngs, "#0033cc", 10);
	this.map.addOverlay(polyline);
}

var roundEx = function(n, l)
{
	return Math.round(n * Math.pow(10 , l)) / Math.pow(10 , l);
}