/**
Copyright (c) 2010 Dennis Hotson

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

(function() {

jQuery.fn.springy = function(graph) {
	var canvas = this[0];
	var ctx = canvas.getContext("2d");
	var layout = new Layout.ForceDirected(graph, 400.0, 400.0, 0.5);
	layout.paddingScale = graph.paddingScale;

	// calculate bounding box of graph layout.. with ease-in
	var currentBB = layout.getBoundingBox();
	var targetBB = {bottomleft: new Vector(-2, -2), topright: new Vector(2, 2)};

	// auto adjusting bounding box
	setInterval(function(){
		targetBB = layout.getBoundingBox();
		// current gets 20% closer to target every iteration
		currentBB = {
			bottomleft: currentBB.bottomleft.add( targetBB.bottomleft.subtract(currentBB.bottomleft)
				.divide(10)),
			topright: currentBB.topright.add( targetBB.topright.subtract(currentBB.topright)
				.divide(10))
		};
	}, 50);

	// convert to/from screen coordinates
	toScreen = function(p) {
		var size = currentBB.topright.subtract(currentBB.bottomleft);
		var sx = p.subtract(currentBB.bottomleft).divide(size.x).x * canvas.width;
		var sy = p.subtract(currentBB.bottomleft).divide(size.y).y * canvas.height;
		return new Vector(sx, sy);
	};

	fromScreen = function(s) {
		var size = currentBB.topright.subtract(currentBB.bottomleft);
		var px = (s.x / canvas.width) * size.x + currentBB.bottomleft.x;
		var py = (s.y / canvas.height) * size.y + currentBB.bottomleft.y;
		return new Vector(px, py);
	};
	

	// half-assed drag and drop
	var selected = null;
	var nearest = null;
	var dragged = null;
	
	var click = null;

	jQuery(canvas).mousedown(function(e){
		jQuery('.actions').hide();
		click = true;
		var pos = jQuery(this).offset();
		var p = fromScreen({x: e.pageX - pos.left, y: e.pageY - pos.top});
		selected = nearest = dragged = layout.nearest(p);

		if (selected.node !== null)
		{
			dragged.point.m = 10000.0;
		}

		renderer.start();
	});

	jQuery(canvas).mousemove(function(e){
		click = false;
		var pos = jQuery(this).offset();
		var p = fromScreen({x: e.pageX - pos.left, y: e.pageY - pos.top});
		nearest = layout.nearest(p);

		if (dragged !== null && dragged.node !== null)
		{
			dragged.point.p.x = p.x;
			dragged.point.p.y = p.y;
		}

		renderer.start();
	});

	jQuery(window).bind('mouseup',function(e){
		dragged = null;		
		if (nearest != null && click) {
			if (nearest.node.data.href != null) window.location.href= nearest.node.data.href;
		}
	});
	
	

	
	
	Node.prototype.radius = 0;
	Node.prototype.maxRadius = 35;
	
	Node.prototype.isNearest = function(){
			return (nearest !== null && nearest.node !== null && nearest.node.id === this.id);
	}
	
	Node.prototype.updateRadius = function() {
		if (this.isNearest()){
			if (this.radius < this.maxRadius){
				this.radius +=  .1 + Math.abs(this.maxRadius - this.radius) / 3;
				this.transitionAmount = 1;
			 } 
		} else {
			if (this.radius > 0){
				this.radius -= Math.max(
					.1 + Math.abs(this.maxRadius - this.radius) / 5,
					0);
				this.transitionAmount = 1;
				
			} else {
				this.radius = 0;
				this.transitionAmount = 0;
			}
		}
	}
	
	Node.prototype.isCircle=function(){
		return this.data.img != null && this.isNearest() || this.radius > 0;
	}
	
	Node.prototype.getFont = function(){
		if (this.data.font != null) return this.data.font;
		return "12px Verdana, serif";
	}
	
	Node.prototype.getBoxWidth = function(){
		ctx.save();
		var text = typeof(this.data.label) !== 'undefined' ? this.data.label : this.id;
		ctx.font = this.getFont();
		var width = ctx.measureText(text).width + 10;
		ctx.restore();
		return width;
	}
	Node.prototype.getWidth = function() {
		//var boxWidth = this.getBoxWidth();
		if (this.isCircle()) return this.radius * 2;
		else return this.getBoxWidth();
	};

	Node.prototype.getHeight = function() {
		if (this.isCircle())
			return Math.max(20, this.radius * 2);
		else return 20;
	};

	var renderer = new Renderer(1, layout,
		function clear()
		{
			ctx.clearRect(0,0,canvas.width,canvas.height);
            
		},
		function drawEdge(edge, p1, p2)
		{
			var x1 = toScreen(p1).x;
			var y1 = toScreen(p1).y;
			var x2 = toScreen(p2).x;
			var y2 = toScreen(p2).y;

			var direction = new Vector(x2-x1, y2-y1);
			var normal = direction.normal().normalise();

			var from = graph.getEdges(edge.source, edge.target);
			var to = graph.getEdges(edge.target, edge.source);

			var total = from.length + to.length;

			var n = 0;
			for (var i=0; i<from.length; i++)
			{
				if (from[i].id === edge.id)
				{
					n = i;
				}
			}

			var spacing = 6.0;

			// Figure out how far off centre the line should be drawn
			var offset = normal.multiply(-((total - 1) * spacing)/2.0 + (n * spacing));

			var s1 = toScreen(p1).add(offset);
			var s2 = toScreen(p2).add(offset);

			var boxWidth = edge.target.getWidth();
			var boxHeight = edge.target.getHeight();

			var intersection = intersect_line_box(s1, s2, {x: x2-boxWidth/2.0, y: y2-boxHeight/2.0}, boxWidth, boxHeight);

			if (!intersection) {
				intersection = s2;
			}


			var stroke = typeof(edge.data.color) !== 'undefined' ? edge.data.color : '#000000';

			var arrowWidth;
			var arrowLength;

			var weight = typeof(edge.data.weight) !== 'undefined' ? edge.data.weight : 1.0;

			ctx.lineWidth = Math.max(weight *  2, 0.1);
			arrowWidth = 1 + ctx.lineWidth;
			arrowLength = 8;

			var directional = typeof(edge.data.directional) !== 'undefined' ? edge.data.directional : true;

			// line
			var lineEnd;
			if (directional)
			{
				lineEnd = intersection.subtract(direction.normalise().multiply(arrowLength * 0.5));
			}
			else
			{
				lineEnd = s2;
			}
			ctx.save();
			ctx.shadowColor = 'rgba(0,0,0,0.3)';
  			 ctx.shadowBlur = 5;
  			 ctx.shadowOffsetX = 10;
  			 ctx.shadowOffsetY = 10;


			ctx.strokeStyle = stroke;
			ctx.beginPath();
			ctx.moveTo(s1.x, s1.y);
			ctx.lineTo(lineEnd.x, lineEnd.y);
			ctx.stroke();
			// arrow

			if (directional)
			{
				ctx.save();
				ctx.fillStyle = stroke;
				ctx.translate(intersection.x, intersection.y);
				ctx.rotate(Math.atan2(y2 - y1, x2 - x1));
				ctx.beginPath();
				ctx.moveTo(-arrowLength, arrowWidth);
				ctx.lineTo(0, 0);
				ctx.lineTo(-arrowLength, -arrowWidth);
				ctx.lineTo(-arrowLength * 0.8, -0);
				ctx.closePath();
				ctx.fill();
				ctx.restore();
			}
			ctx.restore();

		},
		function drawNode(node, p){
			if (node.isCircle()){
				drawNodeCircle(node, p);
			} else {
				drawNodeBox(node, p);
			}
		}
		
		
		
	);

	renderer.start();
	
	function drawNodeBox(node, p)
		{
			// reset the node radius
			node.radius = 0;
			var s = toScreen(p);

			ctx.save();

			var boxWidth = node.getWidth();
			var boxHeight = node.getHeight();

			// fill background
			//ctx.clearRect(s.x - boxWidth/2, s.y - 10, boxWidth, 20);

			// fill background
			if (selected !== null && nearest != null && nearest.node !== null && selected.node != null && selected.node.id === node.id)
			{
				ctx.fillStyle = "#FFFFE0";
			}
			else if (node.isNearest())
			{
				ctx.fillStyle = "rgba(200,255,200,1)";
			}
			else
			{
				ctx.fillStyle = 'rgba(255,255,255,0.75)';
			}

			ctx.fillRect(s.x - boxWidth/2, s.y - 10, boxWidth, 20);

			ctx.textAlign = "left";
			ctx.textBaseline = "top";
			ctx.fillStyle = "#000000";
			var font = node.data.font;
			if (font == null) font = "12px Verdana";
			ctx.font = font;
			var text = typeof(node.data.label) !== 'undefined' ? node.data.label : node.id;
			ctx.fillText(text, s.x - boxWidth/2 + 5, s.y - 8);
		
			ctx.restore();
			node.transitionAmount = 0;
		}
		function drawNodeCircle(node, p){
			var s = toScreen(p);
			ctx.save();
			ctx.shadowColor = 'rgba(0,0,0,0.3)';
  			 ctx.shadowBlur = 5;
  			 ctx.shadowOffsetX = 10;
  			 ctx.shadowOffsetY = 10;
			ctx.beginPath();
			if (node.isNearest()) ctx.strokeStyle = "#7CFF00";
			else ctx.strokeStyle = 'red';
			ctx.arc(s.x, s.y, Math.max(0, node.radius), 0, Math.PI * 2, false);
			ctx.fillStyle="white";
			ctx.fill();
			ctx.stroke();
			ctx.restore();
			if (node.data.img != null){
				ctx.save();
				ctx.clip();
				var img = node.data.img;
				if (img.complete && img.naturalWidth > 0)
				ctx.drawImage(img, s.x - (img.naturalWidth / 2), s.y - (img.naturalHeight / 2));
				ctx.restore();
			}

			node.updateRadius();
		}


	// helpers for figuring out where to draw arrows
	function intersect_line_line(p1, p2, p3, p4)
	{
		var denom = ((p4.y - p3.y)*(p2.x - p1.x) - (p4.x - p3.x)*(p2.y - p1.y));

		// lines are parallel
		if (denom === 0) {
			return false;
		}

		var ua = ((p4.x - p3.x)*(p1.y - p3.y) - (p4.y - p3.y)*(p1.x - p3.x)) / denom;
		var ub = ((p2.x - p1.x)*(p1.y - p3.y) - (p2.y - p1.y)*(p1.x - p3.x)) / denom;

		if (ua < 0 || ua > 1 || ub < 0 || ub > 1) {
			return false;
		}

		return new Vector(p1.x + ua * (p2.x - p1.x), p1.y + ua * (p2.y - p1.y));
	}

	function intersect_line_box(p1, p2, p3, w, h)
	{
		var tl = {x: p3.x, y: p3.y};
		var tr = {x: p3.x + w, y: p3.y};
		var bl = {x: p3.x, y: p3.y + h};
		var br = {x: p3.x + w, y: p3.y + h};

		var result;
		if (result = intersect_line_line(p1, p2, tl, tr)) { return result; } // top
		if (result = intersect_line_line(p1, p2, tr, br)) { return result; } // right
		if (result = intersect_line_line(p1, p2, br, bl)) { return result; } // bottom
		if (result = intersect_line_line(p1, p2, bl, tl)) { return result; } // left

		return false;
	}
}

})();

