/**
 * Snepo slides JS
 *
 * Here we provide an HTML slideshow
 */
$(function() {
  
  // Get all the slideshows
  $( '.snepo_slides' ).each( function() {
    
    // Create slides
    $( this ).snepoSlides();
    
  });
  
});

// Plugin
(function( $ ){
  
  // Methods
  var methods = {
    init: function() {
      // Loop over elements
      return this.each(function() {
        // Get all the slides
        var slides = $( this ).find( '.snepo_slide' );
        
        // Save slides in data
        var data = $( this ).data( 'slides' );
        data = slides;

        // Fix scope
        var slides_element = $( this );

        // Loop over slides
        slides.each( function( index, element ){
          // First is active
          if (index == 0)
          {
            // Set as active
            slides_element.data('active_slide', $( this ));
            return;
          }
          // Hide the rest
          $( this ).css('display', 'none');
        });
        
        // Create controls
        $( this ).append (
          $('<div />').addClass('snepo_font_white snepo_font_bold snepo_font_huge').append(
            $( '<a />', {
              href: '#',
              html: '&laquo;',
              click: function() {
                slides_element.snepoSlides( 'previous', $( this ) );
                return false;
              }
            }).addClass('snepo_slide_control snepo_background_blue snepo_font_white snepo_float_left snepo_vpadding_2 snepo_hpadding_20 snepo_anchor_plain')
          ).append(
              $( '<div />', {
                html: 1
              }).addClass('snepo_slide_position snepo_font_blue snepo_font_small snepo_inline snepo_position_relative')
          ).append(
            $('<a />', {
              href: '#',
              html: '&raquo;',
              click: function() {
                slides_element.snepoSlides( 'next', $( this ) );
                return false;
              }
            }).addClass('snepo_slide_control snepo_background_blue snepo_font_white snepo_float_right snepo_vpadding_2 snepo_hpadding_20 snepo_anchor_plain')
          ).append(
            $('<div />', {
              text: ''
            }).addClass('snepo_clear snepo_height_0')
          )
        );
        
        // Position position
        var frame_width = $( this ).width();
        var control_width = $( this ).find( 'a.snepo_slide_control' ).eq(0).outerWidth() * 2;
        var position = $( this ).find( 'div.snepo_slide_position' );
        var this_width = position.width();
        
        // Get middle - offset
        var this_left = ((frame_width - control_width) / 2) - (this_width  / 2);
        
        // Move
        position.css('left', this_left + 'px');
      });
      
    },
    previous: function( clickedElement ) {
      // Get active slide
      var active_slide = this.data('active_slide');
      
      // Get previous slide and new next slide
      var previous_slide = active_slide.prev( '.snepo_slide' );
      var next_slide = previous_slide.next( '.snepo_slide' );
       
      // Is there a previous slide
      if (previous_slide.length == 0)
      {
        clickedElement.html('&times;');
        return;
      } else {
        clickedElement.html('&laquo;');
      }
      
      // Is there a next slide
      if (next_slide.length == 0)
      {
        clickedElement.siblings('a.snepo_slide_control').html('&times;');
      } else {
        clickedElement.siblings('a.snepo_slide_control').html('&raquo;');
      }
       
      // Hide active slide and show previous slide
      active_slide.css('display', 'none');
      previous_slide.css('display', 'block');
      
      // Set as active
      this.data('active_slide', previous_slide)
      
      // Update position
      var current = parseInt(clickedElement.siblings('div.snepo_slide_position').html()) - 1;
      clickedElement.siblings('div.snepo_slide_position').html(current);
    },
    next: function( clickedElement ) {
      // Get active slide
      var active_slide = this.data('active_slide');
      
      // Get next slide and new previous slide
      var next_slide = active_slide.next( '.snepo_slide' );
      var previous_slide = next_slide.prev( '.snepo_slide' );
       
      // Is there a next slide
      if (next_slide.length == 0)
      {
        clickedElement.html('&times;');
        return;
      } else {
        clickedElement.html('&raquo;');
      }
      
      // Is there a previous slide
      if (previous_slide.length == 0)
      {
        clickedElement.siblings('a.snepo_slide_control').html('&times;');
      } else {
        clickedElement.siblings('a.snepo_slide_control').html('&laquo;');
      }
       
      // Hide active slide and show next slide
      active_slide.css('display', 'none');
      next_slide.css('display', 'block');
      
      // Set as active
      this.data('active_slide', next_slide)
      
      // Update position
      var current = parseInt(clickedElement.siblings('div.snepo_slide_position').html()) + 1;
      clickedElement.siblings('div.snepo_slide_position').html(current);
    }
  };
  
  $.fn.snepoSlides = function( method ) {
    
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.snepoSlides' );
    }
    
  }
  
})( jQuery );
