//NEED TO LOOKE LIKE A NODE APP
var engine;

function initEngine() {
	var Engine = require('./js/engine.js');
	engine = new Engine($);

	$(window).mousemove(function(e) {
		if(engine.dragok){
			if(e.pageX > engine.dragok.x + 6 || e.pageX < engine.dragok.x - 6 || e.pageY > engine.dragok.y + 6 || e.pageY < engine.dragok.y - 6){
				engine.map.setPosition(e.pageX - (engine.dragok.x - engine.dragok.cx), e.pageY - (engine.dragok.y - engine.dragok.cy));
			}
		}
		engine.mouse = {x: e.pageX, y: e.pageY};
	});
	
	$(window).mouseup(function(e) {
		if(engine.dragok){
			engine.dragok = false;
		}
		console.log(engine.mouse);
	});

	$('select').on('keydown', function(e){
		if(e.keyCode === 38 || e.keyCode === 40) { //up or down
			e.preventDefault();
			return false;
		}
	});

	keypress.combo("ctrl up", function() {
		engine.moveAnchor(0,0.1);
	});
	keypress.combo("ctrl left", function() {
		engine.moveAnchor(0.1,0);
	});
	keypress.combo("ctrl down", function() {
		engine.moveAnchor(0,-0.1);
	});
	keypress.combo("ctrl right", function() {
		engine.moveAnchor(-0.1,0);
	});
	keypress.combo("up", function() {
		engine.moveAnchor(0,0.01);
	});
	keypress.combo("left", function() {
		engine.moveAnchor(0.01,0);
	});
	keypress.combo("down", function() {
		engine.moveAnchor(0,-0.01);
	});
	keypress.combo("right", function() {
		engine.moveAnchor(-0.01,0);
	});

	keypress.combo("ctrl s", function() {
		engine.saveFile();
	});

	keypress.combo("ctrl m", function() {
		engine.addAnimation();
	});
};

initEngine();

/// ===== Utils ===== ///
// Find where this is used...
Function.prototype.bind = function (bind) {
	var self = this;
	return function () {
		var args = Array.prototype.slice.call(arguments);
		return self.apply(bind || null, args);
	};
};

var isInt = function(n) {
	return (n % 1) === 0;
};

window.requestAnimFrame = (function(){
	return  window.requestAnimationFrame       || 
			window.webkitRequestAnimationFrame || 
			window.mozRequestAnimationFrame    || 
			window.oRequestAnimationFrame      || 
			window.msRequestAnimationFrame     || 
			function(/* function */ callback, /* DOMElement */ element){
				window.setTimeout(callback, 1000 / 60);
			};
})();