/**
 * Animate the big teaser for the real,- homepage
 */

// start on domready...
$(document).ready(mr_t);

// we put everything into one function to not pollute the namespace
function mr_t(){
    // show only on pages with mrT!
    if (!$('#mrt').length) {
        return;
    }

    // toggle automatic scrolling between elements on/off
    var enableAutoForward = true;
    // animation automation pause length in ms
    var autoForwardEvery = 6500;

    // animation automation timer
    var ourTimeoutHandle;

    // find relevant DOM Elements
    var nextBtn = $("#mrt a.vor");

    var prevBtn = $("#mrt a.zurueck");
    var navBtns = $("#mrt div.pagination_detail a");

    // the <li>s containing the different slides
    var slides = $("#mrt .mrt_slide_wrapper li");
    // the big slider in which all slides reside
    var allSlidesContainer = $("#mrt .mrt_slide_wrapper ul");

    // find the slide width - all slides have to be of the same width
    var slidesWidth = $(slides[0]).width();

    // find the one to start with
    var currentSlide = navBtns.index(navBtns.filter(".active"));

    // a safeguard used to prevent multiple animations or events
    // running at the same time
    var currentlySliding = false;

    // actually slide to a specified slide
    // and update the mr_t controls state afterwards
    function slideTo(slideNo) {
        // no multiple slide actions at once!
        if (currentlySliding) {
            return;
        }
        currentlySliding = true;
        // perform + animate sliding
        allSlidesContainer.animate(
            {'left': -slidesWidth * slideNo},
            1300,
            'swing',
            function() {
                // after sliding do

                // update display
                updateControls(slideNo);
                // set new current slide no
                currentSlide = slideNo;
                currentlySliding = false;
            }
        );
    }

    // slide to the next slide, slide to first if we're on the last slide
    function autoForward() {
        var nextSlide = currentSlide + 1;
        if ( nextSlide > slides.length - 1) {
            nextSlide = 0;
        }
        slideTo(nextSlide);
    }

    // update the controls to reflect the new current slide
    function updateControls(setTo) {
        // remove 'active' - class from previously active
        // numbered button

        $(navBtns[currentSlide]).removeClass('active');

        // add active class to new active button
        $(navBtns[setTo]).addClass('active');

        // update previous - button
        if (setTo <= 0) {
            prevBtn.addClass('disabled');
        } else {
            prevBtn.removeClass('disabled');
        }
        // update next - button
        if (setTo >= slides.length - 1) {
            nextBtn.addClass('disabled');
        } else {
            nextBtn.removeClass('disabled');
        }
    }

    // register events on the navigation page buttons 1...n
    navBtns.each(function(z0, elem){
        $(elem).click(function(event){
            event.preventDefault();
            slideTo(z0);
        });
    });

    // register event for next button click
    nextBtn.click(function(event){
        event.preventDefault();
        if (currentlySliding) {
            return;
        }
        // this shouldn't actually happen
        if (currentSlide >= slides.length - 1) {
            return;
        }
        slideTo(currentSlide + 1);
    });

    // register event for previous button click
    prevBtn.click(function(event){
        event.preventDefault();
        if (currentlySliding) {
            return;
        }
        // this shouldn't actually happen
        if (currentSlide <= 0) {
            return;
        }
        slideTo(currentSlide - 1);
    });

    if (enableAutoForward) {
        startAutoPlay();

        // register event for cancelling autoplaying if the user hovers over mt t
        $('#mrt').bind(
            'mousemove',
            stopAutoPlay
        );

        // register event for starting autoplay again if the user leaces mr t
        $('#mrt').bind(
            'mouseleave',
            startAutoPlay
        );
    }

    // register timeout for autoskipping every n seconds
    function startAutoPlay() {
        ourTimeoutHandle = setTimeout(function(){
            autoForward();
            startAutoPlay();
        }, autoForwardEvery);
    }

    // cancel skipping every n seconds
    function stopAutoPlay() {
        clearTimeout(ourTimeoutHandle);
    }

    

}

