
--===================================================
--									
--								GHI Position
--								ghi_position.lua
--
--			Information about position of the user
--	
-- 						(c)2011 The Gryphonheart Team
--								All rights reservated
--===================================================

local class
function GHI_Position()
	if class then
		return class
	end
	class = GHClass("GHI_Position")
	
	--local variables
	local continent
	local currentContinent
	local x
	local y
	local zoneIndex

	--Class functions
	class.GetCoor = function (unit)
		if not(unit) then unit="player" end
		
		zoneIndex = GetCurrentMapZone()
		continent = GetCurrentMapContinent()
		SetMapToCurrentZone()
		currentContinent = GetCurrentMapContinent()
		if currentContinent < 0 or currentContinent > 4 then
				SetMapByID(zoneIndex)
				return 0.0, 0.0, 0
		end     
		
		if currentContinent == 3 then
			SetMapZoom(3)
		else
			SetMapZoom(0)
		end
		
		local x,y = GetPlayerMapPosition(unit)
		
		if currentContinent == 3 then
			x=x*2228.61382 -- scale for Outland
			y=y*1485.74255
			SetMapByID(zoneIndex)
			return x,y,2
		else
			x=x*11698.9534 -- scale for Azeroth
			y=y*7799.30229
			SetMapByID(zoneIndex)
			return x,y,1
		end
	end
	
	class.GetPlayerPos = function()
		local x,y,continent = class.GetCoor();
		return {
			x=x,
			y=y,
			continent=continent
		};
	end
	
	class.IsPosWithinRange = function(position,range)
		GHCheck("GHI_Position.IsPosWithinRange",{"Table","Number"},{position,range})
		local playerPos = class.GetPlayerPos();
		
		if not(playerPos.continent == position.continent) then
			return false;
		end
		
		local xDiff = position.x-playerPos.x;
		local yDiff = position.y-playerPos.y;
		return math.abs(math.sqrt(xDiff*xDiff + yDiff*yDiff)) <= range
	end
	
	return class
end
