/** The number of images appearing in the banner in the header. */
var BANNER_IMAGE_COUNT = 4;
/** The relative path to fetch the images of the banner from. */
var BANNER_IMAGE_PATH = '/public/4473156a-3e44-4499-8263-64bed1ad9001/banniere/{0}.jpg';
/** The width of the images within the banner. */
var BANNER_IMAGE_WIDTH = 634;
/** The height of the images within the banner. */
var BANNER_IMAGE_HEIGHT = 263;

/**
 * Adds the banner images into the header of the website.
 */
function addImagesToHeaderBanner() {
	var oBanner = $('#header-banner');
	for (var i = 1; i <= BANNER_IMAGE_COUNT; ++i)
		oBanner.append('<img src="' + BANNER_IMAGE_PATH.replace('{0}', i) + '" width="' + BANNER_IMAGE_WIDTH + '" height="' + BANNER_IMAGE_HEIGHT + '"/>');
}

/**
 * Initiates the cycling of the header banner.
 */
function cycleBanner(){
	$('#header-banner').cycle({
		fx: 'fade',
		speed: 2500,
		timeout: 5000,
		random: 1
	});
}

function handleMenuOpacity() {
	$('#menu-banner-bg').hover(
		// function on mouse hover
		function () {
			$(this).animate({opacity: 0.8}, 250);
		},
		
		// function on mouse out
		function (event) {
			if (!isMouseWithinBounds($(this), event))
				$(this).animate({opacity: 0.4}, 250);
		}
	);
}

var _MOUSE_POSITION_CHECKED = false

function checkMousePositionOnLoad(){
	$('body').mousemove(function (event) {
		if (!_MOUSE_POSITION_CHECKED) {
			if (isMouseWithinBounds($('#menu-banner-bg'), event)) {
				$('#menu-banner-bg').animate({opacity: 0.8}, 0);
			}
			_MOUSE_POSITION_CHECKED = true;
		}
	});
}

function isMouseWithinBounds(element, mouseEvent){
	var oOff = element.offset();
	var mouseX = mouseEvent.pageX;
	var mouseY = mouseEvent.pageY;
	var menuLeft = oOff.left + 10;
	var menuRight = oOff.left + element.width() - 10;
	var menuTop = oOff.top + 10;
	var menuBottom = oOff.top + element.height() - 10;
	return menuLeft <= mouseX && mouseX <= menuRight && menuTop <= mouseY && mouseY <= menuBottom ;
}

$(document).ready(function(){
	addImagesToHeaderBanner();
	cycleBanner();
	handleMenuOpacity();
	checkMousePositionOnLoad();
});
