(function($){
  
  var options = {
    delay: 10,           // Seconds that each case study is displayed before transition
    time: 500,           // Milliseconds for the transition between case studies takes
    useButton: true,     // Use the cost effective button to point to each case study rather than for cost effective solutions
    showArrows: false,   // Show the arrows on the case study section. Allows the user to manually navigate through them,
    randomise: true      // Start cycling on a random case study
  }
      
      var methods = {
        init: function(o) {
          var $this = $(this), data = $this.data('casestudy'); // Create some variables to make our life easier
          options = $.extend(options,o); // Merge user passed options with our defaults
          if(!data) { // If there is no data set then make sure we add what we need...
            data = { current: 0, prev: 0, next: 0, wrap: $($this.children('.wrap')[0]) }; // Basic slideshow information and wrapper
            data.caseStudies = data.wrap.children('article'); // Find our case studies
            if(data.caseStudies.length > 0) { // If we have more than one case study available...
              data.next = 1 // Set up our numbers
                data.prev = data.caseStudies.length - 1 // Set up our numbers
                  data.wrap.append('<div class="image" />').css('z-index',0);
              data.caseStudies.each(function(i){ // For every case study...
                $(this).css({'position': 'absolute', 'width': '960px','z-index': 1}); // Set width and position the case studies for easier fading in/out. Remove the ipad background.
                $(this).find('.image').css({'top': '-446px','background': 'transparent'}); // Align image on page and remove ipad background
                if(i > 0) $(this).find('.content,h3,.image').hide(); // Hide every other case study but the first one
              });
            } else { // If we dont have anymore than one case study...
              data.next = 1; // Set up our numbers
              data.prev = data.caseStudies.length; // Set up our numbers
            };
            if(options.useButton) { // Are we going to use the button to link to the case studies?...
              data.button = $this.find('a.cost_effective').removeClass('cost_effective').addClass('view_case_study_button'); // Link up button if we are going to be using it
              data.button.text('View this case study'); // Realign our button with new content
              data.button.attr('href',$('a.view_case_study:first').attr('href'));
            }
            if(options.showArrows && data.caseStudies.length > 1) { // If we are allowing arrows and we have enough case studies to do so...
              var prevArrow = $("<a href='#'>Previous Case Study</a>"), nextArrow = $("<a href='#'>Next Case Study</a>"); // Create our buttons, ready to insert into the dom
              prevArrow.appendTo(data.wrap); nextArrow.appendTo(data.wrap); // Add arrows to DOM
              $.extend(data,{prevArrow: prevArrow, nextArrow: nextArrow}); // Add to our data object
            };
            $this.data('casestudy',data); // Save our data
          }; 
          methods.cycle.call(this); // Start cycling the case studies
        },
        go: function(num) {
          if(arguments[1]) {
            var $this = arguments[1] === 'object' ? $(this) : arguments[1]; // Check if this method is being called from the cycle method
          } else {
            var $this = $(this);
          }
          var data = $this.data('casestudy'); // Easy-to-use variables
          if(data.current != num) {
            var $next = $(data.caseStudies[num]), $current = $(data.caseStudies[data.current]); // Get our current and next case studies
            $current.find('.content,.image').fadeOut(options.time);
            $current.find('h3').fadeOut(options.time,function(){ // Fade out our current case study
              $next.find('.content,h3,.image').fadeIn(options.time); // Fade in our new case study once ready
              data.prev = data.current // Set new previous number
                data.current = num; // Set new current number
              data.next = data.current == data.caseStudies.length-1 ? 0 : num + 1 // Set new next number
                if(options.useButton) data.button.attr('href',$next.find('a.view_case_study').attr('href'));
            }); 
          }
        },
        prev: function() {
          if(arguments[0]) {
            var $this = arguments[0] === 'object' ? $(this) : arguments[0]; // Check if this method is being called from the cycle method
          } else {
            var $this = $(this);
          }
          var data = $this.data('casestudy'); // Easy-to-use variables
          methods.go.call(this, data.prev); // Pass previous slide number to the go method
        },
        next: function() {
          var self = $(this);
          var data = self.data('casestudy'); // Easy-to-use variables
          methods.go.call(this, data.next, self); // Pass next slide number to the go method
        },
        cycle: function() {
          var self = $(this); 
          var data = self.data('casestudy'); // Easy-to-use variables
          var func = jQuery.proxy(methods.next, this);
          data.interval = setInterval(func, options.delay*1000);
        },
        play: function() {
          methods.cycle.call(this); // Alias for cycling the slideshow
        },
        pause: function() {
          var $this = $(this), data = $this.data('casestudy'); // Easy-to-use variables
          clearInterval(data.interval); // Stop cycling
        }
      };
  
  $.fn.casestudy = function(method) {
    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 + ' does not exist');
    }
  };
  
})(jQuery);
