var ctx_pixel;
var ctx_overlay;
var ctx_menu;

mapContextHtml = '<a href="javascript:ctx_zoom_in()"><div class="context">Yaklaş</div></a>'
			 + '<a href="javascript:ctx_zoom_out()"><div class="context">Uzaklaş</div></a>'
			 + '<a href="javascript:ctx_zoom_in_here()"><div class="context">Buraya Yaklaş</div></a>'
			 + '<a href="javascript:ctx_zoom_out_here()"><div class="context">Buradan Uzaklaş</div></a>'
			 + '<a href="javascript:ctx_centre_map_here()"><div class="context">Buraya Ortala</div></a>'
			 + '<hr>'
			 + '<a href="javascript:ctx_pick_my_place()"><div class="context">Konumunu İşaretle</div></a>'
			 + '<a href="javascript:ctx_map_help()"><div class="context">Yardım</div></a>';
markerContextHtml = '<a href="javascript:ctx_poi_info()"><div class="context">&nbsp;&nbsp;Bayi Bilgileri&nbsp;&nbsp;<\/div><\/a>'
			 + '<a href="javascript:ctx_poi_zoom()"><div class="context">&nbsp;&nbsp;Yakınlaş&nbsp;&nbsp;<\/div><\/a>'
			 + '<a href="javascript:ctx_poi_dirs()"><div class="context">&nbsp;&nbsp;Yol Tarifi Al&nbsp;&nbsp;<\/div><\/a>';
mypContextHtml = '<a href="javascript:ctx_myp_delete()"><div class="context">&nbsp;&nbsp;İşareti Kaldır&nbsp;&nbsp;<\/div><\/a>';
carContextHtml = '<a href="javascript:ctx_send_mail()"><div class="context">&nbsp;&nbsp;Posta Gönder&nbsp;&nbsp;<\/div><\/a>'
			 + '<a href="javascript:ctx_send_print()"><div class="context">&nbsp;&nbsp;Yazdır&nbsp;&nbsp;<\/div><\/a>';

function load_ctx() {
	ctx_menu = document.createElement("div");
	ctx_menu.style.visibility="hidden";
	ctx_menu.style.background="#ffffff";
	ctx_menu.style.border="1px solid #8888FF";
	map_5255.getContainer().appendChild(ctx_menu);

	// === listen for singlerightclick ===
	GEvent.addListener(map_5255,"singlerightclick",function(pixel,tile,overlay) {
		// store the "pixel" info in case we need it later
		// adjust the context menu location if near an egde
		// create a GControlPosition
		ctx_pixel = pixel;
		var x=pixel.x;
		var y=pixel.y;
		if (x > map_5255.getSize().width - 120) { x = map_5255.getSize().width - 120 }
		if (y > map_5255.getSize().height - 100) { y = map_5255.getSize().height - 100 }
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));

		// == was the click on the map? ==
		if (!overlay) {
			// insert the map HTMP, apply the position to the context menu, and make it visible 
			ctx_menu.innerHTML = mapContextHtml;
			pos.apply(ctx_menu);
			ctx_menu.style.visibility = "visible";
		} else {

			// == was the click on a GMarker? ==
			if (overlay instanceof GMarker) {
			// insert the marker HTML, apply the position to the ovelay context menu, and make it visible 
			if (overlay == my_place) {
				ctx_menu.innerHTML = mypContextHtml;
			} else if (overlay == dirs_v_marker) {
				ctx_menu.innerHTML = carContextHtml;
			} else {
				ctx_menu.innerHTML = markerContextHtml;
			}
			pos.apply(ctx_menu);
			ctx_menu.style.visibility = "visible";
			ctx_overlay = overlay;
			}          

			// == was the click on a GPolyline? ==
			if (overlay instanceof GPolyline) {
				// insert the polyline HTML, apply the position to the ovelay context menu, and make it visible 
				ctx_menu.innerHTML = polylineContextHtml;
				pos.apply(ctx_menu);
				ctx_menu.style.visibility = "visible";
				ctx_overlay = overlay;
			}          
		}
	});

	// === If the user clicks on aything, close the context menus ===
	GEvent.addListener(map_5255, "click", function() {
		ctx_menu.style.visibility="hidden";
	});

}

// === functions that implement the context menu options ===

// map context >>
function ctx_zoom_in() {
	map_5255.zoomIn();
	ctx_menu.style.visibility="hidden";
}      
function ctx_zoom_out() {
	map_5255.zoomOut();
	ctx_menu.style.visibility="hidden";
}      
function ctx_zoom_in_here() {
	var point = map_5255.fromContainerPixelToLatLng(ctx_pixel);
	map_5255.zoomIn(point,true);
	ctx_menu.style.visibility="hidden";
}      
function ctx_zoom_out_here() {
	var point = map_5255.fromContainerPixelToLatLng(ctx_pixel);
	map_5255.setCenter(point,map_5255.getZoom()-1);
	ctx_menu.style.visibility="hidden";
}      
function ctx_centre_map_here() {
	var point = map_5255.fromContainerPixelToLatLng(ctx_pixel);
	map_5255.setCenter(point);
	ctx_menu.style.visibility="hidden";
}
function ctx_pick_my_place() {
	var point = map_5255.fromContainerPixelToLatLng(ctx_pixel);
	map_5255.removeOverlay(my_place);
	my_place = new GMarker(point, {icon:myp_icon, draggable: true});
	map_5255.addOverlay(my_place);
	ctx_menu.style.visibility="hidden";
}
function ctx_map_help() {
	fancy_call_help();
	ctx_menu.style.visibility="hidden";
}
// << map context

// poi context >>
function ctx_poi_info() {
	util_express_color(ctx_overlay.poi_item.cust_id, ctx_overlay.poi_item.name);
	ctx_menu.style.visibility="hidden";
}
function ctx_poi_zoom() {
	util_marker_zoom_in(ctx_overlay.getLatLng().lat(), ctx_overlay.getLatLng().lng());
	ctx_menu.style.visibility="hidden";
}
function ctx_poi_dirs() {
	util_marker_get_dirs_ctx(ctx_overlay);
	ctx_menu.style.visibility="hidden";
}
// << poi context

// myp context >>
function ctx_myp_delete() {
	map_5255.removeOverlay(my_place);
	ctx_menu.style.visibility="hidden";
}
// << myp context

// car context >>
function ctx_send_mail() {
	fancy_send_mail();
	ctx_menu.style.visibility="hidden";
}
function ctx_send_print() {
	fancy_send_print();
	ctx_menu.style.visibility="hidden";
}
// << car context


