var Map = function(target, nl, el, zoom){
	this.target = target;
	this.nl = nl;
	this.el = el;
	this.zoom = zoom;
	var point = null;
	var gmap = null;
	var markerSet = new Array();

	this.init = function(){
		if(!GBrowserIsCompatible()){
			return false;
		}
		
		this.gmap = new GMap2(this.target);
		this.point = new GLatLng(this.nl, this.el);
		
		return true;
	};
	
	this.showMap = function(showType, showLarge){
		if(showType){
			this.gmap.addControl(new GMapTypeControl());
		}
		if(showLarge){
			this.gmap.addControl(new GOverviewMapControl());
		}
		this.gmap.addControl(new GLargeMapControl());
	};
	
	this.setCenter =  function(showMarker){
		this.gmap.setCenter(this.point, parseInt(this.zoom));
		if(showMarker){
			this.gmap.addOverlay(new GMarker(this.point));
		}
	};
	
	this.setMarkerSet = function(key, object){
		markerSet[key] = object;
	};
	
	this.getMarkerSet = function(target){
		return markerSet[target];
	};
	
	this.showMarkerSet = function(target){
		if(markerSet[target].isLoaded()){
			markerSet[target].showAll();
		}else{
			markerSet[target].loadMarkerSet();
			var markers = markerSet[target].getAll();
			for(i=0;i<markers.length;i++){
				this.gmap.addOverlay(markers[i]);	
			}
			markerSet[target].setLoaded(true);
		}			
	};
	
	this.hideMarkerSet = function(target){
		markerSet[target].hideAll();
	};
}

var MarkerSet = function(label, pin, id){
	this.id = id;
	this.label = label;
	this.pin = pin;
	var markers = new Array();
	var icon = null;
	var loaded = false;
	
	this.addMarker = function(nl, el, text){
		var marker = new GMarker(new GLatLng(nl, el), _getIcon());
		GEvent.addListener(marker, 'click', function(){
			this.openInfoWindowHtml(text);
		});
		markers.push(marker);
	};
	
	this.hideAll = function(){
		for(i=0;i<markers.length;i++){
			markers[i].hide();
		}
	};
	
	this.showAll = function(){
		for(i=0;i<markers.length;i++){
			markers[i].show();
		}
	};
	
	this.getAll = function(){
		return markers;
	};
	
	this.isLoaded = function(){
		return loaded;
	};
	
	this.setLoaded = function(_loaded){
		loaded = _loaded;
	};
	
	var _getIcon = function(){
		if(icon == null){
			var gicon = new GIcon();
			gicon.image = "http://maps.google.co.jp/mapfiles/ms/icons/" + pin + ".png";
			gicon.shadow = "http://maps.google.co.jp/mapfiles/ms/icons/" + pin + ".shadow.png";
			gicon.iconSize = new GSize(32,32);
			gicon.shadowSize = new GSize(59,32);
			gicon.iconAnchor = new GPoint(16,32);
			gicon.infoWindowAnchor = new GPoint(16,0);
			icon = gicon;
		}
		return icon;
	};
};