var basePath = "";

var map = null;
var timer = null;
var restDiv = null;
var restLoc = {};
var restMarker = {};

var disableFloatMap = false;

window.onload = function() {
	restLoc = {};
	restMarker = {};
	initialize(minLat, minLng, maxLat, maxLng);
}

window.onunload = function() {
	GUnload();
}

function setBasePath(base) {
	basePath = base;
}

function makeCopyrightSmaller() {
	if(map.isLoaded()) {
		for(var i = 0; i < map.getContainer().childNodes.length; ++ i) {
			if(map.getContainer().childNodes[i].innerHTML.indexOf(String.fromCharCode(169)) !== -1) {
				map.getContainer().childNodes[i].style.fontSize = '7px';
				window.clearInterval(copyrightTimer);
				break;
			}
		}
	}
}

function initialize(minLat, minLng, maxLat, maxLng) {	
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("GoogleMap"), { size: new GSize(296,292) });
		map.addControl(new TextualZoomControl());
		
		//map.enableScrollWheelZoom();
		//map.setCenter(new GLatLng(22.367942499999998, 114.111907), 10);
		if((minLat == 9999999999.99) && (minLng == 9999999999.99) && (maxLat == 0.00) && (maxLng == 0.00))
			map.setCenter(new GLatLng(22.367942499999998, 114.111907), 10);
		else {
			botLeft = new GLatLng(minLat, minLng);
			topRight = new GLatLng(maxLat, maxLng);
			bounds = new GLatLngBounds(botLeft, topRight);
			map.setCenter(new GLatLng(((maxLat + minLat) / 2.0), ((maxLng + minLng) / 2.0)), map.getBoundsZoomLevel(bounds));
		}
		
		if(!map.isStatic)
			copyrightTimer=window.setInterval('makeCopyrightSmaller()', 150)
		for (var i = 0; i < mapInfo.restsInfo.length; i++) {
			restLoc[i] = new GLatLng(mapInfo.restsInfo[i].lat, mapInfo.restsInfo[i].lon);
			if(mapInfo.restsInfo[i].lat != '' && mapInfo.restsInfo[i].lon != '')
			{
				map.addOverlay(createMarker(i));
			}
		}
		
		//GEvent.addListener(map, "dragend", function() {
		//	map.setCenter(map.getCenter(), 10);
		//});
	}
}

function createMarker(i) {
	var uFoodIcon = new GIcon(G_DEFAULT_ICON);
	uFoodIcon.image = "../image/map/GoogleMap_icon.gif";
	uFoodIcon.iconAnchor = new GPoint(10, 30);
	uFoodIcon.shadow = "../image/map/shadow.png";

	restMarker[i] = new GMarker(restLoc[i], { icon:uFoodIcon });
	restMarker[i].value = i;

	GEvent.addListener(restMarker[i], "mouseover", function() {
		showDetail(i);
	});

	GEvent.addListener(restMarker[i], "mouseout", function() {
		timer = setTimeout(hiddenDetail, 5000);
	});
	
	GEvent.addListener(map, "drag", function(){
		if(restDiv != null) {
			hiddenDetail();
		}
		reLocation(i);
	});  
	
	GEvent.addListener(map, "zoomend", function(){
		if(restDiv != null) {
			hiddenDetail();
		}
		reLocation(i);
	});
	
	return restMarker[i];
}

function reLocation(i){
	var container=map.getContainer();
	var mapPos=DOM.getXY(container);
	
	var divPos = map.fromLatLngToContainerPixel(restLoc[i]);
	//restDiv = document.getElementById("rest_" + mapInfo.restsInfo[i].restId);
	if(restDiv != null) {
		restDiv.style.left=(divPos.x + mapPos[0] + 10) + "px"
		restDiv.style.top=(divPos.y + mapPos[1] - 150) + "px"
	}
}

function showDetail(i) {
	if(restDiv != null) {
		hiddenDetail();
	}
	
	restDiv = createPopDiv(i);
	//restDiv = document.getElementById("rest_" + mapInfo.restsInfo[i].restId);
	//alert(restDiv.innerHTML);
	//map.setCenter(restLoc[i], 10);
	//offsetX = 390;
	//offsetY = 180;
	/*
	$().mousemove(function(e){
		//offsetX = e.pageX + 390;
		//offsetY = e.pageY + 480;
		offsetX = e.pageX;
		offsetY = e.pageY;
	}); 
	*/
	//map.checkResize();
	//var restPixel = map.fromLatLngToDivPixel(restLoc[i]);
	//restDiv.style.top = (restPixel.y - offsetY) + 'px';
	//restDiv.style.left = (restPixel.x - offsetX) + 'px';
	//restDiv.style.top = offsetY;
	//restDiv.style.left = offsetX;
	if(restDiv != null) {
		restDiv.style.zIndex = i + 100;
		restDiv.style.display = "block";
		reLocation(i);
	}
}

function hiddenDetail() {
	if(restDiv != null) {
		restDiv.style.zIndex = 0;
		restDiv.style.display = "none";
		document.body.removeChild(restDiv);
		restDiv = null;
	}
}
// Custom Zoom Icon
function TextualZoomControl() {
}

