window = 	{
			w = 0, h = 0, -- this is the current size of the app window
			ref_w = 0, ref_h = 0, -- this is the initial size of the app window, never changes
			} 
canvas = 	{
			w = 0, h = 0, -- w and h is the current viewable render size
			}

function window.setSize(w,h)
	window.w = w
	window.h = h
	video.setScreenSize(window.w, window.h)
end

function canvas.setSize(w,h)
	canvas.w = w
	canvas.h = h
	video.setRenderScreenSize( canvas.w , canvas.h)
end

function canvas.setToRef()
	if canvas.w ~= canvas.ref_w or canvas.h ~= canvas.ref_h then 
		canvas.setSize(canvas.ref_w, canvas.ref_h)
	end
end

function canvas.setToWindow()
	if canvas.w ~= window.w or canvas.h ~= window.h then 
		canvas.setSize(window.w, window.h)
	end
end

function window.init()
	window.ref_w, window.ref_h = video.getScreenSize()
	window.w, window.h  = window.ref_w, window.ref_h
end


function window.get_best_ui_offset(x,y,w,h, margin)
	local margin = margin or 25
	local ox,oy = 0	,0
	local ww = window.w
	local hh = window.h
	if x < w*0.5+ww*0.1 then
		ox = w*0.5+margin
	elseif x > window.w-w*0.5-ww*0.1 then
		ox = -w*0.5-margin
	end
	if y < h*0.5+hh*0.1 then
		oy = h*0.5+margin
	elseif (y > window.h-h*0.5-hh*0.1) or ox == 0 then
		oy = -h*0.5-margin
	end	
	return ox,oy
end