var QuickLook = function() {return {
  options: {
    listOfImages: "#quicklook .container",
    caption: "#quicklook_caption_text",
    counter: null,
    interval: 8000
  },
  currentImage: 0,
  start: function(base, firstCall) {
    //Set the opacity of all images to 0
    $('ul.ticker_window li').css({opacity: 0.0});
    
    //Get the first image and display it (gets set to full opacity)
    $('ul.ticker_window li:first').css({opacity: 1.0});
    
    if (base.options.caption) {
        //Display Text description of first image
        var caption = $('ul.ticker_window li.show').attr("data-caption");
        if (caption == "") {
            $(base.options.caption).parent().hide();
        }
        $(base.options.caption).html(caption);
    }

    if (base.options.counter) {
      // Set Image number to 01 for first image
      $(base.options.counter + ' span').html("01 ");   
    }

    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    base.currentImage = setInterval(firstCall, base.options.interval);

    return base;
  },
  
  left: function(base) {
    //Get the first image
    var current = ($('ul.ticker_window li.show') ? 
      $('ul.ticker_window li.show') : $('ul.ticker_window li:first'));

    //Get next image, when it reaches the end, rotate it back to the first image
    var prev = ((current.prev().length) ? ((current.prev().hasClass('show')) ? 
      $('ul.ticker_window  li:last') :current.prev()) : $('ul.ticker_window  li:last')); 

    return base.rotate(base, current, prev);
  },
  
  right: function(base) { 
    //Get the first image
    var current = ($('ul.ticker_window  li.show') ? 
      $('ul.ticker_window  li.show') : $('ul.ticker_window  li:first'));

    //Get next image, when it reaches the end, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? 
      $('ul.ticker_window  li:first') :current.next()) : $('ul.ticker_window li:first')); 

    return base.rotate(base, current, next);
  },

  rotate: function(base, current, next) {

    var index = $("ul.ticker_window li").index(next);

    //Set the fade in effect for the next image, the show class has higher z-index
    next.stop().css({opacity: 0.0})
      .addClass('show').stop()
      .animate({opacity: 1.0}, 2000);
    next.children(".title").css({opacity: 0.0})
      .addClass('show').stop()
      .animate({opacity: 1.0}, 2000);
  
    if (base.options.caption) {
      //Display Text description of the image
   //   $(base.options.caption).css({opacity: 0.0}).html($('ul.ticker_window li.show').attr("data-caption")).stop().animate({opacity: 1.0}, 2000);
        var caption = $('ul.ticker_window li:nth-child('+(index+1)+')').attr("data-caption");
	    $(base.options.caption).css({opacity: 0.0}).html(caption).stop();
        if (caption == "") {
            $(base.options.caption).parent().hide();
        } else {
            $(base.options.caption).parent().show().end().animate({opacity: 1.0}, 2000);
        }
    }

    //Hide the current image
    current.stop().animate({opacity: 0.0}, 2000)
      .removeClass('show');
    current.children(".title").stop().animate({opacity: 0.0}, 2000)
      .removeClass('show');

    if (base.options.counter) {
      var index = $("ul.ticker_window li").index(next);
      // Update Image number in "state" div
      $(base.options.counter + ' span').html(index+1);

      if(index < 10){
        $(base.options.counter + ' span').prepend("0");
      }
    }

     return base;
    },

    /**
     * Show an image by its index
     */
    show: function(base, index) {
        //Get the chosen image
        var current = $('ul.ticker_window li.show');

        var next = $($('ul.ticker_window li').get(index));

        // If its already being shown, don't call it again.
        if (!next.hasClass("show")) {
            base.rotate(base, current, next);
        }

        return base;
    }
}};

