/*  C R A Z Y   B U L L E T S  */

var MIN_WINDOW_WIDTH = 800; // px
var MAX_WINDOW_WIDTH = 1280; // px
var MIN_NAVIGATOR_SIZE = 256; // px
var MAX_NAVIGATOR_SIZE = 512; // px
var TOOLTIP_DELAY = 100; // ms

var windowWidth;
var navigatorSize;


$(function() {
	setupWindow();
	$(window).resize(setupWindow);
	setupPage();
});


function setupWindow() {
	windowWidth  = $(window).width();
	windowHeight = $(window).height();

	if (windowWidth > MAX_WINDOW_WIDTH) {
		$('body').css('width', MAX_WINDOW_WIDTH);
		$('body').css('margin-left', (windowWidth-MAX_WINDOW_WIDTH)/2);
		$('body').css('margin-right', (windowWidth-MAX_WINDOW_WIDTH)/2);
		$('div#shot span').css('line-height', MAX_WINDOW_WIDTH/8 + 'px');
		$('div#shot span').css('font-size', MAX_WINDOW_WIDTH/40 + 'px');
	} else if (windowWidth < MIN_WINDOW_WIDTH) {
		$('body').css('width', MIN_WINDOW_WIDTH);
		$('body').css('margin-left', '0%');
		$('body').css('margin-right', '0%');
		$('div#shot span').css('line-height', MIN_WINDOW_WIDTH/8 + 'px');
		$('div#shot span').css('font-size', MIN_WINDOW_WIDTH/40 + 'px');		
	} else {
		$('body').css('width', '100%');
		$('body').css('margin-left', '0%');
		$('body').css('margin-right', '0%');
		$('div#shot span').css('line-height', windowWidth/8 + 'px');
		$('div#shot span').css('font-size', windowWidth/40 + 'px');
	}
	
	if (windowWidth > windowHeight) navigatorSize = windowHeight * 0.8;
	else navigatorSize = windowWidth * 0.8;
	if (navigatorSize > MAX_NAVIGATOR_SIZE) navigatorSize = MAX_NAVIGATOR_SIZE;
	if (navigatorSize < MIN_NAVIGATOR_SIZE) navigatorSize = MIN_NAVIGATOR_SIZE;
	$('#navigator').css('width', navigatorSize);
	$('#navigator').css('height', navigatorSize);
	$('#navigator').css('margin-top', -navigatorSize/2);
	$('#navigator').css('margin-left', -navigatorSize/2);
};


function setupPage() {
	$('#header').bind('mouseenter', showBullet);
	$('.menu').tooltip({ tip: '.menu_tooltip', position: 'bottom center', relative: true, delay: TOOLTIP_DELAY });
	$('.face').tooltip({ tip: '.face_tooltip', position: 'bottom center', relative: true, delay: TOOLTIP_DELAY });
	$('.menu').bind('click', hideNavigator);
	$('.face').bind('click', hideNavigator);
};


function showBullet() {
	$('#header').unbind('mouseenter');
	$('#shot').animate({ left: '50%', marginLeft: '-6.25%' }, 125, 'linear', function() {
		$('#header').bind('mouseleave', hideBullet);
		$('#header').bind('click', showNavigator);
	});
};


function hideBullet() {
	$('#header').unbind('mouseleave');
	$('#header').unbind('click');
	$('#shot').animate({ left: '100%', marginLeft: '0%' }, 125, 'linear', function() {
		$('#shot').animate({ left: '0%', marginLeft: '-12.5%' }, 0);
		$('#header').bind('mouseenter', showBullet);
	});
};


function showNavigator() {
	hideBullet();
	$('#background').show().fadeOut(0).fadeTo(500, 0.8);
	$('#navigator').show().animate({ top: '50%' }, 500, 'linear', function() {
		$('.menu').animate({ width: '16.66%', marginLeft: '-8.33%', height: '16.66%', marginTop: '-8.33%' }, 500);
		$('.face').animate({ width: '12.5%', marginLeft: '-6.25%', height: '12.5%', marginTop: '-6.25%' }, 500);
		$('#bullet').fadeOut(500, function() { $('#navigator').bind('mouseleave', hideNavigator); });
	});
};


function hideNavigator() {
	$('#navigator').unbind('mouseleave');
	$('.menu_tooltip').hide();
	$('.face_tooltip').hide();
	$('.menu').animate({ width: '0%', marginLeft: '0%', height: '0%', marginTop: '0%' }, 500);
	$('.face').animate({ width: '0%', marginLeft: '0%', height: '0%', marginTop: '0%' }, 500);
	$('#bullet').fadeIn(500, function() {
		$('#background').fadeTo(500, 0, function() { $('#background').hide(); });
		$('#navigator').animate({ top: -navigatorSize }, 500, 'linear', function() { $('#navigator').hide(); });
	});
};
