jQuery(function($){
	var icons = $('div.rotary div.icon');
	var tabs = $('div.tabs div a');
	var radius = 124;
	var marginTop = -30;
	var marginLeft = 610;
	var duration = 500;
	var slides = $('.slides');
	var rollover = '_over';
	var selected = '_selected';
	var start = 0;
	var selectedIcon = start;
	var startingSlide = 7;
	
	
	slides.cycle({
		timeout: 0,
		speed: duration,
		//startingSlide: icons.length - start
		startingSlide: 8
	});
	
	var numObjs = icons.length;
	var center = {x: radius + marginLeft, y: radius + marginTop};
	var alpha = Math.PI * 2 / numObjs;
	var degrees = 360 / numObjs;
	var zeroPoint = -1 * degrees - 15;
	icons.each(function(j){
		var el = $(this);
		var arc_params = {
			center: [center.x,center.y],  
			radius: radius,    
			start: (j + start) * degrees + zeroPoint,
			end: (j + start) * degrees + zeroPoint,
			dir: 1
		}
		/*if (j == 0) {
			el.children('img').css({
				width: 150,
				height:150,
				marginTop: -8,
				marginLeft: -33
			});
		} else {*/
			el.children('img').css({
				width: 76,
				height:76,
				marginTop: 22,
				marginLeft: 22
			});
		//}
		var srcOrig = el.children('img').attr('src');
		var temp = srcOrig.split('.');
		var extension = temp.pop();
		var srcOver = temp.join() + rollover + '.' + extension;
		var srcSelected = temp.join() + selected + '.' + extension;
		el.children('img').data('srcOrig', srcOrig);
		el.children('img').data('srcOver', srcOver);
		el.children('img').data('srcSelected', srcSelected);
		var preload = new Image();
		preload.src = srcOver;
		preload.src = srcSelected;
		if (j == 0) {
			//el.children('img').attr('src', srcSelected);
		}
		el.data('position', j + start);
		el.animate({path: new $.path.arc(arc_params)}, 1);
		$('div.container').addClass('processed');
	});
	icons.click(function(){
		animate(numObjs - $(this).data('position'), -1);
	});
	icons.mouseover(function(){
		var el = $(this).children('img');
		if (el.attr('src') != el.data('srcSelected')) {
			el.attr('src', el.data('srcOver'));
		}
	});
	icons.mouseout(function(){
		var el = $(this).children('img');
		if (el.attr('src') != el.data('srcSelected')) {
			el.attr('src', el.data('srcOrig'));
		}
	});
	$('button.next').click(function(e){
		e.preventDefault();
		animate(-1, -1);
	});
	$('button.prev').click(function(e){
		e.preventDefault();
		animate(1, 1);
	});
	tabs.click(function(e){
		//var diff = numObjs - $(icons[tabs.index($(this))]).data('position');
		//if (diff != numObjs) {
		//	e.preventDefault();
		//	animate(diff, -1);
		//}
	});
	function animate(change, dir) {
		icons.each(function(j){
			var el = $(this);
			var newPosition = el.data('position');
			if (newPosition + change >= numObjs) {
				newPosition = Math.floor((newPosition + change) % numObjs);
			} else if (newPosition + change < 0) {
				newPosition = numObjs - 1;
			} else {
				newPosition += change;
			}
			if (newPosition == 0) {
				selectedIcon = j;
			}
		});
		var adjustedDuration = ($(icons[selectedIcon]).data('position') * degrees) + duration;
		icons.each(function(j){
			var el = $(this);
			var newPosition = el.data('position');
			if (newPosition + change >= numObjs) {
				newPosition = Math.floor((newPosition + change) % numObjs);
			} else if (newPosition + change < 0) {
				newPosition = numObjs - 1;
			} else {
				newPosition += change;
			}
			if (newPosition == 0) {
				el.children('img').animate({
					width: 150,
					height:150,
					marginTop: -8,
					marginLeft: -33
				}, adjustedDuration);
				slides.cycle(j);
				el.children('img').attr('src', el.children('img').data('srcSelected'));
				//adjustedDuration = (el.data('position') * degrees + zeroPoint) + (newPosition * degrees + zeroPoint)) * duration;
			} else {
				// Fix the "jump"
				if (el.children('img').attr('src') == el.children('img').data('srcSelected')) {
					el.children('img').css({
						width:140,
						height:140,
						marginTop: 0,
						marginLeft: -23
					});
				}
				el.children('img').animate({
					width: 76,
					height:76,
					marginTop: 22,
					marginLeft: 22
				}, adjustedDuration);
				el.children('img').attr('src', el.children('img').data('srcOrig'));
			}
			var arc_params = {
				center: [center.x,center.y],  
				radius: radius,    
				start: el.data('position') * degrees + zeroPoint,
				end: newPosition * degrees + zeroPoint,
				dir: dir
			}
			el.data('position', newPosition);
			el.animate({path: new $.path.arc(arc_params)}, adjustedDuration);
		});
	}
});