TextualZoomControl.prototype = new GControl();

TextualZoomControl.prototype.initialize = function(map) {
	var container = document.createElement("div");
	
	var zoomInDiv = document.createElement("div");
	this.setButtonStyle_(zoomInDiv);
	container.appendChild(zoomInDiv);
	
	var zoomInImg = document.createElement("img");
	zoomInImg.src = "../image/map/Map_ZoomIn.gif";
	zoomInDiv.appendChild(zoomInImg);
	GEvent.addDomListener(zoomInDiv, "click", function() {
		map.zoomIn();
	});
	
	var zoomOutDiv = document.createElement("div");
	this.setButtonStyle_(zoomOutDiv);
	container.appendChild(zoomOutDiv);
	
	var zoomOutImg = document.createElement("img");
	zoomOutImg.src = "../image/map/Map_ZoomOut.gif";
	zoomOutDiv.appendChild(zoomOutImg);
	GEvent.addDomListener(zoomOutDiv, "click", function() {
		map.zoomOut();
	});

	map.getContainer().appendChild(container);
	return container;
}

TextualZoomControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}

TextualZoomControl.prototype.setButtonStyle_ = function(button) {
	//button.style.textDecoration = "underline";
	//button.style.color = "#0000cc";
	//button.style.backgroundColor = "white";
	//button.style.font = "small Arial";
	//button.style.border = "1px solid black";
	button.style.padding = "1px";
	//button.style.marginBottom = "3px";
	//button.style.textAlign = "center";
	//button.style.width = "6em";
	button.style.cursor = "pointer";
}

// Added by Kira@20100115
function createPopDiv(i) {
	var popRestContainer = document.createElement("DIV");
	//popRestContainer.id = "rest_" + mapInfo.restsInfo[i].restId;
	//popRestContainer.name = "rest_" + mapInfo.restsInfo[i].restId;
	popRestContainer.style.position = "absolute";
	popRestContainer.className = "rest_newmappage_container";
	
	var popRestInfo = document.createElement("DIV");
	popRestInfo.className = "shadow";
	// Pop on Right hand side
	popRestInfo.style.left = "25px";
	var restInfoHTML = "";
	restInfoHTML += "<div class='mainrestinfo'>";
	restInfoHTML += "<div class='item'>";
	restInfoHTML += "<div class='item_photo'><table cellspacing='0' cellpadding='0'><tr><td><a href='" + mapInfo.restsInfo[i].restUrl + "'><img src='" + mapInfo.restsInfo[i].photoPath + "' alt='" + mapInfo.restsInfo[i].name + "' onLoad='reSize(this, 112);' /></a></td></tr></table></div>";
	restInfoHTML += "<div class='name'>" + mapInfo.restsInfo[i].name + "</div>";
	restInfoHTML += "<div class='address'>" + mapInfo.restsInfo[i].addr + "</div>";
	restInfoHTML += "<div class='phone'>" + mapInfo.restsInfo[i].phone + "</div>";
	restInfoHTML += "<div class='rating'>" + mapInfo.restsInfo[i].score + "</div>";
	restInfoHTML += "<div class='type'>";
	restInfoHTML += "<a href='" + mapInfo.restsInfo[i].ftUrl + "'>" + mapInfo.restsInfo[i].ft + "</a>";
	if(mapInfo.restsInfo[i].ft != '')
		restInfoHTML += "&nbsp;";
	restInfoHTML += "<a href='" + mapInfo.restsInfo[i].rtUrl + "'>" + mapInfo.restsInfo[i].rt + "</a>";
	if(mapInfo.restsInfo[i].rt != '')
		restInfoHTML += "&nbsp;";
	restInfoHTML += "<a href='" + mapInfo.restsInfo[i].ctUrl + "'>" + mapInfo.restsInfo[i].ct + "</a></div>";
	restInfoHTML += "<div class='spending'>" + mapInfo.restsInfo[i].avgSpending + "</div>";
	restInfoHTML += "<div class='bottom'><a href='" + mapInfo.restsInfo[i].restUrl + "'><img src='" + basePath + "/image/restaurant/restaurant_map_detail.gif' alt='' width='37' height='15' border='0' /></a></div>";
	restInfoHTML += "</div>";
	restInfoHTML += "</div>";
	popRestInfo.innerHTML = restInfoHTML;
	/* Pop On Left hand side
	var popRestArrow = document.createElement("DIV");
	popRestArrow.className = "position";
	popRestArrow.innerHTML = "<div class='arrow'></div>";
	*/
	// Pop on Right hand side
	var popRestArrow = document.createElement("DIV");
	popRestArrow.className = "position2";
	popRestArrow.innerHTML = "<div class='arrow2'></div>";
	/* Pop On Left hand side
	popRestContainer.appendChild(popRestInfo);
	popRestContainer.appendChild(popRestArrow);
	*/
	// Pop on Right hand side
	popRestContainer.appendChild(popRestArrow);
	popRestContainer.appendChild(popRestInfo);

	document.body.appendChild(popRestContainer);
	
	return popRestContainer;
}