/* ------------------------------------------------------ onLoad -- */

$(document).ready(function() {
    setupObjects();
	place(getRotateObjects(), radius(), baseAlpha(), false, 1000, false);
	acceleratedRotate(25, 300, 1.1, "right", true);
	
/* ------------------------------------------------ other Events -- */

	/* // buuugs
	
	$(window).keyup(function(event){
		if(event.keyCode == '37' || event.keyCode == '38'|| event.keyCode == '39' || event.keyCode == '40') {
			stop(true, true);
			pullIn();
		}
    });
	$(window).keydown(function(event){
		if(event.keyCode == '40' || event.keyCode == '39') {
	    	stop(true, true);
	    	pushOut();
			acceleratedRotate(150, 150, 0.85, "right", false);
		} else if (event.keyCode == '38' || event.keyCode == '37') {
	    	stop(true, true);
	    	pushOut();
			acceleratedRotate(150, 150, 0.85, "left", false);
		}
	});
	$(window).keypress(function(event){
		if(event.keyCode == '40' || event.keyCode == '39') {
	    	stop(true, true);
	    	pushOut();
			acceleratedRotate(150, 150, 0.85, "right", false);
		} else if (event.keyCode == '38' || event.keyCode == '37') {
	    	stop(true, true);
	    	pushOut();
			acceleratedRotate(150, 150, 0.85, "left", false);
		}
	});
	
	*/
	
	$("#down").mouseup(function(){
		stop(true, true);
		pullIn();
    }).mousedown(function(){
    	stop(true, true);
    	pushOut();
		acceleratedRotate(150, 150, 0.85, "right", false);
	});
	
	$("#up").mouseup(function(){
		stop(true, true);
		pullIn();
    }).mousedown(function(){    	
    	stop(true, true);
    	pushOut();
		acceleratedRotate(150, 150, 0.85, "left", false);
    });
    $(".object").click(function(){
    	if($(this).hasClass("centered")) {
    		var url = $(this).attr("title");
    		window.location.href = url;
    	}
    });
    $("#go").click(function(){
    	var url = $(".centered").attr("title");
    	if(url != null) window.location.href = url;
    });
});

/* ----------------------------------------------------- Set(up) -- */

function setupObjects() {
	window.myConf 		= new Object();
	window.myCache 	= new Object();
	var tmpArray 		= new Array();
	var otherArray 		= new Array();
	
	var blog 	= new Object(); blog.jq 	= $("#blog");
	var about 	= new Object(); about.jq 	= $("#about");
	var twitter = new Object(); twitter.jq 	= $("#twitter");
	var LR 		= new Object(); LR.jq 		= $("#LR");
	var rc 		= new Object(); rc.jq 		= $("#rc");   
	var piraten = new Object(); piraten.jq 	= $("#piraten");
	
	var right	= new Object(); right.jq 	= $("#up");
	var left	= new Object(); left.jq 	= $("#down");
		
	tmpArray.push(blog,twitter,piraten,about,rc,LR);
	otherArray.push(right,left);
	window.myConf.rotateObjects = tmpArray;
	window.myConf.outerObjects	= otherArray;
	window.myCache.shouldCenter = false;
	
	setBaseAlpha(-13);
	setRadius(150);
};

function getRotateObjects(){return window.myConf.rotateObjects;};
function getOuterObjects(){return window.myConf.outerObjects;};
function setRadius(r){window.myConf.radius = r;};
function radius(){return window.myConf.radius;};
function setBaseAlpha(d){window.myConf.baseAlpha = d;};
function baseAlpha(){return window.myConf.baseAlpha;};

/* ------------------------------------------------ Calculations -- */

function calcPosition(radius, angle) {
	var newPosition = new Object();
	newPosition.x = radius * Math.cos(angle / 180 * Math.PI) + ($('#content').width()/2);
	newPosition.y = radius * Math.sin(angle / 180 * Math.PI) + ($('#content').height()/2);
	return newPosition;
};

