﻿/*
     Go Republic - Waves
     http://www.webadvanced.com
     @author : Reza Assar   
*/

$(document).ready(function () {

    var slideshow = (function () {

        var that = this;

        var options = {
            idName: '#slideshow',
            slides: '#slideshow > div',
            delay: 10000,
            fadeTime: 600,
            easing: "easeInOutQuint",
            speed: 2000,
            waveLength: 515,
            waveSpeed: 4000,
            startingSlide: 0
        }

        var wrapper = $(options.idName),
            slides = $(options.slides),
            slideImages = $('#slideshow-images').find('> li'),
            currentSlide = options.startingSlide,
            nextSlide,
            isAnimating = false,
            slideButtons,
            waves;

        that.init = function () {
            waves = document.createElement('div');
            waves.setAttribute('id', 'waves');
            wrapper.append(waves);
            waves = $(waves);
            generatePagination();

            slideImages.eq(currentSlide).fadeIn(800, function () {
                $('.spotlightImg', this).animate({ 'bottom': '0px' }, 1000);
                slides.eq(currentSlide).fadeIn(1200);
            });

            nextSlide = (currentSlide < slides.length - 1) ? currentSlide + 1 : 0;

            var imgs;

            $('#slideshow-images li').each(function () {
                imgs = $(this).find('img');
                imgs.eq(0).addClass('bg');
                if (imgs.length > 1) {
                    imgs.eq(1).addClass('spotlightImg');
                }
            });

            run();
        }

        function run() {
            slideInterval = setInterval(function () {
                showContent();
            }, options.delay);
        }

        function showContent() {
            isAnimating = true;
            waves.animate({ backgroundPosition: options.waveLength * -nextSlide + 'px 0px' }, options.waveSpeed, options.easing);
            slideButtons.removeClass('on').eq(nextSlide).addClass('on');
            slides.eq(currentSlide).fadeOut(1000);

            $('.spotlightImg').eq(currentSlide).animate({ 'bottom': '-200px' }, 1000);
            slideImages.eq(currentSlide).fadeOut(1000);

            setTimeout(function () {

                slideImages.eq(nextSlide).fadeIn(1000, function () {
                    $('.spotlightImg', this).animate({ 'bottom': '0px' }, 1000);
                    slides.eq(nextSlide).fadeIn(1000);

                    currentSlide = nextSlide;
                    nextSlide = (currentSlide < slides.length - 1) ? currentSlide + 1 : 0;
                    isAnimating = false;
                });

            }, options.waveSpeed / 2);
        }

        function generatePagination() {

            var pagString = '<ul id="slide-pagination">',
                x;

            for (x = 0; x < slides.length; x++) {
                pagString += '<li><a href="#"></a></li>'
            };

            pagString += "</ul>";

            $('#slideshow').append(pagString);

            $('#slide-pagination a').eq(0).addClass('on');

            $('#slide-pagination a').click(function () {
                if (!isAnimating) {
                    clearInterval(slideInterval);
                    nextSlide = $(this).index('#slide-pagination a');
                    showContent();
                    run();
                }
            });

            var swipeOptions = {
                swipeLeft: onSwipeLeft,
                swipeRight: onSwipeRight,
                threshold: 120
            }

            $('#slideshow').swipe(swipeOptions);

            function onSwipeRight() {
                if (!isAnimating) {
                    clearInterval(slideInterval);
                    nextSlide = (currentSlide > 0) ? currentSlide - 1 : slides.length - 1;
                    showContent();
                    run();
                }
            }

            function onSwipeLeft() {
                if (!isAnimating) {
                    clearInterval(slideInterval);
                    nextSlide = (currentSlide < slides.length - 1) ? currentSlide + 1 : 0;
                    showContent();
                    run();
                }
            }

            slideButtons = $('#slide-pagination a');
        }

        return that;

    })();

    slideshow.init();

});




