
var arr_prop = new Array();
var GmapcomObj;

$(document).ready(function(){
	
	$('div').each(function(){
		regs = this.id.match(/^map_(.*)_(.*)$/);
		if(regs) arr_prop[regs[1]] = parseInt( getelem('mapcount_'+regs[1]).value );
	});
	var actions = function(){};
	actions.prototype = {
		gmap_show: function(){
			this.mapvalue.style.display='';
			this.mapvalue.style.width=this.w.value + 'px';
			this.mapvalue.style.height=this.h.value + 'px';
			this.map = new GMap2(this.mapvalue);
			
		},
		gmap_show_smallbox: function(){
			this.mapvalue.style.width='100px';
			this.mapvalue.style.height='25px';
		},
		gmap_setcenter: function(x, y){
			if(!this.map) return;
			if(!x) x = 36.578273; if(!y) y = 136.647763;
			this.map.setCenter(new GLatLng(x, y), this.gmapcom_zoom);
		},
		gmap_options: function(){
			if(!this.map) return;
			if(this.ctrlvalue.value) eval( 'this.map.addControl(new ' + this.ctrlvalue.value + '());' );
			if(this.typectrlvalue.value) this.map.addControl(new GMapTypeControl());
			if(this.overvalue.value) this.map.addControl(new GOverviewMapControl());
		},
		gmap_hide: function(){
			this.mapvalue.style.width='0px';
			this.mapvalue.style.height='0px';
			this.mapvalue.style.display='none';
		}
	};
	GmapcomObj = function(prop, i){
		
		if(prop == null || i == null) return;
		this._prop = prop; this._i = i;
		
		param = prop + "_" + i;
		
		this.mapvalue = getelem("map_" + param);
		this.addrvalue = getelem("addr_" + param);
		this.zoomvalue = getelem("zoom_" + param);
		this.ctrlvalue = getelem("ctrl_" + param);
		this.typectrlvalue = getelem("typectrl_" + param);
		this.overvalue = getelem("over_" + param);
		this.maptypevalue = getelem("maptype_" + param);
		this.commentvalue = getelem("comment_" + param);
		this.markervalue = getelem("marker_" + param);
		this.displayvalue = getelem("display_" + param);
		
		this.w = getelem("w_" + param);
		this.h = getelem("h_" + param);
		
		this.gmapcom_maptype = this.maptypevalue.value;
		this.gmapcom_comment = this.commentvalue.value ? this.commentvalue.value : this.addrvalue.value;
		this.gmapcom_zoom = parseInt(this.zoomvalue.value);
		this.gmapcom_marker = this.markervalue.value ? parseInt(this.markervalue.value) : 1;
		
		this.address = this.addrvalue.value;
		
	};
	GmapcomObj.prototype = new actions;
	
	if (GBrowserIsCompatible()) {
		for (var prop in arr_prop) {
			for(i=0; i<arr_prop[prop]; i++){
				
				var gmapcom = new GmapcomObj(prop, i);
				if(gmapcom.displayvalue.value == 0){
					gmapcom.gmap_hide();
					gmapcom.gmap_show_smallbox();
					continue;
				}
				gmapcom.gmap_show();
				gmapcom.gmap_options();
				gmapcom.gmap_setcenter();
				get_geocoding(gmapcom);
				
				gmapcom = null;
				
			}
		}
	}
});
function get_geocoding(gmapcom){
	geo = new GClientGeocoder();
	geo.getLatLng(
		gmapcom.address,
		function(point){
			if(point){
				gmapcom.map.setCenter(point, gmapcom.gmapcom_zoom);
				gmapcom.map.setMapType(eval(gmapcom.gmapcom_maptype));
				marker = new GMarker(point);
				gmapcom.map.addOverlay(marker);
				if(gmapcom.gmapcom_marker) marker.openInfoWindowHtml(gmapcom.gmapcom_comment);
			} else {
				// not found.
			}
		}
	);
}
function gmap_show_click(prop, i){
	
	var gmapcom = new GmapcomObj(prop, i);
	
	if(gmapcom.mapvalue.style.display){ // none
		gmapcom.gmap_show();
		gmapcom.gmap_options();
		gmapcom.gmap_setcenter();
		get_geocoding(gmapcom);
		
	} else  gmapcom.gmap_hide();
	
}
function getelem(id){
	return document.getElementById(id);
}

