﻿var imageDisplayNo = 1;
var previousBoxTextAreaId = '';

$(document).ready(function () {
	$("#flash").stop(false, true).fadeIn(500);

	$('.HomePageBox').hover(function (e) {
		homepageBoxMouseIn(this, e);
	}, function (e) {
		homepageBoxMouseOut(this, e);
	});
});

function homepageBoxMouseIn(box, e) {
	var id = box.id;
	var browser = $.browser;
	var top = 0;
	var left = 0;

	if (browser.msie && browser.version < '8.0') {
		var client = box.getBoundingClientRect();

		if (browser.version < '7.0') {
			top = client.top + client.bottom + 47;
			left = client.left - 2;
		}
		else {
			top = client.top + 148;
			left = client.left - 2;
		}
	}
	else {
		top = box.offsetTop + box.offsetHeight;
		left = box.offsetLeft;
	}

	if (previousBoxTextAreaId == null || id != previousBoxTextAreaId) {
		var boxLink = $('#boxLink');
		var moreLink = $('#moreImageLink');
		var backgroundImage;

		clearPreviousBoxTextArea(false, true);

		previousBoxTextAreaId = id;

		if (imageDisplayNo == 1) {
			stopFlashHomePage();

			$("#flash").stop(false, true).fadeOut(500);
		}
		else {
			$(".HomeImage").stop(false, true).fadeOut(500);
		}

		switch (id) {
			case 'boxOne':
				imageDisplayNo = 2;
				backgroundImage = "box-background-hover.png";
				boxLink.addClass('BoxLink1');
				moreLink.attr("href", "web-development/");
				break;
			case 'boxTwo':
				imageDisplayNo = 3;
				backgroundImage = "box-background-hover.png";
				boxLink.addClass('BoxLink2');
				moreLink.attr("href", "facilities-management/");
				break;
			case 'boxThree':
				imageDisplayNo = 4;
				backgroundImage = "box-background-hover.png";
				boxLink.addClass('BoxLink3');
				moreLink.attr("href", "secure-document-transfer/");
				break;
			case 'boxFour':
				imageDisplayNo = 5;
				backgroundImage = "box-background-hover-2.png";
				boxLink.addClass('BoxLink4');
				moreLink.attr("href", "publishing-solutions/");
				break;
		}

		$('#' + id).css({ "background-image": "url('images/" + backgroundImage + "')", "background-repeat": "repeat-y" });

		if (imageDisplayNo == 1) {
			$("#flash").stop(false, true).fadeIn(500);
			startFlashHomePage();
		}
		else {
			$("#homeImage" + imageDisplayNo).stop(false, true).fadeIn(500);
		}

		boxLink.css("top", top);
		boxLink.css("left", left);
		boxLink.slideDown(200);
	}
}

function homepageBoxMouseOut(box, e) {
	var isTargetBoxLink = (e.relatedTarget.id == 'boxLink');
	var isTargetAnotherBox = (e.relatedTarget.className.indexOf('BoxMain') != -1);

	clearPreviousBoxTextArea(!(isTargetBoxLink || isTargetAnotherBox), !isTargetBoxLink);
}

function clearPreviousBoxTextArea(changeHomePageImage, closeBoxLink) {
	if (previousBoxTextAreaId.length > 0) {
		var boxLink = $('#boxLink');

		if (closeBoxLink) {
			switch (previousBoxTextAreaId) {
				case 'boxOne':
					boxLink.removeClass('BoxLink1');
					break;
				case 'boxTwo':
					boxLink.removeClass('BoxLink2');
					break;
				case 'boxThree':
					boxLink.removeClass('BoxLink3');
					break;
				case 'boxFour':					
					boxLink.removeClass('BoxLink4');
					break;
				default:
			}

			boxLink.stop(false, true).hide();			
			$('#' + previousBoxTextAreaId).css({ "background-image": "", "background-repeat": "" });
			previousBoxTextAreaId = '';

			boxLink.unbind('mouseleave');
		}
		else {
			boxLink.bind('mouseleave', function (e) {
				var id = e.relatedTarget.id;
				var isTargetAboveBoxLink = (id == '' || id.indexOf('box') != -1); // Blank seems to refer to the point just above the outline of this div in Firefox.

				clearPreviousBoxTextArea(!isTargetAboveBoxLink, !isTargetAboveBoxLink);
			});
		}

		if (changeHomePageImage) {
			$(".HomeImage").stop(false, true).fadeOut(500);			
			imageDisplayNo = 1;
			$("#flash").stop(false, true).fadeIn(500);
			startFlashHomePage();
		}
	}
}

function stopFlashHomePage() {
	var flashMovie = getFlashMovieObject("homepage");

	if (flashMovie != null) {
		flashMovie.StopPlay();
	}
}

function startFlashHomePage() {
	var flashMovie = getFlashMovieObject("homepage");

	if (flashMovie != null) {
		//flashMovie.Rewind();
		flashMovie.Play();
	}
}

function getFlashMovieObject(movieName) {
	if (window.document[movieName]) {
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[movieName]) {
			return document.embeds[movieName];
		}
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		return document.getElementById(movieName);
	}
}
