/**
 * 
 * Perform jQuery library
 * Developed by Perform
 * 
 * Built on top of the jQuery library
 * http://jquery.com
 *
 * Description: Creates a news reader from an unordered list
 *
 * Version: 1.0 - gearbox
 *
 * 
**/
     
(function($) {
    
    var element;
  var position;
  var count;
  var nav = false;

    $.fn.newsreader = function(options) {               
        return this.each(function() {
            new $nr(this, options);
        });      
    };

  // Default configuration properties.
    var defaults = {
    speed: 5,          // Default transition speed (5 seconds)
    start: 0,           // Starting index on the UL 
    nav: null,            // Newsreaders Navigation Id
    navClickFunc: null,      // Navigation click function call
    navOnHoverInFunc: null,    // Navigation hover over item function call
    navOnHoverOutFunc: null    // Navigation hover out item function call
  }
  
    $.newsreader = function(element, options) {
                          
    // Show the newsreader - Should be hidden in the CSS to render a cleaner load    
        $(element).show();
        
        // Combine the default and user configurations together        
        this.settings = $.extend({}, defaults, options || {});
    settings = this.settings;
        
        // Count the length of items
        count = $(element).children().length-1;
        
        // Check that the count isn't less than the start position
        if(count<settings.start) settings.start = 0;
    
    // Hide all the li items apart the starting position
        $(element).children().each(function(index){
            if(index!=settings.start) {
                $(this).hide();
            }
        });
            
    // Check if there is any navigation
    if(settings.nav!=null) {
      $(settings.nav).children().each(function(index){
      
        // Add the selected class to the first item
        if(index==settings.start) {
          $(this).addClass("selected");
        }
        
        // If the newsreader passes a click function
        if(settings.navClickFunc!=null) {    
          $(this).click(function(){ 
            if (typeof(window[settings.navClickFunc]) === "function") {
              window[settings.navClickFunc](index);
              return false;
            }            
          });
        }
        
        // If the newsreader passes a onHoverIn function
        if(settings.navOnHoverInFunc!=null) {    
          $(this).bind("mouseenter",function(){ 
            if (typeof(window[settings.navOnHoverInFunc]) === "function") {
              window[settings.navOnHoverInFunc](index);
            }            
          });
        }

        // If the newsreader passes a onHoverOut function
        if(settings.navOnHoverOutFunc!=null) {    
          $(this).bind("mouseleave",function(){ 
            if (typeof(window[settings.navOnHoverOutFunc]) === "function") {
              window[settings.navOnHoverOutFunc](index);
            }            
          });
        }
        
      });
      nav = true;      
    }
    
    // Set the current position
    position = settings.start; 
    
    // Change one LI to another
        $.newsreader.changeItem = function (from,to) {
            
      $(element).children().each(function(){ $(this).hide().removeClass("selected");});       // Hide All li elements
      if(nav)  $($(settings.nav).children()).removeClass("selected");     // Remove All Nav Selected Classes      
      
      // Check if the next li is the end of the list
            if(from>=count&&to>from) {
                $($(element).children()[0]).show().addClass("selected");                // Show the next item in it's order
        if(nav)  $($(settings.nav).children()[0]).addClass("selected");  // Add the selected class to the nav
                position=0;
            } else {
                $($(element).children()[to]).show().addClass("selected");              // Show the next item in it's order
        if(nav) $($(settings.nav).children()[to]).addClass("selected");  // Add the selected class to the nav
                position=to;        
            } 
        }
    
    // Get the current position
    $.newsreader.getPosition = function () {          
      return position;
    };
      
    // Get the newsreader length
    $.newsreader.getLength = function () {     
      // add 1 to offset the 0 index    
      return count+1;
    };
    
    // Pause the newsreader
    $.newsreader.pause = function () {          
      clearInterval(changeItemsInterval);      
    };
        
    // Start the newsreader 
    $.newsreader.start = function () { 
      // set interval to call the function changeItem
      changeItemsInterval = setInterval(function () {
        $.newsreader.changeItem(position,position+1)
      }, settings.speed*1000);
    }
    
    // Start the newsreader 
    $.newsreader.start();

  }
    
  // Create shortcut for internal use
    var $nr = $.newsreader;
    
})(jQuery);
