var total_scaling = 0.50;
		
var proximity = 200 * total_scaling;
var maxpercentwidth = 0.50; // percentage bigger

// padding in pixels
var top_padding = 90;
var left_padding = 0;
// [left, top];
var radial_center = scaling([120 +left_padding, 120 + top_padding], total_scaling);
var menu = 
scaling([
	{width:74, left:40+left_padding, top:154+top_padding, polygon:[[0,55],[55,0],[73,7],[73,85],[34,76]]},
	{width:86, left:left_padding, top:125+top_padding, polygon:[[0,0],[77,0],[85,21],[30,75],[8,39]]},
	{width:85, left:left_padding, top:42+top_padding, polygon:[[0,72],[9,32],[29,0],[84,55],[77,72]]},
	{width:76, left:38+left_padding, top:top_padding, polygon:[[0,32],[35,9],[75,0],[75,79],[55,87]]},
	{width:74, left:126+left_padding, top:top_padding, polygon:[[0,0],[38,9],[73,31],[17,87],[0,79]]},
	{width:88, left:152+left_padding, top:40+top_padding, polygon:[[0,56],[56,0],[78,34],[87,76],[7,76]]}
], total_scaling);

// set up slices
jQuery(document).ready(function(){
//	jQuery("#floating_obj_container").corner();
//	var reloffset = jQuery("#pie-container").offset();
	var current_state = new Array();

	// set initial position and size
	for (var i = 0; i < menu.length; i++) {
		var slice = jQuery("#pieslice"+i);
		
		current_state[i] = {scale:1, left:menu[i].left, top:menu[i].top};
		slice.width(menu[i].width);
		slice.css("left", menu[i].left);
		slice.css("top", menu[i].top);				
	}
	
});

function transformation (x, y, x_root, y_root, scale) {
	var newx = ((x - x_root) * scale) + x_root;
	var newy = ((y - y_root) * scale) + y_root;
	return {left:newx|0, top:newy|0}; // truncates decimal places
}

/*
based on http://www.webmasterworld.com/javascript/3551991.htm  originally from  Randolph Franklin
	test if a point is within a polygon
	polygon is array of tuples  [[x,y],[x2,y2],...]  ASSUMES:  uses first point as also the last
	point is a tuple [x,y]
*/
function pointInPoly(point, polygon) {
	var i, j, c = false, npol = polygon.length;
	for (i = 0, j = npol-1; i < npol; j = i++) {
		if ((((polygon[i][1] <= point[1]) && (point[1] < polygon[j][1])) || 
			((polygon[j][1] <= point[1]) && (point[1] < polygon[i][1]))) && 
			(point[0] < (polygon[j][0] - polygon[i][0]) * (point[1] - polygon[i][1]) / (polygon[j][1] - polygon[i][1]) + polygon[i][0])) {
			c =!c; 
		} 
	}
	return c;
}

/* scale all numbers found in the object by the given factor */
function scaling (obj, factor) {
	for (var i in obj) {
		if (typeof(obj[i]) == "object") {
			scaling(obj[i], factor);
		} else if (typeof(obj[i]) == "number") {
			obj[i] = (obj[i] * factor) |0;
		}
	}
	return obj;
}

function enlarge_slice(i,e) {
	var pointer = jQuery.iUtil.getPointer(e);
	var current_state = new Array();
	current_state[i] = {scale:1, left:menu[i].left, top:menu[i].top};
		// get center coords (assume center of image is center)
		var slice = jQuery("#pieslice"+i);
		img_center_coords =  {left:(current_state[i].left + jQuery("#pie-container").offset().left + (menu[i].width * current_state[i].scale) /2)|0, top:(current_state[i].top + jQuery("#pie-container").offset().top + slice.height()/2)|0};
		
		distance = Math.sqrt(
			Math.pow(pointer.x - img_center_coords.left, 2)
			+ Math.pow(pointer.y - img_center_coords.top, 2)
		);
		
		distance = distance > proximity ? proximity : distance;
		current_state[i].scale = 1 + Math.pow((proximity - distance)/proximity * maxpercentwidth, 2);  // make it geometrical (x^2) scaling
		slice.width((menu[i].width * current_state[i].scale)|0);
		var newloc = transformation(menu[i].left, menu[i].top, radial_center[0], radial_center[1], current_state[i].scale);
		slice.css("left", current_state[i].left = newloc.left);
		slice.css("top", current_state[i].top = newloc.top);
		floating_title_obj.innerHTML = ktx_wheels_labels[i];
		floating_title_obj.style.color = ktx_wheels_labels_colors[i];
	
}

function reduce_slice(i,e) {
	var slice = jQuery("#pieslice"+i);
	slice.width(menu[i].width);
	slice.css("left", menu[i].left);
	slice.css("top", menu[i].top);
	ktx_wheels[i].coords = ktx_wheels_coords[i];
	floating_title_obj.innerHTML = floating_title;
	floating_title_obj.style.color = floating_title_color;
}

function launch_url(i) {
	window.location.href = ktx_wheels_urls[i];
}
