
function hookEvent(element, eventName, callback){
	if(typeof(element) == "string"){
		element = document.getElementById(element);
	}
	if(element == null){
		return;
	}
	if(element.addEventListener){
		if(eventName == 'mousewheel'){
			element.addEventListener('DOMMouseScroll', callback, false);  
		}
		element.addEventListener(eventName, callback, false);
	} else if(element.attachEvent) {
		element.attachEvent("on" + eventName, callback);
	}
}

$(document).ready(function() {
	//Powering rotation.
	var divs = new Array();
	$('#spinner > div').each(function(){
		divs.push($(this));
	});
	$('.spoke').css({
		'position' : 'absolute',
		'border-top-left-radius' : '80px', 
		'border-bottom-left-radius' : '80px',
		'-webkit-border-top-left-radius' : '80px', 
		'-webkit-border-bottom-left-radius' : '80px',
		'-moz-border-radius-topleft' : '80px', 
		'-moz-border-radius-bottomleft' : '80px',
		'height' : '150px'
	});
	$('#nav').show();
	$('#content').css({	'overflow' : 'hidden' });
	$('#left').show();
	$('#warning').hide();
	var root = [0, 250];
	var radius = divs[0].height();
	var theta = 50;
	var top, left, n = 0;
	//The actual function to rotate the things into place.
	function rotate(x){
		for(i = 0; i < divs.length; i++){
			top = (root[1] - (divs[i].height()/2));
			left = root[0] + radius;
			divs[i].css({
				'-webkit-transform-origin' : '-'+(100*(radius/600))+'% 50%',
				'-moz-transform-origin' : '-'+(100*(radius/600))+'% 50%',
				'top' : top+'px',
				'left' : left+'px'
			}).rotate({animateTo:(i*theta)-(x*theta)}); //Awesome rotate plugin.
		}
	}
	rotate(n);
	$('#nav li a').click(function(e){
		$('.active').removeClass('active');
		$(this).addClass('active');
		n = parseFloat($(this).attr('href'));
		active = divs[n];
		rotate(n);
		return false;
	});
	//Scrolling
	hookEvent('content', 'mousewheel', function MouseWheel(e){
		e = e ? e : window.event;
		var wheelData = e.detail ? e.detail * -1 : e.wheelDelta / 40;
		if(wheelData > 0){
			if(n > 0){
				n--;
				$('#nav li').eq(n).find('a').click();
			}
		} else if(wheelData < 0){
			if(n < $('.spoke').length){
				n++;
				$('#nav li').eq(n).find('a').click();
			}
		}
	});
	
	//hovering over misc section links.
	$('#misc li a').hover(function(e){
		var t = $(this);
		var o = t.offset();
		$('<div id="hover">').appendTo('body').css({
			'position' : 'absolute',
			'top' : (o.top-t.height()-5)+'px',
			'left' : (o.left+t.width())+'px',
			'background' : '#FFF',
			'padding' : '4px'
		}).html(t.attr('rel'));
	}, function(){
		$('#hover').remove();
	});
	
	//image slideshow
	$('#slider img').css({ 'position' : 'absolute' });
	$('.caption').html($('#slider img:last').attr('title'));
	function gallery(){
		
		$('#slider a:last').fadeOut(500, function(){
			$(this).prependTo('#slider').fadeIn(500);
		});	
		
		$('.caption').fadeOut(500, function(){ 
			$(this).html($('#slider img:last').attr('title'));
		}).fadeIn(500);

	}
	
	var galleryInt, startAgain;
	
	function setInt(){
		galleryInt = setInterval(gallery, 5000);
	}
	
	setInt();
	
	$('#left').click(function(){
		clearTimeout(startAgain);
		clearInterval(galleryInt);
		gallery();
		startAgain = setTimeout(setInt, 10000);
	});
	
});
