var DRAG_HORIZONTAL = 1;
var DRAG_VERTICAL = 2;
var DRAG_NONE = 4;
var DRAG_BOTH = DRAG_HORIZONTAL | DRAG_VERTICAL;

var previewDragger = 
{
	dragConstraints: DRAG_BOTH,
	
	startPosition:{x:0, y:0},
	framePosition:{x:0, y:0},
	
	startDrag: function(e)
	{
		this.startPosition = mouseCoordinates(e);
		this.framePosition = {x: $('tile_preview').offsetLeft, y: $('tile_preview').offsetTop};

		document.onmousemove = previewDragger.drag;
		document.onmouseup = previewDragger.endDrag;
	},
	drag: function(e)
	{
		var currentPosition = mouseCoordinates(e);

		if(previewDragger.dragConstraints & DRAG_VERTICAL)
		{
			var newTop = previewDragger.framePosition.y + (currentPosition.y - previewDragger.startPosition.y);
			
			newTop = 
				Math.min(0, 
					Math.max(
						- $('tile_preview').offsetHeight + $('tile_preview_container').offsetHeight,
						newTop
					)
				);
				
			$S('tile_preview').top = newTop + "px";
		}
		
		if(previewDragger.dragConstraints & DRAG_HORIZONTAL)
		{
			var newLeft = previewDragger.framePosition.x + (currentPosition.x - previewDragger.startPosition.x);
			newLeft = 
				Math.min(0, 
					Math.max(
						- $('tile_preview').offsetWidth + $('tile_preview_container').offsetWidth,
						newLeft
					)
				);
				
			$S('tile_preview').left = newLeft + "px";
		}
	},
	endDrag: function(e)
	{
		document.onmousemove = null;
		document.onmouseup = null;
	}
}