// JavaScript Document
/*
	Author: Willy
	Modified: 2008/11/28
*/
tagala.Contextmenu = Class.create(
{
	initialize:function (map)
	{
		if (!GEvent || !(this.map = map)) return false;
		this.contextmenu_map = this.map.getContainer().appendDiv('mapContextMenu').setStyle(
		{
			background:'#ffffff',
			border:'1px solid #8888FF',
			padding:'3px 0px'
		}).hide();
		var el = this.contextmenu_map.appendDiv('newPostFuncs').setStyle(
		{
			margin:0,
			padding:0
		});
		this.addMenuItem(el, addScene, 'add_scene');
		this.addMenuItem(el, addPath, 'add_path');
		this.addMenuItem(el, addArea, 'add_area');
		this.addMenuItem(this.contextmenu_map.appendDiv('postFunctions').setStyle(
		{
			margin:0,
			padding:0
		}), getMapPosition, 'get_position');
		el = this.contextmenu_map.appendDiv().setStyle(
		{
			margin:0,
			padding:0
		});
		if (Prototype.Browser.IE)
			this.addMenuItem(el, function (event)
			{
				event.findElement('A').setStyle(
				{
					behavior:'url(#default#homepage)'
				}).setHomePage(tagala.config.dst_url);
			}, 'set_home');
		this.addMenuItem(el, function (event)
		{
			addFavorite(tagala.config.dst_url + tagala.config.site_url, 'tagala');
		}, 'add_favorite');

		this.contextmenu_overlay = this.map.getContainer().appendDiv('overlayContextMenu').setStyle(
		{
			background:'#ffffff',
			border:'1px solid #8888FF',
			padding:'3px 0px'
		}).hide();
		el = this.contextmenu_overlay.appendDiv('overlayFuncs').setStyle(
		{
			margin:0,
			padding:0
		});
		this.addMenuItem(el, function ()
		{
			(this.selectedOverlay.enableDrawing || Prototype.emptyFunction)();
		}.bind(this), 'enable_drawing');
		this.addMenuItem(el, function ()
		{
			(this.selectedOverlay.disableEditing || Prototype.emptyFunction)();
		}.bind(this), 'disable_drawing');

		GEvent.addListener(this.map, 'singlerightclick', function(point, src, overlay)
		{
			if (overlay)
			{
				if (overlay.getArea || overlay.getLength)	//polygon or polyline
					this.showContextMenu(point, overlay);
			}
			else
				this.showContextMenu(point);
		}.bind(this));
		GEvent.addListener(map, 'click', function() {
			this.hideContextMenu();
		}.bind(this));

		$('overlayContextMenu').observe('mouseover', function()
		{
			window.clearTimeout(this.hideMenuTimer);
			this.hideMenuTimer = null;
		}.bind(this));

		$('overlayContextMenu').observe('mouseout', this.hideContextMenuDelay.bind(this));
	},

	addMenuItem:function (el, UrlOrFunc, title, keepMenu)
	{
		if (!(el = $(el))) return;
		var aTag = el.appendA(Object.isString(UrlOrFunc) ? UrlOrFunc : '').addClassName('context');
		if (Object.isFunction(UrlOrFunc))
			aTag.observe('click', function (event)
			{
				if (!keepMenu) this.hideContextMenu();
				UrlOrFunc(event);
				event.stop();
			}.bindAsEventListener(this));
		aTag.appendLocale({K:'&nbsp;&nbsp;#{title}&nbsp;&nbsp;', V:{title:{K:title}}}, 'div').addClassName('context');
	},

	showContextMenu:function (point, overlay)
	{
		this.selectedOverlay = overlay;
		this.clickedPoint = point;
		var x = point.x;
		var y = point.y;
		var newLatLng = this.map.fromContainerPixelToLatLng(new GPoint(x, y));
		sceneX = (newLatLng.lng()*1000000).round()/1000000;
		sceneY = (newLatLng.lat()*1000000).round()/1000000;
		if (x > this.map.getSize().width - 120) x = this.map.getSize().width - 120;
		if (y > this.map.getSize().height - 100) y = this.map.getSize().height - 100;

		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));
		if (overlay)
		{
			pos.apply(this.contextmenu_overlay);
			this.contextmenu_map.hide();
			this.contextmenu_overlay.show();
		}
		else
		{
			pos.apply(this.contextmenu_map);
			this.contextmenu_overlay.hide();
			this.contextmenu_map.show();
		}
	
		/* Add following lines by finney */
		if ($('session_id')) {
			if (!$F('session_id').blank())
				$('newPostFuncs').show();
			else
				$('newPostFuncs').hide();
	
			if ($('tabInfo_frame0')) {
				if ($('tabInfo_frame0').contentWindow.document.getElementById('longitude'))
					$('postFunctions').show();
				else
					$('postFunctions').hide();
			}
		}
	},

	hideContextMenu:function ()
	{
		[this.contextmenu_map, this.contextmenu_overlay].invoke('hide');
	},

	hideContextMenuDelay:function ()
	{
		if (!this.hideMenuTimer)
		{
			this.hideMenuTimer = function ()
			{
				this.hideMenuTimer = null;
				this.hideContextMenu();
			}.delay(tagala.config.sameRequestTimeDelay)
		}
	},

	addFavorite:function (url, title)
	{
		if (window.sidebar) 
			window.sidebar.addPanel(title, url, '');
		else if( window.opera && window.print )
		{
			(new Element('A',
			{
				rel:'sidebar',
				href:url,
				title:title
			})).click();
		}
		else if(document.all)
			window.external.AddFavorite(url, title);
	}
});