function place(obj, radius, startAngle, animate, speed, didEndFunction) {
	window.myCache.shouldCenter = didEndFunction;
	var cstrctr = obj.constructor.toString();
	if(cstrctr.indexOf("Array()") != -1) {
		var angleDif = 360 / obj.length;
		for(i = 0; i < obj.length; ++i) {
			var n = calcPosition(radius, startAngle);
			startAngle = startAngle + angleDif;
			obj[i].position = n;
			var xStr = n.x+"px";
			var yStr = n.y+"px";
			if(animate) {
				obj[i].jq.animate({
					top: yStr,
					left: xStr
		      	}, speed, function(){
		      		if(window.myCache.shouldCenter) {
		      			window.myCache.centeredObject.jq.addClass("centered");
		      		}
		      	});  // 1000
			} else {
				obj[i].jq.css({
					top: yStr,
					left: xStr
		      	});
	      	}
		}
	} else if(cstrctr.indexOf("Object()") != -1) {
			var n = calcPosition(radius, startAngle);
			obj.position = n;
			var xStr = n.x+"px";
			var yStr = n.y+"px";
			if(animate) {
				obj.jq.animate({
					top: yStr,
					left: xStr
		      	}, speed);  // 1000
			} else {
				obj.jq.css({
					top: yStr,
					left: xStr
		      	});
	      	}
	} else {
		alert("Error: No object or array passed.");
	}
};

/* -------------------------------------------------- Animations -- */

function stop(timer, animation) {
	if(timer){
		window.clearInterval(window.myCache.rotateTimerID);
	}
	if(animation){
		for(i = 0; i < getRotateObjects().length; ++i) {
			getRotateObjects()[i].jq.stop();
		}
	}
};

function acceleratedRotate(fromValue, toValue, stepFactor, direction, stop) {
	window.myCache.fromSpeed = fromValue;
	window.myCache.toSpeed = toValue;
	window.myCache.currentSpeed = fromValue;
	window.myCache.speedStepFactor = stepFactor;
	window.myCache.rotateDirection = direction;
	window.myCache.stopAnimation = stop;
	window.myCache.rotateTimerID = window.setInterval("handleRotateTimerEvent()", 0);
};

function handleRotateTimerEvent() {
	if(window.myCache.rotateDirection == "left") {
		linearRotate(getRotateObjects(), radius(), baseAlpha(), -1, window.myCache.currentSpeed);
	} else {
		linearRotate(getRotateObjects(), radius(), baseAlpha(), 1, window.myCache.currentSpeed);
	}
	window.clearInterval(window.myCache.rotateTimerID);
	
	if(window.myCache.fromSpeed < window.myCache.toSpeed) {
		if(window.myCache.currentSpeed > window.myCache.toSpeed) {
			if(!window.myCache.stopAnimation) {
				window.myCache.rotateTimerID = 
				window.setInterval("handleRotateTimerEvent()", window.myCache.currentSpeed);
			} else {pullIn();}
		} else {
			window.myCache.currentSpeed *= window.myCache.speedStepFactor;
			window.myCache.rotateTimerID = 
			window.setInterval("handleRotateTimerEvent()", window.myCache.currentSpeed);
		}
	} else {
		if(window.myCache.currentSpeed < window.myCache.toSpeed) {
			if(!window.myCache.stopAnimation) {
				window.myCache.rotateTimerID = 
				window.setInterval("handleRotateTimerEvent()", window.myCache.currentSpeed);
			} else {pullIn();}
		} else {
			window.myCache.currentSpeed *= window.myCache.speedStepFactor;
			window.myCache.rotateTimerID = 
			window.setInterval("handleRotateTimerEvent()", window.myCache.currentSpeed);
		}
	}
}

function linearRotate(array, radius, startAngle, degrees, speed) {
	var angleDif = 360 / array.length;
	for(i = 0; i < array.length; ++i) {
		var n = calcPosition(radius, (startAngle + degrees));
		startAngle = startAngle + angleDif;
		array[i].position = n;
		var xStr = n.x+"px";
		var yStr = n.y+"px";
		array[i].jq.animate({
			top: yStr,
			left: xStr
      	}, (speed - 20), "linear");
      	setBaseAlpha(baseAlpha()+degrees);
      	if(baseAlpha() > 360) {
      		setBaseAlpha(0);
      	}
	}
};

function pullIn() {
	var objs = getRotateObjects();
	var highest = 10000;
	var objIndex;
	var i;
	for(i = 0; i < objs.length; ++i) {
		if(highest > objs[i].position.y) {
			highest = objs[i].position.y;
			objIndex = i;
		}
	}
	window.myCache.centeredObject = objs[objIndex];
	window.myCache.centeredObject.oldIndex = objIndex;
	objs.splice(objIndex, 1);
	place(getRotateObjects(), radius(), baseAlpha(), true, 300, false);
	place(window.myCache.centeredObject, 0, baseAlpha(), true, 300, true);
};

function pushOut() {
	var objs = getRotateObjects();
	window.myCache.centeredObject.jq.removeClass("centered");
	objs.splice(window.myCache.centeredObject.oldIndex,0,window.myCache.centeredObject);
	window.myCache.centeredObject = new Object();
	place(objs, radius(), baseAlpha(), true, 300, false);
};