var maxSpan = 0;
function Dodger(name, left, top, width, height, background, knob, activeknob) {
	// arbitrary, only useful for OSC sends.
	var rangeX = 128;
	var rangeY = 128;

	// game parameter
	var time = 0;

	/* 
		Parasitic inheritance
		This dude is _it_.
	*/
	var self = new PictSlider(name, left, top, width, height, rangeX, rangeY, background, knob, activeknob);

	self.radius = self.knobimg.width / 2;
	// default value in center
	self.reset = function() {
		self.setValue([rangeX/2,rangeY/2]);
	}
	self.reset();
	/********************************************
		Handlers copied from parent and modified.
	*/
	self.moveCursor = function(x, y) {
		if (self.mouseIsDown) {
			if (self.isCollision(x,y)) {

				self.gameOver();

			} else {
				self.x = x;
				self.y = y;
				self.updateValue();
			}
		}
	}
	self.mouseDownHandler = function( event ) {
		time = (new Date()).getTime();
		self.knobimg.src = self.activeknob;
		self.moveCursor( event.docX, event.docY );
	}
	self.mouseUpHandler = function( event ) { 
		self.resetKnob();
		if (self.inRange(event)) {
			self.gameOver();
		} 
	}   
	self.resetKnob = function() {
		self.knobimg.src = self.knob;
	}

	/******************************************
		Bogies
		takes [left,right] and [width,height] as args
	*/
	var Bogie = function(coords,size) {
		this.left = coords[0];
		this.right = coords[0] + size[0];
		this.top = coords[1];
		this.bottom = coords[1] + size[1];

		//draw
		this.div = document.createElement("img");
		this.div.src = "a10.jpg";
		this.div.style.position = "absolute";
		this.div.style.width = this.right - this.left + "px";
		this.div.style.height = this.bottom - this.top + "px";
		this.div.style.left = this.left + "px";
		this.div.style.top = this.top + "px";
		//this.div.style.background = "blue";
		self.div.appendChild(this.div);

		this.contains = function(left, top, right, bottom) {
			var xdiff,ydiff, totalHeight,totalWidth;
			if (left < this.left) {
				xdiff = Math.abs(left - this.right);
			} else {
				xdiff = Math.abs(right - this.left);
			}
			if (top < this.top) {
				ydiff = Math.abs(top - this.bottom);
			} else {
				ydiff = Math.abs(bottom - this.top);
			}
			totalWidth = right - left + this.right - this.left;
			totalHeight = bottom - top + this.bottom - this.top;

			return ydiff < totalHeight && xdiff < totalWidth;
		}
	}
	var scaleCoords = function( xval, yval ) {
		return [ xval * self.width + self.left, yval * self.height + self.top ];
	}
	var scaleSize = function( width, height ) {
		return [ width * self.width, height * self.height ];
	}
	var bogies = [];
	for (var i = 0; i < 20; i++) {
		bogies.push(
			new Bogie(
				scaleCoords(Math.random() * 0.9, Math.random() * 0.9), 
				scaleSize(Math.random() * 0.1, Math.random() * 0.1)
			)
		);
	}
	
	/******************************************
		Game management functions
	*/
	self.gameOver = function() {
		// return cursor to center
		self.reset();
		self.redraw();
		self.resetKnob();

		// simulate mouse released
		self.mouseIsDown = false;

		// print message
		var span = ((new Date()).getTime() - time) / 1000;
		var message = "Game Over!\n  You lasted " + formatSpan(span);
		if (span > maxSpan) {
			maxSpan = span;
			message += "\nCongratulations!  That's a new high score!";
		} else {
			message += "\n\nHigh score: " + formatSpan(maxSpan);
		}
		alert(message);
	}
	var formatSpan = function(span) {
		var message = "";
		if (span > 60) {
			message += Math.floor(span / 60) + " minutes and " + (Math.floor((span % 60) * 100) / 100);
		} else {
			message += span;
		}
		message += " seconds.  ";
		return message;
	}
	self.isCollision = function( x, y ) {
		// walls
		if ( x + self.knobimg.width / 2 >= self.maxx  - 2|| 
			x - self.knobimg.width / 2 <= self.minx + 2 ||
			y + self.knobimg.height / 2 >= self.maxy - 2 ||
			y - self.knobimg.height / 2 <= self.miny + 2 ) {
			return true;
		} else {
			var left = x - self.knobimg.width/2;
			var right = x + self.knobimg.width/2;
			var top = y - self.knobimg.height/2;
			var bottom = y + self.knobimg.height/2;
			for (var i = 0; i < bogies.length; i++) {
				if (bogies[i].contains(left, top, right, bottom)) {
					return true;
				}
			}
		}
		return false;
	}
	return self;
}            
