/*
============================================================================================


	JS-GIS API: JavaScript Geographic Information System browser library
 	Copyright (C) 2001, 2002 Luca Sigfrido Percich

	The JS-GIS API is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation: please refer to

	http://www.gnu.org/licenses/gpl.html

	The JS-GIS API is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.


============================================================================================

	jsgis_cursors.js	define classes fow writing map cursors

 	Author:			Luca Sigfrido Percich (luca.percich@reacoop.it)
 	Version:		2.0
 	Last updated:		june 05, 2002

	Usage: 
		1. include this script in the <HEAD> of main page or map page;
		2. Define a new TMapCursors object: 

			var MapCursors = new TMapCursors(Map, 16, 1, '../img/', '.png')

		3. call document.write(MapCursors.toString()) in the map page after Map.toString()

============================================================================================
*/




	function TMapCursors(aMap, aImgW, aDelta, aImgPath, aImgExt)
	{
		this.map	= aMap
		this.imgW	= (aImgW)	? aImgW		: 16
		delta		= (aDelta)	? aDelta	: 1
		this.imgPath	= (aImgPath)	? aImgPath	: ''
		this.imgExt	= (aImgExt)	? aImgExt	: '.png'

		this.imgH	= this.imgW


		var mapW	= this.map.frame.rect.width - this.imgW
		var mapW2	= Math.floor((this.map.frame.rect.width / 2) - (this.imgW / 2))

		var mapH	= this.map.frame.rect.height - this.imgH
		var mapH2	= Math.floor((this.map.frame.rect.height / 2) - (this.imgH / 2))

		this.buttons	= new Array()

		this.toString	= TMapCursors_toString
		this.addButton	= TMapCursors_addButton
	 
	 	this.addButton(mapW2,	0,	delta,	0,	'cursor_n')
		this.addButton(mapW2,	mapH,	-delta,	0,	'cursor_s')
		this.addButton(0,	mapH2,	0,	-delta,	'cursor_w')
		this.addButton(mapW,	mapH2,	0,	delta,	'cursor_e')
		this.addButton(0,	0,	delta,	-delta,	'cursor_nw')
		this.addButton(mapW,	0,	delta,	delta,	'cursor_ne')
		this.addButton(mapW,	mapH,	-delta,	delta,	'cursor_se')
		this.addButton(0,	mapH,	-delta,	-delta,	'cursor_sw')
	}



	function TMapCursors_toString()
	{
		return this.buttons.join('\n')
	}



	function TMapCursors_addButton(x,y,dy,dx,src)
	{
		this.buttons[this.buttons.length] = 
			new TMapCursorButton(	this,
						this.map.frame.rect.min.x + x, 
						this.map.frame.rect.min.y + y, 
						dy, dx, src, this.imgW, this.imgW
						)
	}



	function TMapCursorButton(aOwner, aX, aY, dy, dx, src, imgW)
	{
		this.inherited	= TFrame
		this.inherited(aX, aY, imgW, imgW)

		this.owner	= aOwner
		this.dx		= dx
		this.dy		= dy
		this.src	= src

		this.contents = new TImage(this.owner.imgPath + this.src + this.owner.imgExt , imgW, imgW)
		this.contents.anchor	= new TAnchor('javascript:' + this.owner.map.varName + '.panMap(' + this.dy + ',' + this.dx + ')')

		this.beforeDraw = TMapCursorButton_beforeDraw
	}



	function TMapCursorButton_beforeDraw()
	{
		var m = this.owner.map
		var rz = this.owner.map.project.zoomLevels.get(this.owner.map.zoomLevel)

		if (	((this.dx > 0) && (m.llTile.x + m.tilesX - 1 >= rz.numTilesX)) ||
			((this.dx < 0) && (m.llTile.x <= 1)) ||
			((this.dy > 0) && (m.llTile.y + m.tilesY - 1 >= rz.numTilesY)) ||
			((this.dy < 0) && (m.llTile.y <= 1))
		   )
			return 0

		return 1
	}


