function DragControl(divName, board, assignedX, assignedY)
{
    this.init(divName, board, assignedX, assignedY);
}

DragControl.onKeyPress = function(event)
{
    var e = event;
    if (e == null)
    {
        e = window.event;
    }
    if (dd.obj != null)
    {
        if (dd.obj.name.substring(0,7) == 'control')
        {
            var key = (e.which) ? e.which : e.keyCode;
            if (KeyboardConstants.isRotateKey(key))
            {
                DragControl.rotateTile(dd.obj.name);
            }
        }
    }
}

DragControl.rotateTile = function(divId)
{
    var dragControl = DragControl.dragControls[divId];

    if (dragControl != null)
    {
        dragControl.rotate();
    }
}


DragControl.activate = function()
{
	DragControl.active = true;
}

DragControl.deactivate = function()
{
	DragControl.dragControlInPlay = null;
	DragControl.active = false;
}

DragControl.beginDrag = function()
{
	if (!DragControl.active)
	{
		return;
	}

	var controlBeingDragged = DragControl.dragControls[dd.obj.id];
	var oldControl = DragControl.dragControlInPlay;

	// if start dragging something different than you did before
	if ((oldControl != null) &&
	    (oldControl.divName != controlBeingDragged.divName))
	{
		oldControl.setSelectedSpace(null);

		// then move the previous control back to its assigned spot
		oldControl.moveToAssignedLocation();
	}

	controlBeingDragged.board.deemphasize();
	DragControl.dragControlInPlay = controlBeingDragged;
	controlBeingDragged.setSelectedSpace(null);

    // and then move the center of the pivot point to be at the mouse location
    var offsetx = Math.round(dd.obj.w/2);
    var offsety = Math.round(dd.obj.h/2);

    dd.obj.moveTo(dd.e.x - offsetx, dd.e.y - offsety);
}

DragControl.drag = function()
{
	if (!DragControl.active)
	{
		return;
	}

    // and then move the center of the pivot point to be at the mouse location
    var offsetx = Math.round(dd.obj.w/2);
    var offsety = Math.round(dd.obj.h/2);

    dd.obj.moveTo(dd.e.x - offsetx, dd.e.y - offsety);
}


DragControl.endDrag = function()
{
    var dragControl = DragControl.dragControls[dd.obj.id];
    if (dragControl.tile == null)
    {
		return;
	}

	if (!DragControl.active)
	{
		dragControl.moveToAssignedLocation();
		return;
	}



    // and then move the center of the pivot point to be at the mouse location
    var offsetx = Math.round(dd.obj.w/2);
    var offsety = Math.round(dd.obj.h/2);

    dd.obj.moveTo(dd.e.x - offsetx, dd.e.y - offsety);

    // translate the drop point into the 'closest' location on the board
    // in which to snap the tile into place.
    var space = game.getBoard().getSpaceByCoordinates(dd.e.x, dd.e.y);
    if (space != null)
    {
        var pos = getElementPosition('board');

		// snap to grid
        dd.obj.moveTo
        (
            Math.round(pos.left + space.left),
            pos.top + space.top - Symbol.HEIGHT
        );

        if ((dragControl != null) && (dragControl.board != null))
        {
            if (dragControl.board.verifyPlacement(dragControl.tile, space))
            {
				dragControl.board.emphasize(dragControl.tile, space);
				dragControl.setSelectedSpace(space);
            }
            else
            {
                // pop the tile over a bit to show that it doesn't snap into place
                dd.obj.moveTo(dd.obj.x + 15, dd.obj.y + 10);
                DragControl.showBadInitialMoveDialog();
            }
        }
        else
        {
			alert('board was null');
		}
    }
}



DragControl.dragControls = new Object();
DragControl.prototype.init = function(divName, board, assignedX, assignedY)
{
    this.divName = divName;
    this.board = board;
    this.assignedX = assignedX;
    this.assignedY = assignedY;
    this.grid = Utility.layoutGrid(divName, 2, 3, this);
    this.selectedSpace = null;
    this.tile = null;

    DragControl.dragControls[this.divName] = this;
}

DragControl.prototype.isOnSpace = function()
{
	return this.selectedSpace != null;
}

DragControl.prototype.setSelectedSpace = function(space)
{
	this.selectedSpace = space;
}

DragControl.prototype.getSelectedSpace = function()
{
	if (this.isOnSpace())
	{
		return this.selectedSpace;
	}
	return null;
}


DragControl.prototype.setTile = function(tile)
{
    this.tile = tile;

    document.getElementById(this.divName).appendChild(this.tile.getTileDiv());
    Utility.moveTo(this.tile.getTileDiv(), -(Math.round(Symbol.WIDTH*0.75)), 0);
	this.tile.setOrientation(0);
	this.renderOrientation();
    this.moveToAssignedLocation();
}

DragControl.prototype.clearTile = function()
{
    document.getElementById(this.divName).removeChild(this.tile.getTileDiv());
    this.tile = null;
	this.selectedSpace = null;
}

DragControl.prototype.getTile = function()
{
    return this.tile;
}

DragControl.prototype.rotate = function()
{
    this.tile.rotate(true);
    this.renderOrientation();
}

DragControl.prototype.moveToAssignedLocation = function()
{
	if ((dd.elements[this.divName] != null) && (this.tile != null))
	{
		this.tile.setOrientation(0);
		this.renderOrientation();

		dd.elements[this.divName].moveTo
		(
			this.assignedX,
			this.assignedY
		);
	}
}


DragControl.ORIENTATION_COORDS = [1,2,0,1,0,0,1,0,2,0,2,1];
DragControl.prototype.renderOrientation = function()
{
    var center = this.grid[1][1];
    Utility.moveTo(this.tile.getPivotDiv(), center.left, center.top);

    var orientation = this.tile.getOrientation();
    var col = DragControl.ORIENTATION_COORDS[orientation * 2];
    var row = DragControl.ORIENTATION_COORDS[(orientation * 2) + 1];

    var floater = this.grid[col][row];
    Utility.moveTo(this.tile.getFloatDiv(), floater.left, floater.top);

    this.tile.show();
}

DragControl.prototype.renderHex = function(controlDiv, space, col, row, colHeight)
{
	/*
    var div = document.createElement('div');
    div.className = 'positionable';

    // must handle shading of hexes in the outter rows
    var hexImage = 'smallPlain.gif';
    var html = '<img src="images/' + hexImage + '" width="' + Symbol.WIDTH + '" height="' + Symbol.HEIGHT + '" border="0" />';
    div.innerHTML = html;
    hideDiv(div);
    controlDiv.appendChild(div);

    div.style.left = space.left + 'px';
    div.style.top = space.top + 'px';
    showDiv(div)
    */
}


DragControl.setDHTMLCallbacks = function(id)
{
	dd.elements[id].setPickFunc(DragControl.beginDrag);
	dd.elements[id].setDragFunc(DragControl.drag);
	dd.elements[id].setDropFunc(DragControl.endDrag);
}

DragControl.showBadInitialMoveDialog = function()
{
	var dialog = new Dialog(350, 160, Language.translate('InvalidTilePlacementDialogHeader'));
	dd.elements[dialog.getId()].moveTo(100+game.anchor.left, 175+game.anchor.top);
	dialog.activate
	(
		'<b>' + Language.translate('InvalidTilePlacementDialogHeader') + '</b><p>' +
        Language.translate('InvalidFirstTileExplanation'),
		dialog.getCloseButtonHtml()
	);
}
