//JAVASCRIPT CAROUSEL
//WRITTEN BY PETER KUTZ
//PETERKUTZ.COM
var speed = 0;
var angle = 0;
var x;
var y = 0;
var z;
var radius = 400;
var scale;
var numItems = 20;
var colors = new Array();
var startTime;
var frames = 0;
function getRGB(object, amount) {
	return("rgb("+Math.floor(object.r*amount)+","+Math.floor(object.g*amount)+","+Math.floor(object.b*amount)+")");
}
function getHex(object) {
	return RGBtoHex(object.r, object.g, object.b);
}
function randomColor() {
	var red = Math.floor(Math.random() * 255); 
	var green = Math.floor(Math.random() * 255); 
	var blue = Math.floor(Math.random() * 255);
	return {r:red, g:green, b:blue};
}
function RGBtoHex(R,G,B) {
	return "#"+toHex(R)+toHex(G)+toHex(B);
}
function toHex(N) {
	if (N==null) return "00";
	N=parseInt(N);
	if (N==0 || isNaN(N)) return "00";
	N=Math.max(0,N);
	N=Math.min(N,255);
	N=Math.round(N);
	return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
}
function startCarousel() {
	for (i=0; i<numItems; i++) {
		colors[i] = randomColor();
		document.getElementById("image" + i).style.backgroundColor = getRGB(colors[i], 1);
	}
	startTime = (new Date()).getTime();
	setInterval(loop, 5);
}
function mouseMoveHandler(e) {
	if (e == null) e = window.event;
	speed = -1 * (e.clientX - document.body.clientWidth / 2) / 5000;
	y = -1 * (e.clientY - 280);
	document.body.focus();
	document.onselectstart = function () { return false; };
	return false;
}
function loop() {
	angle += speed;
	for (var i=0; i<numItems; i++) {
		var a = angle + i * Math.PI / (numItems/2);
		x = Math.cos(a) * radius;
		z = Math.sin(a) * radius + radius;
		scale = 400 / (400 + z);
		displayWidth = 80 * scale;
		displayHeight = 80 * scale;
		displayX = x * scale + document.body.clientWidth / 2 - displayWidth / 2;
		displayY = y * scale + 280 - displayHeight / 2;//document.body.clientHeight
		document.getElementById("image" + i).style.width = Math.round(displayWidth) + "px";
		document.getElementById("image" + i).style.height = Math.round(displayHeight) + "px";
		document.getElementById("image" + i).style.left = Math.round(displayX) + "px";
		document.getElementById("image" + i).style.top = Math.round(displayY) + "px";
		document.getElementById("image" + i).style.zIndex = Math.round(10000 - z);
		var strength = scale*scale;
		document.getElementById("image" + i).style.backgroundColor = getRGB(colors[i], strength);
	}
	frames++;
	var elapsedTime = (new Date()).getTime() - startTime;
	var fps = frames / (elapsedTime / 1000);
	var roundedFPS = Math.round(fps * 10)/10;
	var roundedFPSString = roundedFPS.toString();
	if (roundedFPS == Math.floor(roundedFPS)) roundedFPSString += ".0";
	document.getElementById("fpsText").innerHTML = "<br />" + roundedFPSString + " fps";
}
function gotoLink(number) {
	window.location = "http://www.peterkutz.com/";
}
function mouseOver(number) {
	document.getElementById("colorText").innerHTML = "<br />" + getHex(colors[number]);
}
function mouseOut(number) {
	document.getElementById("colorText").innerHTML = "";
}
document.onmousemove = mouseMoveHandler;
document.oncontextmenu=new Function("return false;");
function createCarouselHere() {
	document.write("<div id='fpsText' style='position:absolute;cursor:default;left:16px;'></div><div id='colorText'></div><div class='container'>");
	for (var i=0; i<numItems; i++) {
		document.write("<div id='image" + i + "' class='image' onmousedown='javascript:gotoLink(" + i + ");' onmouseover='javascript:mouseOver(" + i + ");' onmouseout='javascript:mouseOut(" + i + ");'></div>");
	}
	document.write("</div>");
	startCarousel();
}
//©2009 PETER KUTZ