/* =========================================================

// jquery.cycle.js

// Date: 2007-04-20
// Author: Darren Krape
// Web: http://www.forma3.com

// Based on the work of:
//   Matt Oakes: http://portfolio.gizone.co.uk/applications/slideshow/
//   Torsten Baldes: http://medienfreunde.com/lab/innerfade/

// ========================================================= */


(function($) {

$.fn.cycle = function(options) {

  this.each(function(){   
    
    var settings = {
      animationtype: 'fade',
      speed: 'normal',
      timeout: 2000,
      containerheight: 'auto',
      runningclass: 'cycle',
      playState: 'play'
    };

    if(options)
      $.extend(settings, options);
    var elements = $(this).children();
  
    if (elements.length > 1) {
    
      $(this).css('position', 'relative');
  
      $(this).css('height', settings.containerheight);
      $(this).addClass(settings.runningclass);
      
      $("#stories .nav").toggleClass('nav_active');

      for ( var i = 0; i < elements.length; i++ ) {
        $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute');
        $(elements[i]).hide();
      };
      
      if ( settings.type == 'sequence' ) {
        window.next = 1; window.current = 0;
      } else if ( settings.type == 'random' ) {
        window.current = 0;
        do { window.next = Math.floor ( Math.random ( ) * ( elements.length ) ); } while ( window.next == 0 )
      };

      setTimeout(function(){$.cycle.next(elements, settings, window.next, window.current);}, settings.timeout);
      $(elements[0]).show();

	  $("<div id=\"counter\"></div>").appendTo(".nav")
	  document.getElementById("counter").appendChild(document.createTextNode("1 / " + elements.length));
    };

  //Pause Button
    var pause = document.getElementById("btn_pause");
    $('#btn_pause').click(function() {
     
      replaceText = "testing";

      settings.playState = (settings.playState == "pause") ? "play" : "pause";
      pause.className = (pause.className == "btn_pause") ? "btn_play" : "btn_pause";

      pauseText = pause.childNodes[0].data;
      pause.removeChild(pause.firstChild)

      if (pauseText == "Pause") pauseText = "Play"; else pauseText = "Pause"

      pause.appendChild(document.createTextNode(pauseText));
      return false;

    });

  //Next Button
    $('#btn_next').click(function() {

      settings.playState = "pause";
      pause.className = "btn_play";

      pause.removeChild(pause.firstChild);
      pause.appendChild(document.createTextNode("Play"));

      $.cycle.transition(settings, elements);
      return false;

    });

  //Previous Button
    $('#btn_previous').click(function() {
      
      settings.playState = "pause";
      pause.className = "btn_play";

      pause.removeChild(pause.firstChild)
      pause.appendChild(document.createTextNode("Play"));

      if ( settings.type == 'sequence' ) {            
        if ( window.current == 0 ) {
          window.next = elements.length - 1;
        } else {
          window.next = window.current - 1;  
        };
      } else if ( settings.type == 'random' ) {
        do { window.next = Math.floor ( Math.random ( ) * ( elements.length ) ); } while ( window.next == window.current )
      }
      
      $.cycle.transition(settings, elements);
      return false;
    });

  });
};

$.cycle = function() {}
  $.cycle.next = function (elements, settings, next, current) {

    if ( settings.playState == 'play' ) {
      window.current = current;
      window.next = next;
      $.cycle.transition(settings, elements);
    };

     setTimeout((function(){$.cycle.next(elements, settings, window.next, window.current);}), settings.timeout);
  };

  $.cycle.transition = function (settings, elements) {

    if ( settings.animationtype == 'slide' ) {
      $(elements[window.current]).slideUp(settings.speed, $(elements[window.next]).slideDown(settings.speed));
    } else if ( settings.animationtype == 'fade' ) {
      $(elements[window.current]).fadeOut(settings.speed);
      $(elements[window.next]).fadeIn(settings.speed);
    };
    
    if ( settings.type == 'sequence' ) {
      if ( ( window.next + 1 ) < elements.length ) {
        window.next = window.next + 1;
        window.current = window.next - 1;
      } else {
        window.next = 0;
        window.current = elements.length - 1;
      };
    }  else if ( settings.type == 'random' ) {
      window.current = window.next;
      while (  window.next == window.current ) {
        window.next = Math.floor ( Math.random ( ) * ( elements.length ) );
      };
    };
	
	var counterCurrent = window.current + 1;
	document.getElementById("counter").removeChild(document.getElementById("counter").firstChild);
	document.getElementById("counter").appendChild(document.createTextNode(counterCurrent + " / " + elements.length));
  };
})(jQuery);

$(document).ready(
  function(){
  $('#cycle').cycle({
    animationtype: 'fade',
    speed: 700,
    timeout: 10000,
    type: 'sequence',
    containerheight: 'auto'
  });
});
