/* Source Create meets Google map */
var SCmap = {
	map   : null,
	geo   : null,
    bound : null,
	zoom  : 17,
	init : function(id) {
		if(this.map)
	    	return;

		if(typeof GBrowserIsCompatible != "function")
	    	return;

		this.map = new GMap2(document.getElementById(id));
		this.map.addControl(new GLargeMapControl());	/* 上下左右のナビ */
//		this.map.addControl(new GMapTypeControl()); /	* 地図/航空写真などの切り替え */
//		this.map.addControl(new GOverviewMapControl()); /* 右下のナビ */
//		this.map.addControl(new GScaleControl());		 /* スケール調整 */

		new GKeyboardHandler(this.map);
		this.geo = new GClientGeocoder();
		GEvent.addListener(this.map, 'click', function(overlay, point) {
			if (overlay && overlay.openInfoWindow)
				overlay.openInfoWindowHtml(overlay.info)
		});
	},
	addPin : function(address, title, info) {
		if(!this.geo)
			this.geo = new GClientGeocoder();

		this.geo.getLatLng(
			address,
			function(point) {
				if(!(title && info))
					return;

				SCmap.map.setCenter(new GLatLng(point.lat(),  point.lng()), SCmap.zoom);
				var marker = new GMarker( new GLatLng(point.lat(),  point.lng()) );
				marker.title = title;
				marker.info  = info;
				SCmap.map.addOverlay( marker );
				if(SCmap.bound)
					SCmap.bound.extend( marker.getPoint() );
				else
					SCmap.bound = new GLatLngBounds( marker.getPoint(), marker.getPoint() );

				if(SCmap.zoom)
					SCmap.map.setCenter( SCmap.bound.getCenter(), SCmap.zoom );
				else
					SCmap.map.setCenter( SCmap.bound.getCenter(), SCmap.map.getBoundsZoomLevel( SCmap.bound ) );
			}
		);
	},
	
	setPin : function(id, address, title, info) {
		this.init(id);
		this.addPin(address, title, info);
	}
};


var SCmapStatic = {
    map   : null,
    geo   : null,
    bound : null,
    zoom  : 17,
    init  : function() {
        if(this.map)
            return;

        if(typeof GBrowserIsCompatible != "function")
            return;

        this.geo = new GClientGeocoder();
    },

    addPin : function(id, address, title, width, height, onload) {
        if(!this.geo)
            this.geo = new GClientGeocoder();

        this.geo.getLatLng(
            address,
            function(point) {
                if(!(title && width && height)) {
                    return;
                }

                var e   = document.getElementById(id);
                var src = "http://maps.google.com/staticmap?";
                src += "center=" + point.lat() + "," + point.lng() + "&";
                src += "zoom=" + SCmap.zoom + "&";
                src += "size=" + width + "x" + height + "&";
                src += "maptype=mobile&";
                src += "markers=" + point.lat() + "," + point.lng() + ",red&";
                src += "key=ABQIAAAAovhGic_MGvJnxFEqD6TJshR5YCgGGrudAyqY9xz3eQ4KURO9MhQEAkrhaiQ8BRUhmlGbZRwghfqsew";
                var onload_event = '';
                if(onload) {
                    onload_event = ' onload="'  + onload +  '" ';
                }
                var img_html = '<img src="' + src + '" alt="' + title + '" ' + onload_event + ' />';
                e.innerHTML = img_html;
            }
        );
    },

    setPin : function(id, address, title, width, height, onload) {
        this.init();
        this.addPin(id, address, title, width, height, onload);
    }
};

var SCZoom = {
	frame: 4,
	frame_rate: 16,
	vartical: 60,
	horizon:  80,
	init: function(img) {
		img.zoom_status = "none";
		SCZoom.resize(img, 0);
		img.onmouseover = function(){ SCZoom.zoom_in(this, SCZoom.frame); };
		img.onmouseout  = function(){ SCZoom.zoom_out(this, SCZoom.frame); };
	},
	zoom_in: function(img, frame) {
		if(frame == 0) {
			return
		}
		if(img.zoom_status == "cancel") {
			img.zoom_status = "none";
			return;
		}
		img.zoom_status = "in";

		SCZoom.resize(img, frame);

		if(frame == 1) {
			img.zoom_status = "none";
			return;
		}
		setTimeout(function(){ SCZoom.zoom_in(img, frame-1); }, ((SCZoom.frame+1)*SCZoom.frame_rate) - (frame*SCZoom.frame_rate));
	},
	zoom_out: function(img, frame) {
		if(frame == -1) {
			return
		}
		if(img.zoom_status == "in") {
			img.zoom_status = "cancel";
		}

		SCZoom.resize(img, (SCZoom.frame+1)-frame);

		if(frame == 0) {
			return;
		}
		setTimeout(function(){ SCZoom.zoom_out(img, frame-1); }, ((SCZoom.frame+1)*SCZoom.frame_rate) - (frame*SCZoom.frame_rate));
	},

	resize: function(img, level) {
		if(level == 0) {
			img.style.margin = "0";
			img.style.widht = "120px";
			img.style.height= "160px";
		} else {
			var v = Math.floor(SCZoom.vartical / level);
			var h = Math.floor(SCZoom.horizon / level);
			img.style.margin = " -" + Math.floor(v/4) + "px" +
			                   " -" + Math.floor(h/4) + "px";
			img.style.height= (160 + v) + "px";
			img.style.widht = (120 + h) + "px";
		}
	}
};
