 		sourceSelector = command.sourceselector
 		targetSelector = command.targetselector
 
-		fn = ->
-			srcOffset = $('SELECTOR1').offset()
-		srcOffset = page.evaluate fn.toString().replace( 'SELECTOR1', sourceSelector )
-
-		fn = ->
-			dstOffset = $('SELECTOR2').offset()
-		dstOffset = page.evaluate fn.toString().replace( 'SELECTOR2', targetSelector )
-
-		page.sendEvent 'mousedown', srcOffset.left, srcOffset.top
-		page.sendEvent 'mousemove', dstOffset.left, dstOffset.top
-		page.sendEvent 'mouseup', dstOffset.left, dstOffset.top
+		target = @ResolveSelectorXY sourceSelector
+		page.sendEvent 'mousedown', target.x, target.y
+
+		target = @ResolveSelectorXY targetSelector
+		page.sendEvent 'mousemove', target.x, target.y
+		page.sendEvent 'mouseup', target.x, target.y
 
 		@CompleteAction()
 

	FindMultiple: (command) ->
		@Find(command)

	Click: (command) ->
		selector = command.selector if command.selector isnt ""
		x = if command.x? then parseInt command.x else 0
		y = if command.y? then parseInt command.y else 0

		offset = { top:0, left:0 }
		if selector?
			fn = ->
				offset = $('SELECTOR').offset()
			offset = page.evaluate fn.toString().replace( 'SELECTOR', selector )

		page.sendEvent 'mousemove', offset.left+x, offset.top+y
		page.sendEvent 'click', offset.left+x, offset.top+y

		@CompleteAction()

	DoubleClick: (command) ->
		selector = command.selector if command.selector isnt ""
		x = if command.x? then parseInt command.x else 0
		y = if command.y? then parseInt command.y else 0

		offset = { top:0, left:0 }
		if selector?
			fn = ->
				offset = $('SELECTOR').offset()
			offset = page.evaluate fn.toString().replace( 'SELECTOR', selector )

		page.sendEvent 'mousemove', offset.left+x, offset.top+y
		page.sendEvent 'click', offset.left+x, offset.top+y
		page.sendEvent 'click', offset.left+x, offset.top+y

		@CompleteAction()
		
	RightClick: (command) ->
		selector = command.selector if command.selector isnt ""

		offset = { top:0, left:0 }
		if selector?
			fn = ->
				$('SELECTOR').contextmenu()
			page.evaluate fn.toString().replace( 'SELECTOR', selector )

		@CompleteAction()

	Hover: (command) ->
		selector = command.selector if command.selector isnt ""
		x = if command.x? then parseInt command.x else 0
		y = if command.y? then parseInt command.y else 0

		offset = { top:0, left:0 }
		if selector?
			fn = ->
				offset = $('SELECTOR').offset()
			offset = page.evaluate fn.toString().replace( 'SELECTOR', selector )

		page.sendEvent 'mousemove', offset.left+x, offset.top+y

		@CompleteAction()


	Focus: (command) ->
		@Click command

﻿system = require 'system'
page = require('webpage').create()
viewportWidth = 1280
viewportHeight = 1024
page.viewportSize =
	width: viewportWidth
	height: viewportHeight

class PhantomDriver
	constructor: () ->
