/**
 * Developed by Adam Coogan
 *
 * Built on top of the jQuery library
 * http://jquery.com
**/

(function($) {

     /**
     * Creates a news reader from an unordered list
     **/
     
    var e;

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

    // Default configuration properties.
    var defaults = {
        speed: 3,
        start: 0,
        rtl: true,
        focusOnHover: true,
        textOnHover: false,
        fade: false,
        fadeInSpeed: "fast",
        effect: "none"
    };

    $.newsReader = function(e, o) {
        
        // Stylesheet should be set to display so browsers can render without javascript - AS
        $(e).show();
        
        // Combine the default and user configurations together        
        this.settings = $.extend({}, defaults, o || {});
        s = this.settings;
        
        // TODO: Implement a lock function that disables mouse overs when an effect is happening
        var lock = false;
        
        // The default position of News Reader
        var current = s.start;
        
        // Count the length of items
        var count = $(e).children().length-1;
        
        // Check that the count isn't less than the start position
        count < current ? current = 0 : current = s.start;
        
        // Default vars
        var changeItemsInterval;
        
        // Clone the UL to create the thumbnails
		if (s.effect == 'thumbs') {
			$(e).clone().attr("id", "thumbnails").insertBefore('ul#newsReader');	
		}
		
		if(s.effect == 'bullets') {
			$(e).clone().attr("id", "thumbnails").insertBefore('ul#newsReader');
			$($("#thumbnails").children()[s.start]).addClass("selected");
			$("#thumbnails img").each(function(){
				$(this).attr("src","/javaImages/c3/8c/0,,10265~363715,00.gif")
			});
			$("#thumbnails li a").each(function(){
			    $(this).attr("href","")
			    $(this).click(function(){
			        return false;
			        });
			    $(this).unbind('mouseenter mouseleave')
			});
		}

        // Remove the text from the cloned UL      
        if(!s.textOnHover) {        
            $("#thumbnails div").remove();
        } else {
            $("#thumbnails div p").remove();
            $("#thumbnails div h1").hide();
        } 
        
        // Bind the onMouseOver and onMouseOut to the anchor tags
        $("#thumbnails a").each(function(index){
            
            if (parseInt(jQuery.fn.jquery)>=1.3) {
                $(this).live("hover",function(){jQuery.newsReader.onHoverIn(index);},function(){jQuery.newsReader.onHoverOut(index);});
            } else {
                // Bing onMouseOver function to anchor tag
                $(this).bind("mouseenter",function() { 
                    jQuery.newsReader.onHoverIn(index); 
                });
                // Bing onMouseOut function to anchor tag
                $(this).bind("mouseleave",function() { 
                    jQuery.newsReader.onHoverOut(index); 
                }); 
            }
        });
        
        // Carousel effect
        if(s.effect=="carousel") {
            
            // Add Previous Button
            $($("#thumbnails").children("li")[0]).before('<a class="prev"></a>');
            
            // Add Next Button
            $($("#thumbnails").children("li")[count]).after('<a class="next"></a>');
        }
        
        // Hide all the li items apart the starting position
        $(e).children().each(function(i){
            i != current ? $(this).hide() : false;
        });
        
        // Function onHoverIn, this stops the looping and changes the selected item to the one the mouse is hovering on
        $.newsReader.onHoverIn = function (index) {
            
            // clear the set interval
            clearInterval(changeItemsInterval);
            
            // call the function to change the item to the hovered item            
            changeItem(e,current,index,count);
            
            // If the textOnHover is true show the h1 tag
            s.textOnHover ? $($("#thumbnails div").children("h1")[index]).show() : false; 
            
            // Give selected class to li that is currently hovered over
            $($("#thumbnails").children("li")[index]).addClass("selected");

        };
		
		$.newsReader.pauseNewsreader = function () {
			clearInterval(changeItemsInterval);
		}
        
        // Function onHoverOut, this starts the looping again
        $.newsReader.onHoverOut = function (index) {
            
            // clear the set interval
            clearInterval(changeItemsInterval);
            
            // Set the current item to the last item hovered over
            current=index;
            
            // restart up the setInterval to call the function changeItem
            changeItemsInterval = setInterval(function () {changeItem(e,current,setDirection(current),count)}, s.speed*1000);
            
            // If the textOnHover is true hide the h1 tag
            s.textOnHover ? $($("#thumbnails div").children("h1")[index]).hide() : false; 
            
            // Remove selected class
            // $($("#thumbnails").children("li")[index]).removeClass();
            
        };
        
        // set interval to call the function changeItem
        changeItemsInterval = setInterval(function () {changeItem(e,current,setDirection(current),count)}, s.speed*1000);
        
        // set the direct of the rotation : default is right to left
        function setDirection(current) {
           return s.rtl ? current+1 : current-1;
        }
       
        // Change one LI to another
        function changeItem(e,from,to,count) {
            
            // Check if the 'to' is less than the from
            if(to<from) {
                to < 0 ? to=count : false; // If 'to' is less then 0 then set it to the count of items
            } else {
                to > count ? to=0 : false; // If 'to' is greater then the count set it to zero
            }  
                      
            // If effect, do effect otherwise just hide() the current item
            if(s.fade) {
                $($(e).children()[from]).fadeOut(s.fadeInSpeed,function(){
                    $($(e).children()[to]).fadeIn(s.fadeInSpeed);
                });
            } else {
                $($(e).children()[from]).hide().removeClass("selected");
                $($(e).children()[to]).show().addClass("selected");
				if (s.effect == 'bullets') {
					$($("#thumbnails").children()[from]).removeClass("selected");
					$($("#thumbnails").children()[to]).addClass("selected");
				}
            }

            // set the current var to change item
            current=to;

        }
               
    }
    
    // Create shortcut for internal use
    var $nr = $.newsReader;
	
	$.newsReader.pauseReader = function() {
        $.newsReader.pauseNewsreader();
    }

})(jQuery);


