/*================================================================
  Hex Plugin
  version: 0.1
  for: jquery 1.3.2
  Justin Walduck, November 2009
================================================================*/

(function($) {
		  
	$.hex = function() {}
	
	/*-----------------------------------------------
		simplify - takes a hexCoord and simplifies 
		it to its smallest equivalent
	-----------------------------------------------*/
	$.hex.simplify = function(hexCoord) {
		var fix = 0;
		var tmpHexCoord = hexCoord;
		
		if (tmpHexCoord[0] > 0 && tmpHexCoord[2] > 0) {
			fix = Math.min(tmpHexCoord[0], tmpHexCoord[2]);
			tmpHexCoord[0] -= fix; 
			tmpHexCoord[1] += fix;
			tmpHexCoord[2] -= fix;
		} else if (tmpHexCoord[1] > 0 && tmpHexCoord[0] < 0) {
			fix = Math.min(tmpHexCoord[1], Math.abs(tmpHexCoord[0]));
			tmpHexCoord[1] -= fix; 
			tmpHexCoord[2] += fix;
			tmpHexCoord[0] += fix;
		} else if (tmpHexCoord[2] > 0 && tmpHexCoord[1] < 0) {
			fix = Math.min(tmpHexCoord[2], Math.abs(tmpHexCoord[1]));
			tmpHexCoord[2] -= fix; 
			tmpHexCoord[0] -= fix;
			tmpHexCoord[1] += fix;
		} else if (tmpHexCoord[0] < 0 && tmpHexCoord[2] < 0) {
			fix = Math.min(Math.abs(tmpHexCoord[0]), Math.abs(tmpHexCoord[2]));
			tmpHexCoord[0] += fix;
			tmpHexCoord[1] += fix;
			tmpHexCoord[2] += fix;
		} else if (tmpHexCoord[1] < 0 && tmpHexCoord[0] > 0) {
			fix = Math.min(Math.abs(tmpHexCoord[0]), Math.abs(tmpHexCoord[2]));
			tmpHexCoord[1] += fix;
			tmpHexCoord[2] -= fix;
			tmpHexCoord[0] -= fix;
		} else if (tmpHexCoord[1] > 0 && tmpHexCoord[2] < 0) {
			fix = Math.min(Math.abs(tmpHexCoord[1]), Math.abs(tmpHexCoord[2]));
			tmpHexCoord[0] += fix;
			tmpHexCoord[1] -= fix;
			tmpHexCoord[2] += fix;
		}
		
		return tmpHexCoord;
	}
	
	
	/*-----------------------------------------------
		difference - takes 2 hexCoords and 
		returns a hexCoord for the path between them.
	-----------------------------------------------*/
	$.hex.difference = function(hexCoord1, hexCoord2) {
		var hexDiffence = [0,0,0];
		for (var i = 0; i < 3; i++) {
			hexDiffence[i] = parseInt(hexCoord2[i]) - parseInt(hexCoord1[i]);
		}
		return $.hex.simplify(hexDiffence);
	}
	
	
	/*-----------------------------------------------
		sum - takes 2 hexCoords and 
		returns a hexCoord for their sum.
	-----------------------------------------------*/
	$.hex.sum = function(hexCoord1, hexCoord2) {
		var hexSum = [0,0,0];
		for (var i = 0; i < 3; i++) {
			hexSum[i] = parseInt(hexCoord2[i]) + parseInt(hexCoord1[i]);
		}
		return $.hex.simplify(hexSum);
	}
	
	/*-----------------------------------------------
		distance - takes 2 hexCoords and returns the 
		shortest distance in hexes between them.
	-----------------------------------------------*/
	$.hex.distance = function(hexCoord1, hexCoord2) {
		var hexDistance = 0; 
		var hexDiffence = $.hex.difference(hexCoord1, hexCoord2);
		for (var i = 0; i < 3; i++) {
			hexDistance += Math.abs(hexDiffence[i]);
		}
		return hexDistance;
	}
	
	/*-----------------------------------------------
		direction - takes 2 hexCoords and returns the 
		facing from one to the other.
		NOTE: 0.5 values are any facing between the 
		whole number facings
	-----------------------------------------------*/
	$.hex.direction = function(hexCoord1, hexCoord2) {
		var hexDirection = 0;
		var hexDiffence = $.hex.difference(hexCoord1, hexCoord2);
		
		if (hexDiffence[0] > 0 && hexDiffence[1] == 0 && hexDiffence[2] == 0) { hexDirection = 0; }
		else if (hexDiffence[0] > 0 && hexDiffence[1] > 0 && hexDiffence[2] == 0) { hexDirection = 0.5; }
		else if (hexDiffence[0] == 0 && hexDiffence[1] > 0 && hexDiffence[2] == 0) { hexDirection = 1; }
		else if (hexDiffence[0] == 0 && hexDiffence[1] > 0 && hexDiffence[2] > 0) { hexDirection = 1.5; }
		else if (hexDiffence[0] == 0 && hexDiffence[1] == 0 && hexDiffence[2] > 0) { hexDirection = 2; }
		else if (hexDiffence[0] < 0 && hexDiffence[1] == 0 && hexDiffence[2] > 0) { hexDirection = 2.5; }
		else if (hexDiffence[0] < 0 && hexDiffence[1] == 0 && hexDiffence[2] == 0) { hexDirection = 3; }
		else if (hexDiffence[0] < 0 && hexDiffence[1] < 0 && hexDiffence[2] == 0) { hexDirection = 3.5; }
		else if (hexDiffence[0] == 0 && hexDiffence[1] < 0 && hexDiffence[2] == 0) { hexDirection = 4; }
		else if (hexDiffence[0] == 0 && hexDiffence[1] < 0 && hexDiffence[2] < 0) { hexDirection = 4.5; }
		else if (hexDiffence[0] == 0 && hexDiffence[1] == 0 && hexDiffence[2] < 0) { hexDirection = 5; }
		else if (hexDiffence[0] > 0 && hexDiffence[1] == 0 && hexDiffence[2] < 0) { hexDirection = 5.5; }
		
		return hexDirection;
	}
	
	
	/*-----------------------------------------------
		facingSimplfy - reduces a facing to a 
		number between 0 and 5 inclusive.
	-----------------------------------------------*/
	$.hex.facingSimplify = function(facing) {
		var tmpFacing = parseInt(facing);
		if (tmpFacing >= 6) {
			tmpFacing = tmpFacing % 6;
		}
		if (tmpFacing < 0) {
			tmpFacing = (tmpFacing % 6) + 6;
			if (tmpFacing == 6) {
				tmpFacing = 0;
			}
		}
		return tmpFacing;
	}
	
	
	/*-----------------------------------------------
		move - takes a hexCoord, facing and move 
		returns hexCoord with move added to 
		direction facing.
	-----------------------------------------------*/
	$.hex.move = function(hexCoord, facing, move) {
		var tmpHexCoord = hexCoord;
		var tmpFacing = $.hex.facingSimplfy(facing);
		var tmpMove = parseInt(move);
		if (tmpFacing > 2) {
			tmpFacing = tmpFacing - 3;
			tmpMove = tmpMove * -1;
		}
		tmpHexCoord[tmpFacing] = parseInt(tmpHexCoord[tmpFacing]) + tmpMove;
		return $.hex.simplify(tmpHexCoord);
	}
	
	
	/*-----------------------------------------------
		rotate - takes a hexCoord, and facing change 
		returns hexCoord rotated to new facing.
	-----------------------------------------------*/
	$.hex.rotate = function(hexCoord, facingChange) {
		facingChange = $.hex.facingSimplify(facingChange);
		hexCoord = $.hex.simplify(hexCoord);
		var rotatedHexCoord = hexCoord;
		switch(facingChange){
			case 1:
				rotatedHexCoord = [-1*hexCoord[2],hexCoord[0],hexCoord[1]];
				break;
			case 2:
				rotatedHexCoord = [-1*hexCoord[1],-1*hexCoord[2],hexCoord[0]];
				break;
			case 3:
				rotatedHexCoord = [-1*hexCoord[0],-1*hexCoord[1],-1*hexCoord[2]];
				break;
			case 4:
				rotatedHexCoord = [hexCoord[2],-1*hexCoord[0],-1*hexCoord[1]];
				break;
			case 5:
				rotatedHexCoord = [hexCoord[1],hexCoord[2],-1*hexCoord[0]];
				break;
		}
		return $.hex.simplify(rotatedHexCoord);
	}
	
	
})(jQuery);

