// Copyright Hugh Collins 2010
(function() {
	// Turbine's rotation
	var propAngle = 0, count = 0;;
	// Set the rotation origin for IE
	// A function to run every few milliseconds
	var tick = function() {
		// Increment the rotation
		propAngle = (propAngle + 0.05) % 360;
		// Increment the background movement
		count++;
		// Move the clouds across the screen
		// Select the clouds
		var e = $('#clouds');
		// Update the CSS
		e.css('background-position', '-' + count + 'px 0');
		// Rotate the turbine propellers
		// Select the propeller
		e = $('#propeller');
		// Convert to degrees
		var dAngle = propAngle / Math.PI / 2 * 360;
		// Update the CSS
		e.css({
			'-webkit-transform': 'rotate(' + dAngle + 'deg)',	// Webkit
			'-moz-transform': 'rotate(' + dAngle + 'deg)',	// Firefox
			'rotation': dAngle + 'deg'	// Opera?
		});
		// Fade in and out the sun
		// Select the sun
		e = $('#sun');
		// Update the CSS
		e.css('opacity', Math.cos(propAngle * 1.5) / 4 + 0.75);
		// Rotate the rays
		// Select the first ray
		e = $('#sunrays0');
		dAngle = dAngle / 5;
		// Update the CSS
		e.css({
			'-webkit-transform': 'rotate(' + (-dAngle) + 'deg)',	// Webkit
			'-moz-transform': 'rotate(' + (-dAngle) + 'deg)',	// Firefox
			'rotation': (-dAngle) + 'deg',	// Opera?
			// Opacity
			opacity: Math.cos(propAngle * 2) / 4 + 0.5
		});
		// Select the second ray
		e = $('#sunrays1');
		// Update the CSS
		e.css({
			'-webkit-transform': 'rotate(' + dAngle + 'deg)',	// Webkit
			'-moz-transform': 'rotate(' + dAngle + 'deg)',	// Firefox
			'rotation': dAngle + 'deg',	// Opera?
			// Opacity
			opacity: Math.cos(propAngle * 1.1) / 4 + 0.5
		});
	};
	// Open a file
	var open = function( file ) {
		// Add the click to history
		$.history.add('/' + file.split('/')[1].split('.')[0]);
		// Load the file
		load(file);
	};
	// Load a file
	var load = function( file ) {
		// Get the page
		var page = $('.body');
		// Pop back the old open tab
		var s = $('#sidelinks');
		var o = s.find('.open');
		o.animate({
			marginRight: 10
		}, 200);
		o.removeClass('open');
		// Find the new open tab and pop out
		o = s.find('[href=' + file + ']');
		o.animate({
			marginRight: 0
		}, 200);
		o.addClass('open');
		// Dim the current page
		page.fadeOut(400, function(){
			$.ajax({
				url: file,
				success: function( e ) {
					// Parse and add content
					page.html(e);
					parse(page);
					// Fade the page in
					page.stop().fadeIn(400);
				},
				error: function( e ) {
					alert('Could not load the page. Please try again later.');
				}
			});
		});
	};
	// Parse a page
	var parse = function( page ) {
	
	}
	// On document ready
	$(function() {
		// Load a page
		setTimeout(function() {
			open('pages/' + (location.hash.substring(2) || 'home') + '.php');
		}, 800);
		// Bind a listener to a history event
		$().bind('history', function() {
			// Load the page
			load('pages/' + location.hash.substring(2) + '.php');
		});
		// Bind a click event listener for all internal links
		$('a.internal').live('click', function() {
			// Load the page
			open($(this).attr('href'));
			// Prevent default
			return false;
		});
		// Start up the tick function
		// Check for bad support
		if (!$.support.opacity)
			return;
		var tickRef = setInterval(tick, 50);
		// Pause button
		$('#pause').click(function() {
			var t = $(this);
			// Toggle state
			t.toggleClass('paused');
			// If paused
			if (t.hasClass('paused')) {
				clearInterval(tickRef);
			} else {
				tickRef = setInterval(tick, 50);
			}
		});
	});
})();