﻿var carousel = {   
    runCarousel: false,
    fadeSpeed: "fast",
    showPlayControls: true,
    autoPlay: true,
    cycleOutsideContent: false,
    outsideContentClass: "",                
    currentSlideIndex: 0,
    slidesCount: 0,
    playPauseContainer: ".playPauseControls",
    btnPlay: ".carouselPlay",
    btnPause: ".carouselPause",        
    timeoutId: null,
    inAnimation: false,        
         
    initialize: function(options) {
        this.runCarousel = options.runCarousel;
        this.fadeSpeed = options.fadeSpeed;
        this.showPlayControls = options.showPlayControls;
        this.autoPlay = options.autoPlay;
        this.cycleOutsideContent = options.cycleOutsideContent;
        this.outsideContentClass = options.outsideContentClass;
    
        if (!this.runCarousel)
            return;                 
        if (this.fadeSpeed.length == 0)
            this.fadeSpeed = "fast";    
        if (this.cycleOutsideContent && this.outsideContentClass.length == 0)
            this.cycleOutsideContent = false;
            
        $(".carouselImages .imageLink").eq(0).addClass("selected");
        $(".carouselCaptions .caption").eq(0).addClass("selected");
        $(".carouselImages .imageLink").hide();
        $(".selected").show();            
        this.slidesCount = $(".carouselImages .imageLink").length;

        $(".carouselCaptions .caption span").click(function() {     
            carousel.handleSlideClick(this);
        });
        
        if (this.showPlayControls) 
            $(this.playPauseContainer).show();            
        
        if (this.autoPlay)  
            this.play();
        else
            this.pause();             
    },
    handleSlideClick: function(sender) {
        this.pause();
        this.changeSlideForCaption(sender);
    },
    changeSlideForCaption: function(sender) {        
        if (this.inAnimation)
            return;
        
        this.inAnimation = true;                   
        this.currentSlideIndex = $(sender).parent(".caption").prevAll().length;
        
        $(sender).parents(".carouselCaptions").children(".selected").removeClass("selected");
        $(sender).parent(".caption").addClass("selected");
        
        if (this.cycleOutsideContent) {                                
            var contentToShow = $(this.outsideContentClass).eq(this.currentSlideIndex);
            if (contentToShow.length > 0) {
                $(this.outsideContentClass + ":visible").fadeOut(this.fadeSpeed, function() {
                    contentToShow.fadeIn(carousel.fadeSpeed);
                });
            }
        }
                    
        $(".carouselImages").children(".selected").fadeOut(this.fadeSpeed, function() {
            $(this).removeClass("selected");
            $(this).parents(".carouselImages").children("div.imageLink").eq(carousel.currentSlideIndex).fadeIn(carousel.fadeSpeed, function() {
                $(this).addClass("selected");
                carousel.inAnimation = false;
            });
        });            
    },
    changeSlide: function(slideIndex) {
        this.currentSlideIndex = slideIndex;
        var slideCaption = $(".carouselCaptions .caption span").eq(slideIndex);
        this.changeSlideForCaption(slideCaption);
        
        if (this.autoPlay)
            this.setupNextSlideTimeout();
    },
    setupNextSlideTimeout: function() {
        var delay = $(".carouselCaptions .caption").eq(this.currentSlideIndex).children("input[type=hidden]").val();                                    
        var nextSlide = (this.currentSlideIndex < this.slidesCount - 1) ? this.currentSlideIndex + 1 : 0;            
        var timeDelay = parseInt(delay);
        this.timeoutId = window.setTimeout(function() {carousel.changeSlide(nextSlide);}, timeDelay);
    },
    play: function() {
        this.autoPlay = true;
        $(this.btnPlay).hide();            
        if (this.showPlayControls)
            $(this.btnPause).show();                
        this.setupNextSlideTimeout();
        return false;
    },
    pause: function() {
        window.clearTimeout(this.timeoutId);
        this.timeoutId = null;            
        this.autoPlay = false;
        $(this.btnPause).hide();
        if (this.showPlayControls)
            $(this.btnPlay).show();                
        return false;
    }        
}
