var	child = require('./child.js');

var defaults = {
	'viewportSize': {
		'width': 2880,
		'height': 1440
	},
	'paperSize': {
		'format': 'A4',
		'orientation': 'portrait',
		'margin': {
			'top': '1cm',
			'right': '1cm',
			'bottom': '1cm',
			'left': '1cm'
		}
	},
	'zoomFactor': 1,
	'args': '',
	'captureDelay': 400
};

//code from https://github.com/rxaviers/cldr
var merge = function() {
	var destination = {},
	sources = [].slice.call( arguments, 0 );
	sources.forEach(function( source ) {
		var prop;
		for ( prop in source ) {
			if ( prop in destination && Array.isArray( destination[ prop ] ) ) {
				// Concat Arrays
				destination[ prop ] = destination[ prop ].concat( source[ prop ] );
			} else if ( prop in destination && typeof destination[ prop ] === "object" ) {
				// Merge Objects
				destination[ prop ] = merge( destination[ prop ], source[ prop ] );
			} else {
				// Set new values
				destination[ prop ] = source[ prop ];
			}
		}
	});
	return destination;
};

module.exports = function(url, fileName, opts){
	var ps,
		readStream,
		self = this;

	this.url = url;
	this.fileName = fileName;
	this.filePath = process.env.PWD || process.cwd() || __dirname;

	this.options = merge(defaults, opts);
	this.evts = {};
	this.on = function(evt, callback) {
		self.evts[evt] = callback;
	};

	child.supports(function(support){
		if (!support)
			self.evts['error'].call(self, 'PhanomJS not installed');
	});

	ps = child.exec(this.url, this.fileName, this.options);

	readStream = function(stream) {
		if (stream.toString('utf-8').length === 2) {
			self.evts['done'].call(this, self.filePath+'/'+self.fileName);
			ps.kill();
		} else {
			self.evts['error'].call(this, 'There was a problem');
		};
	};

	ps.stdout.on('data', function(std){
		readStream(std);
	});

	ps.stderr.on('data', function(std){
		self.evts['error'].call(self, std);
	});

	return this;
};

@@ -1569,68 +1569,210 @@
 %0A%09%09%09
-self.evts%5B'done'%5D.call(this, self.filePath+'/'+self.fileName
+var targetFilePath = self.fileName;%0A%09%09%09if (targetFilePath%5B0%5D != '/') %7B%0A%09%09%09%09targetFilePath = self.filePath + '/' + targetFilePath;%0A%09%09%09%7D%0A                        self.evts%5B'done'%5D.call(this, targetFilePath
 );%0A%09

Allow fully qualified target file paths.