var juvah = function() {

	var VERTICAL_ANIMATION = true;
	var INVERSE_PARALLAX = false;
	
	var GRASS_ANIMATION_FACTOR_X = 20;
	var GRASS_HEIGHT_DELTA = 4;
	var GIRL_ANIMATION_FACTOR_X = 10;
	var GIRL_HEIGHT_DELTA = 8;

	var SCROLL_SPEED = 40;
	var BOTTOM_CONTENT_SCROLL_MARGIN = 10;

	var GRASS_X = 178;
	var GIRL_X = 357;
	var GIRL_Y = -76;

	var DROPDOWN_FADE_DELAY = 1500;
	
	var FADE_TIME = 800;

	var defaultBContentTop = 312;
	var bContentScrollTarget = 55;

	var scrollContainer = "html";

	var hideEvents = {};
	
	var fadingStop = {};
	
	function init() {

		// Chrome hack
		if ($(".websqldatabase").length > 0) {
			scrollContainer = "body";
		}

		addHomepageParallax();
		addContentScrollHandler();
		addCustomDropdowns();
		addHoverboxesFade();

		$('.gallery').each(function() {
			$(this).children("a").lightBox({ fixedNavigation: true });
		});
		$('.watch-photos').click(function() {
			$(this).prev(".gallery").children("a:first").click();
		});

		$(".tabs").tabs();

		// Show vacuumcleaner image for the first two tabs and the banner
		$(".stof .top-panel .hoe a, .stof .top-panel .wat a").click(function() {
			$(".left-side, .right-side").removeClass("stof connection empty");
			$(".left-side, .right-side").addClass("stof");
			$(".right-side .banner").show();
		});

		// Show the connection image for the third tab and the banner
		$(".stof .top-panel .installatie a").click(function() {
			$(".left-side, .right-side").removeClass("stof connection empty");
			$(".left-side, .right-side").addClass("connection");
			$(".right-side .banner").show();
		});

		// Hide photos and banner
		if ($(location).attr('href').match(/#prijs/) != null) {
			$(".left-side, .right-side").removeClass("stof connection empty");
			$(".left-side, .right-side").addClass("empty");
			$(".right-side .banner").hide();
		}
		$(".stof .top-panel .prijs a").click(function() {
			$(".left-side, .right-side").removeClass("stof connection empty");
			$(".left-side, .right-side").addClass("empty");
			$(".right-side .banner").hide();
		});

	}

	function addHomepageParallax() {
		if ($(".parallax").length > 0) {
			$("body").mousemove(function(e) {
				var mouseX = e.clientX;
				var mouseY = e.clientY;

				var windowWidth = $("body").width();
				var grassWidthDelta = $(".grass").width() - $(".grass-wrapper").width();

				var windowHeight = $("body").height();

				var movementDelta = (mouseX - windowWidth / 2.0) / windowWidth;

				var grassLeft = -(grassWidthDelta / 2.0 - movementDelta * grassWidthDelta / GRASS_ANIMATION_FACTOR_X);
				var grassTop = mouseY * 1.0 / windowHeight * GRASS_HEIGHT_DELTA;

				var girlLeft = 357 + movementDelta * grassWidthDelta / GIRL_ANIMATION_FACTOR_X;
				var girlTop = -86 + mouseY * 1.0 / windowHeight * GIRL_HEIGHT_DELTA;

				if (!INVERSE_PARALLAX) {
					grassLeft = (grassLeft + GRASS_X) * -1 - GRASS_X;
					grassTop = -grassTop;
					girlLeft = (girlLeft - GIRL_X) * -1 + GIRL_X;
					girlTop = (girlTop - GIRL_Y) * -1 + GIRL_Y;
				}

				$(".grass").css({ "left": grassLeft + "px" });
				$(".girl").css({ "left": girlLeft + "px" });
				if (VERTICAL_ANIMATION) {
					$(".grass").css({ "top": grassTop + "px" });
					$(".girl").css({ "top": girlTop + "px" });
				}
			});
		}
	}

	function addContentScrollHandler() {
		if ($(".bottom-content").length > 0) {
			$(scrollContainer).scrollTop(0);
			defaultBContentTop = parseInt($(".bottom-content").css("top"));
			bContentScrollTarget = $(".content :first-child").height() + BOTTOM_CONTENT_SCROLL_MARGIN;

			$("body").mousewheel(function(event, delta) {
				var scrollValue = calculateScrollValue();
				setNewScrollValue(scrollValue - (SCROLL_SPEED * delta * 1000 / scrollHeight()));
				event.preventDefault();
			});
		}
	}

	function scrollHeight() {
		return bContentScrollTarget + $(".bottom-content").height() +
             parseInt($(".bottom-content").css("padding-bottom"));
	}

	function calculateScrollValue() {
		var bContentTop = defaultBContentTop - parseInt($(".bottom-content").css("top"));
		var scrollVal = $(scrollContainer).scrollTop();
		return bContentTop + scrollVal;
	}

	function setNewScrollValue(value) {
		var bCPoss = 0;
		var scroll = 0;
		if (value > scrollHeight()) {
			scroll = scrollHeight() - bContentScrollTarget;
			bCPoss = bContentScrollTarget;
		} else if (value < 0) {
			scroll = 0;
			bCPoss = defaultBContentTop;
		} else if (value < defaultBContentTop - bContentScrollTarget) {
			scroll = 0;
			bCPoss = defaultBContentTop - value;
		} else {
			scroll = value - defaultBContentTop + bContentScrollTarget;
			bCPoss = bContentScrollTarget;
		}

		$(scrollContainer).scrollTop(scroll);
		$(".bottom-content").css("top", bCPoss);
	}

	function addCustomDropdowns() {
		$(".custom-dropdown .display").click(function() {
			if ($(this).next("ul").is(":visible")) {
				$(this).next("ul").hide();
			} else {
				$(this).next("ul").show();
			}
		});

		$(".custom-dropdown .display").each(function() {
			var display = $(this);
			var dropdownList = $(this).next("ul");
			var input = dropdownList.next("input");
			var name = input.attr("name");

			// Add show/hide functionality for ul
			dropdownList.mouseout(function() { moveOutOf(dropdownList, name) });
			dropdownList.mouseover(function() { clearTimeout(hideEvents[name]) });

			dropdownList.children().each(function() {
			if (input.attr("value") == $(this).attr("value")) {
					display.html($(this).html());
				}
			});
			

			// Add click functionality for list items
			dropdownList.children().click(function() {
				input.attr("value", $(this).attr("value"));
				display.html($(this).html());
				clearTimeout(hideEvents[name]);
				dropdownList.hide();
				refresh_kleppen();
			});
		});
	}

	function moveOutOf(target, name) {
		hideEvents[name] = setTimeout(function() {
			target.hide();
		},
        DROPDOWN_FADE_DELAY
      );
	}
	
	function addHoverboxesFade() {
	  $(".hover-box .shadow-background").css("opacity", 0);
	  
	  $(".hover-box").mouseover(function () {
	    var hoverBox = $(this);
//	    var fadingBox = $(this).find(".shadow-background");

        fadingStop[hoverBox.attr("id")] = false;

        hoverBox.addClass("hover");

        /*if ($(".ie8").length > 0) {
          if (hoverBox.css("opacity") == 1) {
	          hoverBox.css("opacity", 0);
	      }
          hoverBox.stop(true, false).animate({ "opacity": 1}, FADE_TIME);
          hoverBox.find(".banner").stop(true, false).animate({"opacity": 1 }, FADE_TIME );
//          hoverBox.find(".icon").stop(true, false).animate({ "opacity": 1 }, FADE_TIME );
        } else {*/

          hoverBox.find(".shadow-background").stop(true, false).animate({
              "opacity": 1
            },
            FADE_TIME
          );
          
        /*}*/
//		if (!fadingBox.is(":animated")) {
//		  fadeBox(hoverBox);
//		}
	  });
	  
	  $(".hover-box").mouseout(function () {
	    var hoverBox = $(this);
//	    fadingStop[$(this).attr("id")] = true;
        /*if ($(".ie8").length > 0) {
          hoverBox.stop(true,false).animate({ "opacity": 0 }, FADE_TIME, function () { hoverBox.removeClass("hover"); hoverBox.css("opacity", 1); } );
          hoverBox.find(".banner").stop(true,false).animate({ "opacity": 0 }, FADE_TIME );
//          hoverBox.find(".icon").stop(true,false).animate({ "opacity": 0 }, FADE_TIME );
		} else {*/
          hoverBox.find(".shadow-background").stop(true,false).animate({
              "opacity": 0
            },
            FADE_TIME,
            function () {
              hoverBox.removeClass("hover");
            }
          );
        /*}*/
	  });

	}
	
	function fadeBox(target) {
	  target.addClass("hover");	  
	  target.find(".shadow-background").animate({
	      "opacity": 1
	    },
	    FADE_TIME,
	    function () {
	      target.find(".shadow-background").animate({
	          "opacity": 0
	        },
	        FADE_TIME,
	        function () {
	        
	          if (fadingStop[target.attr("id")]) {
	            target.removeClass("hover");
	            target.stop(true,true);
	          } else {
	            fadeBox(target);
	          }
	          
	        }
	      );
	      
	    }
	  );
	}

	return {
		init: init
	}
} ();

// Init on load
$(function () {
  juvah.init();
 });

