﻿//mouse on/off.
jQuery(document).ready(
	function() {
		var mouseOff = ".mouseOff";
		var mouseOn = ".mouseOn";
		var highlightList = ".highlights";
		var arrowright = ".arrowright";
		var arrowleft = ".arrowleft";
		var inputRotationInterval = '.rotationInterval';

		var t;
		var currentSectionIndex = 0;

		//default value for rotation interval is 5 seconds
		var rotationInterval = $(inputRotationInterval).val();
		rotationInterval = rotationInterval * 1000;

		if (rotationInterval == 0)
			rotationInterval = 5000;

		startRotation();

		//start rotation
		function startRotation() {
			t = setTimeout(function() { switchHighlights() }, rotationInterval);
		}

		//mouse enter
		$(mouseOff).bind("mouseenter",
			function() {
				var element = $(this);

				//highlights on the right
				var rightHighlights = $(highlightList).children('div');
				currentSectionIndex = $(mouseOff).index(element);

				var i = 0;
				rightHighlights.each(
					function() {
						var listItem = $(this);
						if (i == currentSectionIndex) {
							listItem.show();
						}
						else {
							listItem.hide();
						}
						i++;
					}
				);
				$(mouseOn).hide();
				$(mouseOff).show();
				element.hide();
				element.parent().find(mouseOn).show();

				//stopping rotation
				clearTimeout(t);

				//restarting rotation
				startRotation();

			}
		);

		//mouse leave
		//$(mouseOn).bind("mouseleave",
		//    function() {
		//        var element = $(this);
		//        element.hide();
		//        element.parent().find(mouseOff).show();
		//    }
		//);

		//next image
		$(arrowright).click(function() {
			var currentArrow = $(this);
			goToNextImage(currentArrow, true);
			clearTimeout(t);
			return false;
		});

		//previous image
		$(arrowleft).click(function() {
			var currentArrow = $(this);
			goToNextImage(currentArrow, false);
			clearTimeout(t);
			return false;
		});

		//go to next image
		function goToNextImage(currentArrow, isNext) {
			if (currentArrow.parent().parent().children('.highlightItem').length > 1) {
				var nextItem = null;
				if (isNext)
					nextItem = currentArrow.parent().next('.highlightItem');
				else
					nextItem = currentArrow.parent().prev('.highlightItem');

				if (nextItem.length == 0) {

					if (isNext) {
						//go back to first item
						nextItem = currentArrow.parent().parent().children('.highlightItem').eq(0);
					}
					else {
						//go back to last item
						nextItem = currentArrow.parent().parent().children('.highlightItem').last();
					}
				}
				currentArrow.parent().hide();
				nextItem.fadeIn(700);
			}
		}

		function switchHighlights() {

			//current slide container - section with all slides
			var currentSlideContainer = $(highlightList).children('div').eq(currentSectionIndex);

			//current slide displayed on a page
			var currentSlide = currentSlideContainer.children('div:visible');

			var slideArrow = $(currentSlide).find(arrowright);
			var highlightItems = slideArrow.parent().parent().children('.highlightItem');

			if (highlightItems.length > 1) {
				goToNextImage(slideArrow, true);
				startRotation();
			}
		}
	}
);
