//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014
var gInteraction = new Interaction();

function Interaction()
{
	this.mouseX = 0;
	this.mouseY = 0;
	this.mouseOver = false;
	this.mouseDown = false;
	
	this.mouseDownCallback;
	this.mouseMoveCallback;
	this.mouseOutCallback;

	this.Init = function(mouseDownCallback, mouseMoveCallback, mouseUpCallback, mouseOutCallback)
	{
		gConsole.Log(1, "[INT] Init");
		
		gDraw.input.addEventListener("touchmove", this.OnTouchMove, false);
		gDraw.input.addEventListener("touchstart", this.OnTouchDown, false);
		gDraw.input.addEventListener("touchend", this.OnTouchUp, false);
		gDraw.input.addEventListener("mousemove", this.OnMouseMove, false);
		gDraw.input.addEventListener("mousedown", this.OnMouseDown, false);
		gDraw.input.addEventListener("mouseup", this.OnMouseUp, false);
		gDraw.input.addEventListener("mouseover", this.OnMouseOver, false);
		gDraw.input.addEventListener("mouseout", this.OnMouseOut, false);
		
		this.mouseDownCallback = mouseDownCallback;
		this.mouseMoveCallback = mouseMoveCallback;
		this.mouseUpCallback = mouseUpCallback;
		this.mouseOutCallback = mouseOutCallback
	}
	
	this.OnTouchMove = function(e)
	{
		e.preventDefault();
		if(e.touches.length == 1)
		{
			var touch = e.touches[0];
			var node = touch.target;
			gInteraction.mouseX = gInteraction.GetEventX(touch);
			gInteraction.mouseY = gInteraction.GetEventY(touch);
			gInteraction.mouseMoveCallback();
			SetInputType(EInputType.Touch);
		}
	}
	
	this.OnTouchDown = function(e)
	{
		gInteraction.OnTouchMove(e);
		gInteraction.OnMouseDown(e);
	}
	
	this.OnTouchUp = function(e)
	{
		gInteraction.OnTouchMove(e);
		gInteraction.OnMouseUp(e);
	}
	
	this.OnMouseMove = function(e)
	{
		gInteraction.mouseX = gInteraction.GetEventX(e);
		gInteraction.mouseY = gInteraction.GetEventY(e);
		gInteraction.mouseMoveCallback();
		SetInputType(EInputType.Mouse);
	}

	this.OnMouseDown = function(e)
	{
		gInteraction.mouseDown = true;
		gInteraction.mouseDownCallback();
		SetInputType(EInputType.Mouse);
	}
	
	this.OnMouseUp = function(e)
	{
		gInteraction.mouseDown = false;
		gInteraction.mouseUpCallback();
		SetInputType(EInputType.Mouse);
	}
	
	this.OnMouseOver = function(e)
	{
		gInteraction.mouseOver = true;
	}
	
	this.OnMouseOut = function(e)
	{
		gInteraction.mouseDown = false;
		gInteraction.mouseOver = false;
		gInteraction.mouseOutCallback();
	}
	
	this.Debug = function()
	{
		if(gPause.IsPaused())
		{
			gDraw.Watch("Paused!");
		}
		gDraw.Watch("Interaction " + gInputType);
		gDraw.Watch("Mouse (" + this.mouseX + ", " + this.mouseY +") " + this.mouseDown + " - " + this.mouseOver);
	}

	this.GetEventX = function(e)
	{
		var x = 0;

		if (e.layerX)
		{ 
			x = e.layerX / gDraw.zoom;
		}
		else
		{ 
			x = (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft) / gDraw.zoom; 
		}
		
		x -= gDraw.input.offsetLeft;
		
		return x;
	}

	this.GetEventY = function(e)
	{
		var y = 0;

		if (e.layerY)
		{ 
			y = e.layerY / gDraw.zoom;
		}
		else
		{ 
			y = (e.clientY + document.body.scrollTop + document.documentElement.scrollTop) / gDraw.zoom; 
		}
	
		y -= gDraw.input.offsetTop;
	
		return y;
	}
}