"use strict";
var fluid_1_2 = fluid || {};
var prepUSA = prepUSA || {};

(function ($, fluid) {

    /*
    *----------Rotating Slideshow
    */

	prepUSA.slideShow = function (container, options) {

        var that, totalImages, currentImage, currentDisp, 
            initialize, renderMarkup, bindEvents, getAjaxData, loadImage;

        that = fluid.initView("prepUSA.slideShow", container, options);

        initialize = function () {
            renderMarkup();
            bindEvents();
            currentDisp =1;
            if(that.options.ajaxURL){
                getAjaxData();
            }
        };//end:function

        bindEvents = function () {
            that.events.onDataRecieved.addListener(that.showPlay);
            if(that.options.autoPlay==true){
                that.options.play=true;
                that.locate('playButton').fadeOut(5000);
            }
            that.locate('playButton').toggle(
                function(){
                    that.locate('playButton').fadeOut(300,function(){
                        that.options.play=true;
                        that.locate('playButton').addClass('pause').show();
                        that.showPlay();
                    });
                },
                function (){
                    that.locate('playButton').removeClass('pause');
                    that.options.play=false;
                }
            );
        };//end:function

        getAjaxData = function (){
            $.ajax({
              dataType: 'json',
              url: that.options.ajaxURL,
              success: function(data) {
                that.options.images = data;
                totalImages = that.options.images.length;
                currentImage = 0;
                that.events.onDataRecieved.fire();
              }
            });
        };//end:function

        renderMarkup = function (){
            $(that.container).html('<div class="fluid-slide-show"><div class="fluid-slide-show-message"></div><img class="fluid-slideshow-image1"/><img class="fluid-slideshow-image2"/></div>');
            $(that.container).find('.fluid-slide-show')
                .css('height',that.options.height + 'px')
                .css('width',that.options.width + 'px');
        };//end:function

        that.showPlay = function () {
            var slideshowTimeout = setTimeout( 
                function(){
                    if (that.options.play == true || currentImage==0) {
                        loadImage(currentImage);
                        currentImage = currentImage + 1;
                        if (currentImage == totalImages) {
                            currentImage = 0;
                        }
                    }
                },
                that.options.showTime
            );
        };//end:function

        loadImage = function (imageNum) {
            var imagePre = new Image(); 
            imagePre.onload = function(){
                $('.fluid-slideshow-image2').attr('src',that.options.directory + that.options.images[imageNum]);
                $('.fluid-slideshow-image1').fadeOut(700,function(){
                    $('.fluid-slideshow-image1').attr('src',that.options.directory + that.options.images[imageNum]);
                    $('.fluid-slideshow-image1').show();
                });
                if(that.options.play == true){
                    that.showPlay();
                }
            };
            imagePre.src = that.options.directory + that.options.images[imageNum];
        };//end:function

        initialize();
        return that; 

    };//function:end

    fluid.defaults("prepUSA.slideShow", {
        autoPlay:false,
        play:false,
        directory : null,
        width:445,
        height:300,
        images : null,
        ajaxURL : null,
        musicURL : null,
        showTime : 3000, 
        selectors: {
            image1:'.fluid-slideshow-image1',
            image2:'.fluid-slideshow-image2',
            playButton:'.fluid-slide-show-message'
        },
        events: {
            onDataRecieved:null
        }
    });

})(jQuery,fluid_1_2)

