

document.onmousedown=dodown;

document.onmouseup=up;

var x = null;

var o = false;

var id = null;

var isMove = 0;

var move = 1;

var rpos = null;

function start() {

	eval(document.cookie);

	if (move != null && move != 1) {

		var el = document.getElementById("ship");

		if (rpos != null) {

			el.style.right = rpos + "px";

		}

		else {

			el.style.right = 50;

		}

		stopMove();

	}

	else {

		startMove();

	}

}	

function getWidth() {

	if (width = document.body.clientWidth) {

		return width;

	}

	else {

		return window.innerWidth;

	}

}

function startMove() {

	var el = document.getElementById("ship");

    var right = parseInt(el.style.right);

	if (right > getWidth()) {

		right = -250;

	}

    el.style.right = right + 2 + "px";

    id = setTimeout("startMove()", 50);

	isMove = 1;

}

function stopMove() {

	clearTimeout(id);

	isMove = 0;

	document.getElementById("switchLink").innerHTML = "start";

	saveMoveState(0);

	var el = document.getElementById("ship");

	saveRPos(el.style.right);

}

function switchMove() {

	if (isMove == 1) {

		stopMove();

	}

	else {

		startMove();

		document.getElementById("switchLink").innerHTML = "stop";

		saveMoveState(1);

	}

}

function onov(v) {

	o = v == 1 ? true : false;

	if (o == true) {

		clearTimeout(id);

	}

	else {

		if (isMove == 1) {

			startMove();

		}

	}

}

function dodown(e) {

	var el = document.getElementById('ship')

	if (window.event) {

		x = window.event.clientX + parseInt(el.style.right);

	}

	else {

		x = e.clientX + parseInt(el.style.right);

	}

	if (o) {

		document.onmousemove = domove;

	}

}

function domove(e){

	var el = document.getElementById('ship');

	el.style.cursor = "move";

	if (window.event) {

		el.style.right = x - window.event.clientX + "px";

	}

	else {

		el.style.right = x - e.clientX + "px";

	}

}

function up(){

	document.onmousemove=null;

	var el = document.getElementById('ship');

	//el.style.cursor = "default";

	if (!isMove) {

		saveRPos(el.style.right);

	}

}

function saveMoveState(s) {

	document.cookie = "move=" + s + " ";

}

function saveRPos(pos) {

	document.cookie = "rpos=" + parseInt(pos) + " ";

}

function resetAllState() {

	saveMoveState(null);

	saveRPos(null);

}