/****************
* initContextMenu
*****************/
function initContextMenu(){
	contextmenu.style.visibility="hidden";
	contextmenu.style.background="#ffffff";
	contextmenu.style.border="1px solid #8888FF";

	contextmenu.innerHTML = ''
						+ '<div class="context" onClick= "setHome()" style= "margin-top:2px;">&nbsp;<img src= "images/home_sm.png" />&nbsp;Set As My Home Location &nbsp;&nbsp;</div>'
						+ '<div class="context" onClick= "removeHome()">&nbsp;<img src= "images/home_rem.png" />&nbsp;Remove My Home Location &nbsp;&nbsp;</div>'
						+ '<div class= "cBorder"></div>'

						+ '<div class="context" onClick= "showPlaces(\'food\')">&nbsp;<img src= "images/food.png" />&nbsp;"Food" In This Area&nbsp;&nbsp;</div>'
						+ '<div class="context" onClick= "showPlaces(\'ent\')">&nbsp;<img src= "images/ent.png" />&nbsp;"Entertainment" In This Area&nbsp;&nbsp;</div>'
						+ '<div class="context" onClick= "showPlaces(\'sports\')">&nbsp;<img src= "images/sports.png" />&nbsp;"Sports" In This Area&nbsp;&nbsp;</div>'
						+ '<div class="context" onClick= "showPlaces(\'all\')">&nbsp;<img src= "images/map.png" />&nbsp;"All Places" In This Area&nbsp;&nbsp;</div>'
						+ '<div class= "cBorder"></div>'

						+ '<div class="context" onClick= "zoomIn()">&nbsp;<img src= "images/zoomIn.png" />&nbsp;Zoom in&nbsp;&nbsp;</div>'
						+ '<div class="context" onClick= "zoomOut()">&nbsp;<img src= "images/zoomOut.png" />&nbsp;Zoom out&nbsp;&nbsp;</div>'
						+ '<div class="context" onClick= "zoomInHere()">&nbsp;<img src= "images/zoomInHere.png" />&nbsp;Zoom in here&nbsp;&nbsp;</div>'
						+ '<div class="context" onClick= "zoomOutHere()">&nbsp;<img src= "images/zoomOutHere.png" />&nbsp;Zoom out here&nbsp;&nbsp;</div>'
						+ '<div class="context" onClick= "centreMapHere()">&nbsp;<img src= "images/centre.png" />&nbsp;Centre map here&nbsp;&nbsp;</div>'
						+ '<div class= "cBorder"></div>'

						+ '<div class="context" onClick= "contextClearMap()">&nbsp;<img src= "images/clear.png" />&nbsp;Clear Map&nbsp;&nbsp;</div>'

	map.getContainer().appendChild(contextmenu);

	// === listen for singlerightclick ===
	GEvent.addListener(map,"singlerightclick",function(pixel,tile) {
		// store the "pixel" info in case we need it later
		// adjust the context menu location if near an egde
		// create a GControlPosition
		// apply it to the context menu, and make the context menu visible
		clickedPixel = pixel;
		var x=pixel.x;
		var y=pixel.y;
		if (x > map.getSize().width - 200) { x = map.getSize().width - 200 }
		if (y > map.getSize().height - 235) { y = map.getSize().height - 235 }
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));
		pos.apply(contextmenu);
		contextmenu.style.visibility = "visible";
	});

	// === If the user clicks on the map, close the context menu ===
	GEvent.addListener(map, "click", function() {
		contextmenu.style.visibility="hidden";
	});

}//end initContextMenu


function zoomIn() {
	map.zoomIn();
	contextmenu.style.visibility="hidden";
}

function zoomOut() {
	map.zoomOut();
	contextmenu.style.visibility="hidden";
}

function zoomInHere() {
	var point = map.fromContainerPixelToLatLng(clickedPixel)
	map.zoomIn(point,true);
	contextmenu.style.visibility="hidden";
}

function zoomOutHere() {
	var point = map.fromContainerPixelToLatLng(clickedPixel)
	map.setCenter(point,map.getZoom()-1); // There is no map.zoomOut() equivalent
	contextmenu.style.visibility="hidden";
}

function centreMapHere() {
	var point = map.fromContainerPixelToLatLng(clickedPixel)
	map.setCenter(point);
	contextmenu.style.visibility="hidden";
}


/***********
* showPlaces
************/
function showPlaces(type){
	centreMapHere();
	if (map.getZoom() < 15){
		map.setZoom(15);
	}//end if
	inMyArea(type);
	contextmenu.style.visibility="hidden";
}//end showPlaces


/****************
* contextClearMap
*****************/
function contextClearMap(){
	clearMap();
	contextmenu.style.visibility="hidden";
}//end contextClearMap


var homeMarker;

/*********
* setHome
**********/
function setHome(pPoint){

	if(homeMarker)
		removeHome();

	if(pPoint)
		var point = pPoint;
	else {
		var point = map.fromContainerPixelToLatLng(clickedPixel);

		setCookie("lat", point.lat(), 999);
		setCookie("lng", point.lng(), 999);
	}//end if

	map.addOverlay(createHomeMarker(point) );
	contextmenu.style.visibility="hidden";
}//end setHome


/*********
* showHome
**********/
function showHome(){
	if (getCookie("lat"))
		map.addOverlay(homeMarker);
}//end showHome


/******************
* createHomeMarker
*******************/
function createHomeMarker(point) {
	var customIcon = new GIcon();
		customIcon.image = 'images/home.png';
		customIcon.iconSize = new GSize(28, 28);
		customIcon.iconAnchor = new GPoint(6, 20);
		customIcon.infoWindowAnchor = new GPoint(5, 1);

	homeMarker = new GMarker(point, {title: "My Home", icon: customIcon});

	GEvent.addListener(homeMarker, "click", function() {
		var lat= homeMarker.getPoint().lat();
		var lng= homeMarker.getPoint().lng();
		var homeHTML;
		homeHTML = '<b>My Home</b>'
		+ '<br/><br/>'
		+ '<img src= "images/search.gif"/> <span class= "context" onClick= \'setZoom(' + lat + ',' + lng + ', this);\'>Zoom In</span><br/>'
		+ '<img src= "images/food.png"/> <span class="context" onClick= "placesNearHome(\'food\')">Restaurants Near My Home</span><br/>'
		+ '<img src= "images/ent.png"/> <span class="context" onClick= "placesNearHome(\'ent\')">Entertainment Near My Home</span><br/>'
		+ '<img src= "images/sports.png"/> <span class="context" onClick= "placesNearHome(\'sports\')">Sports Near My Home</span>';

		homeMarker.openInfoWindowHtml(homeHTML);
	}); // end click
	return homeMarker;
}//end createHomeMarker


/************
* removeHome
*************/
function removeHome(){
	map.removeOverlay(homeMarker);

	var d = new Date();
	document.cookie = "lat=1;expires=" + d.toGMTString() + ";" + ";";
	document.cookie = "lng=1;expires=" + d.toGMTString() + ";" + ";";

	contextmenu.style.visibility="hidden";
}//end removeHome

