//      _                                  _     
//   __| |_ __ __ _  __ _  __ _  ___ _ __ (_)___ 
//  / _` | '__/ _` |/ _` |/ _` |/ _ \ '__|| / __|
// | (_| | | | (_| | (_| | (_| |  __/ | _ | \__ \
//  \__,_|_|  \__,_|\__, |\__, |\___|_|(_)/ |___/
//                  |___/ |___/         |__/     
//
//
function Dragger(pContext, pX, pY)
{
	this.ctx  = pContext;
	
	//Current Position
	this.posX = pX;
	this.posY = pY;
	
	//Dragger State
	//  0 - hidden
	//  1 - standard
	//  2 - hover
	//  3 - drag
	this.stateOld     = 0;
	this.state        = 0;
	
	this.changed = false;
}

Dragger.prototype.getPosX = function()
{
	return this.posX;
}

Dragger.prototype.getPosY = function()
{
	return this.posY;
}	

Dragger.prototype.hover = function(pPos)
{
	if (Math.sqrt(Math.pow(pPos.x - this.posX, 2)
	             +Math.pow(pPos.y - this.posY, 2)) < 8)
		this.changeState(2);
	else
		this.changeState(1);
}

Dragger.prototype.click = function(pPos)
{
	if (Math.sqrt(Math.pow(pPos.x - this.posX, 2)
	             +Math.pow(pPos.y - this.posY, 2)) < 8)
		this.changeState(3);
}

Dragger.prototype.setPosition = function(pPos)
{
	if (this.posX != pPos.x || this.posY != pPos.y)
		this.changed = true;
	this.posX = pPos.x;
	this.posY = pPos.y;
}

Dragger.prototype.changeState = function (pState)
{
	this.state = pState;
	if (this.state != this.stateOld)
		this.changed = true;
	this.stateOld = pState;
}

Dragger.prototype.hasChanged = function()
{
	var out = this.changed;
	this.changed = false;
	return out;
}

Dragger.prototype.draw = function()
{
	if (this.state > 0)
	{
		this.ctx.beginPath();
		this.ctx.fillStyle = "rgb(255,255,255)"; 
		
		switch (this.state)
		{
			case 1: //Standard
				this.ctx.strokeStyle = "rgb(150,150,150)";  
				this.ctx.arc(this.posX,this.posY,5,0,Math.PI*2,true);
			break;
			case 2: //Hover
				this.ctx.strokeStyle = "rgb(0,0,0)";  
				this.ctx.arc(this.posX,this.posY,8,0,Math.PI*2,true);
			break;
			case 3: //Drag
				this.ctx.strokeStyle = "rgb(255,0,0)";  
				this.ctx.arc(this.posX,this.posY,5,0,Math.PI*2,true);
			break;
		}
		
		this.ctx.fill();
		this.ctx.stroke();
	}
}
