"use strict";
/* global $ */

var hero = hero || {};

hero.init = function() {
    hero.pause = false;
    hero.currentSlide=1;
    hero.slideCount = 0;
    hero.transitionInterval = 0;
    hero.steps = 50;
    hero.stepInterval = 25;
    hero.heroContainerId = "";
    hero.heroSlidePrefix = "";
    hero.heroWidth = 0;
    hero.transitionTimer = null;
    hero.swapTimer = null;
};

hero.start = function(transitionIntervalInSecs, heroContainerId, heroSlidePrefix) {
    hero.transitionInterval = transitionIntervalInSecs * 1000;
    hero.heroContainerId = "#" + heroContainerId;
    hero.heroSlidePrefix = "#" + heroSlidePrefix;
    hero.heroWidth = $(hero.heroContainerId).width();
    hero.slideCount = $(hero.heroContainerId).children().size();
    var slideIndex = 1;
    $(hero.heroContainerId).children().each(function() {
        var id = heroSlidePrefix + slideIndex;
        $(this).attr("id", id);
        slideIndex = slideIndex + 1;
    });
    hero.transitionTimer = setTimeout("hero.startTransition()", hero.transitionInterval);
    $("#hero1").css("display", "block");
}


hero.startTransition = function() {
    if (hero.pause) {
        setTimeout("hero.startTransition(" + hero.currentSlide + ")", 250);
    }
    else {
        var nextSlide = hero.currentSlide + 1;
        if (hero.currentSlide == hero.slideCount) {
            nextSlide = 1;
        }
        hero.swapSlides(hero.currentSlide, nextSlide, 1);
        if (hero.transitionInterval > 0) {
            hero.currentSlide = nextSlide;
            hero.transitionTimer = setTimeout("hero.startTransition()", hero.transitionInterval + (hero.steps * hero.stepInterval));
        }
    }
}

hero.swapSlides = function(slideOut, slideIn, step) {
    var slideOutId = hero.heroSlidePrefix + slideOut;
    var slideInId = hero.heroSlidePrefix + slideIn;
    var rightWidth = step * hero.heroWidth / hero.steps;
    $(slideOutId).css("margin-left", -rightWidth);
    $(slideInId).width(rightWidth);
    if (step == 1) {
        $(slideInId).css("margin-left", 0);
        var inDiv = $(hero.heroContainerId).children(slideInId);
        inDiv.detach();
        $(hero.heroContainerId).append(inDiv);
        $(slideInId).css("display", "block");
    }
    if (step == hero.steps) {
        $(slideOutId).css("display", "none");
        hero.swapTimer = null;
    }
    else {
        var nextStep = step + 1;
        hero.swapTimer = setTimeout("hero.swapSlides(" + slideOut + ", " + slideIn + ", " + nextStep + ")", hero.stepInterval);
    }
}

hero.pauseSlides = function() {
    hero.pause = true;
}

hero.playSlides = function() {
    hero.pause = false;
}

hero.gotoSlide = function(slideNo) {
    hero.pause = true;
    hero.currentSlide = slideNo;
    if (hero.swapTimer != null) {
        clearTimeout(hero.swapTimer);
    }
    var slideId = hero.heroSlidePrefix + slideNo;
    $(hero.heroContainerId).children().css("display", "none");
    $(slideId).css("margin-left", 0);
    $(slideId).width(hero.heroWidth);
    $(slideId).css("display", "block");
}
