﻿var MapName = "divMap" ;
var iTop ;
var iLeft = 196 ;
var Map_onLoaded = false ;
var map ;
var geocoder ;
var intZoom = 14 ;

function func_MapSet(){
	if(GBrowserIsCompatible() && arrAddress ){
		map = new GMap2(document.getElementById(MapName));
		map.addControl(new GLargeMapControl());
		geocoder = new GClientGeocoder();
		
		for (var i=arrAddress.length-1; i >= 0 ; i--){
			func_CreatePoint(i);
		}
		map.closeInfoWindow();
		geocoder.getLatLng(
			arrAddress[0], 
			function(point) { if(point) map.panTo(point) ; }
		);
	}
	
	//mapWidthCentering();
	//OSK_moviMap();
	
	Map_onLoaded = true;
}

function func_CreatePoint(id) {
	var address = arrAddress[id];
	var caption = arrCaption[id];
	var icon = func_CreateIcon(id);
	if(address) {
		geocoder.getLatLng(address, 
			function(point) {
				if (point) {
					map.setCenter(point, intZoom);
					var strHTML = "店家名稱：" + caption + "<BR>地址：" + address ;
					var marker = func_CreateMarker(point, icon, strHTML);
					map.addOverlay(marker);
					marker.openInfoWindowHtml(strHTML);
				}
			}
		);
	}
}
function func_CreateMarker(point, icon, html) {
	var marker = new GMarker(point, icon);

	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});
	
	return marker;
}
function func_CreateIcon(id) {
	var icon = new GIcon() ;
	icon.image = "images/gi_" + (id+1) + ".gif" ;
	icon.shadow = "images/shadow.png" ;
	icon.iconSize = new GSize(20, 26) ;
	icon.shadowSize = new GSize(37, 26) ;
	icon.iconAnchor = new GPoint(10, 26) ;
	icon.infoWindowAnchor = new GPoint(10, 1) ;
	
	return icon ;
}

function func_MapMouseOver(id) {
	if( GBrowserIsCompatible() && Map_onLoaded ) {
		var strHTML = "店家名稱：" + arrCaption[id] + "<BR>地址：" + arrAddress[id] ;
		
		map.closeInfoWindow();
		geocoder.getLatLng(
			arrAddress[id], 
			function(point) { if(point) {map.panTo(point) ; map.openInfoWindowHtml(point, strHTML) ; }}
		);
	}
}


//function func_MapWidthCentering() {
//	alert(document.getElementById(MapName).style.top);
//	var setx = func_GetWidth() / 2 + 151 + func_GetDec();
//	//if(func_GetWidth() < 810){
//	//	setx = 810 / 2 + 151 + func_GetDec();
//	//}
//	iTop = setx;
//	//func_move(MapName,iTop,iLeft);
//}

//function func_GetWidth(){
//	if(window.opera){
//		return window.innerWidth;          //o6,o7
//	}else if(document.all){
//		return document.body.clientWidth;  //e4,e5,e6
//	}else if(document.layers){
//		return window.innerWidth;          //n4
//	}else if(document.getElementById){
//		return window.innerWidth;          //Safari,n6,n7,m1,s1
//	}
//	return null;
//}

//function func_GetDec(){
//	if(window.opera){
//		return -6;          //o6,o7
//	}else if(document.all){
//		return 4;  //e4,e5,e6
//	}else if(document.layers){
//		return 0;          //n4
//	}else if(document.getElementById){
//		if(navigator.userAgent.indexOf("Safari") != -1){
//			return 4;          //Safari
//		}else{
//			return -4;          //n6,n7,m1,s1
//		}
//	}
//	return null;
//}


//function func_move(idName,x,y){
//	
//    if(document.getElementById){
//    //alert('a - x:' + x + ' y:' + y);
//     	document.getElementById(idName).style.left = x+'px';
//     	document.getElementById(idName).style.top = y+'px';
//    }
//    else if(document.all){
//     	document.all(idName).style.pixelLeft = x;
//     	document.all(idName).style.pixelTop = y;
//    }
//    else if(document.layers){
//     	document.layers[idName].moveTo(x,y);
//	}
//}
