/**
 * @category    Interface
 * @package     psdup
 * @author	    Bilal Cinarli
 * @copyright	2011 Bilal Cinarli
 * @license     default
 * @version
 * @filesource
 * @link        http://bcinarli.com
 * @see
 * @since
 **/

 $(function(){
    $("#tagline").ntRotator({ transition: 'hslide', thumbnailCarousel: true });
 });

 (function($){
    $.fn.ntRotator = function(options){
        var opts = $.extend({}, $.fn.ntRotator.defaults, options);

        return this.each(function(){
            var $container = $(this);
			var o = opts;

            var $pages = $container.find(o.pages);
            var $navigation = $container.find(o.navigation);
            var $pageNavigations = $navigation.find('a');
            var currentClass = o.currentClass;
            var currentClassName = currentClass.substr(1);
            var nextButton = o.nextButton;
            var previousButton = o.previousButton;
            var transition = o.transition;
            var transitionSpeed = o.transitionSpeed;

            var $nextPreButtons = $container.find(nextButton + ', ' + previousButton);

            var autoSlide = o.autoSlide;
            var slideShowDelay = o.slideShowDelay;

            var thumbnailCarousel = o.thumbnailCarousel;

            var thePage, timer, currentSlide, nextSlide, $e, currentIndex, nextIndex, animating = false;

            $pages.siblings(o.pages).hide().first().show().addClass(currentClassName);
            $pageNavigations.first().addClass(currentClassName).parent().addClass(currentClassName).parent().hide();
            currentSlide = $pages.filter(currentClass);

            var navLength = $pageNavigations.length;

            if(navLength > 1){
                $navigation.show();
                if(autoSlide == true){
                    startSlideShow();

                    $pages.hover(
                        function(){
                            stopSlideShow();
                        },
                        function(){
                            startSlideShow();
                        }
                    );
                }
            }

            if(thumbnailCarousel === true)
            {
                var stepLength, maxLeft, nextLeft = $navigation.position().left, thumbsDim, first = true, last = false, step = 8;
                var carouselWidth = $navigation.width();
                var carouselElems = 0;
                var carouselElemWidth = 102;
                //var carouselElemWidth = $navigation.find('li').width();

                carouselElems = carouselElemWidth * $navigation.find('li').length;

                if(carouselElems > carouselWidth)
                {
                    $navigation.wrap('<div id="slide-nav-carousel"><div id="slide-nav-thumbs"></div><a id="carousel-prev" class="carousel-nav">Prev</a><a id="carousel-next" class="carousel-nav">Next</a></div>');
                    $navigation.width(carouselElems);

                    maxLeft = carouselWidth - carouselElems;
                    stepLength = carouselElemWidth * step;

                    $("#carousel-next").click(function(){
                        moveNext();
                    });

                    $("#carousel-prev").click(function(){
                        movePrev();
                    });

                    function movePrev(){
                        nextLeft += stepLength;
                        if(nextLeft > 0)
                        {
                            nextLeft = 0;
                        }

                        move(nextLeft);
                    }

                    function moveNext(){
                        nextLeft -= stepLength;
                        if(nextLeft < maxLeft)
                        {
                            nextLeft = maxLeft;
                        }

                        move(nextLeft);
                    }

                    function moveCarousel()
                    {
                        var currentThumb = $navigation.find('li.current').position().left;
                        var currentVisible = -nextLeft + carouselWidth - carouselElemWidth;

                        if(currentThumb < -nextLeft)
                        {
                            nextLeft = currentThumb;
                            if(nextLeft > 0)
                            {
                                nextLeft = 0;
                            }
                            move(nextLeft);
                        }
                        else if(currentThumb > currentVisible)
                        {
                            nextLeft -= currentThumb;
                            if(nextLeft < maxLeft)
                            {
                                nextLeft = maxLeft;
                            }
                            move(nextLeft);
                        }
                    }

                    function move(nextLeft)
                    {
                        $navigation.animate({ 'left': nextLeft });
                    }
                }
            }

            // give tab-key navigation support
            $pageNavigations.click(function(){
               this.focus();
               return false;
            });

            $pageNavigations.focus(function(){
                $e = $(this);
                if(!$e.hasClass(currentClassName)){
                    animatePage($e);
                }
                resetSlideShow();
            });

            $nextPreButtons.click(function(){
                if($(this).is(nextButton)){
                    navigateNext('next');

                }
                else{
                    navigateNext('prev');
                }
                resetSlideShow();
                return false;
            });

            function navigateNext(direction){
                currentIndex = $pageNavigations.filter(currentClass).parent().index();
                if(direction == 'prev'){
                    nextIndex = currentIndex - 1;
                    if(nextIndex < 0){
                        nextIndex = navLength - 1;
                    }
                }
                else{
                    nextIndex = currentIndex + 1;
                    if(nextIndex >= navLength){
                        nextIndex = 0;
                    }
                }

                $e = $navigation.find('li:eq(' + nextIndex + ') a');

                animatePage($e);
            }

            function slideshow(){
                navigateNext('next');
            }

            function stopSlideShow(){
                clearInterval(timer);
            }

            function startSlideShow(){
                timer = setInterval(slideshow, slideShowDelay);
            }

            function resetSlideShow(){
                if(autoSlide == true){
                    stopSlideShow();
                    startSlideShow();
                }
            }

            // this function do the all job for showing next slide with or without transition effect
            function animatePage($el){
                if(animating == false){
                    animating = true;
                    nextSlide = $($el.attr('href'));
                    currentSlide = $pages.filter(currentClass);

                    currentSlide.removeClass(currentClassName);
                    nextSlide.addClass(currentClassName);
                    $pageNavigations.removeClass(currentClassName).parent().removeClass(currentClassName);
                    $el.addClass(currentClassName).parent().addClass(currentClassName);

                    if(transition == 'fade'){
                        currentSlide.fadeOut(transitionSpeed);
                        nextSlide.fadeIn(transitionSpeed, function(){
                            animating = false;
                        });
                    }

                    else if(transition == 'vslide'){
                        currentSlide.slideUp(transitionSpeed);
                        nextSlide.slideDown(transitionSpeed, function(){
                            animating = false;
                        });
                    }

                    else if(transition == 'hslide'){
                        var c = $pages.index(currentSlide);
                        var n = $pages.index(nextSlide);

                        var containerW = currentSlide.parent().width();

                        var cM, nM;

                        // slide from right to left
                        if(n > c){
                            cM = -containerW;
                            nM = containerW;
                        }

                        // slide from left to right
                        else{
                           cM = containerW;
                           nM = -containerW;
                        }

                        currentSlide.animate({ left: cM }, transitionSpeed, 'linear');
                        nextSlide.css({ left: nM }).show().animate({ left: 0 }, transitionSpeed, 'linear', function(){
                            currentSlide.hide().css({ left: 0 });
                            animating = false;
                        });
                    }

                    else{
                        currentSlide.hide();
                        nextSlide.show().queue(function(){
                            animating = false;
                            $(this).dequeue();
                        });
                    }

                    if(thumbnailCarousel == true)
                    {
                        moveCarousel();
                    }
                }
            }
        });
    },

   	$.fn.ntRotator.defaults = {
		pages: '.slide',
        navigation: '.slide-nav',
        currentClass: '.current',
        nextButton: '.next',
        previousButton: '.prev',
        transition: 'none', // fade|hslide|vslide|none
        transitionSpeed: 600, // slow|fast|1000|5000 etc.
        autoSlide: true,
        slideShowDelay: 6000,
        thumbnailCarousel: false
	};
})(jQuery);