﻿var infCarousel = {
    carousels: {},

    setup: function(config) {
        jQuery(document).ready(function($) {
            config.$carousel = $('#' + config.carouselId)
            config.$items = config.$carousel.find('.' + config.itemClass)
            config.$parent = config.$carousel.parent().eq(0);
            config.$prevButton = config.prevButtonId ? config.$carousel.find('#' + config.prevButtonId) : null
            config.$nextButton = config.nextButtonId ? config.$carousel.find('#' + config.nextButtonId) : null
            infCarousel.carousels[config.carouselId] = config;
            config.currentIndex = 0;
            config.itemCount = config.$items.length;
            config.origLeft = config.$carousel.css('left');
            config.origTop = config.$carousel.css('top');
            infCarousel.fadeButtons(config);
            if (!config.currentDirection) {
                config.currentDirection = (config.orientation == 'horizontal') ? 'right' : 'down';
            }
            /*
            if (config.$prevButton && config.$nextButton) {
            config.$prevButton.bind('onclick', function(e) { //assign nav button event handlers
            alert('a');
            infCarousel.step(config.carouselId, (config.orientation == 'horizontal') ? 'left' : 'up', false);
            })
            config.$nextButton.bind('onclick', function(e) { //assign nav button event handlers
            alert('b'); infCarousel.step(config.carouselId, (config.orientation == 'horizontal') ? 'right' : 'down', false);
            })
            }
            */
            if (config.autoStep && config.autoStep.enable) {


                var $carouselParts = config.$carousel.children();
                $carouselParts.hover(function() { //onMouseover
                    infCarousel.stopAutoStep(config.carouselId)
                }, function() { //onMouseout
                    infCarousel.startAutoStep(config.carouselId);
                })

                infCarousel.startAutoStep(config.carouselId);
            }
            /*
            x
            config.$carousel.parent().eq(0).prepend(config.$ghost);
            config.,$items.eq(0).clone().prependTo(config.$ghost);
            config.$carousel.css({ "float": "left" })
            config.$ghost.css({ "float": "left" })
            infCarousel.init($, config)*/
        })
    },

    prev: function(carouselId) {
        var config = infCarousel.carousels[carouselId];
        infCarousel.step(carouselId, (config.orientation == 'horizontal') ? 'left' : 'up', false);
    },

    next: function(carouselId) {
        var config = infCarousel.carousels[carouselId];
        infCarousel.step(carouselId, (config.orientation == 'horizontal') ? 'right' : 'down', false);
    },

    step: function(carouselId, direction, isAuto) {
        var config = infCarousel.carousels[carouselId];
        if (typeof config == "undefined") { return; }
        if (!
            (
                (config.orientation == 'horizontal' && (direction == 'left' || direction == 'right'))
                ||
                (config.orientation == 'vertical' && (direction == 'up' || direction == 'down'))
             )
           ) {
            return;
        }
        var $currentpanel = jQuery(config.$items.eq(config.currentIndex));

        var offset =
            (typeof $currentpanel.get(0) != 'undefined') ?
            (
            (config.orientation == 'horizontal') ?
                ($currentpanel.css('marginRight') == 'auto' ? 0 : parseInt($currentpanel.css('marginRight'))) + parseInt($currentpanel.get(0).offsetWidth || $currentpanel.css('width')) :
                ($currentpanel.css('marginBottom') == 'auto' ? 0 : parseInt($currentpanel.css('marginBottom'))) + parseInt($currentpanel.get(0).offsetHeight || $currentpanel.css('height'))
            ) : 0;

        var indexFactor = +1;
        config.currentDirection = direction;
        switch (direction) {
            case 'left':
                if (config.currentIndex == 0) {
                    return false;
                }
                offset = '+=' + offset;
                indexFactor = -1;
                break;
            case 'right':
                if (config.currentIndex + 1 == config.itemCount) {
                    return false;
                }
                offset = '-=' + offset;
                indexFactor = +1;
                break;
            case 'up':
                if (config.currentIndex == 0) {
                    return false;
                }
                offset = '+=' + offset;
                indexFactor = -1;
                break;
            case 'down':
                if (config.currentIndex + 1 == config.itemCount) {
                    return false;
                }
                offset = '-=' + offset;
                indexFactor = +1;
                break;

        }
        if (isAuto == true) {
            infCarousel.stopAutoStep(config.carouselId);
        }

        if (config.orientation == 'horizontal') {
            config.$carousel.animate({ left: offset }, config.speed ? config.speed : 300);
        }
        else {
            config.$carousel.animate({ top: offset }, config.speed ? config.speed : 300);
        }
        config.currentIndex += indexFactor;
        infCarousel.fadeButtons(config);
        config.currentDirection = direction;

        if (isAuto == true) {
            infCarousel.startAutoStep(config.carouselId);
        }

        return true;
        /*
        if (config.currentIndex == config.itemCount - 1) {
        var cl = config.$items.eq(0);
        cl.appendTo(config.$carousel);
        cl.appendTo(config.$items);
        }
        */

    },

    fadeButtons: function(config) {
        if (config.$prevButton) {
            var fade = 0.1;
            config.$prevButton.fadeTo('fast', config.currentIndex == 0 ? fade : 1)
            config.$nextButton.fadeTo('fast', config.currentIndex + 1 == config.itemCount ? fade : 1)
        }
    },

    startAutoStep: function(carouselId) {
        var config = infCarousel.carousels[carouselId];
        if (typeof config == "undefined") { return; }
        config.stepTimer = setInterval(function() { infCarousel.autoRotate(config) }, config.autoStep.wait + config.speed)
    },

    stopAutoStep: function(carouselId) {
        var config = infCarousel.carousels[carouselId];
        if (typeof config == "undefined") { return; }
        clearTimeout(config.stepTimer);
    },

    autoRotate: function(config) {

        if (!config.currentDirection) {
            config.currentDirection = (config.orientation == 'horizontal') ? 'right' : 'down';
        }
        if (infCarousel.step(config.carouselId, config.currentDirection, true) != true) {
            infCarousel.stopAutoStep(config.carouselId);

            config.stepTimer = setTimeout(function() {
            infCarousel.stopAutoStep(config.carouselId);
                
                if (config.autoStep.reverse) {
                    if (config.orientation == 'horizontal') {
                        config.currentDirection = (config.currentDirection == 'left') ? 'right' : 'left';
                    }
                    else {
                        config.currentDirection = (config.currentDirection == 'up') ? 'down' : 'up';
                    }
                    infCarousel.step(config.carouselId, config.currentDirection, true);
                } else {
                    if (config.orientation == 'horizontal') {
                        config.$carousel.css('left', config.origLeft);
                    } else {
                        config.$carousel.css('top', config.origTop);
                    }
                    config.currentIndex = 0;
                    
                }
        }, (config.autoStep.wait ? config.autoStep.wait : 4000) + (config.speed ? config.speed : 400));
            
        }
    },

    init: function($, config) {

    }
}
