﻿$(document).ready(function () {
	var setMainPageAnimation = function () {
		var nbrColorContainer = 4;
		var currentColorContainer = 1;
		var animateColorContainre = true;
		var animationStopTime = 5000;

		var movePicture = function (direction) {
			currentColorContainer += (direction == "left" ? -1 : 1);

			var hideAllExecptCurrent = function (indexCurrent) {
				$(".anim_message").hide();
				$(".anim_message:eq(" + indexCurrent + ")").show();
			}

			if (currentColorContainer > nbrColorContainer) {
				currentColorContainer = 1;
				$(".anim_message").show();
			}
			else if (currentColorContainer == 0) {
				currentColorContainer = nbrColorContainer;
				$(".anim_message").show();
			}

			var indexCurrentContainer = currentColorContainer - 1;
			$(".anim_message:eq(" + indexCurrentContainer + ")").show();
			var fullLeft = (currentColorContainer - 1) * 988;
			var animationTime = (jQuery.browser.msie ? 0 : 1000);
			if (animateColorContainre) {
				$("#messageContainer").animate({ left: "-" + fullLeft + "px" }, animationTime).animate({ border: 0 }, animationStopTime, function () { hideAllExecptCurrent(indexCurrentContainer); movePicture("right"); });
			}
			else {
				$("#messageContainer").animate({ left: "-" + fullLeft + "px" }, animationTime, function () { hideAllExecptCurrent(indexCurrentContainer); });
			}
		}

		var startAnimation = function () {
			animateColorContainre = true;
			movePicture("right");
			$("#playColorAnimationButton").removeClass().addClass("bou_pause");
			$("#playColorAnimationButton").text("Pause");
		}

		var stopAnimation = function () {
			animateColorContainre = false;
			$("#messageContainer").stop(true, true);
			$("#playColorAnimationButton").removeClass().addClass("bou_play");
			$("#playColorAnimationButton").text("Jouer");
		}

		$("#goLeft").click(function (event) {
			event.preventDefault();
			stopAnimation();
			movePicture("left");
		});

		$("#goRight").click(function (event) {
			event.preventDefault();
			stopAnimation();
			movePicture("right");
		});

		$("#playColorAnimationButton").toggle(
			function () { stopAnimation(); },
			function () { startAnimation(); }
		);

		$(".anim_message").hide();
		$(".anim_message:eq(0)").show();
		$("#messageContainer").animate({ border: 0 }, animationStopTime, function () { movePicture("right"); });
	};


	var setDropDownMenu = function () {
		var show = function (element) {
			var ulElement = element.find("ul");
			ulElement.attr("hasFocus", true);
			//	element.find("a img.menuNormal").hide();
			//	element.find("a img.menuHover").show();
			if (jQuery.browser.msie) {
				ulElement.show();
			}
			else {
				ulElement.slideDown("fast");
			}
		};

		var hide = function (element) {
			var ulElement = element.find("ul");
			if (!ulElement.is(":animated")) {
				ulElement.attr("hasFocus", false);
				//	element.find("a img.menuNormal").show();
				//	element.find("a img.menuHover").hide();
				if (jQuery.browser.msie) {
					ulElement.animate({ opacity: 1 }, 300, function () { if (!ulElement.attr("hasFocus")) { ulElement.hide(); } });
				}
				else {
					ulElement.slideUp("fast");
				}
			}
		}

		$(".top_menu > ul > li").hover(
		function () { show($(this)); },
		function () { hide($(this)); }
		);

		$(".top_menu > ul > li")
			.find("a")
				.focus(function () { show($(this).parent()); })
				.blur(function () { $(this).parent().find("ul").stop(true, true); hide($(this).parent()); })
			.end().find("> ul")
				.hover(function () { $(this).parents("ul:first").attr("hasFocus", true); })
			.find("> li a")
				.focus(function () { $(this).parents("ul:first").stop(); })
				.blur(function () { hide($(this).parents("ul:first").parent()); });

		$(".content_wrapper").hover(function () {
			$(".level_02").stop(true, true);
			$(".level_02").hide();
			$(".level_02").parent().find("a img.menuNormal").show();
			$(".level_02").parent().find("a img.menuHover").hide();
		});
	};

	var setMainPageVideoPopup = function () {
		$("div#player object").attr("tabindex", "0");


		$("#launchvideo-pic, #launchvideo-link").bind("click", function (event) {
			event.preventDefault();
			$("div#player").show();
			$("body").css("overflow", "hidden");
			//$("div#player object").focus();
			$("div#home").get(0).scrollIntoView(true);
			$("a#playColorAnimationButton").click();

			//onclick="toggle('player'); hide(); return false;" onfocus="toggle('player'); hide(); return false;"
			//onclick="toggle('player'); hide(); return false;" onfocus="toggle('player'); hide(); return false;"
			//onclick="toggle('player'); hide(); return false;"
		});

		$("#closevideo-link").click(function (event) {
			event.preventDefault();
			$("div#player").hide();
			$("body").css("overflow", "auto");

			$("#launchvideo-link").focus();
		});

	};

	var setupStageVideoPopup = function () {
		$("div#player-doyen object").attr("tabindex", "0");


		$("#showvideo-link").bind("click", function (event) {
			event.preventDefault();
			$("div#player-doyen").show();
			$("body").css("overflow", "hidden");

			$("div#gestion_stages").get(0).scrollIntoView(true);
		});

		$("#videoclose-link").click(function (event) {
			event.preventDefault();
			$("div#player-doyen").hide();
			$("body").css("overflow", "auto");

			$("#showvideo-link").focus();
		});

	};

	//lance le vidéo de la page d'accueil si le paramètre showvid=true est joint à l'adresse (ex : http://www.logiweb.ca/accueil?showvid=true )
	var showVideoIfQuickPlayEnabled = function () {
		var inHomePage = $("div#home").length > 0;

		if (inHomePage) {
			var quickPlayEnable = window.location.href.indexOf("showvid=true") > 0;

			if (quickPlayEnable) {
				$("div#player").show();
				$("body").css("overflow", "hidden");
				$("div#home").get(0).scrollIntoView(true);
				$("a#playColorAnimationButton").click();
			}
		}
	};

	setupStageVideoPopup();
	setMainPageVideoPopup();
	setMainPageAnimation();
	setDropDownMenu();

	showVideoIfQuickPlayEnabled();
});
