/* Author: Nicolai Heilbuth (nicolai@heilbuth.net) */

var numberOfGalleryImages = 0;
var animationRunning = false;

$(document).ready(function () {
    fixTopmenuHeights();

    setupEmailTextbox();

    setupImageGallery();
});

function setupImageGallery() {
    startLoadingImages();
    setupNavArrows();
}

function setupNavArrows() {
    $("#container > .gallery.arrow").click(function () {
        var direction = "left";
        if ($(this).hasClass("right")) {
            direction = "right";
        }

        if (direction == "right") {
            moveToNextImage();
        }
        else {
            moveToPrevImage();
        }
    });

    $(".imageWrapper").click(moveToNextImage);
}

function moveToNextImage() {
    var numberOfImages = $(".imageWrapper").size();
    var currentIndex = $(".imageWrapper.active").index(".imageWrapper");
    var nextIndex = currentIndex + 1;

    if (nextIndex == numberOfGalleryImages) {
        nextIndex = 0;
    }

    changeToImage(nextIndex);
}
function moveToPrevImage() {
    var numberOfImages = $(".imageWrapper").size();
    var currentIndex = $(".imageWrapper.active").index(".imageWrapper");
    var nextIndex = currentIndex - 1;

    if (nextIndex == -1) {
        nextIndex = numberOfGalleryImages - 1;
    }

    changeToImage(nextIndex);
}

function changeToImage(nextIndex) {
    if (animationRunning === false) {
        animationRunning = true;
        var currentImage = $(".imageWrapper.active");
        var nextImage = $(".imageWrapper:eq(" + nextIndex + ")");

        currentImage
            .animate({ opacity: 0 }, 500)
            .removeClass("active");

        nextImage
            .animate({ opacity: 1 }, 500, animationEnded)
            .addClass("active");
    }
}

function animationEnded() {
    animationRunning = false;
}

function startLoadingImages() {
    if ($("#container").hasClass("gallerypage")) {
        numberOfGalleryImages = $(".galleryImage").size();
    setGalleryHeights();

        loadImage(0);
    }
}
function loadImage(index) {
    var image = $(".imageWrapper:eq(" + index + ") .galleryImage");
    var src = image.attr("data-src");

    if ($("body").hasClass("ie")) {
        src += "?seed=" + (Math.random() * 100000);
    }

    image.load(imageLoaded(index));
    image.attr("src", src);
}

function imageLoaded(index) {
  $(".imageWrapper:eq(" + index + ")").removeClass("loader");
 
  if (index < numberOfGalleryImages) {
    if (index == 0) {
      $(document).oneTime(1000, "delay", function() {
        loadImage(index + 1);
      });
    }
    else {
      loadImage(index + 1);
    }
  }
}

function setGalleryHeights() {
    var maxHeight = 0;

    $(".imageWrapper .galleryImage").each(function () {
        var height = parseInt($(this).height());
        if (height > maxHeight) {
            maxHeight = height;
        }
    });

    $("#main").css("height", maxHeight + "px");
}

function setupEmailTextbox() {
    $("input[type=text], input[type=email]").focus(function () {
        var defaultValue = $(this).attr("data-defaultvalue");

        if ($(this).val() == defaultValue) {
            $(this).val("");
        }
    });

    $("input[type=text], input[type=email]").blur(function () {
        var defaultValue = $(this).attr("data-defaultvalue");

        if ($(this).val() == "") {
            $(this).val(defaultValue);
        }
    });
}

function fixTopmenuHeights() {
    var maxHeight = 0;

    $(".menuItem .subMenu").each(function () {
        var height = parseInt($(this).height());
        if (height > maxHeight) {
            maxHeight = height;
        }
    });

    $(".menuItem .subMenu").each(function () {
        $(this).css("height", maxHeight + "px");
    });
}























