/*******************************************************************************
jquery.manoSlide
Copyright (c) 2010. Mattias Norell
email: mattias@mattiasnorell.com
site: http://blog.mattiasnorell.com/

Licences: MIT, GPL
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
******************************************************************************/

/*
* Name:jquery.manoSlide
* Version: 1.0
*/

(function ($) {
    $.manoSlideFunction = {
        plugin: "manoSlide",
        author: "Mattias Norell",
        version: "1.0",
        defaults: {
            startIndex: 0,
            endIndex: -1,
            speed: 5000,
            autoPlay: false,
            tabActiveClass: "active",
            tabContainer: "nav-tab",
            tabElement: "li",
            prevClass: "back",
            nextClass: "forward",
            slideClassName: "item",
            controls: false,
            tabs: false,
            transition: "fade",
            transitionSpeed: 1000,
            adjustIndex: 0,
            exclude: "",
            infinitLoop: false
        },
        data: {
            currentSlide: 0,
            totalSlides: 0,
            autoPlayTimeout: null
        },
        init: function (options, data) {
            if (data == undefined) data = {};
            var $slideshow = this;
            var slideshow = this;
            slideshow.opt = {};
            slideshow.data = {};

            $.extend(slideshow.opt, $.manoSlideFunction.defaults, options)
            $.extend(slideshow.data, $.manoSlideFunction.data, data);

            // Set the first item
            var startItem = parseFloat(slideshow.opt.startIndex) + parseFloat(slideshow.opt.adjustIndex);
            $slideshow.find("." + slideshow.opt.slideClassName).hide().eq(startItem).show();


            // Set currentSlide to startIndex
            slideshow.data.currentSlide = parseFloat(slideshow.opt.startIndex);

            if (slideshow.opt.endIndex != -1) {
                var startEndIndexes = parseInt(slideshow.opt.startIndex) + parseInt(slideshow.opt.endIndex);
                slideshow.data.totalSlides = startEndIndexes + slideshow.opt.adjustIndex;
            } else {
                var slideClassName = "." + slideshow.opt.slideClassName;
                slideshow.data.totalSlides = slideshow.find(slideClassName).length - 1;
            }

            // If forward/backward-buttens are used
            if (slideshow.opt.controls) {

                // Check if the previousbutton-variable refer to a class or id
                var prevClass = "#" + slideshow.opt.prevClass;
                if ($(prevClass).length > 0) {
                    prevClass = "#" + slideshow.opt.prevClass;
                } else {
                    prevClass = "." + slideshow.opt.prevClass;
                }


                $slideshow.find(prevClass).bind({ click: function () {
                    slideshow.data.currentSlide--;
                    var current = slideshow.data.currentSlide;
                    var nrOfSlides = slideshow.data.totalSlides;

                    if (current < 0) {
                        if (slideshow.opt.infinitLoop == true) {
                            slideshow.data.currentSlide = nrOfSlides;
                        } else {
                            slideshow.data.currentSlide = 0;
                        }
                    }

                    $slideshow.changeSlide();
                    return false;
                }
                });

                // Check if the nextbutton-variable refer to a class or id
                var nextClass = "#" + slideshow.opt.nextClass;
                if ($(nextClass).length > 0) {
                    nextClass = "#" + slideshow.opt.nextClass;
                } else {
                    nextClass = "." + slideshow.opt.nextClass;
                }

                $slideshow.find(nextClass).bind({ click: function () {
                    slideshow.data.currentSlide++;
                    var current = slideshow.data.currentSlide;
                    var nrOfSlides = slideshow.data.totalSlides;
                    if (current > nrOfSlides) {

                        if (slideshow.opt.infinitLoop == true) {
                            slideshow.data.currentSlide = 0;
                        } else {
                            slideshow.data.currentSlide = nrOfSlides;
                        }
                    }

                    if ($slideshow.opt.autoPlay == true) {
                        clearTimeout(slideshow.data.autoPlayTimeout);
                    }

                    $slideshow.changeSlide();

                    

                    return false;
                }
                });
            }

            // If navigationtabs are used
            if (slideshow.opt.tabs) {

                var tabContainer = "#" + slideshow.opt.tabContainer;
                var tabElement = slideshow.opt.tabElement;

                if ($(tabContainer).length > 0) {
                    tabContainer = "#" + slideshow.opt.tabContainer;
                } else {
                    tabContainer = "." + slideshow.opt.tabContainer;
                }

                slideshow.opt.tabContainer = tabContainer;

                $slideshow.find(tabContainer + ' ' + tabElement).eq(slideshow.opt.startIndex).addClass(slideshow.opt.tabActiveClass);

                $slideshow.find(tabContainer + ' ' + tabElement).not(slideshow.opt.exclude).bind({ click: function () {
                    if ($slideshow.opt.autoPlay == true) {
                        clearTimeout(slideshow.data.autoPlayTimeout);
                    }

                    if (slideshow.data.currentSlide != $(this).index()) {
                        slideshow.data.currentSlide = $(this).index();
                        $slideshow.changeSlide();
                    }

                    return false;
                }
                });
            }


            if ($slideshow.opt.autoPlay == true) {
                $slideshow.autoPlayFunction();
            }

            return false;
        }, autoPlayFunction: function () {

            var $slideshow = this;
            var slideshow = this;

            slideshow.data.autoPlayTimeout = setTimeout(function () {
                
                slideshow.data.currentSlide++;
                
                var current = slideshow.data.currentSlide;
                var nrOfSlides = slideshow.data.totalSlides;

                if (current > nrOfSlides) {
                    slideshow.data.currentSlide = parseInt(slideshow.opt.startIndex);
                }

                $slideshow.changeSlide();
                $slideshow.autoPlayFunction();
            }, parseInt($slideshow.opt.speed));

        },
        changeSlide: function () {
            var $slideshow = this;
            var slideshow = this;
            var slideName = "." + slideshow.opt.slideClassName;
            var current = parseFloat(slideshow.data.currentSlide) + parseFloat(slideshow.opt.adjustIndex);
            var tabContainer = slideshow.opt.tabContainer;
            var tabElement = slideshow.opt.tabElement;
            var tabActiveClass = slideshow.opt.tabActiveClass;
            var transition = slideshow.opt.transition;
            var transitionSpeed = slideshow.opt.transitionSpeed;


            //alert(parseFloat(slideshow.data.currentSlide) + " :: " + parseFloat(slideshow.opt.adjustIndex) + " :: " + current);

            switch (transition) {
                case "fade":
                    $slideshow.find(slideName).fadeOut(transitionSpeed).eq(current).fadeIn(transitionSpeed);
                    break;
                case "direct":
                    $slideshow.find(slideName).hide().eq(current).show();
                    break;
                case "slide":
                    $slideshow.find(slideName).slideUp().eq(current).slideDown();
                    break;
                case "vertical":

                    break;
                case "horizontal":

                    break;
                default:
                    $slideshow.find(slideName).fadeOut(transitionSpeed).eq(current).fadeIn(transitionSpeed);
            }


            $(tabContainer).find(tabElement).removeClass(tabActiveClass).eq(slideshow.data.currentSlide).addClass(tabActiveClass);

        }
    };

    $.fn.manoSlide = $.manoSlideFunction.init;
    $.fn.changeSlide = $.manoSlideFunction.changeSlide;
    $.fn.autoPlayFunction = $.manoSlideFunction.autoPlayFunction;
})(jQuery);
