function isIphone() {
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		return true;
	}
	return false;
	 if (document.cookie.indexOf("iphone_redirect=false") == -1) window.location = "http://m.espn.go.com/wireless/?iphone&i=COMR";
}


$(function() {
	if (isIphone())
		return;
	
	var canvas = $("#c");
	var canvasHeight;
	var canvasWidth;
	
	var forcedHeight = 600;
	var forcedWidth = 1000;
	var posTopOffset = -180;
	var posLeftOffset = -65;
	
	var ctx;
	var dt = 0.1;

	var pointCollection;

	function init() {
		updateCanvasDimensions();

		var g = [ 
		        new Point(53, 7, 0.0, 8, "#358CB7"),
		        new Point(31, 14, 0.0, 9, "#358CB7"),
		        new Point(10, 49, 0.0, 9, "#358CB7"),
		        new Point(17, 71, 0.0, 9, "#358CB7"),
		        new Point(13, 26, 0.0, 6, "#358CB7"),
		        new Point(34, 31, 0.0, 6, "#358CB7"),
		        new Point(27, 47, 0.0, 6, "#358CB7"),
		        new Point(44, 18, 0.0, 3, "#358CB7"),
		        new Point(44, 63, 0.0, 3, "#358CB7"),

		        new Point(74, 18, 0.0, 8, "#ADE0F1"),
		        new Point(86, 35, 0.0, 8, "#ADE0F1"),
		        new Point(74, 51, 0.0, 8, "#ADE0F1"),
		        new Point(87, 66, 0.0, 8, "#ADE0F1"),
		        new Point(72, 82, 0.0, 9, "#ADE0F1"),
		        new Point(72, 35, 0.0, 4, "#ADE0F1"),
		        new Point(90, 49, 0.0, 6, "#ADE0F1"),
		        new Point(72, 66, 0.0, 5, "#ADE0F1"),
		        new Point(59, 46, 0.0, 3, "#ADE0F1"),
		        new Point(60, 76, 0.0, 2, "#ADE0F1"),

		        new Point(58, 29, 0.0, 9, "#65B7DF"),
		        new Point(43, 48, 0.0, 9, "#65B7DF"),
		        new Point(58, 61, 0.0, 7, "#65B7DF"),
		        new Point(33, 65, 0.0, 6, "#65B7DF"),
		        new Point(35, 88, 0.0, 9, "#65B7DF"),
		        new Point(49, 77, 0.0, 6, "#65B7DF"),
		        new Point(55, 92, 0.0, 6, "#65B7DF"),
		        new Point(45, 33, 0.0, 3, "#65B7DF")
				];

		gLength = g.length;
		for ( var i = 0; i < gLength; i++) {
			g[i].curPos.x = (canvasWidth / 2 + posLeftOffset) + g[i].curPos.x;
			g[i].curPos.y = (canvasHeight / 2 + posTopOffset) + g[i].curPos.y;

			g[i].originalPos.x = (canvasWidth / 2 + posLeftOffset) + g[i].originalPos.x;
			g[i].originalPos.y = (canvasHeight / 2 + posTopOffset) + g[i].originalPos.y;
		}
		;

		pointCollection = new PointCollection();
		pointCollection.points = g;

		initEventListeners();
		timeout();
	}
	;

	function initEventListeners() {
		$(window).bind('resize', updateCanvasDimensions).bind('mousemove',
				onMove);

		canvas.get(0).ontouchmove = function(e) {
			e.preventDefault();
			onTouchMove(e);
		};

		canvas.get(0).ontouchstart = function(e) {
			e.preventDefault();
		};
	}
	;

	function updateCanvasDimensions() {
		var setheight = forcedHeight; //$(window).height()
		var setwidth = forcedWidth; //$(window).width()
		canvas.attr({
			height : setheight,
			width : setwidth
		});
		canvasWidth = canvas.width();
		canvasHeight = canvas.height();

		draw();
	}
	;

	function onMove(e) {
		if (pointCollection) {
			var xOffSet = $("#container").position().left - 680;
			var newXPos = e.pageX - xOffSet + 190;
			//$("#debug").html(e.pageX + " - " + xOffSet + " - " + newXPos);
			pointCollection.mousePos.set(newXPos, e.pageY + 100);
		}
	}
	;

	function onTouchMove(e) {
		if (pointCollection)
			pointCollection.mousePos.set(e.targetTouches[0].pageX,
					e.targetTouches[0].pageY);
	}
	;

	function timeout() {
		draw();
		update();

		setTimeout(function() {
			timeout()
		}, 30);
	}
	;

	function draw() {
		var tmpCanvas = canvas.get(0);

		if (tmpCanvas.getContext == null) {
			return;
		}
		;

		ctx = tmpCanvas.getContext('2d');
		ctx.clearRect(0, 0, canvasWidth, canvasHeight);

		if (pointCollection)
			pointCollection.draw();
	}
	;

	function update() {
		if (pointCollection)
			pointCollection.update();
	}
	;

	function Vector(x, y, z) {
		this.x = x;
		this.y = y;
		this.z = z;

		this.addX = function(x) {
			this.x += x;
		};

		this.addY = function(y) {
			this.y += y;
		};

		this.addZ = function(z) {
			this.z += z;
		};

		this.set = function(x, y, z) {
			this.x = x;
			this.y = y;
			this.z = z;
		};
	}
	;

	function PointCollection() {
		this.mousePos = new Vector(0, 0);
		this.points = new Array();

		this.newPoint = function(x, y, z) {
			var point = new Point(x, y, z);
			this.points.push(point);
			return point;
		};

		this.update = function() {
			var pointsLength = this.points.length;

			for ( var i = 0; i < pointsLength; i++) {
				var point = this.points[i];

				if (point == null)
					continue;

				var dx = this.mousePos.x - point.curPos.x;// + 191; // had to add this to counter the position seems relative to window
				var dy = this.mousePos.y - point.curPos.y;
				var dd = (dx * dx) + (dy * dy);
				var d = Math.sqrt(dd);

				if (d < 150) {
					point.targetPos.x = (this.mousePos.x < point.curPos.x) ? point.curPos.x
							- dx
							: point.curPos.x - dx;
					point.targetPos.y = (this.mousePos.y < point.curPos.y) ? point.curPos.y
							- dy
							: point.curPos.y - dy;
				} else {
					point.targetPos.x = point.originalPos.x;
					point.targetPos.y = point.originalPos.y;
				}
				;

				point.update();
			}
			;
		};

		this.draw = function() {
			var pointsLength = this.points.length;
			for ( var i = 0; i < pointsLength; i++) {
				var point = this.points[i];

				if (point == null)
					continue;

				point.draw();
			}
			;
		};
	}
	;

	function Point(x, y, z, size, colour) {
		this.colour = colour;
		this.curPos = new Vector(x, y, z);
		this.friction = 0.8;
		this.originalPos = new Vector(x, y, z);
		this.radius = size;
		this.size = size;
		this.springStrength = 0.1;
		this.targetPos = new Vector(x, y, z);
		this.velocity = new Vector(0.0, 0.0, 0.0);

		this.update = function() {
			var dx = this.targetPos.x - this.curPos.x;
			var ax = dx * this.springStrength;
			this.velocity.x += ax;
			this.velocity.x *= this.friction;
			this.curPos.x += this.velocity.x;

			var dy = this.targetPos.y - this.curPos.y;
			var ay = dy * this.springStrength;
			this.velocity.y += ay;
			this.velocity.y *= this.friction;
			this.curPos.y += this.velocity.y;

			var dox = this.originalPos.x - this.curPos.x;
			var doy = this.originalPos.y - this.curPos.y;
			var dd = (dox * dox) + (doy * doy);
			var d = Math.sqrt(dd);

			this.targetPos.z = d / 100 + 1;
			var dz = this.targetPos.z - this.curPos.z;
			var az = dz * this.springStrength;
			this.velocity.z += az;
			this.velocity.z *= this.friction;
			this.curPos.z += this.velocity.z;

			this.radius = this.size * this.curPos.z;
			if (this.radius < 1)
				this.radius = 1;
		};

		this.draw = function() {
			ctx.fillStyle = this.colour;
			ctx.beginPath();
			ctx.arc(this.curPos.x, this.curPos.y, this.radius, 0, Math.PI * 2,
					true);
			ctx.fill();
		};
	}
	;

	init();
});
