function initializeEngine(){
	preloadEngine();
	var mouseDown = 0;
	$("#logo_animation").mousedown(function(e){e.preventDefault();mouseDown = 1; $("#logo_animation").addClass("mousedown");});
	$("body").mouseup(function(){mouseDown = 0; $("#logo_animation").removeClass("mousedown");})
	$(document).mousemove(function(e){
		if(mouseDown || $('#animation_toggle').is(':checked')){
			var centerX = $('#outer').offset().left + 174;
			var normalX = e.pageX - centerX;
			var normalY = 178 - e.pageY;
			
			var angle = Math.round(Math.atan(normalY/normalX) / 3.1415926 * 2 * 90);
			if(normalX < 0){
				angle = 180 + angle;
			}
			if(normalX >= 0 && normalY < 0){
				angle = 360 + angle;
			}
			var image = Math.round(angle / 6) % 10 + 1;
			$('#logo_animation').css("background-image", "url(images/logo_animation_"+image+".png)");
		}
	});
}
function preloadEngine(){
	//compressed imagesQ code, thanks to Liel Dulev! http://blog.lieldulev.com/2010/05/21/parallel-image-preloading-in-js/
	imagesQ={onComplete:function(){},onLoaded:function(){},current:null,qLength:0,images:[],inProcess:false,queue:[],queue_images:function(){var arg=arguments;for(var i=0;i<arg.length;i++){if(arg[i].constructor===Array){this.queue=this.queue.concat(arg[i])}else if(typeof arg[i]==='string'){this.queue.push(arg[i])}}},process_queue:function(){this.inProcess=true;this.qLength+=this.queue.length;while(this.queue.length>0){this.load_image(this.queue.shift())}this.inProcess=false},load_image:function(imageSrc){var th=this;var im=new Image;im.onload=function(){th.current=im;th.images.push(im);(th.onLoaded)();if(th.queue.length>0&&!th.inProcess){th.process_queue()}if(th.qLength==th.images.length){(th.onComplete)()}};im.src=imageSrc}}

	show_img = document.getElementById('show_me');
	imagesQueue = imagesQ;
	imagesQueue.onLoaded = function()
	{
		var target_width = (imagesQueue.images.length)/(imagesQueue.qLength)*100;
		$("#animation_progress_bar").animate({width: target_width}, 100);
	}

	imagesQueue.onComplete = function()
	{
		$("#animation_progress_bar_container").fadeOut();
		$("#logo_animation").fadeIn();
		$("#animation_toggle_container").fadeIn();
		$('#animation_toggle').iphoneStyle({ checkedLabel: '&nbsp;', uncheckedLabel: '&nbsp;', resizeContainer: false, resizeHandle: true });
	}

	imagesQueue.queue_images(
	['images/logo_animation_1.png',
	 'images/logo_animation_2.png',
	 'images/logo_animation_3.png',
	 'images/logo_animation_4.png',
	 'images/logo_animation_5.png',
	 'images/logo_animation_6.png',
	 'images/logo_animation_7.png',
	 'images/logo_animation_8.png',
	 'images/logo_animation_9.png',
	 'images/logo_animation_10.png']
	);

	imagesQueue.process_queue();
}
function initializePhotoBrowser(){
	var i = 0;			
	var state = {};
	var $children = $("#photostack_next").children("*");
	var positions = {};

	//Initialize state variables

	$children.each(function(){
		$this = $(this);
		var position = $this.position();
		state[parseInt(this.id.substr(16))] = {
			"normal": position.left, 
			"leftShift": position.left - 100
		};
		
		$this.mouseenter(function(){
			var id = parseInt(this.id.substr(16));
			var $current = null;
			var currentPos = null;
			for (i in state) {
				$current = $("#photostack_next_" + i);
				currentPos = $current.position();
				if (i < id) {
					$current.children("img").show();
					if (currentPos.left == state[i].normal) {
						$current.stop().animate({"left": state[i].leftShift});
					}
				}
				else if (i == id) {
					$current.children("img").hide();
					if (currentPos.left == state[i].leftShift) {
						$current.stop().animate({"left": state[i].normal});
					}
				}
				else if (i > id) {
					$current.children("img").show();
					if (currentPos.left == state[i].leftShift) {
						$current.stop().animate({"left": state[i].normal});
					}
				}
			}
		});
	});
	$("#photostack_next").mouseleave(function (){
		for (i in state) {
			$current = $("#photostack_next_" + i);
			$current.stop().animate({"left": state[i].normal});
		}
		$children.each(function(){
			$(this).children("img").hide();
		});
	});

}
